📄 testad.lst
字号:
C51 COMPILER V7.20 TESTAD 09/18/2011 09:45:52 PAGE 1
C51 COMPILER V7.20, COMPILATION OF MODULE TESTAD
OBJECT MODULE PLACED IN testAD.OBJ
COMPILER INVOKED BY: C:\keil\C51\BIN\C51.EXE testAD.c BROWSE DEBUG OBJECTEXTEND
line level source
1 #include<reg52.h>
2 #include <intrins.h>
3 #define uchar unsigned char
4 #define uint unsigned int
5 sbit rs=P1^0;
6 sbit rw=P1^1;
7 sbit en=P1^2;
8 sbit SDA=P3^7;
9 sbit SCL=P3^6;
10 bit bdata IIC_ERROR;
11 uchar code a[]="AD=0.00V";
12 uchar Recv_Buffer[4];//数据接收缓冲
13 void delayms(uint z)
14 {
15 1 uint x,y;
16 1 for(x=z;x>0;x--)
17 1 for(y=110;y>0;y--);
18 1 }
19 void delayus(uint k)
20 {
21 1 for(;k>0;k--);
22 1 }
23 void write_com(uchar command)//写指令子函数
24 {
25 1 rs=0;
26 1 rw=0;
27 1 P2=command;
28 1 delayms(1);
29 1 en=1;
30 1 delayms(3);
31 1 en=0;
32 1 }
33 void write_data(uchar date)//写数据子函数
34 {
35 1 rs=1;
36 1 rw=0;
37 1 P2=date;
38 1 delayms(1);
39 1 en=1;
40 1 delayms(3);
41 1 en=0;
42 1 }
43 void init()
44 {
45 1 en=0;
46 1 rw=0;
47 1 write_com(0x38);
48 1 write_com(0x01);
49 1 write_com(0x0c);
50 1 write_com(0x06);
51 1 write_com(0x80);
52 1 }
53 void display(uchar aa,uint da)
54 {
55 1 write_com(0x80+aa);
C51 COMPILER V7.20 TESTAD 09/18/2011 09:45:52 PAGE 2
56 1 write_data(da%10000/1000+0x30);
57 1 write_data(a[4]);
58 1 write_data(da%1000/100+0x30);
59 1 write_data(da%100/10+0x30);
60 1 }
61 void I2C_write(unsigned char tmp)//I2C写入一个8位二进制数,高位在前低位在后
62 {
63 1 unsigned char i;
64 1 for(i=0;i<8;i++){
65 2 SCL=0;
66 2 _nop_();
67 2 _nop_();
68 2 _nop_();
69 2 SDA=(bit)(tmp&0x80);
70 2 tmp<<=1;
71 2 _nop_();_nop_();_nop_();_nop_();_nop_();
72 2 SCL=1;
73 2 _nop_();_nop_();_nop_();_nop_();_nop_();
74 2 }
75 1 SCL=0;
76 1 }
77 unsigned char I2C_read(void)////I2C读取一个8位二进制数,也是高位在前低位在后
78 {
79 1 unsigned char i,tmp;
80 1 tmp=0;
81 1 for(i=0;i<8;i++){
82 2 SCL=0;
83 2 _nop_();
84 2 _nop_();
85 2 _nop_(); //加入空指令增加稳定性,这关系到频率问题
86 2 SDA=1;
87 2 _nop_();_nop_();_nop_();_nop_();_nop_();
88 2 SCL=1;
89 2 _nop_();_nop_();_nop_();_nop_();_nop_();
90 2 tmp<<=1;
91 2 if(SDA==1)
92 2 tmp++;
93 2 }
94 1 SCL=0;
95 1 return tmp;
96 1
97 1 }
98 /*void I2C_ACK(bit tmp) //根据tmp的1、0来决定应答信号
99 {
100 SDA=tmp;
101 _nop_();_nop_();_nop_();_nop_();_nop_();
102 SCL=1;
103 _nop_();_nop_();_nop_();_nop_();_nop_();
104 SCL=0;
105 } */
106 void respons()//应答信号
107 {
108 1 uchar i=0;
109 1 SCL=1;_nop_();_nop_();_nop_();_nop_();_nop_();
110 1 while((SDA==1)&&(i<255)) i++;
111 1 SCL=0;
112 1 _nop_();_nop_();_nop_();_nop_();_nop_();
113 1 }
114 void I2C_start(void) //看看I2C开始的波形,再对应SDA、SCL的输出
115 {
116 1 SDA=1;
117 1 _nop_();
C51 COMPILER V7.20 TESTAD 09/18/2011 09:45:52 PAGE 3
118 1 SCL=1;
119 1 _nop_();
120 1 SDA=0;
121 1 _nop_();
122 1 SCL=0;
123 1 _nop_();
124 1 }
125
126 /*********/
127 void I2C_stop(void) //I2C结束
128 {
129 1 SDA=0;
130 1 _nop_();
131 1 SCL=1;
132 1 _nop_();
133 1 SDA=1;
134 1 _nop_();
135 1 SCL=0;
136 1 _nop_();
137 1 }
138 uchar read_ad()
139 {
140 1 uchar dat;
141 1 I2C_start();
142 1 I2C_write(0x90);
143 1 respons();
144 1 I2C_write(0x00);
145 1 respons();
146 1 I2C_start();
147 1 I2C_write(0x91);
148 1 respons();
149 1 dat=I2C_read();
150 1 //I2C_ACK(1);
151 1 respons();
152 1 I2C_stop();
153 1 return dat;
154 1 }
155 void write(uchar d)
156 {
157 1 I2C_start();
158 1 I2C_write(0x90);
159 1 //I2C_ACK(0);
160 1 respons();
161 1 I2C_write(0x40);
162 1 //I2C_ACK(0);
163 1 respons();
164 1 I2C_write(d);
165 1 //I2C_ACK(0);
166 1 respons();
167 1 I2C_stop();
168 1 delayms(5);
169 1 }
170 void main()
171 { uchar i,datt;
172 1 float d;
173 1 uint dattt;
174 1 init();
175 1 for(i=0;a[i]!='\0';i++)
176 1 {
177 2 write_data(a[i]);
178 2 delayus(600);
179 2 }
C51 COMPILER V7.20 TESTAD 09/18/2011 09:45:52 PAGE 4
180 1
181 1 while(1)
182 1 {
183 2
184 2 for(i=255;i>0;i--)
185 2 {write(i);
186 3 datt=read_ad();
187 3 d=(float)datt*16.875;
188 3 //d=(float)datt*19.537;
189 3 dattt=(uint)d;
190 3 display(3,dattt);
191 3 delayms(100);
192 3 delayms(400);}
193 2
194 2 }
195 1
196 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 517 ----
CONSTANT SIZE = 9 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 4 8
IDATA SIZE = ---- ----
BIT SIZE = 1 ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 0 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -