Wim van der Vegt / Mbed 2 deprecated RIT_Demo

Dependencies:   mbed RIT FastAnalogIn

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers rit_demo.cpp Source File

rit_demo.cpp

00001 /* mbed Repetitive Interrupt Timer Library
00002  * Copyright (c) 2011 wvd_vegt
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy
00005  * of this software and associated documentation files (the "Software"), to deal
00006  * in the Software without restriction, including without limitation the rights
00007  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008  * copies of the Software, and to permit persons to whom the Software is
00009  * furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included in
00012  * all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020  * THE SOFTWARE.
00021  */
00022 
00023 #include "mbed.h"
00024 #include "RIT.h"
00025 #include "FastAnalogIn.h"
00026 
00027 //We'll be using the Usb Serial port
00028 Serial serial(USBTX, USBRX); //tx, rx
00029 
00030 DigitalOut rit_led(LED3);
00031 
00032 FastAnalogIn a1(p19,true);   //Ain1  - Peel Force
00033 FastAnalogIn a2(p20,true);   //Ain2  - Seal Force
00034 
00035 #define SAMPLE_BUFFER_LENGTH 1000
00036 
00037 volatile uint32_t rithits = 0;       //timer1 stops when timer1hits==imer1loop
00038 
00039 volatile bool done = false;
00040 volatile short flip = 1;
00041 
00042 Timer rit_timing;
00043 
00044 static short adc1[SAMPLE_BUFFER_LENGTH] __attribute__ ((aligned (64)));
00045 static short adc2[SAMPLE_BUFFER_LENGTH] __attribute__ ((aligned (64)));
00046 static int Timing01[SAMPLE_BUFFER_LENGTH];
00047 
00048 void RIT_IRQHandler(void)
00049 {
00050     if (!done) {
00051         Timing01[rithits] = rit_timing.read_us();
00052         if (flip%2) {
00053             adc1[rithits]=a1.read_u16();
00054         } else {
00055             adc2[rithits]=a2.read_u16();
00056 
00057             //Count Hits.
00058             rithits++;
00059         }
00060 
00061         flip ++;
00062 
00063         //Flash Led.
00064         //rit_led=!rit_led;
00065         done = rithits==SAMPLE_BUFFER_LENGTH;
00066     }
00067 }
00068 
00069 /*
00070 LPC_TIM1->IR = 0x1UL;                                       // Re-enabled
00071 
00072 Timing01[timer1hits] = adc_stamper.read_us();
00073 
00074 //TODO Toggle Channel Mask (01 to 01 in ADCR).
00075 //     Take twice the number of samples at half the sampling time
00076 //     So 2000 samples (1000+1000) at 0.5ms -> 1000 pairs in 1 sec.
00077 
00078 switch (LPC_ADC->ADSTAT & (t_ch0|t_ch1))
00079 {
00080 case t_ch0:
00081     Samples0[timer1hits] = LPC_ADC->ADDR4;
00082     break;
00083 }
00084 }
00085 */
00086 //https://developer.mbed.org/forum/mbed/topic/370/
00087 RIT rit(1000);
00088 
00089 //https://developer.mbed.org/handbook/Ticker
00090 //Ticker ticker;
00091 
00092 //void tock()
00093 //{
00094 //    Timing01[rithits] = rit_timing.read_us();
00095 //}
00096 
00097 int main()
00098 {
00099     // Set the Baudrate.
00100     serial.baud(115200);
00101 
00102     rit.setup_us(10);
00103 
00104     rit.append(RIT_IRQHandler);
00105 
00106     rit.enable();
00107     rit_timing.start();
00108 
00109     while (!done) {
00110         // wait(0.001);
00111     }
00112 
00113     rit_timing.stop();
00114     rit.disable();
00115 
00116     rit.unappend();
00117 
00118     for (int i=0; i<SAMPLE_BUFFER_LENGTH; i++) {
00119         serial.printf("%3d %6.3f - %6d - %6d\r\n",i, Timing01[i]/1000.0f, adc1[i], adc2[i]);
00120     }
00121     serial.printf("%3d %6.3f - %6d - %6d\r\n",0, Timing01[0]/1000.0f, adc1[0], adc2[0]);
00122 
00123     printf("COMPVAL=%d\r\n", LPC_RIT->RICOMPVAL); // 959 (960-1)
00124     printf("COUNTER=%d\r\n", LPC_RIT->RICOUNTER); // 807?
00125     printf("HITS=%d\r\n", rithits);
00126     printf("ELAPSED=%0.3f ms\r\n", rit_timing.read_us()/1000.0f);
00127     printf("ELAPSED=%0.3f ms\r\n", (Timing01[SAMPLE_BUFFER_LENGTH-1]-Timing01[0])/1000.0f);
00128 
00129     /*
00130         //
00131         printf("Requested max sample rate is %u, actual max sample rate is %u.\r\n",
00132                SAMPLE_RATE, adc.actual_sample_rate());
00133 
00134         adc.startmode(0,0);
00135 
00136         adc.burst(1);
00137 
00138         adc.setup(p15,1);
00139         adc.setup(p16,1);
00140 
00141         //For burst mode, only one interrupt is required
00142         //which can be on any enabled pin. We have enabled all
00143         //of them here.
00144         adc.interrupt_state(p15,1);
00145         adc_timing.reset();
00146         adc_timing.start();
00147 
00148         //10 ms sample time.
00149         rit.setup_ms(10);
00150 
00151         //Clear ADC Storage stuff...
00152         adchits = 0;
00153         memset(Samples0, 0,sizeof(Samples0));
00154         memset(Samples1, 0,sizeof(Samples1));
00155 
00156         rit.append(Adc_IRQHandler);
00157 
00158         rit_timing.reset();
00159         rit_timing.start();
00160 
00161         rit.enable();
00162 
00163         //To be sure...
00164         while (adchits!=MAX_SAMPLES) {
00165             wait(0.01);
00166         }
00167 
00168         rit.disable();
00169 
00170         rit_timing.stop();
00171 
00172         rit.unappend();
00173 
00174         for (int i=0; i < MAX_SAMPLES; i++) {
00175             printf("%04d=%04d | %04d @ %08d\r\n", i, Samples0[i], Samples1[i], Timing01[i]);
00176         }
00177 
00178         printf("ELAPSED=%0.2f ms\r\n", rit_timing.read_us()/1000);
00179         printf("HITS=%d\r\n", adchits);
00180 
00181         adc.burst(0);
00182 
00183         adc.setup(p15,0);
00184         adc.setup(p16,0);
00185 
00186         adc.interrupt_state(p15,0);
00187 
00188         adc_timing.stop();
00189     */
00190 }