我想通过定时器3定时,向AD0BUSY位写1启动转换,为了验证能不能实现,我在主函数写入P1=0x81;,ADC中断函数里别写入P1 |= 0x40,然后用万用表测P10,P17可以得到高电平,但P16还是0,那就说明没有实现ad转换,那我的程序哪里不对呢?求大神们指导~
#include "compiler_defs.h"
#include "C8051F040_defs.h"
unsigned int flag=0;
// Peripheral specific initialization functions,
// Called from the Init_Device() function
void Reset_Sources_Init()
{
WDTCN = 0xDE;
WDTCN = 0xAD; //关闭看门狗
}
void Port_IO_Init() //端口配置
{
SFRPAGE = CONFIG_PAGE;
P1MDOUT = 0xFF;
XBR2 = 0x40;
}
void Voltage_Reference_Init()
{
SFRPAGE = ADC0_PAGE;
REF0CN = 0x03;
}
void ADC_Init() //ADC初始化程序
{
SFRPAGE = ADC0_PAGE;
AMX0SL = 0x04; //通道选择
HVA0CN = 0x80;
ADC0CN = 0x80; //向AD0BUSY写1启动转换
}
void Timer_Init() //定时器初始化
{
SFRPAGE = TMR3_PAGE;
TMR3CN = 0x04; //允许定时器3
RCAP3L = 0xFF;
RCAP3H = 0x5F;
}
void Interrupts_Init()
{
IE = 0x80; //使能所有中断
EIE2 = 0x03; //使能AD0INT中断,定时器3中断
}
void Oscillator_Init() //时钟初始化及晶振选择
{
int i = 0;
SFRPAGE = CONFIG_PAGE;
OSCXCN = 0x67;
for (i = 0; i < 3000; i++); //等待外部晶振稳定
while ((OSCXCN & 0x80) == 0);
CLKSEL = 0x01;
}
void Init_Device(void) //调用上面的配置函数
{
Reset_Sources_Init();
Port_IO_Init();
Voltage_Reference_Init();
ADC_Init();
Oscillator_Init();
Timer_Init();
Interrupts_Init();
EA=1;
}
void main()
{
Init_Device();
P1=0x00;
while(1)
{
if(flag==1)
{
P1=0x81;
}
}
}
void Timer3_isr(void) interrupt 14
{
if(TMR3CN & 0x80) //判断TF3是否溢出
{
flag=1;
TMR3CN &=~(1 << 7); //把TF3位置1
SFRPAGE = ADC0_PAGE;
AD0INT=0; //将转换结束标志位置0
AD0BUSY=1; //启动转换
}
}
void ADC0_isr(void) interrupt 15
{
if(ADC0CN & 0x20) //判断AD0INT位是否为1,转换是否完成
{
P1 |= 0x40;
}
SFRPAGE = ADC0_PAGE;
ADC0CN &= ~(1 << 5); //将AD0INT清0
}
注:void Init_Device(void) 中的这些配置函数是都通过configuration wizard2软件生成的,不知道是否有所缺漏,请大神指教 |