历史上的今天
今天是:2025年02月27日(星期四)
2020年02月27日 | 用C51单片机把只带的ADC(12bit)转换为RS232输出
2020-02-27 来源:51hei
#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;
}
}
}
史海拾趣
|
连续读取RAM中的数据,组成流水线进行计算,但是发现读取RAM时读到的数据有时候不正确,经常是第一个数据有错误,请问一下,有没有什么稳定的设计去连续读取RAM的?我的设计是reg [7:0] ReadNum;if (ReadNum[0])begin Dout ...… 查看全部问答> |
|
#include\"reg52.h\" //#include\"intrins.h\" #define uchar unsigned char #define uint unsigned int bit front_move=1, back_move; sbit key1=P3^2; sbit key2=P3^3; sbit key3=P3^4; //uchar jzaj(void); //单4拍正转 zheng[]={0x01 ...… 查看全部问答> |
|
我是用72324,外部4MHz晶体,用inDART硬件仿真,现在有两个问题: 1,调试通信不成功,但用inDART下的DataBlaze完全正常,读写都正常?没办法我先配置成内部振荡,可以进入硬件仿真,只是速度慢? 2,我的S ...… 查看全部问答> |




