Callum Ellor / PulseWidthCapture

Dependents:   PulseWidthCapture_Program

Revision:
0:7076676dd640
Child:
1:6bb38ae2e503
--- /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