51单片机:定时器实现1秒时数码管数字自动加一并循环
题目要求:
定时器中断实现每来1s,P1口接的一位共阳极数码管显示数+1,
加到9之后再来1s清零并重复上述过程
代码如下:
(仅供参考)
/**定时器中断实现每来1s,P1口接的一位共阳极数码管显示数+1,*加到9之后再来1s清零并重复上述过程*/#include《reg52.h》***it SEG = P1^0;unsigned char code DSY_CODE[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; //共阳极数码管void main(){ TMOD &=0X0F; TMOD |=0x01; TH0 = (65536 - 50000)/256;//高四位初值 TL0 = (65536 - 50000)%256;//低四位初值,每隔50ms溢出 EA = 1; //开总中断 ET0 = 1; //T1开时定时器溢出 TR0 = 1; //开启定时器 while(1);}void ET0_ISR(void) interrupt 1 //定时中断{ static unsigned int cnt = 0; static unsigned int i = 0; TH0 = (65536-50000)/256; TL0 = (65536-50000)%256; cnt++; if(cnt 》= 20) //当为1秒时数码管自动+1 { cnt = 0; P1 = DSY_CODE[i++]; while(i》9) //加到9数码管清零 { i = 0; } }} 仿真结果
51单片机:定时器实现1秒时数码管数字自动加一并循环
题目要求:
定时器中断实现每来1s,P1口接的一位共阳极数码管显示数+1,
加到9之后再来1s清零并重复上述过程
代码如下:
(仅供参考)
/**定时器中断实现每来1s,P1口接的一位共阳极数码管显示数+1,*加到9之后再来1s清零并重复上述过程*/#include《reg52.h》***it SEG = P1^0;unsigned char code DSY_CODE[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; //共阳极数码管void main(){ TMOD &=0X0F; TMOD |=0x01; TH0 = (65536 - 50000)/256;//高四位初值 TL0 = (65536 - 50000)%256;//低四位初值,每隔50ms溢出 EA = 1; //开总中断 ET0 = 1; //T1开时定时器溢出 TR0 = 1; //开启定时器 while(1);}void ET0_ISR(void) interrupt 1 //定时中断{ static unsigned int cnt = 0; static unsigned int i = 0; TH0 = (65536-50000)/256; TL0 = (65536-50000)%256; cnt++; if(cnt 》= 20) //当为1秒时数码管自动+1 { cnt = 0; P1 = DSY_CODE[i++]; while(i》9) //加到9数码管清零 { i = 0; } }} 仿真结果
举报