再次启用芯圣的单片机, 使用在HC89f0541上, 其中的ADC12位转换上, 卡壳了“数码管显示不全”:是哪条语句写错! 还是 计算出问题? 望高手过路 指教一下 多谢赐教
#include "VoltADC.h"
#include "string.h"
#define uchar unsigned char
#define uint unsigned int
void delay_ms(u16 nms);
sbit CV= P0^5; // ADC输出口
unsigned int ADvalue;
uchar AdcBufCount =0;
uint ADvalueBuf[8]={0x00}; //ADC缓存值
uchar AdcFlag = 0;
float Vin;
unsigned char dispVin[3]={0,0,0}; //待数码管显示值
void Disp_Vin();
//***************************************************
void Delay_2us(uchar i) // @16MHz 2US 延时
{
while(--i);
}
//***************************************
void Delay1ms(void) //@16MHz
{
unsigned char i, j;
i = 16;
j = 141;
do
{
while (--j);
} while (--i);
}
//************ADC**************************
void voltADC_init(void) //
{
P0M5 = 0x02; // 设置P05为模拟输入
ADCC0 = 0x80; //开启ADC电源
Delay_2us(10); //延时 保稳定
ADCC1 = 0x05; //选择P05为转换通道
ADCC2 = 0x4B; //12位 右对齐
SCANCON = 0x6F; //单通道带缓存连续扫描模式
IE1 |= 0x40; //打开ADC转换中断
EA = 1;
ADCC0 |= 0x40; //启动转换
Delay_2us(10); //
EADC = 1;
}
//**********************************
void ADC_Rpt() interrupt ADC_VECTOR
{
AdcFlag = 1; //
IE1 &=~ 0x40; //
ADCC0 &=~ 0x20;
}
//***************************************
unsigned int ADC_GetResault() //
{
unsigned int AD_Dat = 0; // 定义转换值
unsigned int Tmp = 0;
static uchar i;
voltADC_init();
SCANCON = 0x6F; // 单通道带缓存连续 扫描8次
while(1)
{ if(AdcFlag == 1) //ADC转换结束
{
ADvalueBuf[0] = SCR0;
ADvalueBuf[1] = SCR1;
ADvalueBuf[2] = SCR2;
ADvalueBuf[3] = SCR3;
ADvalueBuf[4] = SCR4;
ADvalueBuf[5] = SCR5;
ADvalueBuf[6] = SCR6;
ADvalueBuf[7] = SCR7;
for(i = 0; i <= 8; i ++)
{
ADvalue += ADvalueBuf[i];
}
ADvalue /= 8; //8次扫描均值
if(ADvalue <= 0x10) {CV = 1;}
else {CV =0;}
AdcFlag = 0; //
AdcBufCount = 0;
memset(ADvalueBuf, 0 ,10); //清缓冲
IE1 |= 0x40;
}
Vin = ADvalue;
return Vin;
}
}
void Disp_Vin() //数码管待显示的 百 十 个
{
unsigned char i;
for(i=0 ; i<3; i++)
{
dispVin[0] = ( ADC_GetResault()*3700/4095)/100;
dispVin[1] = ( ADC_GetResault()*3700/4095 ) % 100 / 10;
dispVin[2] = ( ADC_GetResault()*3700/4095 ) % 10 ;
if(dispVin[0] == 0) //高位0时 不显示
{ dispVin[0] = 10;
if (dispVin[1] == 0) dispVin[1] = 10; //高位0时 不显示
}
}
}
省去后面的数码显示单元程序, 此程序 已经测试验证OK
|