IF statement IGNORED

Discussion in 'Embedded Systems and Microcontrollers' started by graham2550, Jun 24, 2014.

  1. graham2550

    Thread Starter New Member

    Jun 24, 2014
    11
    1
    I am creating a simple bug repeller, all it consists of is a ramping frequency between 20khz and 61khz.
    I'm using a PIC16F688 at 20Mhz.
    I use timer0 interrupt to switch a pin on and off to create a square wave.
    I use timer1 interrupt at its slowest setting to increment the timer0 register, thus increasing the frequency.
    when the variable for the timer0 gets to a certain point (61khz, 655315) I reset the variable to 140( 20khz)
    The first pass starts correct frequency but the IF statement in the ISR to reset the numbers is totally ignored.
    Timer0 just goes from 0 to 65535. Ignoring my "if " statement it seems.
    very strange.
    anyone help please, what am I doing wrong?

    Code ( (Unknown Language)):
    1.  
    2. sbit FOUT at RC0_bit;
    3. sbit FOUT_Direction at TRISC0_bit;
    4.  
    5. unsigned int Timer_Count;
    6.  
    7. void InitTimer1(){            //.1sec
    8.  T1CON         = 0x31;
    9.   TMR1IF_bit         = 0;
    10.   TMR1H         = 0x00;
    11.   TMR1L         = 0x00;
    12.   TMR1IE_bit         = 1;
    13. }
    14.  
    15. void InitTimer0(){             //freq
    16.   OPTION_REG         = 0x88;
    17.   TMR0                 = Timer_Count;
    18.   TMR0IE_bit = 1;
    19. }
    20.  
    21. void Interrupt(){
    22.  
    23.    if (TMR0IF_bit){
    24.      TMR0IF_bit = 0;
    25.      TMR0 = Timer_Count;
    26.      FOUT = ~FOUT;
    27.   }
    28.    if(TMR1IF_bit){                          //0.2 second
    29.      TMR1IF_bit = 0;
    30.      TMR0IE_bit = 0;
    31.      Timer_Count ++;
    32.      if (Timer_Count > 65513) Timer_Count = 140;      //65513 is 61khz, 140 is 20khz
    33.      TMR0IF_bit = 0;
    34.      TMR0IE_bit = 1;
    35.   }
    36. }
    37.  
    38. void main() {
    39. ANSEL = 0;
    40. ADCON0 = 0;     //turn off analog functions
    41. CMCON0 = 7;     //turn off comparators
    42. PORTC = 0;
    43. TRISC = 0;      // port C outputs
    44.  
    45.  
    46. Timer_Count = 140;
    47. INTCON         = 0xE0;
    48. InitTimer1();       //.1 sec
    49. InitTimer0();        //freq
    50.  
    51.  
    52.  
    53. while (1)
    54.   {
    55. }
    56. }
    57.  
    58.  
     
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    Timer 0 is only 8 bits so it can't accept the 16 bit TimerCount.

    Since you are already in an ISR, you don't have to turn off TMR0IE in the TMR1 service.

    EDIT: its MicroC, yes?
    Change
    void Interrupt to
    void interrupt

    Keywords are case sensitive and you did not have any interrupt routine built (it thought 'Interrupt' was a normal routine and since it was not called, got optimized right out). Be sure to turn ON case-sensitivity for the project. Its off by default - a bad idea. From the manual:
    There are a couple of ways you can tell when this is happening:
    Start the Debugger and observe that you can't set breakpoints on any of the code in the interrupt routines - there is no code.
    Open the Listing Output window (not the Assembler Output) and observe that there is nothing built for any interrupt routine. It IS there in the assembler output but the linker optimizes it out.

    Keep in mind that MicroC's software debugger does NOT support interrupts, timer values in a watch window. You might want to create a project in MPLAB 8.x in the same folder and import the .COF and .C files from MicroC and use MPSIM. That's what I did and the problem became immediately apparent.

    Have fun.
     
    Last edited: Jun 24, 2014
  3. graham2550

    Thread Starter New Member

    Jun 24, 2014
    11
    1
    Thank you JohnInTX for waking me up. I dont know what I was thinking. I know its an 8bit timer, max 255. Thanks for the advice on case sensitivity, I'll put that right in settings. Yes MikroC I use, have been sinse I started coding PIC's.
    I want to try MPLABX, is this a good IDE and compiler?
    Anyway, with your advice now works good.
     
  4. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    You couldn't find the BUG in your bug repeller? Oooh the irony... :eek:
    ;)
     
    DerStrom8 likes this.
  5. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    MPLABX / XC8 is good and getting better as bugs are fixed. Compared to MicroC, it probably compiles tighter code and has better debugging support. It supports the cheap PICKit debuggers which support/program a wider variety of PICs. XC8 also handles baseline/midrange PICs better (in my experience).

    MicroC has more library support for easy development with LCD displays, beepers, CAN, Ethernet, USB etc. and is a bit more user-friendly.

    The free version of XC8 has no set memory limitations but it doesn't optimize like the paid version. Give them a look.
     
  6. graham2550

    Thread Starter New Member

    Jun 24, 2014
    11
    1
    Yes thats why I chose MikroC because of the library, its very good. and I use the PicKit programmer with it.
    I use MikroC for PIC32 also, supports ICD.
    But I'll definitely give MPLABX a try.
    Thanks again for your help and advice
     
  7. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Code ( (Unknown Language)):
    1.  if (TMR0IF_bit){...}
    Is TMR0IF_bit actually returning the value of that bit, or is it a constant with the location of that bit inside some register?
     
  8. graham2550

    Thread Starter New Member

    Jun 24, 2014
    11
    1
    TMR0IF is timer 0 interrupt flag.
    It gets set when the timer overflows.
    For an 8 bit timer this is 255, a 16 bit timer 65535. Timers count clock frequency /4.
    You can read it and clear it, TMR0IF =0.
    It will trigger an interrupt.
     
  9. kubeek

    AAC Fanatic!

    Sep 20, 2005
    4,670
    804
    Yes, but you are talking about TMR0IF, not TMR0IF_bit.
     
  10. graham2550

    Thread Starter New Member

    Jun 24, 2014
    11
    1
    TMR0IF_bit
     
  11. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    MikroC allows bitwise logic, and detecting or setting/clring bits directly.

    That if() statement will directly test the INTCON.TMR0IF bit and compile down to a BTFSS asm instruction.
     
  12. graham2550

    Thread Starter New Member

    Jun 24, 2014
    11
    1
    thanks for your reply THE_RB, as you will have noticed in an earlier post, this has been solved.
    anyway, the 'if' statement in question was this one: if (Timer_Count > 65513) Timer_Count = 140;
     
Loading...