Timers

/*==============================================================================
   timers.h optimized for IDE for PIC18F
 
   Written by Ervin Jung (2010)
 
--------------------------------------------------------------------------------
 
   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.
 
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
   In other words, you are welcome to use, share and improve this program.
   You are forbidden to forbid anyone else to use, share and improve
   what you give them.   Help stamp out software-hoarding!
 
   As a special exception, if you link this library with other files,
   some of which are compiled with SDCC, to produce an executable,
   this library does not by itself cause the resulting executable
   to be covered by the GNU General Public License.
   This exception does not however invalidate any other reasons why
   the executable file might be covered by the GNU General Public License.
==============================================================================*/
 
#ifndef __TIMERS_H__
  #define __TIMERS_H__
 
#if defined(pic18f2455) || defined(pic18f2550) || \
    defined(pic18f4455) || defined(pic18f4550) 
#define __sdcc_timer0_pic18f4550_style
#else
  #error Device Timers are uknown, please update your timers.h manually
#endif   
 
 
 
 
/*==============================================================================
  T0CON - Timer0 Control Register 
==============================================================================*/
 
#ifdef __sdcc_timer0_pic18f4550_style
// Timer0 On/Off Control bit
// 1 = Enables Timer0
// 0 = Stops Timer0
#define TMR0_ENABLED 0b10000000 
#define TMR0_DISABLED 0x00
// Timer0 8-Bit/16-Bit Control bit
// 1 = Timer0 is configured as an 8-bit timer/counter 
// 0 = Timer0 is configured as a 16-bit timer/counter          
#define TMR0_8BIT_ENABLED 0b01000000
#define TMR0_16BIT_ENABLED 0x00           
 
// Timer0 Clock Source Select bit
// 1 = Transition on T0CKI pin 
// 0 = Internal instruction cycle clock (CLKO)  
#define TMR0_EXTERNAL 0b00100000      
#define TMR0_INTERNAL 0x00
// Timer0 Source Edge Select bit     
// 1 = Increment on high-to-low transition on T0CKI pin 
// 0 = Increment on low-to-high transition on T0CKI pin
#define TMR0_HIGH_TO_LOW 0b00010000    
#define TMR0_LOW_TO_HIGH 0x00
// Timer0 Prescaler Assignment bit
// 1 = Timer0 prescaler is NOT assigned       
// 0 = Timer0 prescaler is assigned       
#define TMR0_PRESCALER_DISABLED 0b00001000
#define TMR0_PRESCALER_ENABLED  0x00      
 
//Timer0 Prescaler Select bits
#define TMR0_DIV_256  0b00000111           // 1:256
#define TMR0_DIV_128  0b00000110           // 1:128
#define TMR0_DIV_64   0b00000101           // 1:64
#define TMR0_DIV_32   0b00000100           // 1:32
#define TMR0_DIV_16   0b00000011           // 1:16
#define TMR0_DIV_8    0b00000010           // 1:8
#define TMR0_DIV_4    0b00000001           // 1:4
#define TMR0_DIV_2    0b00000000           // 1:2
 
 
/*==============================================================================
  T1CON - Timer1 Control Register 
==============================================================================*/
// RD16 16-Bit Read/Write Mode Enable bit
// 1 =Enables register read/write of Timer1 in one 16-bit operation
// 0 =Enables register read/write of Timer1 in two 8-bit operations
#define TMR1_16BIT_RW 0b10000000
#define TMR1_8BIT_RW  0x00
 
//T1RUN: Timer1 System Clock Status bit
//1 =Device clock is derived from Timer1 oscillator
//0 =Device clock is derived from another source
#define TMR1_RUN_T1_OSC      0b01000000
#define TMR1_RUN_SOURCE  0x00
 
//T1CKPS1:T1CKPS0: Timer1 Input Clock Prescale Select bits
//11 = 1:8 Prescale value 
//10 = 1:4 Prescale value 
//01 = 1:2 Prescale value 
//00 = 1:1 Prescale value
#define TMR1_DIV_8 0b00110000
#define TMR1_DIV_4 0b00100000
#define TMR1_DIV_2 0b00010000
#define TMR1_DIV_1 0x00
 
//T1OSCEN: Timer1 Oscillator Enable bit
//1 =Timer1 oscillator is enabled 
//0 =Timer1 oscillator is shut off 
//The oscillator inverter and feedback resistor are turned off to eliminate power drain.
#define TMR1_OSC1_ON  0b00001000
#define TMR1_OSC1_OFF 0x00
 
//T1SYNC: Timer1 External Clock Input Synchronization Select bit
//When TMR1CS = 1: 
//1 = Do not synchronize external clock input 
//0 = Synchronize external clock input
//When TMR1CS = 0: 
//This bit is ignored. Timer1 uses the internal clock when TMR1CS = 0.
#define TMR1_SYNC_INT 0b00000100
#define TMR1_SYNC_EXT 0x00
 
//TMR1CS: Timer1 Clock Source Select bit
//1 =External clock from RC0/T1OSO/T13CKI pin (on the rising edge)
//0 =Internal clock (FOSC/4)
#define TMR1_SOURCE_EXT 0b00000010
#define TMR1_SOURCE_INT 0x00
 
//TMR1ON: Timer1 On bit
//1 =Enables Timer1 
//0 =Stops Timer1
#define TMR1_ENABLED  0b00000001
#define TMR1_DISABLED 0x00
 
#endif //__sdcc_timer0_pic18f4550_style
 
void timer0_init(unsigned char c) __wparam {
  T0CON = c;
}
 
void timer1_init(unsigned char c) __wparam {
  T1CON = c;
}
 
#endif