nikouz
Junior Member level 2
- Joined
- Feb 5, 2021
- Messages
- 20
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 179
Hi happy new year all
I am trying to set an interrupt and a callback using the mplab code configurator. There is something that i cannot understand. The timer overfow every 500ms and blinking an led. The callback comes after 15s and blinks an led in the main.c setting a variable. Now i have set as TMR0_SetInterruptHandler(TMR0_ISR); Inside the TMR0_ISR calls the TMR0_CallBack(void) to set the callback variable for the main.c. The problem is:
If i set the callback variable in the TMR0_CallBack(void) Then the timer runs ok the led1 blinking 500ms but when the callback kicks in the timer is stucking and the led1 stays on.
Now if inside the TMR0_ISR instead of calling the TMR0_CallBack(void) i set the variable everything works fine means led1 blinkiking @500ms and the led2 @15s. I enclose the codes: first is the main.c and then the tmr0.c made first with the way is not working and then with the way is working. In the code i mark with orange the changes to be easier to see. If someone can explain what am i doing wrong.
main.c
tmr0.c bad version
tmr0.c working version
.c working version
I am trying to set an interrupt and a callback using the mplab code configurator. There is something that i cannot understand. The timer overfow every 500ms and blinking an led. The callback comes after 15s and blinks an led in the main.c setting a variable. Now i have set as TMR0_SetInterruptHandler(TMR0_ISR); Inside the TMR0_ISR calls the TMR0_CallBack(void) to set the callback variable for the main.c. The problem is:
If i set the callback variable in the TMR0_CallBack(void) Then the timer runs ok the led1 blinking 500ms but when the callback kicks in the timer is stucking and the led1 stays on.
Now if inside the TMR0_ISR instead of calling the TMR0_CallBack(void) i set the variable everything works fine means led1 blinkiking @500ms and the led2 @15s. I enclose the codes: first is the main.c and then the tmr0.c made first with the way is not working and then with the way is working. In the code i mark with orange the changes to be easier to see. If someone can explain what am i doing wrong.
main.c
Code:
#include "mcc_generated_files/mcc.h"
char ioc_interrupt = 0, tmr0_callback = 0;
/*
Main application
*/
void main(void)
{
// Initialize the device
SYSTEM_Initialize();
// If using interrupts in PIC18 High/Low Priority Mode you need to enable the Global High and Low Interrupts
// If using interrupts in PIC Mid-Range Compatibility Mode you need to enable the Global and Peripheral Interrupts
// Use the following macros to:
// Enable high priority global interrupts
INTERRUPT_GlobalInterruptHighEnable();
// Enable low priority global interrupts.
//INTERRUPT_GlobalInterruptLowEnable();
// Disable high priority global interrupts
//INTERRUPT_GlobalInterruptHighDisable();
// Disable low priority global interrupts.
//INTERRUPT_GlobalInterruptLowDisable();
// Enable the Peripheral Interrupts
INTERRUPT_PeripheralInterruptEnable();
// Disable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptDisable();
while (1)
{
// Add your application code
if(tmr0_callback)
{
tmr0_callback = 0;
LED3_RD3_SetHigh();
__delay_ms(2000);
LED3_RD3_SetLow();
}
}
}
/**
End of File
*/
tmr0.c bad version
Code:
/**
Section: Included Files
*/
#include <xc.h>
#include "tmr0.h"
#include "mcc.h"
extern char tmr0_callback;
/**
Section: Global Variables Definitions
*/
void (*TMR0_InterruptHandler)(void);
volatile uint16_t timer0ReloadVal;
/**
Section: TMR0 APIs
*/
void TMR0_Initialize(void)
{
// Set TMR0 to the options selected in the User Interface
//Enable 16bit timer mode before assigning value to TMR0H
T0CONbits.T08BIT = 0;
// TMR0H 11;
TMR0H = 0x0B;
// TMR0L 219;
TMR0L = 0xDB;
// Load TMR0 value to the 16-bit reload variable
timer0ReloadVal = (uint16_t)((TMR0H << 8) | TMR0L);
// Clear Interrupt flag before enabling the interrupt
INTCONbits.TMR0IF = 0;
// Enabling TMR0 interrupt.
INTCONbits.TMR0IE = 1;
// Set Default Interrupt Handler
TMR0_SetInterruptHandler(TMR0_ISR);
// T0PS 1:2; T08BIT 16-bit; T0SE Increment_hi_lo; T0CS FOSC/4; TMR0ON enabled; PSA assigned;
T0CON = 0x90;
}
void TMR0_StartTimer(void)
{
// Start the Timer by writing to TMR0ON bit
T0CONbits.TMR0ON = 1;
}
void TMR0_StopTimer(void)
{
// Stop the Timer by writing to TMR0ON bit
T0CONbits.TMR0ON = 0;
}
uint16_t TMR0_ReadTimer(void)
{
uint16_t readVal;
uint8_t readValLow;
uint8_t readValHigh;
readValLow = TMR0L;
readValHigh = TMR0H;
readVal = ((uint16_t)readValHigh << 8) + readValLow;
return readVal;
}
void TMR0_WriteTimer(uint16_t timerVal)
{
// Write to the Timer0 register
TMR0H = timerVal >> 8;
TMR0L = (uint8_t) timerVal;
}
void TMR0_Reload(void)
{
// Write to the Timer0 register
TMR0H = timer0ReloadVal >> 8;
TMR0L = (uint8_t) timer0ReloadVal;
}
void TMR0_ISR(void)
{
static volatile uint16_t CountCallBack = 0;
// clear the TMR0 interrupt flag
INTCONbits.TMR0IF = 0;
// reload TMR0
// Write to the Timer0 register
TMR0H = timer0ReloadVal >> 8;
TMR0L = (uint8_t) timer0ReloadVal;
// callback function - called every 30th pass
if (++CountCallBack >= TMR0_INTERRUPT_TICKER_FACTOR)
{
// ticker function call
TMR0_CallBack();
// reset ticker counter
CountCallBack = 0;
}
// add your TMR0 interrupt custom code
LED1_RC4_Toggle();
}
void TMR0_CallBack(void)
{
// Add your custom callback code here
tmr0_callback = 1;
if(TMR0_InterruptHandler)
{
TMR0_InterruptHandler();
}
}
void TMR0_SetInterruptHandler(void (* InterruptHandler)(void)){
TMR0_InterruptHandler = InterruptHandler;
}
void TMR0_DefaultInterruptHandler(void){
// add your TMR0 interrupt custom code
// or set custom function using TMR0_SetInterruptHandler()
}
/**
End of File
*/
tmr0.c working version
Code:
/**
Section: Included Files
*/
#include <xc.h>
#include "tmr0.h"
#include "mcc.h"
extern char tmr0_callback;
/**
Section: Global Variables Definitions
*/
void (*TMR0_InterruptHandler)(void);
volatile uint16_t timer0ReloadVal;
/**
Section: TMR0 APIs
*/
void TMR0_Initialize(void)
{
// Set TMR0 to the options selected in the User Interface
//Enable 16bit timer mode before assigning value to TMR0H
T0CONbits.T08BIT = 0;
// TMR0H 11;
TMR0H = 0x0B;
// TMR0L 219;
TMR0L = 0xDB;
// Load TMR0 value to the 16-bit reload variable
timer0ReloadVal = (uint16_t)((TMR0H << 8) | TMR0L);
// Clear Interrupt flag before enabling the interrupt
INTCONbits.TMR0IF = 0;
// Enabling TMR0 interrupt.
INTCONbits.TMR0IE = 1;
// Set Default Interrupt Handler
TMR0_SetInterruptHandler(TMR0_ISR);
// T0PS 1:2; T08BIT 16-bit; T0SE Increment_hi_lo; T0CS FOSC/4; TMR0ON enabled; PSA assigned;
T0CON = 0x90;
}
void TMR0_StartTimer(void)
{
// Start the Timer by writing to TMR0ON bit
T0CONbits.TMR0ON = 1;
}
void TMR0_StopTimer(void)
{
// Stop the Timer by writing to TMR0ON bit
T0CONbits.TMR0ON = 0;
}
uint16_t TMR0_ReadTimer(void)
{
uint16_t readVal;
uint8_t readValLow;
uint8_t readValHigh;
readValLow = TMR0L;
readValHigh = TMR0H;
readVal = ((uint16_t)readValHigh << 8) + readValLow;
return readVal;
}
void TMR0_WriteTimer(uint16_t timerVal)
{
// Write to the Timer0 register
TMR0H = timerVal >> 8;
TMR0L = (uint8_t) timerVal;
}
void TMR0_Reload(void)
{
// Write to the Timer0 register
TMR0H = timer0ReloadVal >> 8;
TMR0L = (uint8_t) timer0ReloadVal;
}
void TMR0_ISR(void)
{
static volatile uint16_t CountCallBack = 0;
// clear the TMR0 interrupt flag
INTCONbits.TMR0IF = 0;
// reload TMR0
// Write to the Timer0 register
TMR0H = timer0ReloadVal >> 8;
TMR0L = (uint8_t) timer0ReloadVal;
// callback function - called every 30th pass
if (++CountCallBack >= TMR0_INTERRUPT_TICKER_FACTOR)
{
// ticker function call
tmr0_callback = 1;
// reset ticker counter
CountCallBack = 0;
}
// add your TMR0 interrupt custom code
LED1_RC4_Toggle();
}
void TMR0_CallBack(void)
{
// Add your custom callback code here
if(TMR0_InterruptHandler)
{
TMR0_InterruptHandler();
}
}
void TMR0_SetInterruptHandler(void (* InterruptHandler)(void)){
TMR0_InterruptHandler = InterruptHandler;
}
void TMR0_DefaultInterruptHandler(void){
// add your TMR0 interrupt custom code
// or set custom function using TMR0_SetInterruptHandler()
}
/**
End of File
*/