PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
outcompare_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 #include "pic24_all.h"
30 
31 /** \file
32  * Generate a squarewave using output compare (OC1).
33  * Define the following clock configuration if you wish higher accuracy.
34  * Define CLOCK_CONFIG=PRIPLL_8MHzCrystal_40MHzFCY in the MPLAB project.
35  * Remove this macro if you wish to use the internal oscillator.
36  *
37  * This example is implemented fundamentally different on the PIC24E/dsPIC24E
38 */
39 
40 #ifndef SQWAVE_HALFPERIOD
41 #define SQWAVE_HALFPERIOD 2500 // desired half period, in us
42 #endif
43 
44 void configTimer2(void) {
45  T2CON = T2_OFF | T2_IDLE_CON | T2_GATE_OFF
46  | T2_32BIT_MODE_OFF
47  | T2_SOURCE_INT
48  | T2_PS_1_64 ; //1 tick = 1.6 us at FCY=40 MHz
49  PR2 = 0xFFFF; //maximum period
50  TMR2 = 0; //clear timer2 value
51 }
52 
53 uint16_t u16_sqwaveHPeriodTicks;
54 void _ISRFAST _OC1Interrupt() {
55  _OC1IF = 0;
56 #if (defined(__dsPIC33E__) || defined(__PIC24E__))
57 //nothing to do here since OC1 has internal timer that is reset on OC1RS match.
58 #else
59  OC1R = OC1R + u16_sqwaveHPeriodTicks;
60 #endif
61 }
62 
63 void configOutputCompare1(void) {
64  T2CONbits.TON = 0; //disable Timer when configuring Output compare
65 
66 #if (defined(__dsPIC33E__) || defined(__PIC24E__))
67  CONFIG_OC1_TO_RP(40); //map OC1 to RP40/RB8 (RB2 RP pin is input only)
68 //use OC1R to set pulse high, OC1RS to set pulse low, timer reset on OC1RS match.
69 //assumes TIMER2 initialized before OC1 so PRE bits are set
70  u16_sqwaveHPeriodTicks = usToU16Ticks(SQWAVE_HALFPERIOD, getTimerPrescale(T2CONbits));
71  OC1R = u16_sqwaveHPeriodTicks; //toggles output
72  OC1RS = u16_sqwaveHPeriodTicks; //resets timer
73  OC1CON1 = OC_TIMER2_SRC | //Timer2 source
74  OC_TOGGLE_PULSE; //Continuous pulse mode;
75  OC1CON2 = 0x001F; //reset internal timer when OCxRS match occurs
76 #else
77  CONFIG_OC1_TO_RP(2); //map OC1 to RP2/RB2
78  //initialize the compare register to 1/2 the squarewave period
79  //assumes TIMER2 initialized before OC1 so PRE bits are set
80  u16_sqwaveHPeriodTicks = usToU16Ticks(SQWAVE_HALFPERIOD, getTimerPrescale(T2CONbits));
81  OC1R = u16_sqwaveHPeriodTicks;
82  OC1CON = OC_TIMER2_SRC | //Timer2 source
83  OC_TOGGLE_PULSE; //Toggle OC1 every compare event
84 #endif
85  _OC1IF = 0;
86  _OC1IP = 1; //pick a priority
87  _OC1IE = 1; //enable
88 }
89 
90 int main (void) {
91  configBasic(HELLO_MSG);
92  configTimer2();
93  configOutputCompare1();
94  T2CONbits.TON = 1; //turn on Timer2 to start sqwave
95  while (1) doHeartbeat(); //nothing to do, squarewave generated in hardware
96 }