pic16f1507-Timer1設定問題

作者 lxyz1127 於 2018年05月05日 08:36:50
請教大家,目前使用Timer1作計時,按照計算應該1秒後,RC7 1秒後要亮起來,但我測試後30幾秒才會亮,是否哪裡有弄錯了

----------------------------------------------------------
#include <htc.h>

int AD_NTC_value1,ad_h_tmp,ad_l_tmp,ad_tmp;
char mode_3_tmp,t2_scale3,t2_scale2;
unsigned short TMR1cnt;
unsigned short min,sec;
unsigned char seg_value;

int adc(int channel);
void delay(int num);
void interrupt t1(void);

__CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_ON & CP_OFF & BOREN_OFF & CLKOUTEN_OFF);
__CONFIG(WRT_OFF & STVREN_OFF & BORV_LO & LPBOR_OFF & LVP_OFF);



void main()
{
//--------Oscillation---------
OSCCON=01111011; // internal osc 16M
OSCSTAT=00010001;
//--------I/O----------------
ANSELA=0b00000100; //RA2 analog
TRISA=0b00000100; //RA2 input
PORTA=0b00000000; //all low
ANSELB=0b00000000; //all digital
TRISB=0b00010000; //RB4 in
PORTB=0b00000000; //all low
ANSELC=0x00; //all digital
TRISC=0x00; //all Output
//PORTC=0x00; //all low
//---------Interrupt Timer1-----------

//TMR2IE=1;
//T2CON=0b00000100;
T1CON=0b00000100;

TMR1IF=0; //?埃TMR1い耞篨??
//TMR1H =(65536-40000)/256; //?﹚10msい耞???
//TMR1L =(65536-40000)%256;
GIE=1;
PEIE=1;
TMR1IE=0; //開啟Timer1
TMR1ON=0; //set-up timer1
TMR1GE=0;
//----------A/D---------------
ADCON1=0b01100000;
FVRCON=00000000;

//----------------------
delay(20);
RC3=1; //red led
delay(20);
RC3=0;
delay(5);
RC4=1; //green led
delay(20);
RC4=0;

TMR1cnt=0;
mode_3_tmp=0;
seg_value=0;
min=0;
sec=0;
// RC7=0;

while(1)
{
AD_NTC_value1=adc(2);
delay(5);
if( AD_NTC_value1>=405) //check battery in
{
delay(5);
if( AD_NTC_value1>=405)
{
RC5=0;
TMR1ON=1; //set-up timer1
TMR1IE=1; //開啟Timer1
}
}

else
{
RC5=1;
RC6=0;
RC7=0;
TMR1ON=0; //set-up timer1
TMR1IE=0; //開啟Timer1
}

/*

RC5=1; //test (battery in)
if(RB4)
{
delay(3);
if(RB4)
{
RC4=1; //Green led
}
}
else
{
RC4=0;
}
}
}

else
{
RC5=0; // (battery in)
RC4=0;
}
*/
}
}



//================================================
int adc(int channel)
{

ADCON0=0b10000001 | (channel<<2);

GO=1;
while(GO)
{}
GO=1;
while(GO)
{}
ad_h_tmp=ADRESH<<2;
ad_l_tmp=ADRESL>>6;
ad_tmp=ad_h_tmp+ad_l_tmp;
return(ad_tmp);
}
/*
void interrupt t2(void)
{

TMR2IF=0;
if (mode_3_tmp)
{
if (t2_scale3>=17) //AC送電後,先等待2-3秒
{
t2_scale3=0;

if(t2_scale2>=1) //wait time choise
{
t2_scale2=0;
//tmp^=1;
//RB4=tmp;


}
t2_scale2++;
}
t2_scale3++;
}
}
*/

void interrupt t1(void)
{
if(TMR1IF==1)
{
TMR1IF = 0; // 埃TMR1い耞篨?TMR1IF
TMR1H =(65536-40000)/256 ; //?﹚10msい耞???
TMR1L =(65536-40000)%256 ;
TMR1cnt++;
if (TMR1cnt>=100) //100*10ms=1s
{
RC6=1;
TMR1cnt = 0;
sec++;
if(sec>=1) //1秒(1秒1次))
{
RC7=1;
sec=0;
}
}
}
}




void delay(int num)
{
int aa,bb;
aa=num;
while(aa)
{
bb=num;
while(bb)
{bb--;}
aa--;
}
}

來自: http://www.microchip.com.tw/newbb/viewtopic.php?forum=1&topic_id=22286&post_id=78116