TMR0L not counting up and not overflowing

Discussion in 'Embedded Systems and Microcontrollers' started by AustinCann, Feb 11, 2011.

  1. AustinCann

    Thread Starter New Member

    Jan 30, 2011
    12
    0
    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.

    Code ( (Unknown Language)):
    1.  
    2. void main() {
    3.  
    4. TRISA = 0x00; // set direction to be output
    5. TRISB = 0x00; // set direction to be output
    6. TRISC = 0x00; // set direction to be output
    7. TRISD = 0x00; // set direction to be output
    8. T0CON.B7=1;
    9. TMR0L=250;
    10. T0CON.B6=1;
    11. T0CON.B5=0;
    12. T0CON.B3=1;
    13. INTCON.B5=0;
    14.  
    15. while(INTCON.B2==0);
    16. {
    17. PORTA = 0x00; // Turn OFF LEDs on PORTA
    18. PORTB = 0x00; // Turn OFF LEDs on PORTB
    19. PORTC = 0x00; // Turn OFF LEDs on PORTC
    20. PORTD = 0x00; // Turn OFF LEDs on PORTD
    21. delay_ms(1000);
    22. PORTA = 0xFF; // Turn ON LEDs on PORTA
    23. PORTB = 0xFF; // Turn ON LEDs on PORTB
    24. PORTC = 0xFF; // Turn ON LEDs on PORTC
    25. PORTD = 0xFF; // Turn ON LEDs on PORTD
    26. }
    27. TMR0L=250;
    28. INTCON.B2==0;
    29. }
    30.  
    31.  
     
    Last edited by a moderator: Feb 11, 2011
  2. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    It might help if we knew what device you are using. Also which compiler.
     
  3. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    What are you trying to accomplish?

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

    Thread Starter New Member

    Jan 30, 2011
    12
    0
    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
     
  5. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    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.
     
  6. AustinCann

    Thread Starter New Member

    Jan 30, 2011
    12
    0
    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!

    Code ( (Unknown Language)):
    1.  
    2. unsigned int state, count, cw, countmax;
    3. unsigned char adcvalue;
    4. void init(void)
    5. {
    6. ANSEL.B0=0;
    7. ANSEL.B1=0;
    8. ANSEL.B2=0;
    9. ANSEL.B3=1;
    10. TRISIO.B1=0;
    11. TRISIO.B2=0;
    12. TRISIO.B5=5;
    13. TRISIO.B4=1;
    14. ANSEL.B6=1; ANSEL.B5=0;ANSEL.B4=1;
    15. ADCON0.B3=1;ADCON0.B2=0;
    16. ADCON0.B0=1;
    17. ADCON0.B7=0;
    18. TMR0=15;
    19. OPTION_REG.B2=1;
    20. OPTION_REG.B1=0;
    21. OPTION_REG.B0=0;
    22. OPTION_REG.B3=0;
    23. OPTION_REG.B5=0;
    24. INTCON.B5=1;
    25. INTCON.B7=1;
    26. state=0;
    27. count=0;
    28. cw=1;
    29. countmax=1;
    30. adcvalue=0;
    31. }
    32. void interrupt (void)
    33. {
    34. if((INTCON.B5)&&(INTCON.B2))
    35.   {
    36.   count++;
    37.   if(count>countmax)
    38.     {
    39.     if(cw==1)
    40.     {
    41.         switch(state)
    42.         {
    43.         case 0: state=1; break;
    44.         case 1: state=2; break;
    45.         case 2: state=3; break;
    46.         case 3: state=0; break;
    47.         }
    48.     }
    49.     else{
    50.         switch(state)
    51.         {
    52.         case 0: state=3; break;
    53.         case 1: state=0; break;
    54.         case 2: state=1; break;
    55.         case 3: state=2; break;
    56.         }
    57.     }
    58. count=0;
    59. }
    60. switch(state){
    61. case 0: GPIO=0X05; break;
    62. case 1: GPIO=0X21; break;
    63. case 2: GPIO=0X22; break;
    64. case 3: GPIO=0X06; break;
    65. }
    66. TMR0=190;
    67. INTCON.B2=0;
    68. }
    69. }
    70. void main (void)
    71. {
    72.  init();
    73.  while(1)
    74.  {
    75.  ADCON0.B1=1;
    76.  while(ADCON0.B1)
    77.  ;
    78.  if(ADRESH<35)
    79.  {
    80.  if(CW==1) CW=0;
    81.  else CW=1;
    82.  }
    83.  if(ADRESH>190)
    84.  countmax=1;
    85.  else
    86.  if ((ADRESH>190)&&(ADRESH<170))
    87.  countmax=20;
    88.  }
    89.  }
    90.  
    91.  
     
    Last edited by a moderator: Feb 11, 2011
  7. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Which PIC are you using/simulating?
     
  8. AustinCann

    Thread Starter New Member

    Jan 30, 2011
    12
    0
    I am using PIC18F452.

    Thanks
     
  9. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    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.
     
  10. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,014
    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?
     
  11. AustinCann

    Thread Starter New Member

    Jan 30, 2011
    12
    0
    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
     
  12. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,014
    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.
     
Loading...