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 00030 #include "pic24_all.h" 00031 00038 #define WAVEOUT _LATB2 //state 00039 inline void CONFIG_WAVEOUT() { 00040 CONFIG_RB2_AS_DIG_OUTPUT(); //use RB2 for output 00041 } 00042 00043 //Interrupt Service Routine for Timer2 00044 void _ISRFAST _T2Interrupt (void) { 00045 WAVEOUT = !WAVEOUT; //toggle output 00046 _T2IF = 0; //clear the timer interrupt bit 00047 } 00048 00049 #define ISR_PERIOD 15 // in ms 00050 void configTimer2(void) { 00051 //T2CON set like this for documentation purposes. 00052 //could be replaced by T2CON = 0x0020 00053 T2CON = T2_OFF | T2_IDLE_CON | T2_GATE_OFF 00054 | T2_32BIT_MODE_OFF 00055 | T2_SOURCE_INT 00056 | T2_PS_1_64 ; //results in T2CON = 0x0020 00057 //subtract 1 from ticks value assigned to PR2 because period is PRx + 1 00058 PR2 = msToU16Ticks (ISR_PERIOD, getTimerPrescale(T2CONbits)) - 1; 00059 TMR2 = 0; //clear timer2 value 00060 _T2IF = 0; //clear interrupt flag 00061 _T2IP = 1; //choose a priority 00062 _T2IE = 1; //enable the interrupt 00063 T2CONbits.TON = 1; //turn on the timer 00064 } 00065 00066 int main (void) { 00067 configBasic(HELLO_MSG); 00068 CONFIG_WAVEOUT(); //PIO Config 00069 configTimer2(); //TMR2 config 00070 //interrupt does work of generating the square wave 00071 while (1) { 00072 doHeartbeat(); //ensure that we are alive 00073 } // end while (1) 00074 00075 }