PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
squarewave.c
Go to the documentation of this file.
1 /*
2  * "Copyright (c) 2008 Robert B. Reese, Bryan A. Jones, J. W. Bruce ("AUTHORS")"
3  * All rights reserved.
4  * (R. Reese, reese_AT_ece.msstate.edu, Mississippi State University)
5  * (B. A. Jones, bjones_AT_ece.msstate.edu, Mississippi State University)
6  * (J. W. Bruce, jwbruce_AT_ece.msstate.edu, Mississippi State University)
7  *
8  * Permission to use, copy, modify, and distribute this software and its
9  * documentation for any purpose, without fee, and without written agreement is
10  * hereby granted, provided that the above copyright notice, the following
11  * two paragraphs and the authors appear in all copies of this software.
12  *
13  * IN NO EVENT SHALL THE "AUTHORS" BE LIABLE TO ANY PARTY FOR
14  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
15  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE "AUTHORS"
16  * HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17  *
18  * THE "AUTHORS" SPECIFICALLY DISCLAIMS ANY WARRANTIES,
19  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
21  * ON AN "AS IS" BASIS, AND THE "AUTHORS" HAS NO OBLIGATION TO
22  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
23  *
24  * Please maintain this header in its entirety when copying/modifying
25  * these files.
26  *
27  *
28  */
29 
30 #include "pic24_all.h"
31 
32 /** \file
33 * Generates a square wave using timer 2 and
34 * an ISR.
35 */
36 
37 
38 #define WAVEOUT _LATB2 //state
39 inline void CONFIG_WAVEOUT() {
40  CONFIG_RB2_AS_DIG_OUTPUT(); //use RB2 for output
41 }
42 
43 //Interrupt Service Routine for Timer2
44 void _ISRFAST _T2Interrupt (void) {
45  WAVEOUT = !WAVEOUT; //toggle output
46  _T2IF = 0; //clear the timer interrupt bit
47 }
48 
49 #define ISR_PERIOD 15 // in ms
50 void configTimer2(void) {
51  //T2CON set like this for documentation purposes.
52  //could be replaced by T2CON = 0x0020
53  T2CON = T2_OFF | T2_IDLE_CON | T2_GATE_OFF
54  | T2_32BIT_MODE_OFF
55  | T2_SOURCE_INT
56  | T2_PS_1_64 ; //results in T2CON = 0x0020
57  //subtract 1 from ticks value assigned to PR2 because period is PRx + 1
58  PR2 = msToU16Ticks (ISR_PERIOD, getTimerPrescale(T2CONbits)) - 1;
59  TMR2 = 0; //clear timer2 value
60  _T2IF = 0; //clear interrupt flag
61  _T2IP = 1; //choose a priority
62  _T2IE = 1; //enable the interrupt
63  T2CONbits.TON = 1; //turn on the timer
64 }
65 
66 int main (void) {
67  configBasic(HELLO_MSG);
68  CONFIG_WAVEOUT(); //PIO Config
69  configTimer2(); //TMR2 config
70  //interrupt does work of generating the square wave
71  while (1) {
72  doHeartbeat(); //ensure that we are alive
73  } // end while (1)
74 
75 }