[SOLVED] PIC doesn't rise its timer0 interrupt flag.

Status
Not open for further replies.

mig29fulcrum

Junior Member level 1
Joined
Sep 29, 2011
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,391
Hi...
I have written code for timer0 in which every time TMR0 overflows,it toggles RC0,but i used TMR0IF to indicate that TMR0L and TMR0H are overflow,but when overflow occurs pic doesn't rise its TMR0IF flag and TMR0L resets to 00(TMR0H stays on 0xff). I will appreciate if you help me with this.
Thank you:-D
Code:
#include<P18F2550.h>
#pragma config FOSC=INTOSC_EC
#pragma config WDT=OFF
void main()
{
	TRISCbits.TRISC0=0;
	OSCCON=0x02;
	OSCTUNE=0X80;
	while(1)
        {
		T0CON=0x08;
		TMR0L=0XF0;
		TMR0H=0XFF;
		INTCONbits.TMR0IF=0;
		T0CONbits.TMR0ON=1;
		while(INTCONbits.TMR0IF==0);
		PORTCbits.RC0^=1;
		T0CONbits.TMR0ON=0;
	}
}
I also have same problem with the following code when i use timer0 as 16-bit one(in 8-bit mode everything is ok).
Code:
			LIST P=PIC18F4550
	#INCLUDE P18F4550.INC 
	CONFIG FOSC=INTOSC_EC
	CONFIG IESO=ON
	CONFIG WDT=ON
	ORG   0H
	BCF   TRISC,0
	MOVLW 0X02
	MOVWF OSCCON
	MOVLW 0X08
	MOVWF T0CON
AGAIN
	MOVLW 0XF0
	MOVWF TMR0L
	MOVLW 0XFF
	MOVWF TMR0H
	BCF   INTCON,TMR0IF
	BSF   T0CON,TMR0ON
CHECK
	BTFSS INTCON,TMR0IF
	BRA   CHECK
	BCF   T0CON,TMR0ON
	BTG   LATC,0
	BRA   AGAIN
	END
 

Reference PIC18F2455/2550/4455/4550 Data Sheet, pg 128, Section 11.2 Timer0 Reads and Writes in 16-Bit Mode


The above section indicates that to store a 16-bit value in the TIMER0 registers you must first write the TMR0H followed by the write to TMR0L which triggers both values to be written into the timer module. When reading the TIMER0 16-bit value you must perform the reads exactly opposite, first read TMR0L followed by a read of TMR0H.

Therefore make the following code changes:

Code:
#include<P18F2550.h>
#pragma config FOSC=INTOSC_EC
#pragma config WDT=OFF
void main()
{
	TRISCbits.TRISC0=0;
	OSCCON=0x02;
	OSCTUNE=0X80;
	while(1)
        {
		T0CON=0x08;
		[COLOR="#FF0000"]TMR0H=0XFF;   //switch the TMR0H and TMR0L writes as shown here
		TMR0L=0XF0;[/COLOR]
		INTCONbits.TMR0IF=0;
		T0CONbits.TMR0ON=1;
		while(INTCONbits.TMR0IF==0);
		PORTCbits.RC0^=1;
		T0CONbits.TMR0ON=0;
	}
}

Perform a similar switch in your assembly code version.

Hope the info helps,

BigDog
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…