STC 51核MCU IO推挽输出直接驱动数码管,可比AVR
5位共阳数码管控制脚接led1-led5
P2 leda-leddp接数码管阴级,接法简单,
测试代码如下,直接驱动数码管显示12345
//测试芯片 STC12C5608AD
#include "STC12C5620AD.H"
// http://hi.baidu.com/xiexieluguo
//0-9,10C,11E,12A,13 ,14F,15r,16h,17d
unsigned char LED[]={~0x3f,~0x06,~0x5b,~0x4f,~0x66,~0x6d,~0x7d,~0x07,~0x7f,~0x6f,~0x39,~0x79,~0x77,~0x00,~0x71,~0x50,~0x74,~0x5E};
//5个led选择位
sbit led1=P1^3;
sbit led2=P1^4;
sbit led3=P1^5;
sbit led4=P1^6;
sbit led5=P1^7;
//4个按键
sbit key3=P1^2;
sbit key2=P1^1;
sbit key1=P1^0;
sbit key4=P3^7;
//led字模管脚
#define LEDag P2
//当前显示的数码管位置
unsigned char led_pos=0;
//
//led interrupt 2ms init
void timer_led_init(void)
{
TMOD=0x10;//T1
TH1=0xFC;//T1 value
TL1=0x18;
TR1=1;//start timer1
ET1=1;//enable T1 interrupt
EA=1;//enable CPU interrupt
}
//定时器中断处理程序,刷新LED
void timer_led_isr(void)interrupt 3 using 2
{
TH1=0xFC;//T1 value
TL1=0x18;
switch(led_pos)
{
case 0:
led1=1;
led2=0;
led3=0;
led4=0;
led5=0;
LEDag=LED[1];
break;
case 1:
led1=0;
led2=1;
led3=0;
led4=0;
led5=0;
LEDag=LED[2];
break;
case 2:
led1=0;
led2=0;
led3=1;
led4=0;
led5=0;
LEDag=LED[3];
break;
case 3:
led1=0;
led2=0;
led3=0;
led4=1;
led5=0;
LEDag=LED[4];
break;
case 4:
led1=0;
led2=0;
led3=0;
led4=0;
led5=1;
LEDag=LED[5];
break;
}
led_pos++;
if(led_pos>4)
led_pos=0;
}
void main(void)
{
timer_led_init();//初始化定时器,扫描方式显示5位数码
P1M1=0xF8;//5位数码管共阳控制端置为推挽输出
while(1)
{
;
}
}