ad_da.lst
来自「很实用的单片机例程」· LST 代码 · 共 328 行 · 第 1/2 页
LST
328 行
151 unsigned char Read(void)
152 {
153 1 unsigned char temp=0;
154 1 unsigned char temp1=0;
155 1 unsigned char BitCounter=8;
156 1
157 1 Sda=1;
158 1 do
159 1 {
160 2 Scl=0;
161 2 _nop_();
162 2 Scl=1;
163 2 _nop_();
164 2 if(Sda)
165 2 temp=temp|0x01;
166 2 else
167 2 temp=temp&0xfe;
168 2
169 2 if(BitCounter-1)
170 2 {
171 3 temp1=temp<<1;
172 3 temp=temp1;
173 3 }
174 2 BitCounter--;
175 2 }
176 1 while(BitCounter);
177 1 return(temp);
178 1 }
C51 COMPILER V7.06 AD_DA 11/04/2011 00:35:32 PAGE 4
179
180 /*------------------------------------------------
181 写入DA数模转换值
182 ------------------------------------------------*/
183 void DAC(unsigned char Data)
184 {
185 1 Start();
186 1 Send(AddWr); //写入芯片地址
187 1 Ack();
188 1 Send(0x40); //写入控制位,使能DAC输出
189 1 Ack();
190 1 Send(Data); //写数据
191 1 Ack();
192 1 Stop();
193 1
194 1 }
195
196 /*------------------------------------------------
197 读取AD模数转换的值,有返回值
198 ------------------------------------------------*/
199 unsigned char ReadADC(unsigned char Chl)
200 {
201 1 unsigned char Data;
202 1 Start(); //写入芯片地址
203 1 Send(AddWr);
204 1 Ack();
205 1 Send(0x40|Chl);//写入选择的通道,本程序只用单端输入,差分部分需要自行添加
206 1 //Chl的值分别为0、1、2、3,分别代表1-4通道
207 1 Ack();
208 1 Start();
209 1 Send(AddRd); //读入地址
210 1 Ack();
211 1 Data=Read(); //读数据
212 1 Scl=0;
213 1 NoAck();
214 1 Stop();
215 1 return Data; //返回值
216 1 }
217
218 void cmg(void)//数码管锁存函数 关时钟DS1302
219 {
220 1 dula=1;
221 1 P0=0x00;
222 1 dula=0;
223 1 wela=1;
224 1 P0=0x00;
225 1 wela=0;
226 1 RST=0; // 关时钟DS1302
227 1 }
228
229
230 /*------------------------------------------------
231 主程序
232 ------------------------------------------------*/
233 void main()
234 {
235 1
236 1
237 1 unsigned char num; //DA数模输出变量
238 1 unsigned char ADtemp; //定义中间变量
239 1 InitLcd();
240 1 mDelay(20);
C51 COMPILER V7.06 AD_DA 11/04/2011 00:35:32 PAGE 5
241 1 Init_Timer1();
242 1 cmg(); //数码管锁存
243 1 while(1)
244 1 {
245 2 DAC(num); //DA输出,可以用LED模拟电压变化
246 2 num++; //累加,到256后溢出变为0,往复循环。显示在LED上亮度逐渐变化
247 2 mDelay(20); //延时用于清晰看出变化
248 2 if(ADFlag) //定时采集输入模拟量
249 2 {
250 3 ADFlag=0;
251 3
252 3 ADtemp=ReadADC(0);
253 3 TempData[0]=(ReadADC(0))/50;//处理0通道电压显示
254 3 TempData[1]=((ReadADC(0))%50)/10;
255 3
256 3 ADtemp=ReadADC(1);
257 3 TempData[2]=(ReadADC(1))/50;//处理1通道电压显示 此通道暂时屏蔽,可以自行添加
258 3 TempData[3]=((ReadADC(1))%50)/10;
259 3
260 3 ADtemp=ReadADC(2);
261 3 TempData[4]=(ReadADC(2))/50;//处理1通道电压显示 此通道暂时屏蔽,可以自行添加
262 3 TempData[5]=((ReadADC(2))%50)/10;
263 3
264 3 ADtemp=ReadADC(3);
265 3 TempData[6]=(ReadADC(3))/50;//处理1通道电压显示 此通道暂时屏蔽,可以自行添加
266 3 TempData[7]=((ReadADC(4))%50)/10;
267 3
268 3 disp();
269 3 }
270 2 }
271 1 }
272
273 /*------------------------------------------------
274 定时器中断程序
275 ------------------------------------------------*/
276 void Timer1_isr(void) interrupt 3 using 1//定时器1执行数码管动态扫描
277 {
278 1
279 1 static unsigned int j;
280 1 TH1=0xfb; //重新赋值
281 1 TL1=0x00;
282 1 j++;
283 1 if(j==200)
284 1 {j=0;ADFlag=1;} //定时置位AD采样标志位
285 1
286 1 // P0=Display[count];//用于动态扫描数码管
287 1 // P2=count;
288 1 // count++;
289 1 // if(count==8) //表示扫描8个数码管
290 1 // count=0;
291 1
292 1
293 1 }
MODULE INFORMATION: STATIC OVERLAYABLE
CODE SIZE = 728 ----
CONSTANT SIZE = 10 ----
XDATA SIZE = ---- ----
PDATA SIZE = ---- ----
DATA SIZE = 52 7
IDATA SIZE = ---- ----
C51 COMPILER V7.06 AD_DA 11/04/2011 00:35:32 PAGE 6
BIT SIZE = 1 ----
END OF MODULE INFORMATION.
C51 COMPILATION COMPLETE. 1 WARNING(S), 0 ERROR(S)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?