Init Timer0; // no interrupt needed
while(1)
{
reload TMR0
TMROIF=0;
Turn ON pin B1 as output and wait for 100 ms
Read another pin B2 as input
Send a string to UART
Turn OFF the pin B1
Read again B2
Send a string to UART
//wait elapsed time of 1 sec
while(TMROIF==0);
}
I guess none of them. They all are for "controlling" TMR0.But I am not sure which function will check the interrupt "while(TMROIF==0)"
// Initialize the device
SYSTEM_Initialize();
TMR0_Initialize();
// Enable the Global Interrupts
INTERRUPT_GlobalInterruptEnable();
// Enable the Peripheral Interrupts
INTERRUPT_PeripheralInterruptEnable();
while (1)
{
// Add your application code
TMR0_Reload();
TMR0_StartTimer();
////////////////////////////////////////////////
// Perform the task
////////////////////////////////////////////////
// wait elapsed time of 1 sec
TMR0_ISR();
TMR0_StopTimer();
}
#include <xc.h>
#include "tmr0.h"
/**
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 255;
TMR0H = 0xFF;
// TMR0L 55;
TMR0L = 0x37;
// 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_DefaultInterruptHandler);
// 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 10000th pass
if (++CountCallBack >= TMR0_INTERRUPT_TICKER_FACTOR)
{
// ticker function call
TMR0_CallBack();
// reset ticker counter
CountCallBack = 0;
}
// add your TMR0 interrupt custom code
}
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()
}
while (1)
{
// Add your application code
TMR0_Initialize();
TMR0_StartTimer();
////////////////////////////////////////////////
// Perform the task
////////////////////////////////////////////////
// wait elapsed time of 1 sec
TMR0_ISR();
TMR0_StopTimer();
}
while (1)
{
// Add your application code
if (TMR0_HasOverflowOccured()) //Has Timer0 Overflowed?
{
IO_RA2_Toggle(); //Switch state of RA2 LED when overflow occurs
TMR0IF = 0; //Clear the Timer0 overflow flag
}
}
I also read the tutorial on how to use timer polling.
Here I know that we don't need to enable timer interrupt in polling example because it is based on polling which means that we need to check the timer and again at some point in our code until the over flow happen and when the timer over flow happen, we need to reload the timer registers and start the timer again in the while loop.
Code:while (1) { // Add your application code if (TMR0_HasOverflowOccured()) //Has Timer0 Overflowed? { IO_RA2_Toggle(); //Switch state of RA2 LED when overflow occurs TMR0IF = 0; //Clear the Timer0 overflow flag } }
I think I need to use timer polling in my task because I need to wait inside the while loop until the next second is over and that can only be done by implementing the timer polling example. I have described my task in the first post. Kindly comment here if I am thinking right or not.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?