你这代码怎么像是我当初学C语言时的代码?
给你一段我改过之后的代码
注释全在里面了,实际验证过的/*──────────────────────────────────────────────────────────────────────────────────Stc8a4k_Adc.c文件
编写:YGG
完成日期:20211120
功能:Stc8a系列单片机的ADC功能,采用中断方式进行ADC
完成状态:已经完成
──────────────────────────────────────────────────────────────────────────────────*/
#include "Stc8a.h"
#include "Stc8a_Adc.h"
#define ADC_P10 0x80
#define ADC_P11 0x81
#define ADC_P12 0x82
#define ADC_P13 0x83
#define ADC_P14 0x84
#define ADC_P15 0x85
#define ADC_P16 0x86
#define ADC_P17 0x87
#define ADC_P1f 0x8f
#define ADC_COUNT 32
//──────────────────────────────────────────────────────────────────────────────────────────────────
unsigned char ADC_LINE; //ADC通道记录
unsigned int ADC_AN0; //ADC0数据
unsigned int ADC_AN1; //ADC1数据
//──────────────────────────────────────────────────────────────────────────────────────────────────
void Stc8a_Adc_Init() //ADC初始化
{
unsigned int i;
P1M1|=0x03; //p1.0和p1.1用作ADC端口
ADC_CONTR=0x80; //打开adc电源
ADCCFG=0x2f; //设定ADC转换结果保存格式,ADC时钟选择最慢
for(i=10000;i>0;i--); //延时一段时间
ADC_CONTR=ADC_P10; //选择P1.f端口ADC
ADC_LINE=ADC_P10; //默认启动的ADC通道
ADC_CONTR|=0x40; //启动ADC
EADC=1; //ADC中断允许
}
//─────────────────────────────────────────────────────────────────────────────────────────────
void ADC_ISR()interrupt 5 //ADC中断处理
{
static unsigned char count0; //ADC通道6转换次数记录
static unsigned char count1; //ADC通道f转换次数记录,内部参考电压通道
static unsigned long int ADC_DATA0; //ADC0数据,累加
static unsigned long int ADC_DATA1; //ADCf数据,累加
ADC_CONTR&=~0x20; //清除中断标志位
//──────────────────────────────────────────────────────────────────────────
if(ADC_LINE==ADC_P10) //检测触发ADC中断的通道
{
if(count0>=ADC_COUNT) //检测ADC转换次数
{
ADC_AN0=ADC_DATA0/ADC_COUNT; //取平均值
ADC_DATA0=0x00; //清除ADC累加结果
count0=0x00; //清除ADC转换次数记录
}
ADC_DATA0+=ADC_RES*256+ADC_RESL; //ADC结果累加
count0++;
ADC_CONTR=ADC_LINE=ADC_P11; //选择下一个ADC通道
}
//──────────────────────────────────────────────────────────────────────────
else if(ADC_LINE==ADC_P11)
{
if(count1>=ADC_COUNT)
{
ADC_AN1=ADC_DATA1/ADC_COUNT;
ADC_DATA1=0x00;
count1=0x00;
}
ADC_DATA1+=ADC_RES*256+ADC_RESL;
count1++;
ADC_CONTR=ADC_LINE=ADC_P10;
}
//──────────────────────────────────────────────────────────────────────────
ADC_CONTR|=0x40; //启动ADC
}
//─────────────────────────────────────────────────────────────────────────────────────────────
|