PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
outcompare_contpulse.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 an asymmetric square wave using output compare (OC1), continuous pulse mode.
33  * If you wish greater accuracy, then use a clock choice with an external crystal, such as:
34  * CLOCK_CONFIG=PRIPLL_8MHzCrystal_40MHzFCY
35  * Remove this macro if you wish to use the internal oscillator.
36 */
37 
38 #ifndef SQWAVE_PWHIGH
39 #define SQWAVE_PWHIGH 2000 // desired PW High, in us
40 #endif
41 #ifndef SQWAVE_PERIOD
42 #define SQWAVE_PERIOD 20000 // desired period, in us
43 #endif
44 
45 
46 void configTimer2(void) {
47  T2CON = T2_OFF | T2_IDLE_CON | T2_GATE_OFF
48  | T2_32BIT_MODE_OFF
49  | T2_SOURCE_INT
50  | T2_PS_1_64 ; //1 tick = 1.6 us at FCY=40 MHz
51  PR2 = usToU16Ticks(SQWAVE_PERIOD, getTimerPrescale(T2CONbits));
52  TMR2 = 0; //clear timer2 value
53 }
54 
55 void configOutputCompare1(void) {
56  T2CONbits.TON = 0; //disable Timer when configuring Output compare
57  CONFIG_RB8_AS_DIG_OUTPUT();
58 #if (defined(__dsPIC33E__) || defined(__PIC24E__))
59  CONFIG_OC1_TO_RP(40); //map OC1 to RB8/RP40
60 #else
61  CONFIG_OC1_TO_RP(8); //map OC1 to RB8/RP13
62 #endif
63 //initialized the primary,secondary compare registers
64 //assumes TIMER2 initialized before OC1 so PRE bits are set
65  OC1R = 0; //go high when Timer2 == 0
66  OC1RS = usToU16Ticks(SQWAVE_PWHIGH, getTimerPrescale(T2CONbits)); //go low when Timer2 == u16_sqwavePWHighTicks
67 //turn on the compare toggle mode using Timer2
68 #if (defined(__dsPIC33E__) || defined(__PIC24E__))
69  OC1CON1 = OC_TIMER2_SRC | //Timer2 source
70  OC_CONTINUE_PULSE; //Continuous pulse mode
71  OC1CON2 = 0x000C; //sync source is Timer2.
72 #else
73  OC1CON = OC_TIMER2_SRC | //Timer2 source
74  OC_CONTINUE_PULSE; //Continuous pulse mode
75 #endif
76  _OC1IF = 0;
77 }
78 
79 int main (void) {
80  configBasic(HELLO_MSG);
81  configTimer2();
82  configOutputCompare1();
83  T2CONbits.TON = 1; //turn on Timer2 to start sqwave
84  while (1) doHeartbeat(); //nothing to do, squarewave generated in hardware
85 }