发布网友 发布时间:2022-04-25 00:26
共1个回答
热心网友 时间:2023-10-17 12:22
#include <reg51.h>
#include <INTRINS.H>
#include <STDIO.H>
// define P1.0 to check STATUS.
sbit STATUS = P1^0;//用P1.0来检测状态
unsigned char xdata CTRL _at_ 0x2FFF;
unsigned char xdata ADSEL _at_ 0x4FFF;
unsigned char hByte;//存放ADC转换结果的高8位
unsigned char lByte;//存放ADC转换结果的低8位
void adc_Convert (void)//ADC函数,进行模数转换
{ // Start a conversion with A0 and A/$C$ low.
// The convesion takes place on rising CE edge.
CTRL = 0x00;
ADSEL = 0x00;
// Wait until we have completed a conversion .
while(STATUS==1);//检测状态,一直等到转换完成,才往下走
// Set R/$C$ with A0 low and read the low byte.
//读出ADC转换结果的高8位
CTRL = 0x02;
hByte = ADSEL;
// Set R/$C$ with A0 high and read the high.
//读出ADC转换结果的低8位
CTRL = 0x03;
lByte = ADSEL;
}
void main(void)
{ unsigned int delay, MSB , LSB, adc_Res;
// Initialize serial interface
//初始化串口,波特率1200bps
SCON = 0xDA; // SCON: mode 1, 8-bit UART, enable rcvr */
TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit reload */
TH1 = 0xFD; // TH1: reload value for 1200 baud @ 12MHz */
TR1 = 1; // TR1: timer 1 run */
TI = 1; // TI: set TI to send first char of UART */
while(1)
{ adc_Convert();//进行AD转换
MSB=(unsigned int)(hByte << 4);
LSB=(unsigned int)(lByte >> 4);
// adc_Res now has the converted data with 12-bit resolution.
//将ADC结果组合成12位数据,存放于adc_Res
adc_Res = MSB + LSB;
// Send adc results to the serial interface
//通过串口将ADC结果发送出去
printf("ADC READINGS: %03Xh\n", adc_Res);
// simple delay - it is mcu clock dependent !
//简单延时一下,由单片机时钟决定
for (delay=0; delay<10000; delay++)
;
}
}
整个程序的功能就是:单片机控制ADC芯片进行转换,并读取ADC结果,从串行口发送。