下面就是完整程序了,具体流程是先读取带隙电压,再对传感器的输出电压进行读取
有关带隙电压的相关程序与知识,请看这里N76E003之ADC带隙电压(Band-gap)
#include "N76E003.h"
#include "Common.h"
#include "Delay.h"
#include "SFR_Macro.h"
#include "Function_define.h"
//#include "DHT11.h"
//#include "BMP180.h"
//#include "HMC5883.h"
//#include "BH1750.h"
#define uint unsigned int
#define uchar unsigned char
double Bandgap_Voltage,ADC_Voltage; //please always use "double" mode for this
unsigned char xdata ADCdataH[5], ADCdataL[5];
int ADCsumH=0, ADCsumL=0;
unsigned char ADCavgH,ADCavgL;
UINT8 BandgapHigh,BandgapLow,BandgapMark;
double Bandgap_Value,Bandgap_Voltage_Temp;
double Coe,bgvalue,RC_value;//比例系数,测量带隙电压
/*
程序功能:读取UID中带隙电压值;通过ADC,测量实际的带隙电压;得到比例系数COE;
本程序需要放在ADC正常测量前。
*/
void READ_BANDGAP()
{
unsigned int i;
set_IAPEN;
IAPCN = READ_UID;
IAPAL = 0x0d;
IAPAH = 0x00;
set_IAPGO;
BandgapLow = IAPFD;
BandgapMark = BandgapLow&0xF0;
if (BandgapMark==0x80)
{
BandgapLow = BandgapLow&0x0F;
IAPAL = 0x0C;
IAPAH = 0x00;
set_IAPGO;
BandgapHigh = IAPFD;
Bandgap_Value = (BandgapHigh<<4)+BandgapLow;
Bandgap_Voltage_Temp = Bandgap_Value*3/4;
Bandgap_Voltage = Bandgap_Voltage_Temp - 33; //the actually banggap voltage value is similar this value.
}
if (BandgapMark==0x00)
{
BandgapLow = BandgapLow&0x0F;
IAPAL = 0x0C;
IAPAH = 0x00;
set_IAPGO;
BandgapHigh = IAPFD;
Bandgap_Value = (BandgapHigh<<4)+BandgapLow;
Bandgap_Voltage= Bandgap_Value*3/4;
}
if (BandgapMark==0x90)
{
IAPAL = 0x0E;
IAPAH = 0x00;
set_IAPGO;
BandgapHigh = IAPFD;
IAPAL = 0x0F;
IAPAH = 0x00;
set_IAPGO;
BandgapLow = IAPFD;
BandgapLow = BandgapLow&0x0F;
Bandgap_Value = (BandgapHigh<<4)+BandgapLow;
Bandgap_Voltage= Bandgap_Value*3/4;
}
clr_IAPEN;
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
Enable_ADC_BandGap; //使能ADC带隙电压
CKDIV = 0x02; // IMPORTANT!! Modify system clock to 4MHz ,then add the ADC sampling clock base to add the sampling timing.
for(i=0;i<5;i++) //采样5次,不要前面三次
{
clr_ADCF;
set_ADCS;
while(ADCF == 0);
ADCdataH[i] = ADCRH;
ADCdataL[i] = ADCRL;
}
CKDIV = 0x00;
//--------均值滤波--------------------------------------------
for(i=2;i<5;i++) // use the last 3 times data to make average
{
ADCsumH = ADCsumH + ADCdataH[i];
ADCsumL = ADCsumL + ADCdataL[i];
}
ADCavgH = ADCsumH/3;
ADCavgL = ADCsumL/3;
bgvalue = (ADCavgH<<4) + ADCavgL;
Coe=(Bandgap_Voltage/bgvalue);
ADCsumH = 0;
ADCsumL = 0;
}
void ADC_ISR (void) interrupt 11
{
if(ADCF)
{
clr_ADCF;//清除ADC转化完成标志,进行下一次转换
set_ADCS;//当单次转换完成后,ADCS会硬件置0,需要重新使能
RC_value= (ADCRH<<4) + ADCRL;//得到ADC转换值
ADC_Voltage=RC_value*Coe;//测量数据*修正系数=实际值
}
}
void main(void)
{
Set_All_GPIO_Quasi_Mode;//所有IO设置为双向模式
READ_BANDGAP();
Enable_ADC_AIN5;//配置使能P04,作为AIN5。
PWM0_END_TRIG_ADC;//PWM0末端触发ADC中断
set_EADC;//使能ADC中断
EA = 1;
set_ADCS;//使能ADCS,启动ADC测量
//------------------------------------------------
// PWM5_P03_OUTPUT_ENABLE;//使能PWM5,通过P03引脚输出
PWM0_P12_OUTPUT_ENABLE;
clr_PWMTYP;//边沿对齐模式
clr_PWMMOD0;//设置为独立输出模式
clr_PWMMOD1;
PWM_CLOCK_DIV_8;//8分频模式
PWMPH = 0x4e;
PWMPL = 0x2f;
set_SFRPAGE;//PWM4 and PWM5 duty seting is in SFP page 1
PWM0H = 0x02;
PWM0L = 0x81;
clr_SFRPAGE;
// PWM0_OUTPUT_INVERSE;//开启极性控制
set_LOAD;//载入周期和占空比
set_PWMRUN;//开始输出PWM
while(1)
{
//构建你的代码,LOAD会自动重载,PWM持续输出。
}
}
|