• AVR Freaks

Hot!PIC18F47K42 Timer0 Interrupt executes executes only once

Author
Andreas12345
New Member
  • Total Posts : 2
  • Reward points : 0
  • Joined: 2020/02/19 07:29:50
  • Location: 0
  • Status: offline
2020/02/20 03:21:51 (permalink)
0

PIC18F47K42 Timer0 Interrupt executes executes only once

Hi everybody, 
 
i'm using a PIC18F47K42 programmed with PICKit 3 in MPLAB v 5 and XC 8 Compiler v2.05. I'm trying to toggle an output pin by using the timer0 interrupt. IVT are enabled. The problem is: The first time the timer overflows the interrupt is executed but after that the interrupt isn't triggered anymore, altough the timer overflows again. 
 
Attached below you find the source code:
 
Help is greatly appreciated! Thanks in advance!
 
Greetings
 
main.c

// 'C' source line config statements

// CONFIG1L
#pragma config FEXTOSC = ECH // External Oscillator Selection (EC (external clock) above 8 MHz; PFM set to high power)
#pragma config RSTOSC = EXTOSC // Reset Oscillator Selection (EXTOSC operating per FEXTOSC bits (device manufacturing default))

// CONFIG1H
#pragma config CLKOUTEN = OFF // Clock out Enable bit (CLKOUT function is disabled)
#pragma config PR1WAY = ON // PRLOCKED One-Way Set Enable bit (PRLOCK bit can be cleared and set only once)
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)

// CONFIG2L
#pragma config MCLRE = EXTMCLR // MCLR Enable bit (If LVP = 0, MCLR pin is MCLR; If LVP = 1, RE3 pin function is MCLR )
#pragma config PWRTS = PWRT_OFF // Power-up timer selection bits (PWRT is disabled)
#pragma config MVECEN = ON // Multi-vector enable bit (Multi-vector enabled, Vector table used for interrupts)
#pragma config IVT1WAY = ON // IVTLOCK bit One-way set enable bit (IVTLOCK bit can be cleared and set only once)
#pragma config LPBOREN = OFF // Low Power BOR Enable bit (ULPBOR disabled)
#pragma config BOREN = SBORDIS // Brown-out Reset Enable bits (Brown-out Reset enabled , SBOREN bit is ignored)

// CONFIG2H
#pragma config BORV = VBOR_2P45 // Brown-out Reset Voltage Selection bits (Brown-out Reset Voltage (VBOR) set to 2.45V)
#pragma config ZCD = OFF // ZCD Disable bit (ZCD disabled. ZCD can be enabled by setting the ZCDSEN bit of ZCDCON)
#pragma config PPS1WAY = ON // PPSLOCK bit One-Way Set Enable bit (PPSLOCK bit can be cleared and set only once; PPS registers remain locked after one clear/set cycle)
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config DEBUG = ON // Debugger Enable bit (Background debugger enabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Extended Instruction Set and Indexed Addressing Mode disabled)

// CONFIG3L
#pragma config WDTCPS = WDTCPS_31// WDT Period selection bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF // WDT operating mode (WDT Disabled; SWDTEN is ignored)

// CONFIG3H
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC // WDT input clock selector (Software Control)

// CONFIG4L
#pragma config BBSIZE = BBSIZE_512// Boot Block Size selection bits (Boot Block size is 512 words)
#pragma config BBEN = OFF // Boot Block enable bit (Boot block disabled)
#pragma config SAFEN = OFF // Storage Area Flash enable bit (SAF disabled)
#pragma config WRTAPP = OFF // Application Block write protection bit (Application Block not write protected)

// CONFIG4H
#pragma config WRTB = OFF // Configuration Register Write Protection bit (Configuration registers (300000-30000Bh) not write-protected)
#pragma config WRTC = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
#pragma config WRTSAF = OFF // SAF Write protection bit (SAF not Write Protected)
#pragma config LVP = ON // Low Voltage Programming Enable bit (Low voltage programming enabled. MCLR/VPP pin function is MCLR. MCLRE configuration bit is ignored)

// CONFIG5L
#pragma config CP = OFF // PFM and Data EEPROM Code Protection bit (PFM and Data EEPROM code protection disabled)

// CONFIG5H

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

#include <xc.h>
#include "System.h"



void main(void) {
    
    Initialize();
 
    while(1)
    {
        if(PORTDbits.RD4 == 1)
        {
// TimerStart();
        }
    }
    
    return;
}

 
system.c 
#include <xc.h>
#include <pic18f47k42.h>


void Initialize()
{
    TRISDbits.TRISD0 = 0;
    LATDbits.LATD0 = 0;
    TRISDbits.TRISD4 = 0;
    LATDbits.LATD4 = 1;
    
    INTCON0bits.IPEN = 1;
    INTCON0bits.GIEH = 1;
    INTCON0bits.GIEL = 1;
    
    T0CON0bits.MD16 = 1;
    T0CON1bits.CS = 0b010;
    T0CON1bits.ASYNC = 0;
    T0CON1bits.CKPS = 0b0000;
      

    
    PIE3bits.TMR0IE = 1;
    IPR3bits.TMR0IP = 1;
    T0CON0bits.EN = 1;
    
}

void TimerStart()
{
    T0CON0bits.EN = 1;
}

void __interrupt(irq(IRQ_TMR0), high_priority) tc0Int(void)
{
    TMR0IF = 0;
    if(LATDbits.LATD0 == 0){
        LATDbits.LATD0 = 1;
        
    }
    else{
        LATDbits.LATD0 = 0;
    }
    
    return;
   
}

#1

3 Replies Related Threads

    ric
    Super Member
    • Total Posts : 26090
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC18F47K42 Timer0 Interrupt executes executes only once 2020/02/20 12:34:10 (permalink)
    0

    The problem is: The first time the timer overflows the interrupt is executed but after that the interrupt isn't triggered anymore, altough the timer overflows again.

    How do you know?
    Just watching the LED?
    Setting breakpoints in a debugger?
    The HOW of your testing is just as important as the WHAT.
     
    Not your problem, but don't do this.
    system.c 
     
    #include <xc.h>
    #include <pic18f47k42.h>

    xc.h has already included pic18f47k42.h for you. Do not manually include the device specific file.
     

    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!
    #2
    Aussie Susan
    Super Member
    • Total Posts : 3685
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: PIC18F47K42 Timer0 Interrupt executes executes only once 2020/02/20 18:34:28 (permalink)
    0
    Also:
    - don't set the DEBUG config value to anything - the IDE will do that for you
    - not related to your problem but I also suggest that you have PPS1WAY set to OFF so that you don't lock yourself out until everything is working as expected (probably ditto for the other xxx1WAY config settings)
    - you have RD4 set to output but you are trying to read from it in the main loop (I know that does not affect the problem but indicates that there is an inconsistency in your setup)
    - what is the clock speed: you use the external oscillator and leave everything else at the default
    Susan
    #3
    ric
    Super Member
    • Total Posts : 26090
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC18F47K42 Timer0 Interrupt executes executes only once 2020/02/20 19:01:55 (permalink)
    0
    Recoding this
        if(LATDbits.LATD0 == 0){
            LATDbits.LATD0 = 1;       
        }
        else{
            LATDbits.LATD0 = 0;
        }


    as
        LATDbits.LATD0 ^= 1;

     
    Means the compiler may well just code it as a single BTG instruction.

    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
    Jump to:
    © 2020 APG vNext Commercial Version 4.5