拜托各位大佬指导指导,下周就要去拿元件了
/*------------------------------------------------------------------*/
/* --- STC MCU International Limited -------------------------------*/
/* --- STC 1T Series MCU RC Demo -----------------------------------*/
/* If you want to use the program or the program referenced in the */
/* article, please specify in which data and procedures from STC */
/*------------------------------------------------------------------*/
#include "adc.h"
ADC初始化程序
void ADC_Inilize(ADC_InitTypeDef *ADCx)
{
P1ASF = ADCx->ADC_Px;
ADC_CONTR = (ADC_CONTR & ~ADC_90T) | ADCx->ADC_Speed;
if(ADCx->ADC_Power == ENABLE) ADC_CONTR |= 0x80;
else ADC_CONTR &= 0x7F;
if(ADCx->ADC_AdjResult == ADC_RES_H2L8) PCON2 |= (1<<5);
else PCON2 &= ~(1<<5);
if(ADCx->ADC_Interrupt == ENABLE) EADC = 1; // ENABLE,DISABLE
else EADC = 0;
if(ADCx->ADC_Polity == PolityHigh) PADC = 1;
else PADC = 0;
}
ADC电源控制程序
void ADC_PowerControl(u8 pwr)
{
if(pwr == ENABLE) ADC_CONTR |= 0x80;
else ADC_CONTR &= 0x7f;
}
查询法读一次ADC结果
u16 Get_ADC10bitResult(u8 channel) //channel = 0~7
{
u16 adc;
u8 i;
if(channel > ADC_CH7) return 1024;
ADC_RES = 0;
ADC_RESL = 0;
ADC_CONTR = (ADC_CONTR & 0xe0) | ADC_START | channel;
NOP(4);
for(i=0; i<250; i++)
{
if(ADC_CONTR & ADC_FLAG)
{
ADC_CONTR &= ~ADC_FLAG;
if(PCON2 & (1<<5))
{
adc = (u16)(ADC_RES & 3);
adc = (adc << 8) | ADC_RESL;
}
else
{
adc = (u16)ADC_RES;
adc = (adc << 2) | (ADC_RESL & 3);
}
return adc;
}
}
return 1024; //´íÎó,·µ»Ø1024,µ÷ÓõijÌÐòÅжÏ
}
ADC中断函数
用ad设计原理图的时候,不用另外画一个单独的ad转换模块,拜托拜托了
void ADC_int (void) interrupt ADC_VECTOR
{
ADC_CONTR &= ~ADC_FLAG;
}
配置ADC
void ADC_config(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Px = ADC_P10;
ADC_InitStructure.ADC_Speed = ADC_360T; // ADC_90T,ADC_180T,ADC_360T,ADC_540T
ADC_InitStructure.ADC_Power = ENABLE; // ENABLE,DISABLE
ADC_InitStructure.ADC_AdjResult = ADC_RES_H8L2; // ADC_RES_H2L8,ADC_RES_H8L2
ADC_InitStructure.ADC_Polity = PolityLow; // PolityHigh,PolityLow
ADC_InitStructure.ADC_Interrupt = DISABLE; // ENABLE,DISABLE
ADC_Inilize(&ADC_InitStructure); //
ADC_PowerControl(ENABLE); / ENABLE或DISABLE
}
|