Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: PulseWidthCapture_Program
Revision 0:7076676dd640, committed 2014-12-05
- Comitter:
- Ellor1
- Date:
- Fri Dec 05 10:49:31 2014 +0000
- Child:
- 1:6bb38ae2e503
- Commit message:
- working;
Changed in this revision
| PulseWidthCapture.cpp | Show annotated file Show diff for this revision Revisions of this file |
| PulseWidthCapture.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/PulseWidthCapture.cpp Fri Dec 05 10:49:31 2014 +0000
@@ -0,0 +1,277 @@
+#include "mbed.h"
+#include "TextLCD_16x4.h"
+#include "USBSerial.h"
+#include "PulseWidthCapture.h"
+#include "rtos.h"
+
+USBSerial serial;
+DigitalOut led(P0_9);
+DigitalOut led2(P0_8);
+DigitalOut led3(P0_7);
+DigitalOut led4(P0_17);
+
+//AnalogIn Ain(P0_11);
+
+Capture *Capture::instance;
+
+/*********************************************************************//**
+ * @brief Create a Capture object and configure it.
+ * @param None Uses P1_29 as the Capture input (Pin 7)
+ * @param None Uses P0_12 as the Capture input (Pin 16)
+ * @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_SYSCON->SYSAHBCLKCTRL |= CT32B1_CLK_ENABLE;
+
+ LPC_SYSCON->SYSAHBCLKCTRL |= CT16B0_CLK_ENABLE;
+
+ LPC_CT32B0->PR = 0; // Prescale counter divide by ...
+ LPC_CT32B1->PR = 0;
+ LPC_CT16B0->PR = 239; //200KHZ
+
+ /* Configure IOCON for the proper function of the capture pin. Use a pull-up. */
+ LPC_IOCON->PIO1_29 = (CT32B0_CAP1); // See page 125 of the 11U24 User Guide
+
+ LPC_IOCON->TMS_PIO0_12 = CT32B1_CAP0 | ADMODE;
+
+ // Select the capture edge and int mode
+ LPC_CT32B0->CCR = CCR_CAP1RE | CCR_CAP1I; // Select inputs and interrupts.
+
+ LPC_CT32B1->CCR = CCR_CAP0FE | CCR_CAP0I;
+
+ LPC_CT16B0->MCR = CT16B0_MR0I | CT16B0_MR0R;
+
+ // Set up the rising edge clear, using timer mode
+ LPC_CT32B0->CTCR = CT32B0_CTCR_CTM_TIMER | CT32B0_CTCR_ENCC | CT32B0_CTCR_SEICC_CAP1FE;
+
+ // clear timer on falling edge
+ LPC_CT32B1->CTCR = CT32B1_CTCR_CTM_TIMER | CT32B1_CTCR_ENCC | CT32B1_CTCR_SEICC_CAP0RE;
+
+ // Clear interrupt flag
+ LPC_CT32B0->IR = CT32B0_IR_CR1INT;
+
+ LPC_CT32B1->IR = CT32B1_IR_CR0INT;
+
+ LPC_CT16B0->IR = CT16B0_IR_MR0INT;
+
+
+ //* Attach IRQ
+ instance = this;
+ NVIC_SetVector(TIMER_32_0_IRQn, (uint32_t)&_Captureisr1);
+ NVIC_SetVector(TIMER_32_1_IRQn, (uint32_t)&_Captureisr2);
+ NVIC_SetVector(TIMER_16_0_IRQn, (uint32_t)&_Captureisr3);
+
+}
+
+/*********************************************************************//**
+ * @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_1(void) {
+ capturedata = 0; // Clear the accumulator
+ // No data ready
+
+ /* Enable interrupt for CAPTURE */
+ NVIC_EnableIRQ(TIMER_32_0_IRQn);
+
+ // Start the timer
+ LPC_CT32B0->TCR = CT32B0_TCR_CEN; // enable
+
+}
+
+void Capture::Start_2(void) {
+ capturedata_2 = 0; // Clear the accumulator
+ // No data ready
+
+ /* Enable interrupt for CAPTURE */
+ NVIC_EnableIRQ(TIMER_32_1_IRQn);
+
+ // Start the timer
+ LPC_CT32B1->TCR = CT32B1_TCR_CEN; // enable
+
+}
+
+/*********************************************************************//**
+ * @brief Read (accumulated) sample count.
+ *
+ * @return (unsigned int) Accumulated capture value
+ **********************************************************************/
+
+ unsigned int Capture::Debug(void) {
+
+ debug = LPC_IOCON->TMS_PIO0_12 ; // to check teh values in registers
+
+ return debug;
+
+}
+
+//unsigned int Capture::Read_1(void) {
+void Capture::Read_1(void) {
+
+ // static unsigned int i_1 = 0;
+ i_1 = capturedata;
+ // fall = ((i_1/48003280)*1000000);
+ //test_1 = 101;
+ // i_1 = capturedata_2;
+
+ // return capturedata;
+
+}
+
+
+//unsigned int Capture::Read_2(void) {
+void Capture::Read_2(void) {
+
+ // static unsigned int j_1 = 0;
+ j_1 = capturedata_2;
+ // rise = ((j_1/48003280)*1000000);
+ // test_2 = 105;
+ // j_1 = capturedata_2;
+
+ //return capturedata_2;
+
+}
+
+
+void Capture::Wait(void) {
+
+ led3 = !led3;
+
+ //Enable interrupt
+ NVIC_EnableIRQ(TIMER_16_0_IRQn);
+
+ // Start the timer
+ LPC_CT16B0->TCR = CT16B0_TCR_CEN; // enable
+
+ LPC_CT16B0->MR0 = 10000000;
+
+}
+
+
+
+/* Capture isr instantiator */
+void Capture::_Captureisr1(void)
+{
+ instance->Captureisr1();
+}
+
+/* Capture isr instantiator */
+void Capture::_Captureisr2(void)
+{
+ instance->Captureisr2();
+}
+
+/* Capture isr instantiator */
+
+
+void Capture::_Captureisr3(void)
+{
+ instance->Captureisr3();
+}
+
+
+/*********************************************************************//**
+ * @brief Capture interrupt service routine. Handles accumulation.
+ *
+ * @param[in] none
+ *
+ * @return none
+ **********************************************************************/
+void Capture::Captureisr1(void) {
+
+ static int capturesum = 0; // Accumulates the readings
+
+ led = !led;
+ capturesum = LPC_CT32B0->CR2;
+ rise = (((float)capturesum/48003280)*1000000);
+ // capturedata = 101;
+ capturedata = capturesum;
+ // rise = ((capturedata/48003280)*1000000);
+ //test_1 = 101;
+ LPC_CT32B0->IR |= CT32B0_IR_CR1INT;
+
+ return;
+
+}
+
+/*********************************************************************//**
+ * @brief Capture interrupt service routine. Handles accumulation.
+ *
+ * @param[in] none
+ *
+ * @return none
+ **********************************************************************/
+void Capture::Captureisr2(void) {
+
+ static int capturesum_2 = 0; // Accumulates the readings
+
+ led2 = !led2;
+ capturesum_2 = LPC_CT32B1->CR0;
+ // rise = capturesum_2;
+ // rise_2 = ((rise/48003280)*1000000);
+ //rise = ((capturesum_2/48003280)*1000000);
+ capturedata_2 = capturesum_2;
+
+ fall = (((float)capturesum_2/48003280)*1000000);
+ //test_2 = 105;
+ LPC_CT32B1->IR |= CT32B1_IR_CR0INT;
+
+ return;
+}
+
+
+
+void Capture::Captureisr3(void) {
+
+ NVIC_DisableIRQ(TIMER_32_0_IRQn);
+ NVIC_DisableIRQ(TIMER_32_1_IRQn);
+
+ led4 = !led4;
+ //m_1 = i_1 + j_1;
+
+ // ((i_1/48003280)*1000000);
+ //rise = ((capturedata/48003280)*1000000);
+
+
+
+ //serial.printf("rise:%d \n\r", rise);
+
+
+ //serial.printf("test_1:%d \n\r", test_1);
+ // serial.printf("test_2:%d \n\r", test_2);
+
+ // serial.printf("rise:%d \n\r", rise);
+ //serial.printf("rise_2:%d \n\r", rise_2);
+
+ //serial.printf("%.2f \n\r",ADCdata);
+
+ //serial.printf("fall:%d \n\r", fall);
+ if (serial.writeable())
+ {
+ serial.printf("capturedata:%d \n\r", capturedata);
+ serial.printf("fall:%f \n\r", rise);
+ serial.printf("fall:%f \n\r", fall);
+ serial.printf("i_1:%.8f \n\r\n", i_1);
+ serial.printf("i:%.8f \n\r\n", ((i_1/48003280)*1000000));
+
+ }
+ // serial.printf("j:%.8f \n\r\n", ((j_1/48003280)*1000000));
+
+ // serial.printf("i+j:%.8f \n\r\n", m_1);
+
+ NVIC_EnableIRQ(TIMER_32_0_IRQn);
+ NVIC_EnableIRQ(TIMER_32_1_IRQn);
+
+ LPC_CT16B0->IR |= CT16B0_IR_MR0INT;
+
+ return;
+
+ }
+
+
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/PulseWidthCapture.h Fri Dec 05 10:49:31 2014 +0000
@@ -0,0 +1,199 @@
+ /* mbed Library - PulseWidthCapture
+ * Copyright (c) 2014
+ * released under MIT license http://mbed.org/licence/mit
+ */
+
+/***********************************************************************//**
+ * @file PulseWidthCapture.h
+ * @brief Header file for the PulseWidthCapture library.
+ * @version 0.0
+ * @date 03 Dec 2014
+ * @author
+ **************************************************************************/
+/***************************************************************************
+ * Revision Date Comments
+ *---------- -------- -----------------------------------------------
+ *
+ * 0.0 03/12/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_1(void);
+
+void Start_2(void);
+
+void Wait(void);
+
+
+/** Read (accumlated) sample count.
+ *
+ * @return (unsigned int) Accumulated capture value
+ */
+//unsigned int Read_1(void);
+void Read_1(void);
+
+//unsigned int Read_2(void);
+void Read_2(void);
+
+unsigned int Debug(void);
+
+/* Public Macros -------------------------------------------------------------- */
+
+private:
+static void _Captureisr1(void);
+static void _Captureisr2(void);
+static void _Captureisr3(void);
+void Captureisr1(void);
+void Captureisr2(void);
+void Captureisr3(void);
+static Capture *instance;
+//double capturedata;
+unsigned int capturedata;
+unsigned int capturedata_2;
+unsigned int test_1, test_2;
+float rise, fall;
+double rise_2, fall_2, i_1, j_1, m_1;
+unsigned int debug;
+
+/* Private Macros ------------------------------------------------------------- */
+/* --------------------- BIT DEFINITIONS -------------------------------------- */
+/* Timer Capture Register Definitions --------------------- */
+
+/*********************************************************************//**
+ * Macro defines for CT32B0->IR Interrupt tegister
+ **********************************************************************/
+#define CT32B0_IR_CR0INT ((uint32_t)(1<<4))
+#define CT32B0_IR_CR1INT ((uint32_t)(1<<6)) /**< Interrupt flag for capture channel 1 */
+
+#define CT16B0_IR_MR0INT ((uint32_t)(1<<0))
+
+
+/*********************************************************************//**
+ * Macro defines for CT32B1->IR Interrupt tegister
+ **********************************************************************/
+#define CT32B1_IR_CR0INT ((uint32_t)(1<<4)) /**< Interrupt flag for capture channel 1 */
+#define CT32B1_IR_CR1INT ((uint32_t)(1<<5))
+
+/*********************************************************************//**
+ * Macro defines for IOCON->PIO1_29 register bits LPC11U24
+ **********************************************************************/
+#define P1_29_PIN_PULL_UP 2UL
+#define P1_29_PIN_REPEATER 3UL
+#define P1_29_PIN_NORESISTOR 0UL
+#define P1_29_PIN_PULL_DOWN 1UL
+
+/* FUNC bits */
+#define PORT_PIO1_29 0UL
+#define SCKO 1UL
+#define CT32B0_CAP1 2UL
+
+
+
+/* MODE bits */
+#define P1_29_CAP_PULLUP ((uint32_t)(PIN_PULL_UP<<3)) // Pull up on the CT32B0_CAP1 input pin
+
+/*********************************************************************//**
+ * Macro defines for IOCON->PIO0_12 register bits LPC11U24
+ **********************************************************************/
+#define P0_12_PIN_PULL_UP 2UL
+#define P0_12_PIN_REPEATER 3UL
+#define P0_12_PIN_NORESISTOR 0UL
+#define P0_12_PIN_PULL_DOWN 1UL
+
+/* FUNC bits */
+#define PIO0_12 1UL
+#define ADL 2UL
+#define CT32B1_CAP0 3UL //forPIO0_12
+#define ADMODE ((uint32_t)(1<<7))
+#define FILTR ((uint32_t)(1<<8))
+
+/* MODE bits */
+#define P0_12_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 */
+
+#define CT32B1_CLK_ENABLE ((uint32_t)(1<<10))
+
+#define CT16B0_CLK_ENABLE ((uint32_t)(1<<7))
+/*********************************************************************//**
+ * Macro defines for CT32B0->TCR register bits LPC11U24
+ **********************************************************************/
+#define CT32B0_TCR_CEN 1UL
+
+/*********************************************************************//**
+ * Macro defines for CT32B1->TCR register bits LPC11U24
+ **********************************************************************/
+#define CT32B1_TCR_CEN 1UL
+
+/*********************************************************************//**
+ * Macro defines for CT16B0->TCR register bits LPC11U24
+ **********************************************************************/
+#define CT16B0_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 CT32B1->CCR register bits LPC11U24
+ **********************************************************************/
+#define CCR_CAP0RE ((uint32_t)(1<<0))
+#define CCR_CAP0FE ((uint32_t)(1<<1))
+#define CCR_CAP0I ((uint32_t)(1<<2))
+
+/*********************************************************************//**
+ * Macro defines for CT32B0->CTCR register bits LPC11U24
+ **********************************************************************/
+#define CT32B0_CTCR_CTM_TIMER 0UL
+#define CT32B0_CTCR_ENCC ((uint32_t)(1<<4))
+#define CT32B0_CTCR_SEICC_CAP1RE ((uint32_t)(4<<5))
+#define CT32B0_CTCR_SEICC_CAP1FE ((uint32_t)(5<<5))
+
+
+/*********************************************************************//**
+ * Macro defines for CT32B1->CTCR register bits LPC11U24
+ **********************************************************************/
+#define CT32B1_CTCR_CTM_TIMER 0UL
+#define CT32B1_CTCR_ENCC ((uint32_t)(1<<4))
+#define CT32B1_CTCR_SEICC_CAP0RE ((uint32_t)(0<<5)) //CT32B1_CAP0
+#define CT32B1_CTCR_SEICC_CAP0FE ((uint32_t)(1<<5))
+
+#define CT16B0_MR0I ((uint32_t)(1<<0))
+
+#define CT16B0_MR0R ((uint32_t)(1<<1))
+
+
+
+}; // End of Capture class information
+#endif /* CAPTURE_H */
+/* --------------------------------- End Of File ------------------------------ */