|
/*ADC初始化*/
void ADC_Init()
{
ADC0MX = 0x12; //P2.2模拟输入
ADC0CF = 0x00; //转换时间周期控制,复位值0xF0;
ADC0CN = 0x02; //AD0EN=0,ADC0 Burst Mode closed,CN上升启动中断,右对齐
ADC0TK = 0xF9; //Pre-tracking mode F8 3个 F9 4
}
/*引脚定义*/
void Port_IO_Init()
{
P0MDIN = 0xFD; //P0.1 模拟输入 MISO 0.5不能配置成模拟输出,否则接收不到数据
P1MDIN = 0xFB; //P1.2 Vref模拟输入
P2MDIN = 0xFB; //P2.2 ADC
P0MDOUT = 0x9D; //P0.3,P0.4Tx,P0.7LYreset P0.0(SCK),P0.2(MOSI)推免输出
P1MDOUT = 0x40; //P1.6 SPI CS 推免输出
P2MDOUT = 0x1B; //P2.0 SHT11 HTSCK,P2.1 SHT11 HTDATA,P2.3 GATE,P2.4 LY WK
P1SKIP = 0x07; //X1,X2,Vref跳过
P2SKIP = 0x04; //ADC跳过
XBR0 = 0x03; //UART0连接到端口
XBR1 = 0x50; //使能
}
/********中断初始化********/
void Interrupts_Init()
{
EIE1 = 0x08; //ADC转换结束中断使能
IE = 0x30; //UART0中断使能 TIME2
ES0=1;
}
/********中断********/
void ADC_INTI(void) interrupt 10
{
float Volt;
AD0INT=0;
ADCtemp=(ADC0H&0xFFFF)<<8|ADC0L;
if(ADCTimes<32) //转换16次,每秒钟可转换25次
{
AddADCtemp=AddADCtemp+ADCtemp;
ADCTimes++;
AD0BUSY=1;
return;
}
AD0BUSY=0;
AddADCtemp = AddADCtemp>>5;
Volt = AddADCtemp; //电压转换系数
DataTable[1]=Volt;
ADCTimes=0;
AddADCtemp=0;
AD0EN = 0; //关闭ADC,以便节省功耗
EIE1 &=0xF7; //关闭ADC中断
}
//测量电池电压函数,主函数直接调用
void MeasurePower()
{
EIE1 |= 0x08; //开启ADC中断
ADC0CN = 0x00; //AD0EN=0,低功耗模式,Buzy写1启动转换
AD0EN = 1; //使能ADC
AddADCtemp = 0;
ADCTimes = 0;
AD0BUSY = 1; //启动一次转换 Delay(1000);
EIE1 |= 0x08; //启动ADC0转换结束中断
}
引脚不够用了,定时器用了两个,也没相关参考程序,用的busy写1中断
|
|