Jim Patterson / Mbed 2 deprecated MultiSensor_00

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "daq.h"
00003 #include "comms.h"
00004 
00005 #define NODE_ID 1
00006 #define SAMPLE_PERIOD_US 20000
00007 #define EXT_BIAS_VOLTAGE 0.1
00008 
00009 DigitalOut myled(LED1);
00010 Serial pc(USBTX, USBRX);
00011 Serial radioUART(p13,p14);
00012 DigitalOut rst(p29);
00013 DigitalOut en_mux_EXT(p29);
00014 BusOut mux_EXT(p23,p24);
00015 AnalogOut bias_EXT(p18);
00016 
00017 ADC_BURST adc;
00018 Decimator dec_0(5);
00019 Decimator dec_1(5);
00020 Decimator dec_2(5);
00021 Decimator dec_3_0(5);
00022 Decimator dec_3_1(5);
00023 Decimator dec_3_2(5);
00024 Decimator dec_3_3(5);
00025 Ticker sampler;
00026 
00027 Transport transmission(0xC0A80001, 0xC0A80000, NODE_ID);
00028 
00029 uint8_t* radio_ptr = NULL;
00030 uint16_t radio_len = 0;
00031 
00032 uint8_t ext_mux_channel = 0;
00033 bool waiting = false;
00034 
00035 /**
00036  *  Collects sample from multiple sensor sources.
00037  *  Create a packets of samples from multiple sensor and pack
00038  *  them into a single, synchronised (via decimators) packet.
00039  */
00040 void pack_samples_ISR() {
00041     uint16_t syncd_samples[7];
00042     syncd_samples[0] = dec_0.read();
00043     syncd_samples[1] = dec_1.read();
00044     syncd_samples[2] = dec_2.read();
00045     syncd_samples[3] = dec_3_0.read();
00046     syncd_samples[4] = dec_3_1.read();
00047     syncd_samples[5] = dec_3_2.read();
00048     syncd_samples[6] = dec_3_3.read();
00049     transmission.load_data(syncd_samples, 7);
00050     waiting = true;
00051 }
00052 
00053 void adc_ISR() {
00054     // Toggle a LED for visual feedback of activity
00055     myled = !myled;
00056     // Send the latest set of samples to the decimators
00057     dec_0.write((uint16_t)(adc.data[0] >> 4));
00058     dec_1.write((uint16_t)(adc.data[1] >> 4));
00059     dec_2.write((uint16_t)(adc.data[2] >> 4));
00060     switch (ext_mux_channel)  {
00061         case 0:
00062             dec_3_0.write((uint16_t)(adc.data[3] >> 4));
00063             break;
00064         case 1:
00065             dec_3_1.write((uint16_t)(adc.data[3] >> 4));
00066             break;
00067         case 2:
00068             dec_3_2.write((uint16_t)(adc.data[3] >> 4));
00069             break;
00070         case 3:
00071             dec_3_3.write((uint16_t)(adc.data[3] >> 4));
00072             break;
00073         default:
00074             error("Random multiplexer channel: %d!\n",ext_mux_channel);
00075     }
00076     // Select the next channel on the external multiplexer
00077     if (++ext_mux_channel == 4) ext_mux_channel = 0;
00078     mux_EXT = ext_mux_channel;
00079 }
00080 
00081 int main() {
00082     // Set up UART interface over USB
00083     pc.baud(115200);
00084 
00085     // Set up UART interface to radio module node
00086     // - CTS1 is set to DIP12 = port0.17
00087     LPC_PINCON->PINSEL1 &=     ~((uint32_t)0x3 << 2);
00088     LPC_PINCON->PINSEL1 |=      ((uint32_t)0x1 << 2);
00089     // - CTS1 is given a weak pull-up
00090     LPC_PINCON->PINMODE1 &=    ~((uint32_t)0x3 << 2);
00091     // - Enable autocts mode on UART1
00092     LPC_UART1->MCR |= (1 <<7 );
00093     // - Baud rate set fairly high
00094     radioUART.baud(115200);
00095 
00096     // Reset the radio board and wait a while for it to boot
00097     rst = 0;
00098     rst = 1;
00099     wait(0.25);
00100 
00101     // Attach a function to the sample period ticker
00102     // - this will enable continuous sampling of the ADC
00103     sampler.attach_us(&pack_samples_ISR, SAMPLE_PERIOD_US);
00104 
00105     // Set up the external analogue circuitry
00106     bias_EXT = EXT_BIAS_VOLTAGE;
00107     en_mux_EXT = 1;
00108     mux_EXT = ext_mux_channel;
00109     
00110     // Set up any remaining comm's
00111 
00112     // Choose which function to process the latest ADC values
00113     adc.attach(&adc_ISR);
00114 
00115 
00116     while (1) {
00117         if (waiting) {
00118             
00119             transmission.get_packet(&radio_ptr, &radio_len);
00120             //radioUART.attach(&uartISR,Serial::TxIrq);
00121             while (radio_len-- != 0) {
00122                 radioUART.putc(*radio_ptr++);
00123             }
00124             pc.printf("C = %d\n",radio_len);
00125             waiting = false;
00126         }
00127     }
00128 }