TMR0L not counting up and not overflowing

Thread Starter

AustinCann

Joined Jan 30, 2011
12
Hello there. I am new with timers and I want to see timer0 overflowing and the value of TMR0L counting up. I got the code ready but not sure what I am missing. My code is below. Please help !

The main should start with initial value of 250 for TMR0L as long as INTCON.B2 TMR0IF is not set as 1. The TMR0L value should increase upto 255 and then overflow and start again from zero. After main() it should be again set to 255 and the last instruction clears the overflow flag.

Rich (BB code):
void main() {

TRISA = 0x00; // set direction to be output
TRISB = 0x00; // set direction to be output
TRISC = 0x00; // set direction to be output
TRISD = 0x00; // set direction to be output
T0CON.B7=1;
TMR0L=250;
T0CON.B6=1;
T0CON.B5=0;
T0CON.B3=1;
INTCON.B5=0;

while(INTCON.B2==0); 
{
PORTA = 0x00; // Turn OFF LEDs on PORTA
PORTB = 0x00; // Turn OFF LEDs on PORTB
PORTC = 0x00; // Turn OFF LEDs on PORTC
PORTD = 0x00; // Turn OFF LEDs on PORTD
delay_ms(1000); 
PORTA = 0xFF; // Turn ON LEDs on PORTA
PORTB = 0xFF; // Turn ON LEDs on PORTB
PORTC = 0xFF; // Turn ON LEDs on PORTC
PORTD = 0xFF; // Turn ON LEDs on PORTD 
}
TMR0L=250;
INTCON.B2==0;
}
 
Last edited by a moderator:

thatoneguy

Joined Feb 19, 2009
6,359
What are you trying to accomplish?

Blinking an LED with an interrupt? You shouldn't poll the interrupt control register for that.
 

Thread Starter

AustinCann

Joined Jan 30, 2011
12
I read the timer register overflow from 255 to zero. And each instruction takes 4 times the oscillation rate provided prescaler is not turned on.
To start with controlling program rate, I want to see in simulation mode the value of TMR0L ramping up on each oscillation. However it is staying zero.

I am using PIC18F452 and Mikroelectronica compiler
 

AlexR

Joined Jan 16, 2008
732
Why are you pre-loading TMR0 with 250? By the time your program gets to the "while(INTCON.B2==0); " command timer 0 is quite likely to have over-flowed and the condition will never be satisfied. I personally would clear TMR0 to make sure that the timer is running when you reach the "while(INTCON.B2.....)" instruction.

Also you need to add an endless loop to your main function. As it stands the program will terminate as soon as timer0 overflows.
 

Thread Starter

AustinCann

Joined Jan 30, 2011
12
In watch window where I have selected TMR0L, every fourth time I click "step into" the value in TMR0L should increase by 1 in theory( 4/Fosc). And when the count exceed by 1 from 255, the interrupt routine should run. However in my code below it waits for ever in while(ADCON0.B1) loop. Why! Why God why!

Rich (BB code):
unsigned int state, count, cw, countmax;
unsigned char adcvalue;
void init(void)
{
ANSEL.B0=0;
ANSEL.B1=0;
ANSEL.B2=0;
ANSEL.B3=1;
TRISIO.B1=0;
TRISIO.B2=0;
TRISIO.B5=5;
TRISIO.B4=1;
ANSEL.B6=1; ANSEL.B5=0;ANSEL.B4=1;
ADCON0.B3=1;ADCON0.B2=0;
ADCON0.B0=1;
ADCON0.B7=0;
TMR0=15;
OPTION_REG.B2=1;
OPTION_REG.B1=0;
OPTION_REG.B0=0;
OPTION_REG.B3=0;
OPTION_REG.B5=0;
INTCON.B5=1;
INTCON.B7=1;
state=0;
count=0;
cw=1;
countmax=1;
adcvalue=0;
}
void interrupt (void)
{
if((INTCON.B5)&&(INTCON.B2))
  {
  count++;
  if(count>countmax)
    {
    if(cw==1)
    {
        switch(state)
        {
        case 0: state=1; break;
        case 1: state=2; break;
        case 2: state=3; break;
        case 3: state=0; break;
        }
    }
    else{
        switch(state)
        {
        case 0: state=3; break;
        case 1: state=0; break;
        case 2: state=1; break;
        case 3: state=2; break;
        }
    }
count=0;
}
switch(state){
case 0: GPIO=0X05; break;
case 1: GPIO=0X21; break;
case 2: GPIO=0X22; break;
case 3: GPIO=0X06; break;
}
TMR0=190;
INTCON.B2=0;
}
}
void main (void)
{
 init();
 while(1)
 {
 ADCON0.B1=1;
 while(ADCON0.B1)
 ;
 if(ADRESH<35)
 {
 if(CW==1) CW=0;
 else CW=1;
 }
 if(ADRESH>190)
 countmax=1;
 else
 if ((ADRESH>190)&&(ADRESH<170))
 countmax=20;
 }
 }
 
Last edited by a moderator:

thatoneguy

Joined Feb 19, 2009
6,359
Does it work in simulation? In Mikro C/Basic/etc, there's a debugging mode after compiling that lets you step through code line by line and see what all the registers are doing. Verify the correct timer has the scale you want and is working in simulation.
 

spinnaker

Joined Oct 29, 2009
7,830
Looks like you are referencing TMR0IE (Bit5) in your interrupt routine. There should be no reason to do this. Just check for TMR0IF.


Doesn't your compiler have TMR0IF and TMR0IE references? Seems awfully confusing to me to have to reference the bit number rather than there function.

How do you know the interrupt routine is not being called and it is your interrupt code that is faulty? Have you done any debugging?
 

Thread Starter

AustinCann

Joined Jan 30, 2011
12
I have done debugging and the timer register does not count up. The execution stays in while loop ( while (ADCON.B1) for ever and interrupt does not execute.
I tried it many times and I am failing. I read the datasheet and throughly followed instructions. Does not understand why its not being interrupted by the ISR.
Am I missing some some code? Am I initializing the TIMER module properly?
Many thanks in advance
 

spinnaker

Joined Oct 29, 2009
7,830
Exactly what did you do to debug your code? Did you place a break point at the top of the interrupt routine? It timer 0 is the only interrupt enabled, it should be the only on tripping the interrupt routine.

Here is a tip. Start simple. You have tons of code that is not really relevant to your interrupt problem. Write a simple interrupt routine just to get timer 0 interrupt working.
 
Top