第一次寫博客這玩意,不太會,算了,直接粘代碼吧。 ...
第一次寫博客這玩意,不太會,算了,直接粘代碼吧。
1 #include <reg52.h> 2 #include <intrins.h> //頭文件,將會用到迴圈右移 3 //巨集定義 4 #define uchar unsigned char 5 #define uint unsigned int 6 //位、變數聲明 7 sbit led1 = P1^0; 8 sbit dula = P2^6; 9 sbit wela = P2^7; 10 uchar counter_ms, counter_s, led_flow = 0xfe; 11 uchar code digitron_data[] = { 0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f }; 12 //延時函數 13 void delay(uint z) 14 { 15 uint x, y; 16 for (x = 0; x < z; x++) 17 for (y = 0; y < 114; y++); 18 } 19 //流水燈函數 20 void display_led () 21 { 22 while(1) { 23 led_flow = _cror_(led_flow, 1); 24 P1 = led_flow; 25 delay(500);//調用延時函數 26 } 27 } 28 //數位管函數 29 void display_digitron(uchar i) 30 { 31 uchar tens, ones; 32 tens = i / 10;//求模得到十位 33 ones = i % 10;//求餘得到個位 34 35 P0 = 0xff;//使所有數位管熄滅 36 wela = 1; 37 P0 = 0xfe; 38 wela = 0; 39 40 dula = 1; 41 P0 = digitron_data[tens]; 42 dula = 0; 43 delay(1); 44 45 P0 = 0xff; 46 wela = 1; 47 P0 = 0xfd; 48 wela = 0; 49 50 dula = 1; 51 P0 = digitron_data[ones]; 52 dula = 0; 53 delay(1); 54 } 55 //定時器初始化函數 56 void timer_init() 57 { 58 TMOD = 0x10;//TMOD的地址是0x89,不能被8整除,只能對位元組操作,不能位操作 59 TH1 = 0x4c; 60 TL1 = 0x00; 61 TR1 = 1; //TCON的地址是0x88,可以對位操作 62 } 63 //主函數main 64 void main() 65 { 66 timer_init();//調用定時器初始化函數 67 while(1) { 68 if (TF1 == 1) { 69 TF1 = 0; 70 TH1 = 0x4c; 71 TL1 = 0x00; 72 counter_ms++; 73 } 74 if (counter_ms == 20) {//counter_ms累加20次是1秒 75 counter_ms = 0; 76 counter_s++; 77 } 78 if (counter_s ==60) {//counter_s累加60次是1分鐘 79 wela = 1; 80 P0 = 0xff; 81 wela = 0; 82 TR1 = 0; 83 display_led();//調用流水燈函數 84 /* 85 led_flow = _cror_(led_flow, 1); 86 P1 = led_flow; 87 delay(500); 88 */ 89 } else { 90 display_digitron(counter_s);//調用數位管函數 91 } 92 } 93 }