00001 /* 00002 * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")" 00003 * All rights reserved. 00004 * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University) 00005 * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University) 00006 * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University) 00007 * 00008 * Permission to use, copy, modify, and distribute this software and its 00009 * documentation for any purpose, without fee, and without written agreement is 00010 * hereby granted, provided that the above copyright notice, the following 00011 * two paragraphs and the authors appear in all copies of this software. 00012 * 00013 * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR 00014 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT 00015 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS" 00016 * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00017 * 00018 * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES, 00019 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 00020 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 00021 * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO 00022 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS." 00023 * 00024 * Please maintain this header in its entirety when copying/modifying 00025 * these files. 00026 * 00027 * 00028 */ 00029 #include "pic24_all.h" 00030 00038 #ifndef SQWAVE_HALFPERIOD 00039 #define SQWAVE_HALFPERIOD 2500 // desired half period, in us 00040 #endif 00041 00042 void configTimer2(void) { 00043 T2CON = T2_OFF | T2_IDLE_CON | T2_GATE_OFF 00044 | T2_32BIT_MODE_OFF 00045 | T2_SOURCE_INT 00046 | T2_PS_1_64 ; //1 tick = 1.6 us at FCY=40 MHz 00047 PR2 = 0xFFFF; //maximum period 00048 TMR2 = 0; //clear timer2 value 00049 } 00050 00051 uint16 u16_sqwaveHPeriodTicks; 00052 void _ISRFAST _OC1Interrupt() { 00053 _OC1IF = 0; 00054 OC1R = OC1R + u16_sqwaveHPeriodTicks; 00055 } 00056 00057 void configOutputCapture1(void) { 00058 T2CONbits.TON = 0; //disable Timer when configuring Output compare 00059 CONFIG_OC1_TO_RP(2); //map OC1 to RP2/RB2 00060 //initialized the compare register to 1/2 the squarewave period 00061 //assumes TIMER2 initialized before OC1 so PRE bits are set 00062 u16_sqwaveHPeriodTicks = usToU16Ticks(SQWAVE_HALFPERIOD, getTimerPrescale(T2CONbits)); 00063 OC1R = u16_sqwaveHPeriodTicks; 00064 //turn on the compare toggle mode using Timer2 00065 OC1CON = OC_TIMER2_SRC | //Timer2 source 00066 OC_TOGGLE_PULSE; //Toggle OC1 every compare event 00067 _OC1IF = 0; 00068 _OC1IP = 1; //pick a priority 00069 _OC1IE = 1; //enable 00070 } 00071 00072 int main (void) { 00073 configBasic(HELLO_MSG); 00074 configTimer2(); 00075 configOutputCapture1(); 00076 T2CONbits.TON = 1; //turn on Timer2 to start sqwave 00077 while (1) doHeartbeat(); //nothing to do, squarewave generated in hardware 00078 }