首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

单片机和ADC0832的AD模数转换(3)

单片机和ADC0832的AD模数转换(3)

相信看到这里,你应该可以理解我们是如何利用单片机来进行模数转换的处理了,你也可以根据自己的需要来写些AD模数转换相关的应用程序,如数字温度计,湿度传感应用,压力传感应用等等。由于篇幅有限,读者朋友可以通过网站或电子邮件一起交流与学习。在下几期中,我们将陆续介绍51单片机综合学习系统的其它功能原理与应用。
STC单片机的相关程序如下:
#include<STC12C2052AD.h>   //定义的 系统头文件和全局变量
#include <intrins.h>

#define uchar unsignedchar
#define uint unsigned int
#define DogReset()   WDT_CONTR=0x35

// T1 定时0.1ms.作为系统计时用,
#define vT01ms    2
#define vT10ms    10
#define vT100ms    10
#define vT01S    100  //1 s = 10 ms * 100
#define vT0HVal    0xfe //0xff //0xfe //0xf6
#define vT0LVal    0x33 //0x9c //0x0c //0x4c


uchar codedisplay_AD_channel_ID[2] = {0x00,0x01};
static unsigned chardata CS;
uchar dataAD_channel_result[2][5]; //各通道A/D转换结果。前是通道号;后是转换的值
uint cT01ms;
uchar cT10ms;
uchar cT100ms;
uchar cT01s;
uchar  THTL;

bit  OutFlag;

void delay_ms(register uint Count){
register uchar T;

for(;Count>0;Count--){
  for(T=0;T<80;T++){
   _nop_();_nop_(); _nop_(); _nop_(); _nop_();
   _nop_();_nop_(); _nop_(); _nop_(); _nop_();
  }
  DogReset();
}
}


void send_char_com(unsigned char OutData){
SBUF = OutData; //输出字符
while(!TI); //空语句判断字符是否发完
TI = 0; //清TI
}

void send_string_com(uchar *str,uchar strlen){
uchar i;
for(i=strlen; i>0; i--){
  send_char_com(*str);
  str++;
  DogReset();
}
}


uchar Ad_Change(uchar channel){
uint AD_Result_Temp = 0 ;
//---------------------将P1.0--P1.1设置成适合AD转换的模式
/// P1 =0xff;                //将P1口置高,为A/D转换作准备
ADC_CONTR = ADC_CONTR|0x80;//1000,0000打开A/D转换电源
P1M0 =0x03;               //0000,0011用于A/D转换的P1.x口,先设为开漏
P1M1 =0x03;               //0000,0011P1.0--P1.1先设为开漏。断开内部上拉电阻
delay_ms(20);                  //20

ADC_CONTR = ADC_CONTR&0xE0;//1110,0000 清ADC_FLAG,ADC_START位和低3位
ADC_CONTR =ADC_CONTR|(display_AD_channel_ID[channel]&0x07);//设置当前通道号

delay_ms(1);                //延时使输入电压达到稳定
ADC_DATA =0;              //清A/D转换结果寄存器
ADC_LOW2 = 0;

    ADC_CONTR = ADC_CONTR|0x08; //0000,1000ADCS = 1,启动转换
do {DogReset();}
    while((ADC_CONTR &0x10)==0);  //0001,0000等待A/D转换结束


ADC_CONTR= ADC_CONTR&0xE7;//1110,0111清ADC_FLAG位,停止A/D转换
AD_Result_Temp =((AD_Result_Temp|ADC_DATA)<<2)|(ADC_LOW2&0x03);
//保存返回AD转换的 结果
//----------------------------转换成可由串口显示的字符
AD_channel_result[channel][0] =AD_Result_Temp/1000+0x30;
AD_channel_result[channel][1] =(AD_Result_Temp%1000)/100+0x30;
AD_channel_result[channel][2] =(AD_Result_Temp%100)/10+0x30;
AD_channel_result[channel][3] =AD_Result_Temp%10+0x30;


//------------------------串口监视
// send_char_com(ADC_DATA);   //////发送转换 的 到的 值,这里只是 高8位,值的转换需要考虑
//send_char_com(ADC_LOW2);   //////发送转换 的 到的 值,这里只是 低2位,值的转换需要考虑

// send_string_com(AD_channel_result[channel],4);
delay_ms(10);   //
return(ADC_DATA);

}
继承事业,薪火相传
返回列表