单片机 stc8a8k
2路电压采集异常,求大佬指导一下程序,哪边有问题?
调试设置单通道,能采集单通道电压,设置双通道,2个都采集不了求大佬指导一下
#include"stc8f.h"#include <intrins.h>
#include <stdio.h>
#define VCC 2.50
#define uint unsigned int
#define uchar unsigned char
#define ADC_CHANNEL0 0x00 //转换通道P1.0
#define ADC_CHANNEL1 0x01 //转换通道P1.1
#define ADC_CHANNEL2 0x02 //转换通道P1.2
#define ADC_CHANNEL3 0x03 //转换通道P1.3
#define ADC_CHANNEL4 0x04 //转换通道P1.4
#define ADC_CHANNEL5 0x05 //转换通道P1.5
#define ADC_CHANNEL6 0x06 //转换通道P1.6
#define ADC_CHANNEL7 0x07 //转换通道P1.7
#define ADC_CHANNEL8 0x08 //转换通道P0.0
#define ADC_CHANNEL9 0x09 //转换通道P0.1
#define ADC_CHANNEL10 0x0a //转换通道P0.2
#define ADC_CHANNEL11 0x0b //转换通道P0.3
#define ADC_CHANNEL12 0x0c //转换通道P0.4
#define ADC_CHANNEL13 0x0d //转换通道P0.5
#define ADC_CHANNEL14 0x0e //转换通道P0.6
uint a;
bit flag_1= 0;bit flag_2=0;bit flag_3=0; bit flag_4=0;
uchar count=0; // 变量计数器
void Delay500ms() //@11.0592MHz
{
unsigned char i, j, k;
i = 29;
j = 14;
k = 54;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Delay1ms(a) //@11.0592MHz
{
unsigned char i, j;
i = 15;
j = 90;
do
{
while (--j);
} while (--i);
}
void UartInit(void) //9600bps@11.0592MHz
{
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器1时钟为Fosc,即1T
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设定定时器1为16位自动重装方式
TL1 = 0xE0; //设定定时初值
TH1 = 0xFE; //设定定时初值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
TI=1;
}
void ADC_init()
{
P1M0 = 0x00;
P1M1 = 0xff;
P0M0 = 0x00;
P0M1 = 0xff;
ADCCFG = 0x0f;
ADC_CONTR|=0x80; //开启AD转换电源
Delay1ms(2);
}
uint ADC_GetResult(unsigned char ch)
{
uint ADC_Value=0;
ADC_RES=0;
ADC_RESL=0;
ADC_CONTR|= ch;
ADC_CONTR|= 0x40;//启动AD转换
Delay1ms(1);
while(!(ADC_CONTR&0x20));
ADC_CONTR&=~0x20; //清完成标志
ADC_Value=ADC_RES; //读取ADC高八位
ADC_Value=(ADC_Value<<8)|ADC_RESL;//读取ADC低四位,并且数据合并
ADC_Value>>=4; //数据右移4位,高12位为所需要的数据
return ADC_Value;
}
void Timer0Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xCD; //设置定时初值
TH0 = 0xD4; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA = 1; //开总中断
ET0 = 1; //开定时器T1中断
}
void Timer0(void) interrupt 1
{
TL0 = 0xCD; //设置定时初值
TH0 = 0xD4; //设置定时初值
count++; //每次中断加1
if(count>=20) //如果计数到了100ms
{
flag_1= 1;
}
if (count>=40)
{
flag_2= 1;
}
if (count>=60)
{
flag_3= 1;
}
if (count>=80)
{
count=0;
flag_4= 1;
}
}
void main()
{
float Vin1,Vin2;
uint AD1=0;
uint AD2=0;
ADC_init();
Timer0Init();
UartInit();
printf("串口初始化完毕");
while(1)
{
if(flag_1==1)
{
flag_1=0;
AD1=ADC_GetResult(ADC_CHANNEL6);
}
if(flag_2==1)
{
flag_2=0;
Vin1 = VCC*AD1/4090;
}
if(flag_3==1)
{
flag_3=0;
AD2=ADC_GetResult(ADC_CHANNEL5);
}
if(flag_4==1)
{
flag_4=0;
Vin2 = VCC*AD2/4090;
}
printf ("%.2f,%.2f",Vin1,Vin2);
Delay500ms();
}
}
|