PIC24 Support Libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
ledflash_timer.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  * Demonstrates use of a periodic interrupt to flash an LED.
34  * Timer3 is configured for a 300 ms interrupt to flash an
35  * LED on RB14.
36 */
37 
38 /// LED1
39 #define CONFIG_LED1() CONFIG_RB14_AS_DIG_OUTPUT()
40 #define LED1 _LATB14 //led1 state
41 
42 //Interrupt Service Routine for Timer3
43 void _ISRFAST _T3Interrupt (void) {
44  LED1 = !LED1; //toggle the LED
45  _T3IF = 0; //clear the timer interrupt bit
46 }
47 
48 #if (defined(__dsPIC33E__) || defined(__PIC24E__))
49 //for these families, need shorter period because of potentially faster clock
50 //which will overflow 16-bit timer
51 #define ISR_PERIOD 150 // in ms
52 #else
53 #define ISR_PERIOD 300 // in ms
54 #endif
55 
56 void configTimer3(void) {
57  //ensure that Timer2,3 configured as separate timers.
58  T2CONbits.T32 = 0; // 32-bit mode off
59  //T3CON set like this for documentation purposes.
60  //could be replaced by T3CON = 0x0030
61  T3CON = T3_OFF | T3_IDLE_CON | T3_GATE_OFF
62  | T3_SOURCE_INT
63  | T3_PS_1_256 ; //results in T3CON= 0x0030
64  PR3 = msToU16Ticks (ISR_PERIOD, getTimerPrescale(T3CONbits)) - 1;
65  TMR3 = 0; //clear timer3 value
66  _T3IF = 0; //clear interrupt flag
67  _T3IP = 1; //choose a priority
68  _T3IE = 1; //enable the interrupt
69  T3CONbits.TON = 1; //turn on the timer
70 }
71 
72 int main (void) {
73  configBasic(HELLO_MSG);
74  /** GPIO config ***************************/
75  CONFIG_LED1(); //config the LED
76  LED1 = 1;
77  /** Config the Timer, use Timer3***********/
78  configTimer3();
79  while (1) {
80  //enter idle mode while waiting for timer to go off
81  //Timer interrupt will wake us from idle mode
82  IDLE(); //macro for __asm__ volatile ("pwrsav #1")
83  }
84  // End program
85  return 0;
86 }