• AVR Freaks

Hot!PIC18F45K80 timer interrupt

Page: 12 > Showing page 1 of 2
Author
Djsarkar
Senior Member
  • Total Posts : 155
  • Reward points : 0
  • Joined: 2020/07/27 01:14:06
  • Location: 0
  • Status: offline
2020/09/25 00:44:52 (permalink)
0

PIC18F45K80 timer interrupt

 
I have PIC18F45K80. I am using MPLABX 5.40 and XC8 2.30. I want to generate interrupt at every 1 seconds
 
I have written program Its suppose to change LED states when interrupt occur 

/*
 * File:   blink.c
 * Author: Embedded System
 *
 */
#define _XTAL_FREQ 20000000
#include <xc.h>

// PIC18F45K80 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1L
#pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
#pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
#pragma config SOSCSEL = HIGH   // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
#pragma config XINST = OFF       // Extended Instruction Set (Enabled)

// CONFIG1H
#pragma config FOSC = INTIO2    // Oscillator (Internal RC oscillator)
#pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

// CONFIG2L
#pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
#pragma config BOREN = SBORDIS  // Brown Out Detect (Enabled in hardware, SBOREN disabled)
#pragma config BORV = 3         // Brown-out Reset Voltage bits (1.8V)
#pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)

// CONFIG2H
#pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)

// CONFIG3H
#pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
#pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
#pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Overflow Reset (Enabled)
#pragma config BBSIZ = BB2K     // Boot Block Size (2K word Boot Block size)

// CONFIG5L
#pragma config CP0 = OFF        // Code Protect 00800-01FFF (Disabled)
#pragma config CP1 = OFF        // Code Protect 02000-03FFF (Disabled)
#pragma config CP2 = OFF        // Code Protect 04000-05FFF (Disabled)
#pragma config CP3 = OFF        // Code Protect 06000-07FFF (Disabled)

// CONFIG5H
#pragma config CPB = OFF        // Code Protect Boot (Disabled)
#pragma config CPD = OFF        // Data EE Read Protect (Disabled)

// CONFIG6L
#pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)
#pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)
#pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)
#pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)

// CONFIG6H
#pragma config WRTC = OFF       // Config. Write Protect (Disabled)
#pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
#pragma config WRTD = OFF       // Data EE Write Protect (Disabled)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)
#pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)
#pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)
#pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)

// CONFIG7H
#pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

 

void main(void) {
   TRISB = 0; //RB as Output Port
  
    RB4 = 0;  // LED OFF
   
//Timer0
//Prescaler 1:128; TMR0 Preload = 26473; Actual Interrupt Time : 1.0000128 s

  T0CON  = 0x86;
  TMR0H  = 0x67;
  TMR0L  = 0x69;
 
  GIE = 1;  // Global interrupt enable bit
  TMR0IE  = 1; //Timer0 overflow enable bit
 
  
  while(1)
  {
   
  }
   
  
    return ;
}

void Interrupt(){
  if (TMR0IF == 1) // Timer0 overflow interrupt flag bit
  { 
    TMR0IF = 0;
    RB4 =~ RB4;  // LED ON
    TMR0H  = 0x67;
    TMR0L  = 0x69;
 
  }
}


 
When I run the code LED is not turning ON. What's the problem in code ? 
#1

30 Replies Related Threads

    Hen
    Senior Member
    • Total Posts : 97
    • Reward points : 0
    • Joined: 2018/10/24 04:01:44
    • Location: 0
    • Status: online
    Re: PIC18F45K80 timer interrupt 2020/09/25 01:26:44 (permalink)
    0
    And then I wondered if RB4 was shared with an analog pin, like AN9?
    #2
    Hen
    Senior Member
    • Total Posts : 97
    • Reward points : 0
    • Joined: 2018/10/24 04:01:44
    • Location: 0
    • Status: online
    Re: PIC18F45K80 timer interrupt 2020/09/25 01:39:37 (permalink)
    0
    Then realizing that don't matter for digital output pins?
     
    Have you confirmed your LED can be lit?
    Are you sure the interrupt is taken and where you expect it to be?
    The not semes legit but i woud go for RB4 ^= 1;
    #3
    ric
    Super Member
    • Total Posts : 28676
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC18F45K80 timer interrupt 2020/09/25 02:26:50 (permalink)
    +1 (1)
    Hen
    Then realizing that don't matter for digital output pins?

    Actually it DOES matter if you are silly enough to write to a pin via the PORTx register, which is what using RB4 does.
    Change all reference to RB4 to use LATB4 instead, and the problem will go away.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    Hen
    Senior Member
    • Total Posts : 97
    • Reward points : 0
    • Joined: 2018/10/24 04:01:44
    • Location: 0
    • Status: online
    Re: PIC18F45K80 timer interrupt 2020/09/25 02:46:47 (permalink)
    +1 (1)
    ric
    Actually it DOES matter if you are silly enough to write to a pin via the PORTx register, which is what using RB4 does.
    Change all reference to RB4 to use LATB4 instead, and the problem will go away.

    Mmm, even if it's superfluous I always fibble with the ANSEL regardless of the signal direction...
    ...and yes using the LAT instead of PORT can save *lives*
    #5
    Djsarkar
    Senior Member
    • Total Posts : 155
    • Reward points : 0
    • Joined: 2020/07/27 01:14:06
    • Location: 0
    • Status: offline
    Re: PIC18F45K80 timer interrupt 2020/09/25 02:52:37 (permalink)
    +1 (1)
    I have just started to work on PIC microcontroller. Can you tell me which port pin use for LED ? 
    #6
    Hen
    Senior Member
    • Total Posts : 97
    • Reward points : 0
    • Joined: 2018/10/24 04:01:44
    • Location: 0
    • Status: online
    Re: PIC18F45K80 timer interrupt 2020/09/25 03:10:36 (permalink)
    +1 (1)
    You can select (for a very few exceptions) any port pin.
    The chapter 11 (I/O Ports) of the data sheet tells you the details.
     
    Table 11-3 tells you that RB4 can drive a LED (the amount of current you can drive is specified in the electrical part of the document (page 545/546) but usually a tiny LED is no problem.
     
    #7
    ric
    Super Member
    • Total Posts : 28676
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC18F45K80 timer interrupt 2020/09/25 03:46:10 (permalink)
    +2 (2)
    Djsarkar
    I have just started to work on PIC microcontroller. Can you tell me which port pin use for LED ?

    RB4 and LATB4 access the same pin.
    Only use RB4 to read the state of the pin. Use LATB4 to write to the pin.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    Djsarkar
    Senior Member
    • Total Posts : 155
    • Reward points : 0
    • Joined: 2020/07/27 01:14:06
    • Location: 0
    • Status: offline
    Re: PIC18F45K80 timer interrupt 2020/09/25 04:22:30 (permalink)
    0
    ric
    Djsarkar
    I have just started to work on PIC microcontroller. Can you tell me which port pin use for LED ?

    RB4 and LATB4 access the same pin.
    Only use RB4 to read the state of the pin. Use LATB4 to write to the pin.
     


    I have replaced line but still my led is not turning on 

    /*
     * File:   blink.c
     * Author: Embedded System
     *
     */
    #define _XTAL_FREQ 20000000
    #include <xc.h>

    // PIC18F45K80 Configuration Bit Settings

    // 'C' source line config statements

    // CONFIG1L
    #pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
    #pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
    #pragma config SOSCSEL = HIGH   // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
    #pragma config XINST = OFF       // Extended Instruction Set (Enabled)

    // CONFIG1H
    #pragma config FOSC = INTIO2    // Oscillator (Internal RC oscillator)
    #pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
    #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
    #pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

    // CONFIG2L
    #pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
    #pragma config BOREN = SBORDIS  // Brown Out Detect (Enabled in hardware, SBOREN disabled)
    #pragma config BORV = 3         // Brown-out Reset Voltage bits (1.8V)
    #pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)

    // CONFIG2H
    #pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
    #pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)

    // CONFIG3H
    #pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
    #pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
    #pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)

    // CONFIG4L
    #pragma config STVREN = ON      // Stack Overflow Reset (Enabled)
    #pragma config BBSIZ = BB2K     // Boot Block Size (2K word Boot Block size)

    // CONFIG5L
    #pragma config CP0 = OFF        // Code Protect 00800-01FFF (Disabled)
    #pragma config CP1 = OFF        // Code Protect 02000-03FFF (Disabled)
    #pragma config CP2 = OFF        // Code Protect 04000-05FFF (Disabled)
    #pragma config CP3 = OFF        // Code Protect 06000-07FFF (Disabled)

    // CONFIG5H
    #pragma config CPB = OFF        // Code Protect Boot (Disabled)
    #pragma config CPD = OFF        // Data EE Read Protect (Disabled)

    // CONFIG6L
    #pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)
    #pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)
    #pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)
    #pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)

    // CONFIG6H
    #pragma config WRTC = OFF       // Config. Write Protect (Disabled)
    #pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
    #pragma config WRTD = OFF       // Data EE Write Protect (Disabled)

    // CONFIG7L
    #pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)
    #pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)
    #pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)
    #pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)

    // CONFIG7H
    #pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)

    // #pragma config statements should precede project file includes.
    // Use project enums instead of #define for ON and OFF.

     

    void main(void) {
       TRISB = 0; //RB as Output Port
      
         LATB4  = 0;  // LED OFF
       
    //Timer0
    //Prescaler 1:128; TMR0 Preload = 26473; Actual Interrupt Time : 1.0000128 s

      T0CON  = 0x86;
      TMR0H  = 0x67;
      TMR0L  = 0x69;
     
      GIE = 1;  // Global interrupt enable bit
      TMR0IE  = 1; //Timer0 overflow enable bit
     
      
      while(1)
      {
       
      }
       
      
        return ;
    }

    void Interrupt(){
      if (TMR0IF == 1) // Timer0 overflow interrupt flag bit
      { 
        TMR0IF = 0;
         LATB4  =~  LATB4 ;  // LED ON
        TMR0H  = 0x67;
        TMR0L  = 0x69;
     
      }
    }


    #9
    davekw7x
    Entropy++
    • Total Posts : 1891
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Second star on the right, straight on till morning
    • Status: offline
    Re: PIC18F45K80 timer interrupt 2020/09/25 10:37:02 (permalink)
    +3 (3)
    Djsarkar
    ...my led is not turning on 

    ...
    void Interrupt()
    ...


    You are enabling the timer interrupt, but you have not defined a timer ISR (Interrupt Service Routine)

    The following defines a (non-ISR) user function in your code.  It is not an interrupt service routine:

    void Interrupt()
    {
      // Some Stuff
    }



    Look in the XC8 user's guide in your XC8 distribution.

    The information in Section 5.8.1 of MPLAB_XC8_C_Compiler_User_Guide_For_PIC.pdf in the docs directory works for XC8 version  2.30 in C90 or C99 mode

    Additional note:
    Your definition for _XTAL_FREQ tells me that you think the system is running at 20 MHz, and I assume that your timer settings were calculated using that value.  In fact I think your system is running at 8 MHz, so if you find that your LED indicates timer overflow about every 2.5 seconds, you will have to make some adjustments.

    Here's my suggestion:  First of all, do not enable interrupts and make your main loop look like this:

    // My definitions for 18F26K80 on Curiosity HPC use the LED on RA5
    // Make  changes, if necessary, for your hardware setup
    #define LED1 LATAbits.LATA5
    #define LED1_TRIS TRISAbits.TRISA5
    void main(void)
    {
        // Your initialization, but do not enable interrupts
        LED1 = 0;
        LED1_TRIS = 0;
        while (1) {
            LED1 = ~LED1;
            __delay_ms(1000);
        }
    }

    If the LED blinks at all, it will verify that your system is running.  If it blinks one second on / one second off, it indicates your definition for _XTAL_FREQ is correct

    If it blinks 2.5 seconds on  / 2.5 seconds off, it means that your system is actually running at 20 MHz / 2.5 = 8 MHz

    Now, without making any other changes, comment out the statements in my while() loop and enable interrupts.  Change the definition of your interrupt function to valid ISR notation defined in the users manual, and you should see the LED blink at the same rate it did in the previous test.

    Bottom line: That's the way your code works with a PIC18F46K80 on my Curiosity HPC board with XC8 version 2.30 in  C90 mode and in C99 mode.  (Once I made changes to the way your Interrupt() function is  defined so that it is an ISR)

    Regards,

    Dave
       


    post edited by davekw7x - 2020/09/25 11:53:18

    Sometimes I just can't help myself...
    #10
    Djsarkar
    Senior Member
    • Total Posts : 155
    • Reward points : 0
    • Joined: 2020/07/27 01:14:06
    • Location: 0
    • Status: offline
    Re: PIC18F45K80 timer interrupt 2020/09/25 18:30:48 (permalink)
    0
    davekw7x
    Here's my suggestion:  First of all, do not enable interrupts and make your main loop look like this:

    // My definitions for 18F26K80 on Curiosity HPC use the LED on RA5
    // Make  changes, if necessary, for your hardware setup
    #define LED1 LATAbits.LATA5
    #define LED1_TRIS TRISAbits.TRISA5
    void main(void)
    {
        // Your initialization, but do not enable interrupts
        LED1 = 0;
        LED1_TRIS = 0;
        while (1) {
            LED1 = ~LED1;
            __delay_ms(1000);
        }
    }

    If the LED blinks at all, it will verify that your system is running.  If it blinks one second on / one second off, it indicates your definition for _XTAL_FREQ is correct

    If it blinks 2.5 seconds on  / 2.5 seconds off, it means that your system is actually running at 20 MHz / 2.5 = 8 MHz

    Now, without making any other changes, comment out the statements in my while() loop and enable interrupts.  Change the definition of your interrupt function to valid ISR notation defined in the users manual, and you should see the LED blink at the same rate it did in the previous test.

    Bottom line: That's the way your code works with a PIC18F46K80 on my Curiosity HPC board with XC8 version 2.30 in  C90 mode and in C99 mode.  (Once I made changes to the way your Interrupt() function is  defined so that it is an ISR)

    Regards,

    Dave
       

    When I run your code led doesn't flash so I tried mine code 
     

    void main(void)
    {
        // Your initialization, but do not enable interrupts
         TRISB = 0; //RB as Output Port

         RB4  = 0;  // Pin Low
        while (1) {
        RB4  = 1;  // Pin Low
        __delay_ms(1000); 
        RB4  = 0;  // Pin Low
         __delay_ms(1000); 
        }
    }


     
    When I run my code LED flash for 2.5 seconds 
     
    #11
    ric
    Super Member
    • Total Posts : 28676
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC18F45K80 timer interrupt 2020/09/25 18:42:42 (permalink)
    +2 (2)
    Djsarkar
    When I run your code led doesn't flash

    I guess you ignored this comment in the code

    // My definitions for 18F26K80 on Curiosity HPC use the LED on RA5
    // Make  changes, if necessary, for your hardware setup
    #define LED1 LATAbits.LATA5
    #define LED1_TRIS TRISAbits.TRISA5

    If you didn't change the references from "A5" to "B4" in the code, then it would be flashing the wrong pin.
     

    When I run my code LED flash for 2.5 seconds

    Which confirms Dave's guess that your PIC is really running at 8MHz.
    The "#define _XTAL_FREQ" does not control the clock.
    It just tells the compiler how fast the clock is running so it can calculate delays for __delay_ms() correctly.
    If you want to change the clock from the default 8MHz, then it is your job to add code to write to the registers to do that.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #12
    Djsarkar
    Senior Member
    • Total Posts : 155
    • Reward points : 0
    • Joined: 2020/07/27 01:14:06
    • Location: 0
    • Status: offline
    Re: PIC18F45K80 timer interrupt 2020/09/25 23:18:19 (permalink)
    0
    ric
    Which confirms Dave's guess that your PIC is really running at 8MHz.
    The "#define _XTAL_FREQ" does not control the clock.
    It just tells the compiler how fast the clock is running so it can calculate delays for __delay_ms() correctly.
    If you want to change the clock from the default 8MHz, then it is your job to add code to write to the registers to do that.



    When I run my code LED flash for 2.5 seconds
     

     
    #define _XTAL_FREQ 20000000
    #include <xc.h>

     
    // PIC18F45K80 Configuration Bit Settings

     
    // 'C' source line config statements

     
    // CONFIG1L
    #pragma config RETEN = OFF      // VREG Sleep Enable bit (Ultra low-power regulator is Disabled (Controlled by REGSLP bit))
    #pragma config INTOSCSEL = HIGH // LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
    #pragma config SOSCSEL = HIGH   // SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
    #pragma config XINST = OFF       // Extended Instruction Set (Enabled)

     
    // CONFIG1H
    #pragma config FOSC = INTIO2    // Oscillator (Internal RC oscillator)
    #pragma config PLLCFG = OFF     // PLL x4 Enable bit (Disabled)
    #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor (Disabled)
    #pragma config IESO = OFF       // Internal External Oscillator Switch Over Mode (Disabled)

     
    // CONFIG2L
    #pragma config PWRTEN = OFF     // Power Up Timer (Disabled)
    #pragma config BOREN = SBORDIS  // Brown Out Detect (Enabled in hardware, SBOREN disabled)
    #pragma config BORV = 3         // Brown-out Reset Voltage bits (1.8V)
    #pragma config BORPWR = ZPBORMV // BORMV Power level (ZPBORMV instead of BORMV is selected)

     
    // CONFIG2H
    #pragma config WDTEN = OFF      // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
    #pragma config WDTPS = 1048576  // Watchdog Postscaler (1:1048576)

     
    // CONFIG3H
    #pragma config CANMX = PORTB    // ECAN Mux bit (ECAN TX and RX pins are located on RB2 and RB3, respectively)
    #pragma config MSSPMSK = MSK7   // MSSP address masking (7 Bit address masking mode)
    #pragma config MCLRE = ON       // Master Clear Enable (MCLR Enabled, RE3 Disabled)

     
    // CONFIG4L
    #pragma config STVREN = ON      // Stack Overflow Reset (Enabled)
    #pragma config BBSIZ = BB2K     // Boot Block Size (2K word Boot Block size)

     
    // CONFIG5L
    #pragma config CP0 = OFF        // Code Protect 00800-01FFF (Disabled)
    #pragma config CP1 = OFF        // Code Protect 02000-03FFF (Disabled)
    #pragma config CP2 = OFF        // Code Protect 04000-05FFF (Disabled)
    #pragma config CP3 = OFF        // Code Protect 06000-07FFF (Disabled)

     
    // CONFIG5H
    #pragma config CPB = OFF        // Code Protect Boot (Disabled)
    #pragma config CPD = OFF        // Data EE Read Protect (Disabled)

     
    // CONFIG6L
    #pragma config WRT0 = OFF       // Table Write Protect 00800-01FFF (Disabled)
    #pragma config WRT1 = OFF       // Table Write Protect 02000-03FFF (Disabled)
    #pragma config WRT2 = OFF       // Table Write Protect 04000-05FFF (Disabled)
    #pragma config WRT3 = OFF       // Table Write Protect 06000-07FFF (Disabled)

     
    // CONFIG6H
    #pragma config WRTC = OFF       // Config. Write Protect (Disabled)
    #pragma config WRTB = OFF       // Table Write Protect Boot (Disabled)
    #pragma config WRTD = OFF       // Data EE Write Protect (Disabled)

     
    // CONFIG7L
    #pragma config EBTR0 = OFF      // Table Read Protect 00800-01FFF (Disabled)
    #pragma config EBTR1 = OFF      // Table Read Protect 02000-03FFF (Disabled)
    #pragma config EBTR2 = OFF      // Table Read Protect 04000-05FFF (Disabled)
    #pragma config EBTR3 = OFF      // Table Read Protect 06000-07FFF (Disabled)

     
    // CONFIG7H
    #pragma config EBTRB = OFF      // Table Read Protect Boot (Disabled)

     
    // #pragma config statements should precede project file includes.
    // Use project enums instead of #define for ON and OFF.

     
    #define LED1 LATBbits.LATB5
    #define LED1_TRIS TRISBbits.TRISB5

     

    void main(void)
    {
        // Your initialization, but do not enable interrupts
        LED1 = 0;
        LED1_TRIS = 0;
       
        T0CON     = 0x88;
        TMR0H     = 0xEC;
        TMR0L     = 0x78;

     
        GIE = 1;  // Global interrupt enable bit
        TMR0IE     = 1; //Timer0 overflow enable bit

     
        while (1) {
        
        }
    }

     
    volatile int icnt=0;

     
    void __interrupt(high_priority) tcInt(void){
      if (TMR0IF == 1) // Timer0 overflow interrupt flag bit
      {
        if(icnt++ == 400){
        
          LED1 = ~LED1;
            icnt = 0;
        }
        TMR0H     = 0xEC;
        TMR0L     = 0x78;
        TMR0IF = 0;
      }
    }

     

     
    Now I am trying  to simulated code in simulator 
     
    What should be instruction frequency to simulate code ?
     
    post edited by Djsarkar - 2020/09/25 23:21:15

    Attached Image(s)

    #13
    ric
    Super Member
    • Total Posts : 28676
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC18F45K80 timer interrupt 2020/09/25 23:45:26 (permalink)
    +2 (2)
    In PIC18F chips, the instruction frequency is 1/4 of the clock frequency.
    So, if you are running with the default 8MHz clock, then the instruction frequency is 2MHz.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #14
    Djsarkar
    Senior Member
    • Total Posts : 155
    • Reward points : 0
    • Joined: 2020/07/27 01:14:06
    • Location: 0
    • Status: offline
    Re: PIC18F45K80 timer interrupt 2020/09/26 00:23:04 (permalink)
    0
    ric
    In PIC18F chips, the instruction frequency is 1/4 of the clock frequency.
    So, if you are running with the default 8MHz clock, then the instruction frequency is 2MHz.
     


    Hi 
    Thank I got it point 
    I have external 20 Mhz crystal oscillator on board. How to use external 20 Mhz crystal instead of internal 8 Mhz   
    #15
    ric
    Super Member
    • Total Posts : 28676
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC18F45K80 timer interrupt 2020/09/26 01:54:15 (permalink)
    +2 (2)
    Djsarkar
    I have external 20 Mhz crystal oscillator on board. How to use external 20 Mhz crystal instead of internal 8 Mhz  

    Change
    #pragma config FOSC = INTIO2    // Oscillator (Internal RC oscillator)
    to
    #pragma config FOSC = HS2       // Oscillator (HS oscillator (High power, 16 MHz - 25 MHz))
     
    That really would not have taken much reading in the datasheet to discover for yourself.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #16
    Djsarkar
    Senior Member
    • Total Posts : 155
    • Reward points : 0
    • Joined: 2020/07/27 01:14:06
    • Location: 0
    • Status: offline
    Re: PIC18F45K80 timer interrupt 2020/09/26 02:26:52 (permalink)
    0
    if PIC18 is running with the default 8MHz clock, so the instruction frequency is 2MHz.
     
    What would the timer register value and prescale value to get 1 ms time ?
     
    #17
    upand_at_them
    Super Member
    • Total Posts : 681
    • Reward points : 0
    • Joined: 2005/05/16 07:02:38
    • Location: Pennsylvania
    • Status: offline
    Re: PIC18F45K80 timer interrupt 2020/09/26 05:12:36 (permalink)
    +3 (3)
    Assuming this applies to 18F PIC's:
     
    PIC Timer Calculator
    http://eng-serve.com/pic/pic_timer.html
    #18
    Djsarkar
    Senior Member
    • Total Posts : 155
    • Reward points : 0
    • Joined: 2020/07/27 01:14:06
    • Location: 0
    • Status: offline
    Re: PIC18F45K80 timer interrupt 2020/09/26 09:32:51 (permalink)
    0
    upand_at_them
    Assuming this applies to 18F PIC's:
     
    PIC Timer Calculator
    http://eng-serve.com/pic/pic_timer.html


    How do you calculate timer register value from calculation ? TMR0H and TMR0H
    #19
    1and0
    Access is Denied
    • Total Posts : 11340
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC18F45K80 timer interrupt 2020/09/26 09:49:37 (permalink)
    +1 (1)
    Djsarkar
    if PIC18 is running with the default 8MHz clock, so the instruction frequency is 2MHz.
     
    What would the timer register value and prescale value to get 1 ms time ?

    Djsarkar
    How do you calculate timer register value from calculation ? TMR0H and TMR0H

    Do you know how many instruction cycles are there in 1 ms at that instruction frequency? Once you know that, set your timer registers and scalers to a value so it will overflow and interrupt at that number of instructions cycles.


    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5