void InitClock()
{
OSCCON=0x7a; //内部振荡器16M,
DelayUS(2);
while(HFIOFR==0)
;
}
void InitTimer0()
{
OPTION_REG=0x04; //分频 1:32
TMR0=0x83;
}
void InitTimer1()
{
T1CON=0x31; //分频 1:8
PIE1|=0x01;
TMR1H=0xf6;
TMR1L=0x3b;
}
void InitGPIO()
{
TRISA=0xf8;
ANSELA=0x00;
TRISB=0x05;
ANSELB=0x0;
WPUB=0x00;
IOCBP=0x05;
IOCBN=0x05;
TRISC=0x01;
TRISD=0x20;
ANSELD=0x00;
TRISE=0x06;
ANSELE=0x00;
}
void InitUSart()
{
TRISC6=1; //txd pin
TRISC7=1; //rxd pin
SPBRG=0x67; //16000000/(16*(SPBRG+1))=9600
TXSTA=0x24;
RCSTA=0x90;
BAUDCON=0x00;
PIE1|=0x30; //open usart interrupt
}
void InitAD()
{
TRISA0=1;
ANSELA|=0x01;
ADCON0=0x81;
ADCON1=0xe0; //Fosc/64 16M转换时间4us
ADIF=0;
DelayUS(30);
ADCON0|=0x02;
}
void EnableInt()
{
INTCON=0xd0; //GIE,PEIE,TMR0IE,RB0/INT,IOCIE
}
void InitSystem()
{
InitGPIO();
InitClock();
InitTimer0();
InitTimer1();
InitUSart();
EnableInt();
}
if(RB2==0&&RD1==0)
{
TMR1ON=0;
PIE1&=0xcf;
PIE1&=0xfe;
RCSTA&=0x7f;
CREN=0;
TXEN=0;
TXIE=0;
RCIE=0;
ADCON0&=0xfe;
TRISA=0xff;
TRISB=0xfa;
TRISC=0xfc;
TRISD=0xff;
TRISE=0xff;
ANSELA=0xff;
ANSELB=0xfe;
ANSELD=0xff;
ANSELE=0xff;
OSCCON=0x02;
while(LFIOFR==0)
;
SLEEP();
NOP();
NOP();
RESET();
}
进入休眠模式后,还有18mA电流,工作时23mA电流,上面代码有什么问题吗?请大家百忙之中帮我看看。谢谢! |