These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!
TIMER/FreqMeasure/freqmeasure.c@0:bf7b9fba3924, 2011-03-20 (annotated)
- Committer:
- frank26080115
- Date:
- Sun Mar 20 05:38:56 2011 +0000
- Revision:
- 0:bf7b9fba3924
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
frank26080115 | 0:bf7b9fba3924 | 1 | /******************** (C) COPYRIGHT 2010 NXPSemiconductors ************ |
frank26080115 | 0:bf7b9fba3924 | 2 | * @file freqmeasure.c |
frank26080115 | 0:bf7b9fba3924 | 3 | * @purpose This example describes how to use TIMER capture function |
frank26080115 | 0:bf7b9fba3924 | 4 | * @version 1.0 |
frank26080115 | 0:bf7b9fba3924 | 5 | * @date 16. July. 2010 |
frank26080115 | 0:bf7b9fba3924 | 6 | * @author NXP MCU SW Application Team |
frank26080115 | 0:bf7b9fba3924 | 7 | *--------------------------------------------------------------------- |
frank26080115 | 0:bf7b9fba3924 | 8 | * Software that is described herein is for illustrative purposes only |
frank26080115 | 0:bf7b9fba3924 | 9 | * which provides customers with programming information regarding the |
frank26080115 | 0:bf7b9fba3924 | 10 | * products. This software is supplied "AS IS" without any warranties. |
frank26080115 | 0:bf7b9fba3924 | 11 | * NXP Semiconductors assumes no responsibility or liability for the |
frank26080115 | 0:bf7b9fba3924 | 12 | * use of the software, conveys no license or title under any patent, |
frank26080115 | 0:bf7b9fba3924 | 13 | * copyright, or mask work right to the product. NXP Semiconductors |
frank26080115 | 0:bf7b9fba3924 | 14 | * reserves the right to make changes in the software without |
frank26080115 | 0:bf7b9fba3924 | 15 | * notification. NXP Semiconductors also make no representation or |
frank26080115 | 0:bf7b9fba3924 | 16 | * warranty that such application will be suitable for the specified |
frank26080115 | 0:bf7b9fba3924 | 17 | * use without further testing or modification. |
frank26080115 | 0:bf7b9fba3924 | 18 | **********************************************************************/ |
frank26080115 | 0:bf7b9fba3924 | 19 | #include "lpc17xx_timer.h" |
frank26080115 | 0:bf7b9fba3924 | 20 | #include "lpc17xx_libcfg.h" |
frank26080115 | 0:bf7b9fba3924 | 21 | #include "lpc17xx_pinsel.h" |
frank26080115 | 0:bf7b9fba3924 | 22 | #include "debug_frmwrk.h" |
frank26080115 | 0:bf7b9fba3924 | 23 | |
frank26080115 | 0:bf7b9fba3924 | 24 | |
frank26080115 | 0:bf7b9fba3924 | 25 | /* Example group ----------------------------------------------------------- */ |
frank26080115 | 0:bf7b9fba3924 | 26 | /** @defgroup TIMER_FreqMeasure FreqMeasure |
frank26080115 | 0:bf7b9fba3924 | 27 | * @ingroup TIMER_Examples |
frank26080115 | 0:bf7b9fba3924 | 28 | * @{ |
frank26080115 | 0:bf7b9fba3924 | 29 | */ |
frank26080115 | 0:bf7b9fba3924 | 30 | |
frank26080115 | 0:bf7b9fba3924 | 31 | /************************** PRIVATE VARIABLES *************************/ |
frank26080115 | 0:bf7b9fba3924 | 32 | uint8_t menu1[] = |
frank26080115 | 0:bf7b9fba3924 | 33 | "********************************************************************************\n\r" |
frank26080115 | 0:bf7b9fba3924 | 34 | "Hello NXP Semiconductors \n\r" |
frank26080115 | 0:bf7b9fba3924 | 35 | "Timer measure frequency demo \n\r" |
frank26080115 | 0:bf7b9fba3924 | 36 | "\t - MCU: LPC17xx \n\r" |
frank26080115 | 0:bf7b9fba3924 | 37 | "\t - Core: ARM Cortex-M3 \n\r" |
frank26080115 | 0:bf7b9fba3924 | 38 | "\t - Communicate via: UART0 - 115200 bps \n\r" |
frank26080115 | 0:bf7b9fba3924 | 39 | " Use timer 0 to measure input signal frequency through its CAP0.0 \n\r" |
frank26080115 | 0:bf7b9fba3924 | 40 | " Use timer 2 to generate different frequency signals \n\r" |
frank26080115 | 0:bf7b9fba3924 | 41 | "********************************************************************************\n\r"; |
frank26080115 | 0:bf7b9fba3924 | 42 | |
frank26080115 | 0:bf7b9fba3924 | 43 | TIM_TIMERCFG_Type TIM_ConfigStruct; |
frank26080115 | 0:bf7b9fba3924 | 44 | TIM_CAPTURECFG_Type TIM_CaptureConfigStruct; |
frank26080115 | 0:bf7b9fba3924 | 45 | |
frank26080115 | 0:bf7b9fba3924 | 46 | BOOL_8 first_capture,done; |
frank26080115 | 0:bf7b9fba3924 | 47 | uint32_t capture; |
frank26080115 | 0:bf7b9fba3924 | 48 | uint8_t count=0; |
frank26080115 | 0:bf7b9fba3924 | 49 | /************************** PRIVATE FUNCTIONS *************************/ |
frank26080115 | 0:bf7b9fba3924 | 50 | /* Interrupt service routines */ |
frank26080115 | 0:bf7b9fba3924 | 51 | void TIMER0_IRQHandler(void); |
frank26080115 | 0:bf7b9fba3924 | 52 | |
frank26080115 | 0:bf7b9fba3924 | 53 | void print_menu(void); |
frank26080115 | 0:bf7b9fba3924 | 54 | /*----------------- INTERRUPT SERVICE ROUTINES --------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 55 | /*********************************************************************//** |
frank26080115 | 0:bf7b9fba3924 | 56 | * @brief TIMER0 interrupt handler sub-routine |
frank26080115 | 0:bf7b9fba3924 | 57 | * @param[in] None |
frank26080115 | 0:bf7b9fba3924 | 58 | * @return None |
frank26080115 | 0:bf7b9fba3924 | 59 | **********************************************************************/ |
frank26080115 | 0:bf7b9fba3924 | 60 | void TIMER0_IRQHandler(void) |
frank26080115 | 0:bf7b9fba3924 | 61 | { |
frank26080115 | 0:bf7b9fba3924 | 62 | if (TIM_GetIntCaptureStatus(LPC_TIM0,0)) |
frank26080115 | 0:bf7b9fba3924 | 63 | { |
frank26080115 | 0:bf7b9fba3924 | 64 | TIM_ClearIntCapturePending(LPC_TIM0,0); |
frank26080115 | 0:bf7b9fba3924 | 65 | if(first_capture==TRUE) |
frank26080115 | 0:bf7b9fba3924 | 66 | { |
frank26080115 | 0:bf7b9fba3924 | 67 | TIM_Cmd(LPC_TIM0,DISABLE); |
frank26080115 | 0:bf7b9fba3924 | 68 | TIM_ResetCounter(LPC_TIM0); |
frank26080115 | 0:bf7b9fba3924 | 69 | TIM_Cmd(LPC_TIM0,ENABLE); |
frank26080115 | 0:bf7b9fba3924 | 70 | count++; |
frank26080115 | 0:bf7b9fba3924 | 71 | if(count==5)first_capture=FALSE; //stable |
frank26080115 | 0:bf7b9fba3924 | 72 | } |
frank26080115 | 0:bf7b9fba3924 | 73 | else |
frank26080115 | 0:bf7b9fba3924 | 74 | { |
frank26080115 | 0:bf7b9fba3924 | 75 | count=0; //reset count for next use |
frank26080115 | 0:bf7b9fba3924 | 76 | done=TRUE; |
frank26080115 | 0:bf7b9fba3924 | 77 | capture = TIM_GetCaptureValue(LPC_TIM0,0); |
frank26080115 | 0:bf7b9fba3924 | 78 | } |
frank26080115 | 0:bf7b9fba3924 | 79 | } |
frank26080115 | 0:bf7b9fba3924 | 80 | } |
frank26080115 | 0:bf7b9fba3924 | 81 | |
frank26080115 | 0:bf7b9fba3924 | 82 | /*-------------------------PRIVATE FUNCTIONS------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 83 | /*********************************************************************//** |
frank26080115 | 0:bf7b9fba3924 | 84 | * @brief Print menu |
frank26080115 | 0:bf7b9fba3924 | 85 | * @param[in] None |
frank26080115 | 0:bf7b9fba3924 | 86 | * @return None |
frank26080115 | 0:bf7b9fba3924 | 87 | **********************************************************************/ |
frank26080115 | 0:bf7b9fba3924 | 88 | void print_menu(void) |
frank26080115 | 0:bf7b9fba3924 | 89 | { |
frank26080115 | 0:bf7b9fba3924 | 90 | _DBG(menu1); |
frank26080115 | 0:bf7b9fba3924 | 91 | } |
frank26080115 | 0:bf7b9fba3924 | 92 | |
frank26080115 | 0:bf7b9fba3924 | 93 | |
frank26080115 | 0:bf7b9fba3924 | 94 | /*-------------------------MAIN FUNCTION------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 95 | /*********************************************************************//** |
frank26080115 | 0:bf7b9fba3924 | 96 | * @brief c_entry: Main TIMER program body |
frank26080115 | 0:bf7b9fba3924 | 97 | * @param[in] None |
frank26080115 | 0:bf7b9fba3924 | 98 | * @return int |
frank26080115 | 0:bf7b9fba3924 | 99 | **********************************************************************/ |
frank26080115 | 0:bf7b9fba3924 | 100 | int c_entry(void) |
frank26080115 | 0:bf7b9fba3924 | 101 | { |
frank26080115 | 0:bf7b9fba3924 | 102 | PINSEL_CFG_Type PinCfg; |
frank26080115 | 0:bf7b9fba3924 | 103 | TIM_MATCHCFG_Type TIM_MatchConfigStruct; |
frank26080115 | 0:bf7b9fba3924 | 104 | uint8_t idx; |
frank26080115 | 0:bf7b9fba3924 | 105 | uint16_t tem; |
frank26080115 | 0:bf7b9fba3924 | 106 | uint32_t freq,temcap; |
frank26080115 | 0:bf7b9fba3924 | 107 | /* Initialize debug via UART0 |
frank26080115 | 0:bf7b9fba3924 | 108 | * 115200bps |
frank26080115 | 0:bf7b9fba3924 | 109 | * 8 data bit |
frank26080115 | 0:bf7b9fba3924 | 110 | * No parity |
frank26080115 | 0:bf7b9fba3924 | 111 | * 1 stop bit |
frank26080115 | 0:bf7b9fba3924 | 112 | * No flow control |
frank26080115 | 0:bf7b9fba3924 | 113 | */ |
frank26080115 | 0:bf7b9fba3924 | 114 | debug_frmwrk_init(); |
frank26080115 | 0:bf7b9fba3924 | 115 | |
frank26080115 | 0:bf7b9fba3924 | 116 | // print welcome screen |
frank26080115 | 0:bf7b9fba3924 | 117 | print_menu(); |
frank26080115 | 0:bf7b9fba3924 | 118 | |
frank26080115 | 0:bf7b9fba3924 | 119 | |
frank26080115 | 0:bf7b9fba3924 | 120 | //Config P1.26 as CAP0.0 |
frank26080115 | 0:bf7b9fba3924 | 121 | PinCfg.Funcnum = 3; |
frank26080115 | 0:bf7b9fba3924 | 122 | PinCfg.OpenDrain = 0; |
frank26080115 | 0:bf7b9fba3924 | 123 | PinCfg.Pinmode = 0; |
frank26080115 | 0:bf7b9fba3924 | 124 | PinCfg.Portnum = 1; |
frank26080115 | 0:bf7b9fba3924 | 125 | PinCfg.Pinnum = 26; |
frank26080115 | 0:bf7b9fba3924 | 126 | PINSEL_ConfigPin(&PinCfg); |
frank26080115 | 0:bf7b9fba3924 | 127 | // Configure P0.6 as MAT2.0 |
frank26080115 | 0:bf7b9fba3924 | 128 | PinCfg.Funcnum = 3; |
frank26080115 | 0:bf7b9fba3924 | 129 | PinCfg.OpenDrain = 0; |
frank26080115 | 0:bf7b9fba3924 | 130 | PinCfg.Pinmode = 0; |
frank26080115 | 0:bf7b9fba3924 | 131 | PinCfg.Portnum = 0; |
frank26080115 | 0:bf7b9fba3924 | 132 | PinCfg.Pinnum = 6; |
frank26080115 | 0:bf7b9fba3924 | 133 | PINSEL_ConfigPin(&PinCfg); |
frank26080115 | 0:bf7b9fba3924 | 134 | |
frank26080115 | 0:bf7b9fba3924 | 135 | while(1) |
frank26080115 | 0:bf7b9fba3924 | 136 | { |
frank26080115 | 0:bf7b9fba3924 | 137 | // Initialize timer 0, prescale count time of 1uS |
frank26080115 | 0:bf7b9fba3924 | 138 | TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; |
frank26080115 | 0:bf7b9fba3924 | 139 | TIM_ConfigStruct.PrescaleValue = 1; |
frank26080115 | 0:bf7b9fba3924 | 140 | |
frank26080115 | 0:bf7b9fba3924 | 141 | // use channel 0, CAPn.0 |
frank26080115 | 0:bf7b9fba3924 | 142 | TIM_CaptureConfigStruct.CaptureChannel = 0; |
frank26080115 | 0:bf7b9fba3924 | 143 | // Enable capture on CAPn.0 rising edge |
frank26080115 | 0:bf7b9fba3924 | 144 | TIM_CaptureConfigStruct.RisingEdge = ENABLE; |
frank26080115 | 0:bf7b9fba3924 | 145 | // Enable capture on CAPn.0 falling edge |
frank26080115 | 0:bf7b9fba3924 | 146 | TIM_CaptureConfigStruct.FallingEdge = DISABLE; |
frank26080115 | 0:bf7b9fba3924 | 147 | // Generate capture interrupt |
frank26080115 | 0:bf7b9fba3924 | 148 | TIM_CaptureConfigStruct.IntOnCaption = ENABLE; |
frank26080115 | 0:bf7b9fba3924 | 149 | |
frank26080115 | 0:bf7b9fba3924 | 150 | // Set configuration for Tim_config and Tim_MatchConfig |
frank26080115 | 0:bf7b9fba3924 | 151 | TIM_Init(LPC_TIM0, TIM_TIMER_MODE,&TIM_ConfigStruct); |
frank26080115 | 0:bf7b9fba3924 | 152 | TIM_ConfigCapture(LPC_TIM0, &TIM_CaptureConfigStruct); |
frank26080115 | 0:bf7b9fba3924 | 153 | TIM_ResetCounter(LPC_TIM0); |
frank26080115 | 0:bf7b9fba3924 | 154 | /////////////////////////////////////////////////////////////////// |
frank26080115 | 0:bf7b9fba3924 | 155 | idx=0;freq=0;tem=0; |
frank26080115 | 0:bf7b9fba3924 | 156 | while(idx<3) |
frank26080115 | 0:bf7b9fba3924 | 157 | { |
frank26080115 | 0:bf7b9fba3924 | 158 | if(idx==0) |
frank26080115 | 0:bf7b9fba3924 | 159 | _DBG("\n\rPlease input frequency (from 1 to 999 hz):"); |
frank26080115 | 0:bf7b9fba3924 | 160 | tem = _DG; |
frank26080115 | 0:bf7b9fba3924 | 161 | switch(tem) |
frank26080115 | 0:bf7b9fba3924 | 162 | { |
frank26080115 | 0:bf7b9fba3924 | 163 | case '0':case'1':case '2':case '3':case '4':case '5':case '6':case '7':case'8':case '9': |
frank26080115 | 0:bf7b9fba3924 | 164 | tem = tem - 0x30; |
frank26080115 | 0:bf7b9fba3924 | 165 | idx++; |
frank26080115 | 0:bf7b9fba3924 | 166 | if(idx==1) tem = tem * 100; |
frank26080115 | 0:bf7b9fba3924 | 167 | else if (idx==2) tem = tem * 10; |
frank26080115 | 0:bf7b9fba3924 | 168 | freq = freq + tem; |
frank26080115 | 0:bf7b9fba3924 | 169 | if(idx==3)_DBD16(freq); |
frank26080115 | 0:bf7b9fba3924 | 170 | tem = 0; |
frank26080115 | 0:bf7b9fba3924 | 171 | break; |
frank26080115 | 0:bf7b9fba3924 | 172 | default: |
frank26080115 | 0:bf7b9fba3924 | 173 | _DBG("...Please input digits from 0 to 9 only!"); |
frank26080115 | 0:bf7b9fba3924 | 174 | idx = 0; tem = 0; freq = 0; |
frank26080115 | 0:bf7b9fba3924 | 175 | break; |
frank26080115 | 0:bf7b9fba3924 | 176 | } |
frank26080115 | 0:bf7b9fba3924 | 177 | } |
frank26080115 | 0:bf7b9fba3924 | 178 | TIM_ConfigStruct.PrescaleOption = TIM_PRESCALE_USVAL; |
frank26080115 | 0:bf7b9fba3924 | 179 | TIM_ConfigStruct.PrescaleValue = 1;//1us |
frank26080115 | 0:bf7b9fba3924 | 180 | TIM_Init(LPC_TIM2, TIM_TIMER_MODE,&TIM_ConfigStruct); |
frank26080115 | 0:bf7b9fba3924 | 181 | |
frank26080115 | 0:bf7b9fba3924 | 182 | // use channel 0, MR0 |
frank26080115 | 0:bf7b9fba3924 | 183 | TIM_MatchConfigStruct.MatchChannel = 0; |
frank26080115 | 0:bf7b9fba3924 | 184 | // Disable interrupt when MR0 matches the value in TC register |
frank26080115 | 0:bf7b9fba3924 | 185 | TIM_MatchConfigStruct.IntOnMatch = FALSE; |
frank26080115 | 0:bf7b9fba3924 | 186 | //Enable reset on MR0: TIMER will reset if MR0 matches it |
frank26080115 | 0:bf7b9fba3924 | 187 | TIM_MatchConfigStruct.ResetOnMatch = TRUE; |
frank26080115 | 0:bf7b9fba3924 | 188 | //Stop on MR0 if MR0 matches it |
frank26080115 | 0:bf7b9fba3924 | 189 | TIM_MatchConfigStruct.StopOnMatch = FALSE; |
frank26080115 | 0:bf7b9fba3924 | 190 | //Toggle MR0.0 pin if MR0 matches it |
frank26080115 | 0:bf7b9fba3924 | 191 | TIM_MatchConfigStruct.ExtMatchOutputType =TIM_EXTMATCH_TOGGLE; |
frank26080115 | 0:bf7b9fba3924 | 192 | // Set Match value |
frank26080115 | 0:bf7b9fba3924 | 193 | TIM_MatchConfigStruct.MatchValue = 500000/freq; |
frank26080115 | 0:bf7b9fba3924 | 194 | TIM_ConfigMatch(LPC_TIM2,&TIM_MatchConfigStruct); |
frank26080115 | 0:bf7b9fba3924 | 195 | |
frank26080115 | 0:bf7b9fba3924 | 196 | TIM_Cmd(LPC_TIM2,ENABLE); |
frank26080115 | 0:bf7b9fba3924 | 197 | ////////////////////////////////////////////////////////////////// |
frank26080115 | 0:bf7b9fba3924 | 198 | /* preemption = 1, sub-priority = 1 */ |
frank26080115 | 0:bf7b9fba3924 | 199 | NVIC_SetPriority(TIMER0_IRQn, ((0x01<<3)|0x01)); |
frank26080115 | 0:bf7b9fba3924 | 200 | /* Enable interrupt for timer 0 */ |
frank26080115 | 0:bf7b9fba3924 | 201 | NVIC_EnableIRQ(TIMER0_IRQn); |
frank26080115 | 0:bf7b9fba3924 | 202 | |
frank26080115 | 0:bf7b9fba3924 | 203 | first_capture = TRUE;done=FALSE;capture=0; |
frank26080115 | 0:bf7b9fba3924 | 204 | // To start timer 0 |
frank26080115 | 0:bf7b9fba3924 | 205 | TIM_Cmd(LPC_TIM0,ENABLE); |
frank26080115 | 0:bf7b9fba3924 | 206 | |
frank26080115 | 0:bf7b9fba3924 | 207 | _DBG("\n\rMeasuring......"); |
frank26080115 | 0:bf7b9fba3924 | 208 | while(done==FALSE); |
frank26080115 | 0:bf7b9fba3924 | 209 | temcap = 1000000 / capture;_DBD16(temcap);_DBG("hz"); |
frank26080115 | 0:bf7b9fba3924 | 210 | NVIC_DisableIRQ(TIMER0_IRQn); |
frank26080115 | 0:bf7b9fba3924 | 211 | TIM_DeInit(LPC_TIM0); |
frank26080115 | 0:bf7b9fba3924 | 212 | TIM_DeInit(LPC_TIM2); |
frank26080115 | 0:bf7b9fba3924 | 213 | _DBG("\n\rPress c to continue measuring other signals..."); |
frank26080115 | 0:bf7b9fba3924 | 214 | while(_DG != 'c'); |
frank26080115 | 0:bf7b9fba3924 | 215 | } |
frank26080115 | 0:bf7b9fba3924 | 216 | while (1); |
frank26080115 | 0:bf7b9fba3924 | 217 | return 1; |
frank26080115 | 0:bf7b9fba3924 | 218 | } |
frank26080115 | 0:bf7b9fba3924 | 219 | |
frank26080115 | 0:bf7b9fba3924 | 220 | /* Support required entry point for other toolchain */ |
frank26080115 | 0:bf7b9fba3924 | 221 | int main (void) |
frank26080115 | 0:bf7b9fba3924 | 222 | { |
frank26080115 | 0:bf7b9fba3924 | 223 | return c_entry(); |
frank26080115 | 0:bf7b9fba3924 | 224 | } |
frank26080115 | 0:bf7b9fba3924 | 225 | #ifdef DEBUG |
frank26080115 | 0:bf7b9fba3924 | 226 | /******************************************************************************* |
frank26080115 | 0:bf7b9fba3924 | 227 | * @brief Reports the name of the source file and the source line number |
frank26080115 | 0:bf7b9fba3924 | 228 | * where the CHECK_PARAM error has occurred. |
frank26080115 | 0:bf7b9fba3924 | 229 | * @param[in] file Pointer to the source file name |
frank26080115 | 0:bf7b9fba3924 | 230 | * @param[in] line assert_param error line source number |
frank26080115 | 0:bf7b9fba3924 | 231 | * @return None |
frank26080115 | 0:bf7b9fba3924 | 232 | *******************************************************************************/ |
frank26080115 | 0:bf7b9fba3924 | 233 | void check_failed(uint8_t *file, uint32_t line) |
frank26080115 | 0:bf7b9fba3924 | 234 | { |
frank26080115 | 0:bf7b9fba3924 | 235 | /* User can add his own implementation to report the file name and line number, |
frank26080115 | 0:bf7b9fba3924 | 236 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ |
frank26080115 | 0:bf7b9fba3924 | 237 | |
frank26080115 | 0:bf7b9fba3924 | 238 | /* Infinite loop */ |
frank26080115 | 0:bf7b9fba3924 | 239 | while(1); |
frank26080115 | 0:bf7b9fba3924 | 240 | } |
frank26080115 | 0:bf7b9fba3924 | 241 | #endif |
frank26080115 | 0:bf7b9fba3924 | 242 | /* |
frank26080115 | 0:bf7b9fba3924 | 243 | * @} |
frank26080115 | 0:bf7b9fba3924 | 244 | */ |