请高手帮忙,小弟第一次用HOLTEK单片机做项目,就遇到问题.
程序如下,不知道怎么回事,没次上电开机后,都会执行下面红色的部分.就算我把该AD0口接地也还会进入,搞不懂,请高手帮忙看看!
#include <HT46R064.h>
#pragma vector isr_4 @ 0x4 //external interrupt
#pragma vector isr_8 @ 0x8 //timer/event 0
#pragma vector isr_c @ 0xc //A/D Conversion Complete
#pragma vector isr_10 @ 0x10 //Time Base Overflow
#define DELAY_MS_PARA 122
//ISR for safequard
void isr_10(){}
//unsigned long ad_current;
unsigned long ad_temperature;
unsigned long ad_voltage;
unsigned long adresult;
unsigned char i,j;
bit adflag,flag;
//initialize registers for safeguard
void safeguard_init()
{
_intc0 = 0;
_intc1 = 0;
_tmr0c = 0;
_tmr0 = 0;
_pac = 0xff;
_pbc = 0xff;
}
void initial();
void aditial();
//void pwmitial();
void timeitial();
void extinitial();
void delay_1ms();
void delay_ms(unsigned int num);
void main()
{
safeguard_init();
initial();
_pb0 = 1; //电源自锁
//delay_ms(5); //10ms
flag = 0;
i=0; //红色LED初始化
j=0;
adresult = 0;
aditial();
_start=0;
_start=1;
_start=0; //ado go
timeitial();
_emi = 1; //开总中断
extinitial();
while(1)
{
if(flag==1)
{
if(ad_voltage < 0x94d) //小于6V
{
_pb0 = 0; //关闭电源
delay_ms(50); //delay 100ms
}
if((ad_voltage <= 0xa13)&&(ad_voltage>=0x94d)) //>=6V <=6.5v
{
// _pac4 = 0;
_pa4 = 1; //打开LED
// _pb3 = 1;
// _pb2 = 1; //测试用
_t0on = 1; //红色LED闪烁
}
if(ad_voltage > 0xa13) //>6.5v
{
// _pac4 = 0;
_pa4 = 1; //打开LED
// _pb3 = 1; //测试用
_t0on = 0;
_pb5 = 0;
}
if(ad_temperature < 0x4b9)
{
_pb0 = 0; //关闭电源
delay_ms(50); //delay 100ms
// _t0on = 1;
}
// else _t0on = 0;
/*if(_pac3==1)
{
_pb0 = 1;
pwmitial();
aditial();
_pa4 = 1; //pwm go
pwmitial();
_start=0;
_start=1;
_start=0; //ado go
_emi = 1; //开总中断
}*/
}
}
}
void initial()
{
_pac3 = 1; //set port A to input port
_pac4 = 0;
_pa4 = 0;
_pac0 = 1;
_pac1 = 1;
_pbc = 0; //set port B to output port
_pb = 0;
}
void aditial()
{
_acsr=0x01; //system clock/8
// _adcs2 = 0;
// _adcs1 = 1;
// _adcs0 = 0;
_adcr=0x50; //an0~an1 , an0:外部电压检测
// _pcr2 = 0;
// _pcr1 = 1;
// _pcr0 = 0;
// _adonb = 0;
// _acs2 = 0;
// _acs1 = 0;
// _acs0 = 0;
// _adcr = 0x19; //准备删
adflag = 0;
_ade = 1; //AD中断允许
}
/*void pwmitial()
{
_pwm0 = 256; //duty:50/128 31k
_pwmsel = 0; //6+2
_pwmc0 = 1; //select pin PA4 to have a PWM function
}*/
void timeitial()
{
_tmr0c = 0x87;
_tmr0 = 0;
_t0e = 1;
}
void extinitial()
{
_integ1 = 0;
_integ0 = 1; //上升沿
_inte = 1;
}
void isr_c() //ad int
{
unsigned long ad_temp;
*((unsigned char *)&ad_temp+1)=_adrh;
*((unsigned char *)&ad_temp+0)=_adrl;
ad_temp = (ad_temp>>4);
_adf = 0;
if(adflag==0)
{
adresult = adresult + ad_temp;
j++;
if(j==16)
{
//j=0;
ad_voltage = (adresult>>4);
_acs2 = 0;
_acs1 = 0;
_acs0 = 1;
// _acs2 = 0;
// _acs1 = 1;
// _acs0 = 0;
adflag = 1; //切换通道AN1
adresult = 0;
}
}
else
{
adresult = adresult + ad_temp;
j++;
if(j==32)
{
flag=1;
j=0;
ad_temperature = (adresult>>4);
_acs2 = 0;
_acs1 = 0;
_acs0 = 0;
// _acs2 = 0;
// _acs1 = 0;
// _acs0 = 1;
adflag = 0; //切换AN0
adresult = 0;
}
}
_start=0;
_start=1;
_start=0; //ado go
}
void isr_8() //time 0
{
_t0f = 0;
i++;
if(i == 50)
{
i=0;
_pb5 = ~_pb5;
}
}
void isr_4() //external interrupt
{
_intf = 0;
_pb0 = 0; //关闭总电源
// delay_ms(50);
}
void delay_1ms(void)
{
unsigned int i;
for(i=0;i<(DELAY_MS_PARA);i++);
}
void delay_ms(unsigned int num)
{
unsigned int i;
for(i=0;i<num;i++) delay_1ms();
} |