基于tx-1c 51單片機中斷使用及數(shù)碼管顯示的使用方法
初學51單片機,中斷快把我弄暈了,終于明白點了,怕忘了,貼在這里需要時方便自己找
鎖存器電路
LED顯示器連接電路
數(shù)碼表編制方式:共8位從低4位到高4位,顯示送高電平,如數(shù)字2二進制是0101 1011即16進制為:0x5B
C語言程序代碼如下:
1 #include
2 #define uint unsigned int
3 #define uchar unsigned char
4
5 sbit dula = P2^6; //段選
6 sbit wela = P2^7; //位選
7 uchar time,num; //定時器0計數(shù)次數(shù),數(shù)碼管顯示字符
8
9 void init(); //初始函數(shù)化定義
10 void desplay(); //顯示函數(shù)定義
11 void delay(uint); //延時函數(shù)定義
12
13 //數(shù)字字碼表 0-F
14 uchar code table[]={
15 0x3f,0x06,0x5b,0x4f,
16 0x66,0x6d,0x7d,0x07,
17 0x7f,0x6f,0x77,0x7c,
18 0x39,0x5e,0x79,0x71};
19
20 //主函數(shù)
21 void main()
22 {
23 init(); //初始化
24 desplay(); //顯示
25 }
26
27 //初始化
28 void init()
29 {
30 TMOD = 1; //工作方式
31 TH0 = 0xEE;
32 TL0 = 0x00;
33 ET0 = 1; //打開定時器0中斷
34 EA = 1; //打開總中斷
35 TR0 = 1; //開始計時(數(shù))
36 num = 1;
37 }
38 //數(shù)碼管顯示
39 void desplay()
40 {
41 wela = 1;
42 P0 = 0xFE;
43 wela = 0;
44 P0 = 0xff;
45 dula = 1;
46 P0 = 0x3F;
47 dula = 0;
48 while (1)
49 {
50 if (time==200)
51 {
52 time=0;
53 dula=1;
54 P0=table[num];
55 dula=0;
56 num++;
57 if(num==16)
58 num=0;
59 delay(10);
60 }
61 }
62 }
63 //定時器0中斷
64 void time0() interrupt 1
65 {
66 TH0 = 0xEE;
67 TL0 = 0x00;
68 time++;
69 }
70 //顯示延時
71 void delay(uint x)
72 {
73 uchar i;
74 while (x--)
75 for (i=0;i<110;i++);
76 }