Menu

Timer 1 Interrupt and actual interrupt timing error using PIC16F1827

Awais
2023-11-12
2023-11-30
  • Awais

    Awais - 2023-11-12

    Hi Evan,
    I am programming PIC16F1827 in which Timer1 is used to generate a delay of 100 msec. Code file is attached. First problem is that as per timer reload value of 15536 for 100msec, interrupt is generated at 25msec which I verified also from an oscilloscope at pin A2 whereas the following timer code in mplab generates 100msec interrupt which I verified.

    void TMR1_Initialize(void)
    {
    //Set the Timer to the options selected in the GUI

    //T1GSS T1G_pin; TMR1GE disabled; T1GTM disabled; T1GPOL low; T1GGO done; T1GSPM disabled; 
    T1GCON = 0x00;
    
    //TMR1H 60; 
    TMR1H = 0x3C;
    
    //TMR1L 176; 
    TMR1L = 0xB0;
    
    // Load the TMR value to reload variable
    timer1ReloadVal=(uint16_t)((TMR1H << 8) | TMR1L);
    
    // Clearing IF flag before enabling the interrupt.
    PIR1bits.TMR1IF = 0;
    
    // Enabling TMR1 interrupt.
    PIE1bits.TMR1IE = 1;
    
    // Set Default Interrupt Handler
    TMR1_SetInterruptHandler(TMR1_DefaultInterruptHandler);
    
    // T1CKPS 1:8; T1OSCEN disabled; nT1SYNC synchronize; TMR1CS FOSC/4; TMR1ON enabled; 
    T1CON = 0x31;
    

    }

    Kindly reply.
    Regards,
    Awais

     
    • G. C.

      G. C. - 2023-11-24

      Try to change line 106 to this:

      InitTimer1( FOsc, PS_1_8 )

      You are getting the oscillator frequency divided by 4 times using InitTimer1( Osc, PS_1_8 )...

      By preloading Timer1 with 15536 you have an overflow/interrupt every 50Kcycles.
      At 4 Mhz, 1cycle is 1uSec (because of Fosc/4), times 8 (prescaler) 8usec, times 50K cycles = overflow at 400 mS.

      If you use Fosc, at 4Mhz 1 cycle= 0.25uSec, times 8 = 2uSec, times 50K = 100mSec as expected..

      Hope this solves yr problem..
      Rgds,
      GC

       
      • Awais

        Awais - 2023-11-26

        Thanks G. C. for your reply. Now there are two issues.
        First, if I write as follows

           #chip 16f1827, 16
           #option explicit
        

        then the code does not compile and gives the following error.
        Error: Variable FOSC was not explicitly declared

        Second, without option explicit, it compiles successfully and I am getting frequency of around 40Hz at LED pins.

        But my target is to get 100msec interrupt at 16MHz internal oscillator.

        I configured Timer1 control register at bit level using the following link and got the results with output frequency of 5 Hz at LED1 pin, 50% Duty Cycle, Ton=Toff=100msec. Sample code is attached.
        http://eng-serve.com/pic/pic_timer.html

         

        Last edit: Awais 2023-11-26
        • G. C.

          G. C. - 2023-11-27

          Hi Awais,

          for what concerns the compiler error on "#option explicit", I guess is something due to the GCB's header file for yr uC, as it seems to consider a Keyword as a program variable.. I am sure Evan will look onto it and fix it. For tests purposes, leave that line commented out.

          For the Timer1 issue, I gave you the numbers for 4 MHz because that was the frequency you set in your first source code (the one named Test.gcb). It you run the uC at 16 Mhz, then you should change the row number 6 to reflect the 16Mhz speed: #chip 16f1827, 16 instead of #chip 16f1827, 4.
          Then there is an error at line 106: InitTimer1( Osc, PS_1_8 )... Change it to InitTimer1(Osc, PS1_8). You should then get the 100mSec interrupt desired.

          In your last code named led_blink there is an error at line 28: T1CON.T1OSCEN = 1. This will enable the clock input to Timer1 as if it comes from an external indipendent oscillator, and not from the uC base clock 16Mhz (Fosc/4). Change it as T1CON.T1OSCEN = 0.

          But better yet, leave the configuration of the Timer to GCB and remove all the individual bits setting.. If you have an hw breadboard set up with the two leds on Ra2/Ra3, try to load the hex I attach here, togheter with corrected source (even here you had a syntax error on the Prescaler command) and simulation workspace on Pic SimLab. Test it out and you should get the leds blinking alternatively at about 100mSec LED1, 1Sec for LED2. You might probe them with a scope on Ra2/Ra3.

          Pls let us know if this solves.

          Bye,
          GC

           
          • Awais

            Awais - 2023-11-27

            Hi G. C.,

            I will check your code. I tested LED_blink.gcb on hardware PIC and results were good.
            As per my observation, there is some issue in configuring Timer Control Register.

            11:47:30.361 -> T1CON Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
            11:47:30.401 ->
            11:47:30.401 -> 0 0 0 0 0 0 0 1

            Bits 4 and 5 should be 1 for prescale value of 1:8. See my below post.

            Regards,
            Awais

             
            • Anobium

              Anobium - 2023-11-27

              Yes, I am aware. I am tryin to determine what is incorrect. Hence, my post https://sourceforge.net/p/gcbasic/discussion/579125/thread/bf206f5808/?limit=25#0d37

               
              • Awais

                Awais - 2023-11-27

                Got it.

                 
            • G. C.

              G. C. - 2023-11-27

              I think these wrong individual bit settings are due to your syntax error in GCB source code, see the above post of mine at:

              "...... there is an error at line 106: InitTimer1( Osc, PS_1_8 )... Change it to InitTimer1(Osc, PS1_8)..... "

              It's only a typo error you made on the SetTimer command line..
              Use my source7hex and you'll see that everything works fine.
              GC

               
              • Awais

                Awais - 2023-11-27

                I will update after testing the code on hardware.
                Thanks.

                 
    • G. C.

      G. C. - 2023-11-24

      Here is a sample program that blinks two leds alternatively at 1Sec..
      Tested on the simulator, if you want to try out..
      Best,
      GC

       

      Last edit: G. C. 2023-11-24
  • Anobium

    Anobium - 2023-11-13

    The chip frequency baseline for the C is the same as the GCBASIC frequency?
    The timer clock source is the same in the C compared to GCBASIC?

    You can check the GCBASIC registers by sending to a serial terminal and then to compare to determine any differences.

    Evan

     
    • Awais

      Awais - 2023-11-22

      I checked the registers values. Thanks to William Roth for his code to Display Registers on a Terminal. https://sourceforge.net/p/gcbasic/discussion/629990/thread/f5d6355560/

      Below is the serial output.

      11:47:28.321 -> ------------------------------------------------------------
      11:47:28.361 -> OSCCON Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
      11:47:28.441 ->
      11:47:28.441 -> 0 1 1 1 1 0 0 0
      11:47:28.521 -> ------------------------------------------------------------
      11:47:28.601 ->
      11:47:28.601 ->
      11:47:28.601 -> ------------------------------------------------------------
      11:47:28.641 -> OSCSTAT Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
      11:47:28.721 ->
      11:47:28.721 -> 1 0 1 1 1 1 1 1
      11:47:28.801 -> ------------------------------------------------------------
      11:47:28.881 ->
      11:47:28.881 ->
      11:47:28.881 -> ------------------------------------------------------------
      11:47:28.921 -> PIE2 Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
      11:47:29.001 ->
      11:47:29.001 -> 0 0 0 0 0 0 0 0
      11:47:29.081 -> ------------------------------------------------------------
      11:47:29.161 ->
      11:47:29.161 ->
      11:47:29.161 -> ------------------------------------------------------------
      11:47:29.201 -> PIR2 Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
      11:47:29.281 ->
      11:47:29.281 -> 0 0 0 0 0 0 0 0
      11:47:29.361 -> ------------------------------------------------------------
      11:47:29.441 ->
      11:47:29.441 ->
      11:47:29.441 -> ------------------------------------------------------------
      11:47:29.481 -> T1CON Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
      11:47:29.561 ->
      11:47:29.561 -> 0 0 0 0 0 0 0 1
      11:47:29.641 -> ------------------------------------------------------------
      11:47:29.721 ->
      11:47:29.721 ->
      11:47:29.721 -> ------------------------------------------------------------
      11:47:29.761 -> TMR1H Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
      11:47:29.841 ->
      11:47:29.841 -> 1 1 0 1 0 1 1 0
      11:47:29.921 -> ------------------------------------------------------------
      11:47:30.001 ->
      11:47:30.001 ->
      11:47:30.001 -> ------------------------------------------------------------
      11:47:30.041 -> TMR1L Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
      11:47:30.121 ->
      11:47:30.121 -> 1 0 0 1 1 0 0 1
      11:47:30.201 -> ------------------------------------------------------------
      11:47:30.281 ->
      11:47:30.281 ->
      11:47:30.281 -> ------------------------------------------------------------
      11:47:30.361 -> T1CON Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
      11:47:30.401 ->
      11:47:30.401 -> 0 0 0 0 0 0 0 1
      11:47:30.481 -> ------------------------------------------------------------
      11:47:30.561 ->
      11:47:30.561 ->
      11:47:30.561 -> ------------------------------------------------------------
      11:47:30.641 -> PIE1 Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
      11:47:30.691 ->
      11:47:30.691 -> 0 0 0 0 0 0 0 1
      11:47:30.771 -> ------------------------------------------------------------
      11:47:30.851 ->
      11:47:30.851 ->
      11:47:30.851 -> ------------------------------------------------------------
      11:47:30.891 -> PIR1 Bit7 Bit6 Bit5 Bit4 Bit3 Bit2 Bit1 Bit0
      11:47:30.971 ->
      11:47:30.971 -> 1 0 0 1 0 0 0 0
      11:47:31.051 -> ------------------------------------------------------------

      I observed that T1CON Bits 4 and 5 (T1CKPS) are 0 instead of 1 for prescale value of 1:8 with reference to PIC16F1827 datasheet DS41391D-page 185.

      Awais

       
  • Anobium

    Anobium - 2023-11-27

    Hello, let us try to resolve this.

    Can I start by confirming the basics ? As I do not have that specific chip I need you to confirm that that following program operates at expected at 10Hz/Period=100ms.

    This is important as this proves the oscillator registers controlling the frequency are operating as expected. If the oscillator registers are not correct then the setting of the timer1 will also be incorrect.

    So, here this generates a 10Hz/Period=100ms square wave.

    Do it work for you? Please change the chip.

    #chip 16F1828, 16
    #option explicit
    
    
        #define LED1 PORTC.2
        #define LED2 PORTC.3
    
        dir LED1 out
        dir LED2 out
    
        LED1 = Off
        LED2 = Off
    
        do
          wait 50 ms 
          LED1 = !LED1
        loop
    
     

    Last edit: Anobium 2023-11-27
    • Awais

      Awais - 2023-11-27

      Please find attached the test results with PIC16F1827 along with code file.
      Regards,
      Awais

       
      • Anobium

        Anobium - 2023-11-27

        That looks correct? that is is question to confirm things.

         
  • Awais

    Awais - 2023-11-27

    I loaded two programs in PIC16 Simulator IDE to see the value of T1CON.
    File names are self explanatory.

     
  • G. C.

    G. C. - 2023-11-27

    Did you see my post on the typing error of the Init Timer command?
    I believe if you just remove the first undertscore the Compiler will create the correct code for the prescaler bits..
    GC

    **** copied down here too, for yr reference

    "...... there is an error at line 106: InitTimer1( Osc, PS_1_8 )... Change it to InitTimer1(Osc, PS1_8)..... "

    It's only a typo error you made on the SetTimer command line..
    Use my source7hex and you'll see that everything works fine.
    GC

     
    • Awais

      Awais - 2023-11-27

      Thanks, GC. I missed that underscore mistake.
      Regards,

       
    • Anobium

      Anobium - 2023-11-27

      Total brilliant spot that typo!

      :-)

       
  • Anobium

    Anobium - 2023-11-27

    Here is a revised timer.h

    This isolates AVR and PIC constants. This will prevent selection of PS_1_8 when a PIC....


    Restart GC Studio to get the latest build, then, download this file.
    Put the file in the gcbasic\include\lowlevel folder.

    Enjoy

     
  • Anobium

    Anobium - 2023-11-27

    My little test program.

    Supports GCBASIC commands and Handcranking the registers.

    :-)

     

    Last edit: Anobium 2023-11-27
  • Anobium

    Anobium - 2023-11-27

    When this thread is resolved... I think it is. :-)

    Donate here

    Please donate to the operational costs of GCBASIC for the coming year - Thank you. paypal.me/gcbasic

     
  • Awais

    Awais - 2023-11-30

    Thank you Evan, rest of the GCBasic team and G. C. for your efforts to solve the issue.
    I will test the code with new timer.h file on PIC16F1827 and will update.
    Regards,
    Awais

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.