• AVR Freaks

PIC18F45Q10 timer interrupt blinking led.

Author
henbez_72
New Member
  • Total Posts : 21
  • Reward points : 0
  • Joined: 2020/10/20 11:27:10
  • Location: 0
  • Status: offline
2020/10/21 06:04:24 (permalink)
0

PIC18F45Q10 timer interrupt blinking led.

Trying to blink LED using timer and interrupt routine.See code below.

LED on pin RD0 blink with delay function no problem

LED on pin RD1 no response.

Program compile no warnings all good.

Thanks for help

/***********************************************
* Microcontroller used is the PIC18F45Q10
* Compiler used is XC8 VERSION v2.1
* This program blinks an LED using interrupt
****************************/


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

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

// CONFIG1H
#pragma config CLKOUTEN = ON // Clock Out Enable bit (CLKOUT function is disabled)
#pragma config CSWEN = ON // Clock Switch Enable bit (Writing to NOSC and NDIV is allowed)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor enabled)

// CONFIG2L
#pragma config MCLRE = EXTMCLR // Master Clear Enable bit (MCLR pin (RE3) is MCLR)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (Power up timer disabled)
#pragma config LPBOREN = OFF // Low-power BOR enable bit (Low power BOR is disabled)
#pragma config BOREN = SBORDIS // Brown-out Reset Enable bits (Brown-out Reset enabled , SBOREN bit is ignored)

// CONFIG2H
#pragma config BORV = VBOR_190 // Brown Out Reset Voltage selection bits (Brown-out Reset Voltage (VBOR) set to 1.90V)
#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 XINST = OFF // Extended Instruction Set Enable bit (Extended Instruction Set and Indexed Addressing Mode disabled)

// CONFIG3L
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = ON // WDT operating mode (WDT always enabled)

// 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 WRT0 = OFF // Write Protection Block 0 (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection Block 1 (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection Block 2 (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF // Write Protection Block 3 (Block 3 (006000-007FFFh) not write-protected)

// CONFIG4H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-30000Bh) not write-protected)
#pragma config WRTB = 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 SCANE = ON // Scanner Enable bit (Scanner module is available for use, SCANMD bit can control the module)
#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 // UserNVM Program Memory Code Protection bit (UserNVM code protection disabled)
#pragma config CPD = OFF // DataNVM Memory Code Protection bit (DataNVM code protection disabled)

// CONFIG5H

// CONFIG6L
#pragma config EBTR0 = OFF // Table Read Protection Block 0 (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection Block 1 (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection Block 2 (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection Block 3 (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)

// CONFIG6H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)


#include <xc.h>
#define _XTAL_FREQ 20000000 // 20MHz crystal

//Variables
#define LED1 PORTDbits.RD1 //ISR
#define LED0 PORTDbits.RD0 //delay

volatile int count = 0; //volatile means value may be changed externally without program doing anything
volatile int num_overflows = 19;

// interrupt service routine
void __interrupt() ISR(void) {
// has the interrupt been fired by timer 1?
if(PIR0bits.TMR0IF == 1) {
// increase the number of times interrupt has been fired
count++;
// count till 19 interrupts have been fired (about 1 second has elapsed)using 20 MHZ clock

if(count == num_overflows) {
// toggle RD1 pin
LATDbits.LATD1 ^= 1;
//PORTDbits.RD1 = ~PORTDbits.RD1; //flip the bit
// reset counter
count = 0;
}
// reset the overflow interrupt flag
PIR0bits.TMR0IF = 0;
}
}
//Timer_0
void timer_init(){
//T0CON0 reg
T0CON0bits.T0EN =1; //enable timer0
T0CON0bits.T016BIT =1; //enable 16bit timer

T0CON0bits.T0OUTPS0=0; //postcaler =1
T0CON0bits.T0OUTPS1=0;
T0CON0bits.T0OUTPS2=0;
T0CON0bits.T0OUTPS3=0;

//T0CON1 reg
T0CON1bits.T0CS0=0; //clock source FOSC/4 for timer0
T0CON1bits.T0CS1=1;
T0CON1bits.T0CS2=0;

T0CON1bits.T0ASYNC=0; //sync to fosc/4

T0CON1bits.T0CKPS0=0; //prescaler =4
T0CON1bits.T0CKPS1=1;
T0CON1bits.T0CKPS2=0;
T0CON1bits.T0CKPS3=0;

// clear timer1 high and low holding registers
TMR0H = 0;
TMR0L = 0;
// enable global interrupts
INTCONbits.GIE = 1; // enable global interrupts
// enable peripheral interrupts
INTCONbits.PEIE = 1;
// interupt priority
INTCONbits.IPEN = 1;
// enable interrupt flag for timer 1
PIE0bits.TMR0IE = 1;
// TMR0 priority high
IPR0bits.TMR0IP = 1;
// reset the overflow interrupt flag
PIR0bits.TMR0IF = 0;
}
void init_port()
{
ANSELD = 0b00000000; //Configure PORTD as digital
TRISD = 0b00000000; //Configure PORTD as output
//LED1 = 0;

}

int main()
{
init_port();
timer_init();//timer0 init


while(1)//infinite loop or use while(1)
{
LED0 = 1; //LED0 ON
__delay_ms(10);
LED0 = 0; //LED0 OFF
__delay_ms(100);

// LED1 = 1; //LED0 ON
// __delay_ms(10);
// LED1 = 0; //LED0 OFF
// __delay_ms(100);

}
}
#1

1 Reply Related Threads

    ric
    Super Member
    • Total Posts : 28952
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC18F45Q10 timer interrupt blinking led. 2020/10/21 12:17:26 (permalink)

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