go away spammer

Author Topic: New Timer library 100% Axon compatible  (Read 2101 times)

0 Members and 1 Guest are viewing this topic.

Offline TrickyNekroTopic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,208
  • Helpful? 15
  • Hardware and Firmware Designer
    • The Hellinic Robots Portal
New Timer library 100% Axon compatible
« on: May 18, 2010, 07:44:33 PM »
Well... since I got into the game of writing libraries, I'm just getting more and more ready... by altering the
quite old AVR lib....

ALSO!!!!
This is going to work ONLY with Atmega640/1281/1280/2561/2560 for NOW!!!!!!!
Keep that in mind before trying!!!!!!

This library is meant for guys that they know what the *chicken* they are doing....
It's basically a quick way to get the Timers up and running, no tricks...
You also need to know how to set the Compare output modes, at least for now...
They are the most difficult to integrate...

I know that there is also webbot lib... But I personally find pleasure in pain  :P
This library again is meant to easy up the life of those been using the AVRlib up to now...
Your code AREN'T going to work with the new library.... Recommend for new projects...

I will also keep updating it, as far as I can... Add some goodies perhaps ;-)

Code: [Select]
/*! \file timer.h \brief System Timer function library. */
//*****************************************************************************
//
// File Name : 'TTimer.h'
// Title : System Timer function library
// Author : Lefteris Provatos - Copyright (C) 2004-2010
// Created : 5/16/2010
// Revised :
// Version : 1.1
// Target MCU : ATmega640/1280/1281/2560/2561
// Ext Resourse : Avr Lib

#ifndef TTIMER_H
#define TTIMER_H

// Timer/clock prescaler values and timer overflow rates
// tics = rate at which the timer counts up
// 8bitoverflow = rate at which the timer overflows 8bits (or reaches 256)
// 16bit [overflow] = rate at which the timer overflows 16bits (65536)
//
// overflows can be used to generate periodic interrupts
//
// for 8MHz crystal
// 0 = STOP (Timer not counting)
// 1 = CLOCK tics= 8MHz 8bitoverflow= 31250Hz 16bit= 122.070Hz
// 2 = CLOCK/8 tics= 1MHz 8bitoverflow= 3906.25Hz 16bit=  15.259Hz
// 3 = CLOCK/64 tics= 125kHz 8bitoverflow=  488.28Hz 16bit=   1.907Hz
// 4 = CLOCK/256 tics= 31250Hz 8bitoverflow=  122.07Hz 16bit= 0.477Hz
// 5 = CLOCK/1024 tics= 7812.5Hz 8bitoverflow=   30.52Hz 16bit=   0.119Hz
// 6 = External Clock on T(x) pin (falling edge)
// 7 = External Clock on T(x) pin (rising edge)

// for 4MHz crystal
// 0 = STOP (Timer not counting)
// 1 = CLOCK tics= 4MHz 8bitoverflow= 15625Hz 16bit=  61.035Hz
// 2 = CLOCK/8 tics= 500kHz 8bitoverflow= 1953.125Hz 16bit=   7.629Hz
// 3 = CLOCK/64 tics= 62500Hz 8bitoverflow=  244.141Hz 16bit=   0.954Hz
// 4 = CLOCK/256 tics= 15625Hz 8bitoverflow=   61.035Hz 16bit=   0.238Hz
// 5 = CLOCK/1024 tics= 3906.25Hz 8bitoverflow=   15.259Hz 16bit=   0.060Hz
// 6 = External Clock on T(x) pin (falling edge)
// 7 = External Clock on T(x) pin (rising edge)

// for 3.69MHz crystal
// 0 = STOP (Timer not counting)
// 1 = CLOCK tics= 3.69MHz 8bitoverflow= 14414Hz 16bit=  56.304Hz
// 2 = CLOCK/8 tics= 461250Hz 8bitoverflow= 1801.758Hz 16bit=   7.038Hz
// 3 = CLOCK/64 tics= 57625.25Hz 8bitoverflow=  225.220Hz 16bit=   0.880Hz
// 4 = CLOCK/256 tics= 14414.063Hz 8bitoverflow=   56.305Hz 16bit=   0.220Hz
// 5 = CLOCK/1024 tics=  3603.516Hz 8bitoverflow=   14.076Hz 16bit=   0.055Hz
// 6 = External Clock on T(x) pin (falling edge)
// 7 = External Clock on T(x) pin (rising edge)

// for 32.768KHz crystal on timer 2 (use for real-time clock)
// 0 = STOP
// 1 = CLOCK tics= 32.768kHz 8bitoverflow= 128Hz
// 2 = CLOCK/8 tics= 4096kHz 8bitoverflow=  16Hz
// 3 = CLOCK/32 tics= 1024kHz 8bitoverflow=   4Hz
// 4 = CLOCK/64 tics= 512Hz 8bitoverflow=   2Hz
// 5 = CLOCK/128 tics= 256Hz 8bitoverflow=   1Hz
// 6 = CLOCK/256 tics= 128Hz 8bitoverflow=   0.5Hz
// 7 = CLOCK/1024 tics= 32Hz 8bitoverflow=   0.125Hz

// Timers except Timer2
#define PRESC_S 0x00 ///< Timer Stopped
#define PRESC_1 0x01 ///< Timer clocked at F_CPU
#define PRESC_8 0x02 ///< Timer clocked at F_CPU/8
#define PRESC_64 0x03 ///< Timer clocked at F_CPU/64
#define PRESC_256 0x04 ///< Timer clocked at F_CPU/256
#define PRESC_1024 0x05 ///< Timer clocked at F_CPU/1024
#define PRESC_T_FALL 0x06 ///< Timer clocked at T falling edge
#define PRESC_T_RISE 0x07 ///< Timer clocked at T rising edge

// Timer2
#define PRESC_H_S 0x00 ///< High Res Timer Stopped
#define PRESC_H_1 0x01 ///< High Res Timer clocked at F_CPU
#define PRESC_H_8 0x02 ///< High Res Timer clocked at F_CPU/8
#define PRESC_H_32 0x03 ///< High Res Timer clocked at F_CPU/32
#define PRESC_H_64 0x04 ///< High Res Timer clocked at F_CPU/64
#define PRESC_H_128 0x05 ///< High Res Timer clocked at F_CPU/128
#define PRESC_H_256 0x06 ///< High Res Timer clocked at F_CPU/256
#define PRESC_H_1024 0x07 ///< High Res Timer clocked at F_CPU/1024

#define PRESC_MASK 0x07 ///< Timer Prescaler Bit-Mask

#define Noise_Canceler 0x40 // Enables the Noise Canceler at counter input
#define Input_Cap_Edge 0x30 // When set counter captures on rising edge
// When cleared counter captures on falling edge
// 16Bit Timers
#define MGM_N 0x00 // Normal mode
#define MGM_PWM_PC_8 0x01 // PWM Phase Correct 8bit mode
#define MGM_PWM_PC_9 0x02 // PWM Phase Correct 9bit mode
#define MGM_PWM_PC_10 0x03 // PWM Phase Correct 10bit mode
#define MGM_CTC_1 0x04 // Clear Timer on Compare (top OCRnA) mode
#define MGM_F_PWM_8 0x05 // Fast PWM 8 bit mode
#define MGM_F_PWM_9 0x06 // Fast PWM 9 bit mode
#define MGM_F_PWM_10 0x07 // Fast PWM 10 bit mode
#define MGM_PWM_PFC_1 0x10 // PWM Phase/Freq Correct (top ICRn) mode
#define MGM_PWM_PFC_2 0x11 // PWM Phase/Freq Correct (top ORCnA) mode
#define MGM_PWM_PC_1 0x12 // PWM Phase Correct (top ICRn) mode
#define MGM_PWM_PC_2 0x13 // PWM Phase Correct (top OCRnA) mode
#define MGM_CTC_2 0x14 // Clear Timer on Compare (top ICRn) mode
#define MGM_PWM_1 0x16 // Fast PWM (top ICRn) mode
#define MGM_PWM_2 0x17 // Fast PWM (top OCRnA) mode

// 8Bit Timers
#define MGM8_N 0x00 // Normal mode
#define MGM8_PWM_PC_1 0x01 // PWM Phase Correct (top 0xFF) mode
#define MGM8_CTC 0x02 // Clear Timer on Compare (top OCRA) mode
#define MGM8_F_PWM_1 0x03 // Fast PWM (top 0xFF) mode
#define MGM8_PWM_PC_2 0x09 // PWM Phase Correct (top ORCA) mode
#define MGM8_F_PWM_2 0x0B // Fast PWM (top ORCA) mode

void timer0Init (void);

#endif

/********************* This is where you define the Timer Prescalers *********************/

// Define Starting Prescaler for Timer 0
#define Presc0 PRESC_1

// Define Starting Prescaler for Timer 1
#define Presc1 PRESC_1

// If it exists, define Starting Prescaler for Timer 2
#ifdef TCNT2

#define Presc2 PRESC_H_1

#endif
// If it exists, define Starting Prescaler for Timer 3
#ifdef TCNT3

#define Presc3 PRESC_1

#endif
// If it exists, define Starting Prescaler for Timer 4
#ifdef TCNT4

#define Presc4 PRESC_1

#endif
// If it exists, define Starting Prescaler for Timer 5
#ifdef TCNT5

#define Presc5 PRESC_1

#endif


/********************* This is where you define the Timer Operation **********************/

// Define Starting mode of Operation for Timer 0
#define MGM_C0 MGM8_N

// Define Starting Prescaler for Timer 1
#define MGM_C1 MGM_N

// If it exists, define Starting Prescaler for Timer 2
#ifdef TCNT2

#define MGM_C2 MGM8_N

#endif
// If it exists, define Starting Prescaler for Timer 3
#ifdef TCNT3

#define MGM_C3 MGM_N

#endif
// If it exists, define Starting Prescaler for Timer 4
#ifdef TCNT4

#define MGM_C4 MGM_N

#endif
// If it exists, define Starting Prescaler for Timer 5
#ifdef TCNT5

#define MGM_C5 MGM_N

#endif

/*****************************************************************************************/
/*****************************************************************************************/
/*********************************** Timer Call Functions ********************************/


/********************************* Choosing Mode of Operation ****************************/
void SetTimer0Mode (u08 WGM)
{
// Clear and write WGM00 & WGM01
TCCR0A &= ~0x03;
TCCR0A |= (WGM & 0x03);
// Clear and write WGM02
TCCR0B &= ~0x08;
TCCR0B |= (WGM & 0x08);
}

void SetTimer1Mode (u08 WGM)
{
// Clear and write WGM00 & WGM01
TCCR1A &= ~0x03;
TCCR1A |= (WGM & 0x03);
// Clear and write WGM02
TCCR1B &= ~0x18;
TCCR1B |= (WGM & 0x18);
}

#ifdef TCNT2
void SetTimer2Mode (u08 WGM)
{
// Clear and write WGM00 & WGM01
TCCR2A &= ~0x03;
TCCR2A |= (WGM & 0x03);
// Clear and write WGM02
TCCR2B &= ~0x08;
TCCR2B |= (WGM & 0x08);
}
#endif

#ifdef TCNT3
void SetTimer3Mode (u08 WGM)
{
// Clear and write WGM00 & WGM01
TCCR3A &= ~0x03;
TCCR3A |= (WGM & 0x03);
// Clear and write WGM02
TCCR3B &= ~0x18;
TCCR3B |= (WGM & 0x18);
}
#endif

#ifdef TCNT4
void SetTimer4Mode (u08 WGM)
{
// Clear and write WGM00 & WGM01
TCCR4A &= ~0x03;
TCCR4A |= (WGM & 0x03);
// Clear and write WGM02
TCCR4B &= ~0x18;
TCCR4B |= (WGM & 0x18);
}
#endif

#ifdef TCNT5
void SetTimer5Mode (u08 WGM)
{
// Clear and write WGM00 & WGM01
TCCR5A &= ~0x03;
TCCR5A |= (WGM & 0x03);
// Clear and write WGM02
TCCR5B &= ~0x18;
TCCR5B |= (WGM & 0x18);
}
#endif


/********************************* Choosing Prescaller ***********************************/

void SetTimer0Pres (u08 Pres)
{
// Clear and write CSnX
TCCR0B &= ~PRESC_MASK;
TCCR0B |= (Pres & PRESC_MASK);
}
void SetTimer1Pres (u08 Pres)
{
// Clear and write CSnX
TCCR1B &= ~PRESC_MASK;
TCCR1B |= (Pres & PRESC_MASK);
}

#ifdef TCNT2
void SetTimer2Pres (u08 Pres)
{
// Clear and write CSnX
TCCR2B &= ~PRESC_MASK;
TCCR2B |= (Pres & PRESC_MASK);
}
#endif

#ifdef TCNT3
void SetTimer3Pres (u08 Pres)
{
// Clear and write CSnX
TCCR3B &= ~PRESC_MASK;
TCCR3B |= (Pres & PRESC_MASK);
}
#endif

#ifdef TCNT4
void SetTimer4Pres (u08 Pres)
{
// Clear and write CSnX
TCCR4B &= ~PRESC_MASK;
TCCR4B |= (Pres & PRESC_MASK);
}
#endif

#ifdef TCNT5
void SetTimer5Pres (u08 Pres)
{
// Clear and write CSnX
TCCR5B &= ~PRESC_MASK;
TCCR5B |= (Pres & PRESC_MASK);
}
#endif


/********************************* Initialize Function ***********************************/

void timer0Init (void)
{
// Initialize specified Timer
SetTimer0Pres(Presc0);
SetTimer0Mode(MGM_C0);
// Set specified Timer to zero
TCNT0 = 0;
}

void timer1Init (void)
{

// Initiaze specified Timer
SetTimer1Pres(Presc1);
SetTimer1Mode(MGM_C1);
// Set specified Timer to zero
TCNT1H = 0;
TCNT1L = 0;
}

#ifdef TCNT2
void timer2Init (void)
{
// Initialize specified Timer
SetTimer0Pres(Presc2);
SetTimer0Mode(MGM_C2);
// Set specified Timer to zero
TCNT2 = 0;
}
#endif

#ifdef TCNT3
void timer3Init (void)
{

// Initiaze specified Timer
SetTimer1Pres(Presc3);
SetTimer1Mode(MGM_C3);
// Set specified Timer to zero
TCNT3H = 0;
TCNT3L = 0;
}
#endif

#ifdef TCNT4
void timer4Init (void)
{

// Initiaze specified Timer
SetTimer1Pres(Presc4);
SetTimer1Mode(MGM_C4);
// Set specified Timer to zero
TCNT4H = 0;
TCNT4L = 0;
}
#endif

#ifdef TCNT5
void timer5Init (void)
{

// Initiaze specified Timer
SetTimer1Pres(Presc5);
SetTimer1Mode(MGM_C5);
// Set specified Timer to zero
TCNT5H = 0;
TCNT5L = 0;
}
#endif


/********************************* Public Function ***************************************/

void timer0PinB (u08 compare_out)
{

//Set the Compare Output Mode
TCCR0A |= (compare_out & 0xF0);

}

void timer1PinB (u08 compare_out)
{

//Set the Compare Output Mode
TCCR0A |= (compare_out & 0xFC);

}

void timer2PinB (u08 compare_out)
{

//Set the Compare Output Mode
TCCR0A |= (compare_out & 0xF0);

}

void timer3PinB (u08 compare_out)
{

//Set the Compare Output Mode
TCCR0A |= (compare_out & 0xFC);

}

void timer4PinB (u08 compare_out)
{

//Set the Compare Output Mode
TCCR0A |= (compare_out & 0xFC);

}

void timer5PinB (u08 compare_out)
{

//Set the Compare Output Mode
TCCR0A |= (compare_out & 0xFC);

}





Best Regards, Lefteris
Greece
« Last Edit: May 18, 2010, 07:46:14 PM by TrickyNekro »
For whom the interrupts toll...

Offline TrickyNekroTopic starter

  • Contest Winner
  • Supreme Robot
  • ****
  • Posts: 1,208
  • Helpful? 15
  • Hardware and Firmware Designer
    • The Hellinic Robots Portal
Re: New Timer library 100% Axon compatible
« Reply #1 on: May 23, 2010, 05:28:41 PM »
Basically the code I posted doesn't work... I was too sloppy not to test it before I post the actual code...
Again the code is guarantied to work with Atmega640/1280/2560....

But now is correct  :D

Code: [Select]
/*! \file timer.h \brief System Timer function library. */
//*****************************************************************************
//
// File Name : 'TTimer.h'
// Title : System Timer function library
// Author : Lefteris Provatos - Copyright (C) 2004-2010
// Created : 5/16/2010
// Revised :
// Version : 1.1
// Target MCU : ATmega640/1280/1281/2560/2561
// Ext Resourse : Avr Lib

#ifndef TTIMER_H
#define TTIMER_H

// Timer/clock prescaler values and timer overflow rates
// tics = rate at which the timer counts up
// 8bitoverflow = rate at which the timer overflows 8bits (or reaches 256)
// 16bit [overflow] = rate at which the timer overflows 16bits (65536)
//
// overflows can be used to generate periodic interrupts
//
// for 8MHz crystal
// 0 = STOP (Timer not counting)
// 1 = CLOCK tics= 8MHz 8bitoverflow= 31250Hz 16bit= 122.070Hz
// 2 = CLOCK/8 tics= 1MHz 8bitoverflow= 3906.25Hz 16bit=  15.259Hz
// 3 = CLOCK/64 tics= 125kHz 8bitoverflow=  488.28Hz 16bit=   1.907Hz
// 4 = CLOCK/256 tics= 31250Hz 8bitoverflow=  122.07Hz 16bit= 0.477Hz
// 5 = CLOCK/1024 tics= 7812.5Hz 8bitoverflow=   30.52Hz 16bit=   0.119Hz
// 6 = External Clock on T(x) pin (falling edge)
// 7 = External Clock on T(x) pin (rising edge)

// for 4MHz crystal
// 0 = STOP (Timer not counting)
// 1 = CLOCK tics= 4MHz 8bitoverflow= 15625Hz 16bit=  61.035Hz
// 2 = CLOCK/8 tics= 500kHz 8bitoverflow= 1953.125Hz 16bit=   7.629Hz
// 3 = CLOCK/64 tics= 62500Hz 8bitoverflow=  244.141Hz 16bit=   0.954Hz
// 4 = CLOCK/256 tics= 15625Hz 8bitoverflow=   61.035Hz 16bit=   0.238Hz
// 5 = CLOCK/1024 tics= 3906.25Hz 8bitoverflow=   15.259Hz 16bit=   0.060Hz
// 6 = External Clock on T(x) pin (falling edge)
// 7 = External Clock on T(x) pin (rising edge)

// for 3.69MHz crystal
// 0 = STOP (Timer not counting)
// 1 = CLOCK tics= 3.69MHz 8bitoverflow= 14414Hz 16bit=  56.304Hz
// 2 = CLOCK/8 tics= 461250Hz 8bitoverflow= 1801.758Hz 16bit=   7.038Hz
// 3 = CLOCK/64 tics= 57625.25Hz 8bitoverflow=  225.220Hz 16bit=   0.880Hz
// 4 = CLOCK/256 tics= 14414.063Hz 8bitoverflow=   56.305Hz 16bit=   0.220Hz
// 5 = CLOCK/1024 tics=  3603.516Hz 8bitoverflow=   14.076Hz 16bit=   0.055Hz
// 6 = External Clock on T(x) pin (falling edge)
// 7 = External Clock on T(x) pin (rising edge)

// for 32.768KHz crystal on timer 2 (use for real-time clock)
// 0 = STOP
// 1 = CLOCK tics= 32.768kHz 8bitoverflow= 128Hz
// 2 = CLOCK/8 tics= 4096kHz 8bitoverflow=  16Hz
// 3 = CLOCK/32 tics= 1024kHz 8bitoverflow=   4Hz
// 4 = CLOCK/64 tics= 512Hz 8bitoverflow=   2Hz
// 5 = CLOCK/128 tics= 256Hz 8bitoverflow=   1Hz
// 6 = CLOCK/256 tics= 128Hz 8bitoverflow=   0.5Hz
// 7 = CLOCK/1024 tics= 32Hz 8bitoverflow=   0.125Hz

// Timers except Timer2
#define PRESC_S 0x00 ///< Timer Stopped
#define PRESC_1 0x01 ///< Timer clocked at F_CPU
#define PRESC_8 0x02 ///< Timer clocked at F_CPU/8
#define PRESC_64 0x03 ///< Timer clocked at F_CPU/64
#define PRESC_256 0x04 ///< Timer clocked at F_CPU/256
#define PRESC_1024 0x05 ///< Timer clocked at F_CPU/1024
#define PRESC_T_FALL 0x06 ///< Timer clocked at T falling edge
#define PRESC_T_RISE 0x07 ///< Timer clocked at T rising edge

// Timer2
#define PRESC_H_S 0x00 ///< High Res Timer Stopped
#define PRESC_H_1 0x01 ///< High Res Timer clocked at F_CPU
#define PRESC_H_8 0x02 ///< High Res Timer clocked at F_CPU/8
#define PRESC_H_32 0x03 ///< High Res Timer clocked at F_CPU/32
#define PRESC_H_64 0x04 ///< High Res Timer clocked at F_CPU/64
#define PRESC_H_128 0x05 ///< High Res Timer clocked at F_CPU/128
#define PRESC_H_256 0x06 ///< High Res Timer clocked at F_CPU/256
#define PRESC_H_1024 0x07 ///< High Res Timer clocked at F_CPU/1024

#define PRESC_MASK 0x07 ///< Timer Prescaler Bit-Mask

#define Noise_Canceler 0x40 // Enables the Noise Canceler at counter input
#define Input_Cap_Edge 0x30 // When set counter captures on rising edge
// When cleared counter captures on falling edge
// 16Bit Timers
#define MGM_N 0x00 // Normal mode
#define MGM_PWM_PC_8 0x01 // PWM Phase Correct 8bit mode
#define MGM_PWM_PC_9 0x02 // PWM Phase Correct 9bit mode
#define MGM_PWM_PC_10 0x03 // PWM Phase Correct 10bit mode
#define MGM_CTC_1 0x04 // Clear Timer on Compare (top OCRnA) mode
#define MGM_F_PWM_8 0x05 // Fast PWM 8 bit mode
#define MGM_F_PWM_9 0x06 // Fast PWM 9 bit mode
#define MGM_F_PWM_10 0x07 // Fast PWM 10 bit mode
#define MGM_PWM_PFC_1 0x10 // PWM Phase/Freq Correct (top ICRn) mode
#define MGM_PWM_PFC_2 0x11 // PWM Phase/Freq Correct (top ORCnA) mode
#define MGM_PWM_PC_1 0x12 // PWM Phase Correct (top ICRn) mode
#define MGM_PWM_PC_2 0x13 // PWM Phase Correct (top OCRnA) mode
#define MGM_CTC_2 0x14 // Clear Timer on Compare (top ICRn) mode
#define MGM_PWM_1 0x16 // Fast PWM (top ICRn) mode
#define MGM_PWM_2 0x17 // Fast PWM (top OCRnA) mode

// 8Bit Timers
#define MGM8_N 0x00 // Normal mode
#define MGM8_PWM_PC_1 0x01 // PWM Phase Correct (top 0xFF) mode
#define MGM8_CTC 0x02 // Clear Timer on Compare (top OCRA) mode
#define MGM8_F_PWM_1 0x03 // Fast PWM (top 0xFF) mode
#define MGM8_PWM_PC_2 0x09 // PWM Phase Correct (top ORCA) mode
#define MGM8_F_PWM_2 0x0B // Fast PWM (top ORCA) mode

void timer0Init (void);

#endif

/********************* This is where you define the Timer Prescalers *********************/

// Define Starting Prescaler for Timer 0
#define Presc0 PRESC_1

// Define Starting Prescaler for Timer 1
#define Presc1 PRESC_1

// If it exists, define Starting Prescaler for Timer 2
#ifdef TCNT2

#define Presc2 PRESC_H_1

#endif
// If it exists, define Starting Prescaler for Timer 3
#ifdef TCNT3

#define Presc3 PRESC_1

#endif
// If it exists, define Starting Prescaler for Timer 4
#ifdef TCNT4

#define Presc4 PRESC_1

#endif
// If it exists, define Starting Prescaler for Timer 5
#ifdef TCNT5

#define Presc5 PRESC_1

#endif


/********************* This is where you define the Timer Operation **********************/

// Define Starting mode of Operation for Timer 0
#define MGM_C0 MGM8_N

// Define Starting Prescaler for Timer 1
#define MGM_C1 MGM_N

// If it exists, define Starting Prescaler for Timer 2
#ifdef TCNT2

#define MGM_C2 MGM8_N

#endif
// If it exists, define Starting Prescaler for Timer 3
#ifdef TCNT3

#define MGM_C3 MGM_N

#endif
// If it exists, define Starting Prescaler for Timer 4
#ifdef TCNT4

#define MGM_C4 MGM_N

#endif
// If it exists, define Starting Prescaler for Timer 5
#ifdef TCNT5

#define MGM_C5 MGM_N

#endif

/*****************************************************************************************/
/*****************************************************************************************/
/*********************************** Timer Call Functions ********************************/


/********************************* Choosing Mode of Operation ****************************/
void SetTimer0Mode (u08 WGM)
{
// Clear and write WGM00 & WGM01
TCCR0A &= ~0x03;
TCCR0A |= (WGM & 0x03);
// Clear and write WGM02
TCCR0B &= ~0x08;
TCCR0B |= ((WGM<<1) & 0x08);
}

void SetTimer1Mode (u08 WGM)
{
// Clear and write WGM00 & WGM01
TCCR1A &= ~0x03;
TCCR1A |= (WGM & 0x03);
// Clear and write WGM02
TCCR1B &= ~0x18;
TCCR1B |= ((WGM<<1) & 0x18);
}

#ifdef TCNT2
void SetTimer2Mode (u08 WGM)
{
// Clear and write WGM00 & WGM01
TCCR2A &= ~0x03;
TCCR2A |= (WGM & 0x03);
// Clear and write WGM02
TCCR2B &= ~0x08;
TCCR2B |= ((WGM<<1) & 0x08);
}
#endif

#ifdef TCNT3
void SetTimer3Mode (u08 WGM)
{
// Clear and write WGM00 & WGM01
TCCR3A &= ~0x03;
TCCR3A |= (WGM & 0x03);
// Clear and write WGM02
TCCR3B &= ~0x18;
TCCR3B |= ((WGM<<1) & 0x18);
}
#endif

#ifdef TCNT4
void SetTimer4Mode (u08 WGM)
{
// Clear and write WGM00 & WGM01
TCCR4A &= ~0x03;
TCCR4A |= (WGM & 0x03);
// Clear and write WGM02
TCCR4B &= ~0x18;
TCCR4B |= ((WGM<<1) & 0x18);
}
#endif

#ifdef TCNT5
void SetTimer5Mode (u08 WGM)
{
// Clear and write WGM00 & WGM01
TCCR5A &= ~0x03;
TCCR5A |= (WGM & 0x03);
// Clear and write WGM02
TCCR5B &= ~0x18;
TCCR5B |= ((WGM<<1) & 0x18);
}
#endif


/********************************* Choosing Prescaller ***********************************/

void SetTimer0Pres (u08 Pres)
{
// Clear and write CSnX
TCCR0B &= ~PRESC_MASK;
TCCR0B |= (Pres & PRESC_MASK);
}
void SetTimer1Pres (u08 Pres)
{
// Clear and write CSnX
TCCR1B &= ~PRESC_MASK;
TCCR1B |= (Pres & PRESC_MASK);
}

#ifdef TCNT2
void SetTimer2Pres (u08 Pres)
{
// Clear and write CSnX
TCCR2B &= ~PRESC_MASK;
TCCR2B |= (Pres & PRESC_MASK);
}
#endif

#ifdef TCNT3
void SetTimer3Pres (u08 Pres)
{
// Clear and write CSnX
TCCR3B &= ~PRESC_MASK;
TCCR3B |= (Pres & PRESC_MASK);
}
#endif

#ifdef TCNT4
void SetTimer4Pres (u08 Pres)
{
// Clear and write CSnX
TCCR4B &= ~PRESC_MASK;
TCCR4B |= (Pres & PRESC_MASK);
}
#endif

#ifdef TCNT5
void SetTimer5Pres (u08 Pres)
{
// Clear and write CSnX
TCCR5B &= ~PRESC_MASK;
TCCR5B |= (Pres & PRESC_MASK);
}
#endif


/********************************* Initialize Function ***********************************/

void timer0Init (void)
{
// Initialize specified Timer
SetTimer0Pres(Presc0);
SetTimer0Mode(MGM_C0);
// Set specified Timer to zero
TCNT0 = 0;
}

void timer1Init (void)
{

// Initiaze specified Timer
SetTimer1Pres(Presc1);
SetTimer1Mode(MGM_C1);
// Set specified Timer to zero
TCNT1H = 0;
TCNT1L = 0;
}

#ifdef TCNT2
void timer2Init (void)
{
// Initialize specified Timer
SetTimer2Pres(Presc2);
SetTimer2Mode(MGM_C2);
// Set specified Timer to zero
TCNT2 = 0;
}
#endif

#ifdef TCNT3
void timer3Init (void)
{

// Initiaze specified Timer
SetTimer3Pres(Presc3);
SetTimer3Mode(MGM_C3);
// Set specified Timer to zero
TCNT3H = 0;
TCNT3L = 0;
}
#endif

#ifdef TCNT4
void timer4Init (void)
{

// Initiaze specified Timer
SetTimer4Pres(Presc4);
SetTimer4Mode(MGM_C4);
// Set specified Timer to zero
TCNT4H = 0;
TCNT4L = 0;
}
#endif

#ifdef TCNT5
void timer5Init (void)
{

// Initiaze specified Timer
SetTimer5Pres(Presc5);
SetTimer5Mode(MGM_C5);
// Set specified Timer to zero
TCNT5H = 0;
TCNT5L = 0;
}
#endif


/********************************* Public Function ***************************************/

void timer0PinB (u08 compare_out)
{

//Set the Compare Output Mode
TCCR0A |= (compare_out & 0xF0);

}

void timer1PinB (u08 compare_out)
{

//Set the Compare Output Mode
TCCR1A |= (compare_out & 0xFC);

}

void timer2PinB (u08 compare_out)
{

//Set the Compare Output Mode
TCCR2A |= (compare_out & 0xF0);

}

void timer3PinB (u08 compare_out)
{

//Set the Compare Output Mode
TCCR3A |= (compare_out & 0xFC);

}

void timer4PinB (u08 compare_out)
{

//Set the Compare Output Mode
TCCR4A |= (compare_out & 0xFC);

}

void timer5PinB (u08 compare_out)
{

//Set the Compare Output Mode
TCCR5A |= (compare_out & 0xFC);

}




BTW if anyone test this and it is good...
Why not sharing the experience with us  ;)


Best Regards, Lefteris
Greece
For whom the interrupts toll...

 


Get Your Ad Here