51单片机蓝牙

 1 #include<reg52.h>
 2 
 3 void init();
 4 void delay(unsigned int ms);
 5 
 6 unsigned char input;
 7 void display(unsigned char num_decimal);
 8 unsigned char code character[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
 9 0xf8,0x80,0x90};
10 
11 sbit SegSelection1=P2^0;
12 sbit SegSelection2=P2^1;
13 sbit SegSelection3=P2^2;
14 sbit SegSelection4=P2^3;
15 sbit p1=P1^0;
16 sbit p2=P1^1;    
17 
18 void main()
19 {
20 p1=1;
21 p2=0;
22 init();
23 
24 while(1)
25 {
26 
27 display(input);
28 }
29 }
30 
31 void init(void)
32 {
33 EA = 1;    
34 ES = 1;    
35 PCON = 0x00; 
36 SCON = 0x50;    //串口工作在方式1,并且启动串行接收
37 
38 TMOD = 0x20; 
39 TH1 = 0xFd; //设置波特率 9600
40 TL1 = 0xFd;
41 TR1 = 1;    
42 }
43 
44 void rt() interrupt 4 //中断4:串行收发数据
45 {
46 EA=0;
47 if(RI==1)
48 {
49 input=SBUF-48;
50 RI=0;
51 
52 SBUF=input+48;
53 while(!TI);
54 TI=0;
55 }
56 EA=1;
57 }
58 
59 void display(unsigned char num_decimal)    //在数码管显示传输的数据
60 {
61 unsigned int thousand,hundred,tens,unit;
62 
63 thousand=num_decimal/1000;
64 hundred=(num_decimal-thousand*1000)/100;
65 tens=(num_decimal-thousand*1000-hundred*100)/10;
66 unit=num_decimal%10;
67 
68 SegSelection1=0;
69 P0=character[thousand];
70 delay(5);
71 SegSelection1=1;
72 
73 SegSelection2=0;
74 P0=character[hundred];
75 delay(5);
76 SegSelection2=1;
77 
78 SegSelection3=0;
79 P0=character[tens];
80 delay(5);
81 SegSelection3=1;
82 
83 SegSelection4=0;
84 P0=character[unit];
85 delay(5);
86 SegSelection4=1;
87 }
88 
89 void delay(unsigned int ms)
90 {
91 unsigned int x;
92 while(ms--)
93 for(x=110;x>0;x--);
94 }

猜你喜欢

转载自www.cnblogs.com/clam-hao/p/10077035.html