非常感谢。可以用了。 |
![]() |
#include <reg52.h> #define uchar unsigned char #define uint unsigned int sbit wela =P2^3; sbit dula =P2^4; sbit red = P1^4; sbit yellow = P1^3; sbit green=P1^2; uchar shi,ge; uchar code table[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,0x7f, 0x6f}; bit flag1s = 1; void Timer0_init(); void light_crol(); void delay(uint x); void LedScan(); void main() { Timer0_init(); while (1) { LedScan(); if (flag1s) { flag1s = 0; light_crol(); } } } void Timer0_init() { TMOD = 0x01; TH0 = (65536-50000)/256 ; TL0 = (65536-50000)%256 ; EA = 1; ET0 = 1; TR0 = 1; } void light_crol() { uchar num=1; static unsigned char color = 2; static unsigned char timer = 0; if (timer == 0) { switch (color) { case 0: red=1; green=1; if(num%2) color = 1; else color = 2; timer = 2; yellow=0; if(num==11) num=1; break; case 1: yellow=1; green=1; color = 0; timer = 29; red=0; num++; break; case 2: red=1; yellow=1; color = 0; timer = 39; green=0; num++; break; default: break; } } else { timer--; } ge =timer%10; shi =timer/10; } void LedScan() { wela=1; P0=0xfe; wela=0; dula=1; P0=table[shi]; dula=0; P0 = 0xff; delay(20); wela=1; P0=0xfd; wela=0; dula=1; P0=table[ge]; dula=0; P0 = 0xff; delay(20); } void InterruptTimer0() interrupt 1 { static unsigned int tmr1s = 0; TH0 =(65536-50000)/256 ; TL0 = (65536-50000)/256 ; tmr1s++; if (tmr1s >= 20) { tmr1s = 0; flag1s = 1; } } void delay(uint x) { uint i,j; for(i=x;i>0;i--) for(j=110;j>0;j--); } |
mqwu 发表于 2014-12-20 17:15 您好,感谢您认真的修改和帮助,可是还是不正确。数码管依然显示不出来。而且修改后的二极管都不亮了。 |
笔误, 要把Dula改成dula |
改了三处: 1。 timer 改为全局变量,2。修改函数LedScan(uchar m) 放到while(1)内,放在中断内每隔50ms 扫描一次太慢,3, 分离变量直接在LedScan()的形参内,无需在其它地方分离。 #include <reg52.h> #define uchar unsigned char #define uint unsigned int sbit wela =P2^3; sbit dula =P2^4; sbit red = P1^4; sbit yellow = P1^3; sbit green=P1^2; uchar shi,ge; uchar code table[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,0x7f, 0x6f}; static unsigned char timer = 0; bit flag1s = 1; void Timer0_init(); void light_crol(); void delay(uint x); void main() { Timer0_init(); while (1) { LedScan(timer/10); LedScan(timer%10); if (flag1s) { flag1s = 0; light_crol(); } } } void Timer0_init() { TMOD = 0x01; TH0 = (65536-50000)/256 ; TL0 = (65536-50000)%256 ; EA = 1; ET0 = 1; TR0 = 1; } void light_crol() {uchar num=1; static unsigned char color = 2; // static unsigned char timer = 0; if (timer == 0) { switch (color) { case 0: red=1; green=1; if(num%2) color = 1; else color = 2; timer = 2; yellow=0; if(num==11) num=1; break; case 1: yellow=1; green=1; color = 0; timer = 29; red=0; num++; break; case 2: red=1; yellow=1; color = 0; timer = 39; green=0; num++; break; default: break; } } else { timer--; } // ge =timer%10; //shi =timer/10; } void LedScan(uchar m) { Dula=1; P0=table[m]; Dula=0; P0=0xff; P0=0xfe; wela=1; delay(1); wela=0; P0=0xfd; wela=1; delay(10); wela=0; } void InterruptTimer0() interrupt 1 { static unsigned int tmr1s = 0; TH0 =(65536-50000)/256 ; TL0 = (65536-50000)/256 ; // LedScan(); tmr1s++; if (tmr1s >= 20) { tmr1s = 0; flag1s = 1; } } |
admin 发表于 2014-12-20 12:53 你好,能帮忙改下吗? |
电路图呢 最好贴出来一下 方便调试 |