Uses Timer 0 of the 11U24 to capture input pulsesl
Revision 0:22c6b94e7da6, committed 2014-03-13
- Comitter:
- hexley
- Date:
- Thu Mar 13 17:52:43 2014 +0000
- Commit message:
- Initial version
Changed in this revision
capturet0_11u24.cpp | Show annotated file Show diff for this revision Revisions of this file |
capturet0_11u24.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/capturet0_11u24.cpp Thu Mar 13 17:52:43 2014 +0000 @@ -0,0 +1,120 @@ + /* mbed Library - Capture driver for CT32B0 + * Copyright (c) 2010 + * released under MIT license http://mbed.org/licence/mit + */ + /***********************************************************************//** + * @file capturet1_11u24.cpp + * @brief Driver file for the CAP1 function of CT32B0 in the mbed 11U24. + * @version 0.0 + * @date 05 Feb 2014 + * @author + **************************************************************************/ +#include "mbed.h" +#include "capturet0_11u24.h" + +Capture *Capture::instance; + +/*********************************************************************//** + * @brief Create a Capture object and configure it. + * @param None Uses p7 as the Capture input + * @return None + **********************************************************************/ +Capture::Capture(void) +{ + /* Set up clock and power, using default peripheral system clock divider */ + LPC_SYSCON->SYSAHBCLKCTRL |= CT32B0_CLK_ENABLE; // See page 30 of the 11U24 User Guide + // Check the clock divider ratio by reading LPC_SYSCON->SYSAHBCLKDIV ls byte + LPC_CT32B0->PR = 0; // Prescale counter divide by 1 + + /* Configure IOCON for the proper function of the capture pin. Use a pull-up. */ + LPC_IOCON->PIO1_29 = (CT32B0_CAP1 | CAP_PULLUP); // See page 114 of the 11U24 User Guide + + // Select the capture edge and int mode + LPC_CT32B0->CCR = CCR_CAP1FE | CCR_CAP1I; // Select inputs and interrupts. See page 345 + + // Set up the rising edge clear, using timer mode + LPC_CT32B0->CTCR = CTCR_CTM_TIMER | CTCR_ENCC | CTCR_SEICC_CAP1RE; // See page 350 + + + // Clear interrupt flag + LPC_CT32B0->IR = IR_CR1INT; // See page 342 + + /* preemption = 1, sub-priority = 1 */ +// NVIC_SetPriority(TIMER2_IRQn, ((0x01<<3)|0x01)); +// NVIC_SetPriority(TIMER2_IRQn, 0); // Set to top priority to minimize interference from tickers. + + //* Attach IRQ + instance = this; + NVIC_SetVector(TIMER_32_0_IRQn, (uint32_t)&_Captureisr); + +} + +/*********************************************************************//** + * @brief Start capturing data. Average the specified number of samples. + * @param (int) numsamples Log base 2 of the number of samples to be averaged + * @return None + **********************************************************************/ +void Capture::Start(int numsamples) { + Nsamples = numsamples; + capturedata = 0; // Clear the accumulator + flag = 0; // No data ready + + /* Enable interrupt for CAPTURE */ + NVIC_EnableIRQ(TIMER_32_0_IRQn); + + // Start the timer + LPC_CT32B0->TCR = TCR_CEN; // enable +} + +/*********************************************************************//** + * @brief Read (accumulated) sample count. + * + * @return (unsigned int) Accumulated capture value + **********************************************************************/ +unsigned int Capture::Read(void) { + flag = 0; // Clear previous status (or error) + return capturedata; +} + +/*********************************************************************//** + * @brief Check for accumulation complete (data is ready to read). + * + * @return (unsigned int) Accumulated capture value + **********************************************************************/ +int Capture::Ready(void) { + return flag; +} + + +/* Capture isr instantiator */ +void Capture::_Captureisr(void) +{ + instance->Captureisr(); +} + +/*********************************************************************//** + * @brief Capture interrupt service routine. Handles accumulation. + * + * @param[in] none + * + * @return none + **********************************************************************/ +void Capture::Captureisr(void) { + static unsigned int samplecount = 0; // Counts passes + static unsigned int capturesum = 0; // Accumulates the readings + + if(LPC_CT32B0->IR & IR_CR1INT) { + LPC_CT32B0->IR |= IR_CR1INT; // Clear the interrupt request + capturesum += LPC_CT32B0->CR2; // Read and accumulate the captured results + // It is necessary to use "CR2" in order to get the proper + // offset from the base address (an issue with CMSIS) + if(++samplecount == Nsamples) { + capturedata = capturesum; // Update the output data + samplecount = 0; // Restart the pass counter + capturesum = 0; // Re-initialize the running total + if(flag != 0) flag = -1; // Make flag negative on an overrun + else flag = 1; // Set flag to 1 if no error + } + } + return; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/capturet0_11u24.h Thu Mar 13 17:52:43 2014 +0000 @@ -0,0 +1,133 @@ + /* mbed Library - CaptureT0_11U24 + * Copyright (c) 2014 + * released under MIT license http://mbed.org/licence/mit + */ + +/***********************************************************************//** + * @file capturet1_11u24.h + * @brief Header file for the CaptureT0_11U24 library. + * @version 0.0 + * @date 5 Feb 2014 + * @author + **************************************************************************/ +/*************************************************************************** + * Revision Date Comments + *---------- -------- ----------------------------------------------- + * + * 0.0 2/5/14 Initial creation + ***************************************************************************/ + +#ifndef CAPTURET0_H +#define CAPTURET0_H + +/* Includes ------------------------------------------------------------------- */ +#include "mbed.h" + + +/* Public Types --------------------------------------------------------------- */ + + +/* Public Functions ----------------------------------------------------------- */ +/** @defgroup Capture_Public_Functions Capture Public Functions +*/ +class Capture { +public: + +/** Create a Capture object and configure it + * + * @param none + */ +Capture(void); + +/** Start capturing data. Accumulate the specified number of samples. + * @param numsamples The number of samples to be accumulated + * @return None + */ +void Start(int numsamples); + +/** Check to see if a new count is ready. + * + * @return (int) Status value. Negative means overrun. + */ +int Ready(void); + +/** Read (accumlated) sample count. + * + * @return (unsigned int) Accumulated capture value + */ +unsigned int Read(void); + + + +/* Public Macros -------------------------------------------------------------- */ + + + + +private: +static void _Captureisr(void); +void Captureisr(void); +static Capture *instance; +int flag; +int channel; +//int capturecount; +unsigned int capturedata; +int Nsamples; +//int Nsamplebits; +//int *flagaddresspointer; + + + +/* Private Macros ------------------------------------------------------------- */ +/* --------------------- BIT DEFINITIONS -------------------------------------- */ +/* Timer Capture Register Definitions --------------------- */ + +/*********************************************************************//** + * Macro defines for CT32B0->IR Interrupt tegister + **********************************************************************/ +#define IR_CR1INT ((uint32_t)(1<<6)) /**< Interrupt flag for capture channel 1 */ + +/*********************************************************************//** + * Macro defines for IOCON->PIO1_29 register bits LPC11U24 + **********************************************************************/ +#define PIN_PULL_UP 2UL +#define PIN_REPEATER 3UL +#define PIN_NORESISTOR 0UL +#define PIN_PULL_DOWN 1UL + +/* FUNC bits */ +#define CT32B0_CAP1 2UL + +/* MODE bits */ +#define CAP_PULLUP ((uint32_t)(PIN_PULL_UP<<3)) // Pull up on the CT32B0_CAP1 input pin + +/*********************************************************************//** + * Macro defines for SYSCON->SYSAHBCLKDIV register bits LPC11U24 + **********************************************************************/ +#define CT32B0_CLK_ENABLE ((uint32_t)(1<<9)) /**< CT32B0 clock enable */ + +/*********************************************************************//** + * Macro defines for CT32B0->TCR register bits LPC11U24 + **********************************************************************/ +#define TCR_CEN 1UL + + +/*********************************************************************//** + * Macro defines for CT32B0->CCR register bits LPC11U24 + **********************************************************************/ +#define CCR_CAP1RE ((uint32_t)(1<<6)) +#define CCR_CAP1FE ((uint32_t)(1<<7)) +#define CCR_CAP1I ((uint32_t)(1<<8)) + +/*********************************************************************//** + * Macro defines for CT32B0->CTCR register bits LPC11U24 + **********************************************************************/ +#define CTCR_CTM_TIMER 0UL +#define CTCR_ENCC ((uint32_t)(1<<4)) +#define CTCR_SEICC_CAP1RE ((uint32_t)(4<<5)) + + + +}; // End of Capture class information +#endif /* CAPTURE_H */ +/* --------------------------------- End Of File ------------------------------ */