标题:
用C51单片机把只带的ADC(12bit)转换为RS232输出
[打印本页]
作者:
zhaobolove
时间:
2020-2-21 17:11
标题:
用C51单片机把只带的ADC(12bit)转换为RS232输出
#include "reg51.h" // STC8G1K08A-SOP8
#include "intrins.h"
/*
STC8G1K08A-SOP8
读取 VCC值 ,先发送出去
然后 循环开始 读取ch4的 (P55 ,第3脚)值 发送出去
*/
sfr ADC_CONTR = 0xbc;
sfr ADC_RES = 0xbd;
sfr ADC_RESL = 0xbe;
sfr ADCCFG = 0xde;
sbit EADC = IE^5;
sfr P3M1=0xb1;
sfr P3M0=0xb2;
sfr P5M1=0xc9;
sfr P5M0=0xca;
sfr AUXR =0x8e;
unsigned int myADC;
bit bFlag;
int *BGV;
bit busy;
#define BRT (65536-11059200UL/115200/4)
void ADC_Isr() interrupt 5
{
ADC_CONTR &= ~0x20; //清中断标志
ADCCFG=0x20; //右对齐
myADC= (ADC_RES<<8) + ADC_RESL ;
// P0 = ADC_RES; //High 2bit
// P2 = ADC_RESL; //Low 8bit
ADC_CONTR |= 0x40;
bFlag=1;
}
void UartISR() interrupt 4
{
if(TI)
{
TI=0;
busy=0;
}
if(RI)
RI=0;
}
void UartInit()
{
SCON=0x50;
TMOD=0x0;
TL1= BRT;
TH1= BRT>>8;
TR1=1;
AUXR=0x40;
busy=0;
}
void UartSend( char dat)
{
while(busy);
busy=1;
SBUF=dat;
}
void ADCInit()
{
ADCCFG=0x2f;
ADC_CONTR=0x8f;// Bandgap voltage
}
int ADCRead()
{
int res;
ADC_CONTR|=0x40;
_nop_();
_nop_();
while(!(ADC_CONTR & 0x20));
ADC_CONTR &=~0x20;
res=(ADC_RES<<8)|ADC_RESL;
return res;
}
void main()
{
int res,vcc,adc,i;
P5M0=0;
P5M1=0;
BGV=(int idata*)0xef;
ADCInit();
UartInit();
ES=1;
EA=1;
ADCRead();
ADCRead();
res=0;
for(i=0;i<8;i++)
res+=ADCRead();
res>>=3;
vcc=(int)(4095L**BGV/res);
UartSend(vcc>>8);UartSend(vcc); //这里先输出VCC电压
ADCCFG = 0x0f; //set sysCLk
ADC_CONTR = 0x84; //使能并启动ADC模块 0x84读通道4(P55 ,第3脚), 0x8f读通道F:BGV值
EADC = 1; //使能ADC中断 //0x85读通道5(P54 ,第1脚)
EA = 1; //0x83读通道3(P33 ,第8脚)
ADC_CONTR |= 0x40; //Run ADC change //0x82读通道2(P32 ,第7脚)
//P30,P31为程序下载口Rx,Tx
res=0;
while (1)
{
if(bFlag)
{
i++;
res+=myADC;
bFlag=0;
}
if(i>=8)
{
res>>=3;
adc=(int)(4095L**BGV/res);
UartSend(adc>>8);UartSend(adc); //这里 循环 输出 ADC的电压,
res=0;
i=0;
}
}
}
复制代码
mA 232.rar
2020-2-21 17:10 上传
点击文件名下载附件
下载积分: 黑币 -5
27.24 KB, 下载次数: 7, 下载积分: 黑币 -5
用C51 把只带的ADC12bit)转换为RS232输出
作者:
hxszrb
时间:
2020-2-27 00:00
来学习的,谢谢
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1