00001 /* 00002 00003 Copyright (c) 2010, Embedded Adventures, www.embeddedadventures.com 00004 All rights reserved. 00005 00006 Redistribution and use in source and binary forms, with or without 00007 modification, are permitted provided that the following conditions are met: 00008 00009 - Redistributions of source code must retain the above copyright notice, 00010 this list of conditions and the following disclaimer. 00011 00012 - Redistributions in binary form must reproduce the above copyright 00013 notice, this list of conditions and the following disclaimer in the 00014 documentation and/or other materials provided with the distribution. 00015 00016 - Neither the name of Embedded Adventures nor the names of its contributors 00017 may be used to endorse or promote products derived from this software 00018 without specific prior written permission. 00019 00020 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00021 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00022 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00023 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 00024 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00025 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00026 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00027 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00028 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00029 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 00030 THE POSSIBILITY OF SUCH DAMAGE. 00031 00032 Contact us at admin@embeddedadventures.com 00033 00034 */ 00035 00042 #include "pic_utils.h" 00043 00045 #define TIMER_16BIT_MODE 0 00046 00047 #define TIMER_8BIT_MODE 1 00048 00049 00050 #define TIMER_PRESCALER_OFF 0xff 00051 #define TIMER_PRESCALER_1_TO_2 0x00 00052 #define TIMER_PRESCALER_1_TO_4 0x01 00053 #define TIMER_PRESCALER_1_TO_8 0x02 00054 #define TIMER_PRESCALER_1_TO_16 0x03 00055 #define TIMER_PRESCALER_1_TO_32 0x04 00056 #define TIMER_PRESCALER_1_TO_64 0x05 00057 #define TIMER_PRESCALER_1_TO_128 0x06 00058 #define TIMER_PRESCALER_1_TO_256 0x07 00059 00060 #ifdef _PIC18 00061 extern uns16 timer_0_start_value; 00062 #endif 00063 00064 #ifdef _PIC16 00065 extern uns8 timer_0_start_value; 00066 #endif 00067 00074 void timer_setup_0(bit mode_16_bit, uns8 prescaler_setting, uns16 timer_start_value); 00075 00082 void timer_start_0(); 00083 00090 void timer_stop_0(); 00091 00100 extern void timer_0_callback(); 00101 00109 #ifdef _PIC18 00110 inline void timer_handle_0_isr() { 00111 uns16 start_value; 00112 if (test_bit(intcon, TMR0IF)) { // interrupt? 00113 start_value = + tmr0l + timer_0_start_value + 8; // adjust start value 00114 tmr0h = start_value >> 8; // set high value 00115 tmr0l = start_value & 0xff; // set low value (must be done in this order) 00116 clear_bit( intcon, TMR0IF ); //clear timer 0 interrupt bit 00117 timer_0_callback(); // call the callback 00118 } 00119 } 00120 #endif 00121 00122 #ifdef _PIC16 00123 inline void timer_handle_0_isr() { 00124 uns8 start_value; 00125 if (test_bit(intcon, TMR0IF)) { // interrupt? 00126 start_value = + tmr0 + timer_0_start_value + 8; // adjust start value 00127 tmr0 = start_value; // reset timer value 00128 clear_bit( intcon, TMR0IF ); //clear timer 0 interrupt bit 00129 timer_0_callback(); // call the callback 00130 } 00131 } 00132 #endif 00133 00134 00135