0% found this document useful (0 votes)
7 views2 pages

Timer 0 Initialization and Management

The document contains C code for a timer module, specifically for Timer 0, including initialization, reloading, and interrupt service routines. It defines global variables and functions to set up the timer with a specified prescale and preset value. The code also includes mechanisms to handle timer timeouts and interrupts.

Uploaded by

technosanky
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

Timer 0 Initialization and Management

The document contains C code for a timer module, specifically for Timer 0, including initialization, reloading, and interrupt service routines. It defines global variables and functions to set up the timer with a specified prescale and preset value. The code also includes mechanisms to handle timer timeouts and interrupts.

Uploaded by

technosanky
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/*

* File: timer.c
* Author: Sanket
*/

#include "timer.h"
#include "interrupt.h"
#include "display.h"

/*
* Global variables
*/
#ifdef USE_TIMER_0
static uint8_t timer_0_preset;
//static uint8_t timer_0_ovf;
//static uint8_t timer_0_timeout;

//uint8_t timer_0_ovf;
uint8_t timer_0_timeout;
#endif

/*
* Private function prototypes
*/

/*
* Function definitions
*/
#ifdef USE_TIMER_0

void init_timer_0(const timer_0_prescale_t prescale, const uint8_t preset) {


// Disable interrupt.
INTR_TIMER_0_DS();

// Stop timer.
TIMER_0_STOP();

// Timer 0 clock source is internal instruction cycle clock (FOSC/4)


OPTION_REGbits.T0CS = BIT_CLR;

// Default prescaler settings.


OPTION_REGbits.PS = 0b000;
if (prescale == TIMER_0_PS_1_1) {
OPTION_REGbits.PSA = BIT_SET;
}
else {
OPTION_REGbits.PSA = BIT_CLR;
OPTION_REGbits.PS = (uint8_t) prescale;
}

// Backup preset.
timer_0_preset = preset;

// Default preset settings.


TMR0 = timer_0_preset;

// Reset overflow.
//timer_0_ovf = 0;
}
void timer_0_reload(const timer_0_prescale_t prescale, const uint8_t preset) {
// Stop timer.
TIMER_0_STOP();

// Prescaler settings.
OPTION_REGbits.PS = 0b000;
if (prescale == TIMER_0_PS_1_1) {
OPTION_REGbits.PSA = BIT_SET;
}
else {
OPTION_REGbits.PSA = BIT_CLR;
OPTION_REGbits.PS = (uint8_t) prescale;
}

// Backup preset.
timer_0_preset = preset;

// Preset settings.
TMR0 = timer_0_preset;
}

void timer_0_isr(void) {
// Reload timer with preset count.
TMR0 = timer_0_preset;

#if 0
if (timer_0_ovf) {
timer_0_ovf--;

if (!timer_0_ovf) {
// To Do...
}
}
#endif

if (timer_0_timeout) {
timer_0_timeout--;
}
}
#endif

You might also like