Lab 3 the ADC part with the leds

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // LAB 3 SAMPLE PROGRAM 1
00002 //   Revised for mbed 5 
00003 //   Revised to replace Ticker with event queue and thread
00004 
00005 #include "mbed.h"
00006 
00007 AnalogIn ain(A0) ;          // Analog input
00008 
00009 DigitalOut led0(PTA13);
00010 DigitalOut led1(PTD5);
00011 DigitalOut led2(PTD0);
00012 DigitalOut led3(PTD2);
00013 DigitalOut led4(PTD3);
00014 
00015 DigitalIn b1(PTA1, PullUp);
00016 
00017 //DigitalOut led1(REDLED);   // Red LED
00018 EventQueue queue;  // creates an event queue, to call read ADC
00019 
00020 Serial pc(USBTX, USBRX); // tx, rx, for debugging
00021 
00022 // This thread runs the event queue
00023 Thread eventThread ;
00024 
00025 // Message type
00026 typedef struct {
00027   uint16_t analog; /* Analog input value */
00028   int button;
00029 } message_t;
00030 
00031 // Mail box
00032 Mail<message_t, 2> mailbox;
00033 
00034 // Function called every 10ms to read ADC
00035 // Average using a low pass filter  
00036 // Every 10th value is sent to mailbox
00037 volatile int samples = 0 ;
00038 volatile uint16_t smoothed = 0 ; 
00039 
00040 //Thread pollT ; 
00041 volatile int pressEvent = 0 ;
00042 
00043 enum buttonPos { up, down, bounce }; // Button positions
00044 
00045 buttonPos pos = up ;
00046 int bcounter = 0 ;
00047 
00048 void readA0() {   
00049     smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ;
00050     samples++ ;
00051     
00052     switch (pos) {
00053             case up :
00054                 if (!b1.read()) {    // now down 
00055                     pressEvent = 1 ;  // transition occurred
00056                     pos = down ;
00057                 }
00058                 break ;
00059             case down : 
00060                 if (b1 == 1) { // no longer down
00061                     bcounter = 3 ; // wait four cycles
00062                     pos = bounce ;
00063                 }
00064                 break ;
00065             case bounce :
00066                 if (b1 == 0) { // down again - button has bounced
00067                     pos = down ;   // no event
00068                 } else if (bcounter == 0) {
00069                     pos = up ;     // delay passed - reset to up
00070                 } else {
00071                     bcounter-- ;   // continue waiting
00072                 }
00073                 break ;
00074         }
00075         wait(0.03);
00076     
00077     if (samples == 10) {
00078         // send to thread
00079         message_t *mess = mailbox.alloc() ; // may fail but does not block
00080         if (mess) {
00081             mess->analog = smoothed ;
00082             mess->button = pressEvent ;
00083             mailbox.put(mess); // fails but does not block if full
00084             pressEvent = 0;// clear press
00085         }
00086         samples = 0;
00087         
00088     }
00089 }
00090 
00091 // Write voltage digits
00092 //   v  Voltage as scale int, e.g. 3.30 is 330
00093 void vToString(int v, char* s) {    
00094     s[3] = '0' + (v % 10) ;
00095     v = v / 10 ;
00096     s[2] = '0' + (v % 10) ;
00097     v = v / 10 ;
00098     s[0] = '0' + (v % 10) ;
00099 }
00100 
00101 // Main program
00102 //   Initialise variables
00103 //   Attach ISR for ticker
00104 //   Procss messages from mailbox    
00105 int main() {
00106     led0 = 0 ; // turn off 
00107     led1 = 0 ;
00108     led2 = 0 ;
00109     led3 = 0 ;
00110     led4 = 0 ;
00111     
00112     int volts = 0 ;
00113     int threshold[] = {55,110,165,220,275,330} ;
00114     int counter = 0 ;
00115     char vstring[] = "X.XX\r\n" ;
00116     
00117     //pollT.start(callback(polling));
00118     
00119     // Start the event queue
00120     eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
00121 
00122     // call the readA0 function every 10ms 
00123     queue.call_every(10, readA0) ; 
00124 
00125     while (true) {
00126         osEvent evt = mailbox.get(); // wait for mail 
00127         if (evt.status == osEventMail) {
00128             message_t* mess = (message_t*)evt.value.p ;
00129             volts = (mess->analog * 330) / 0xffff ;
00130             int mypressEvent = mess->button;
00131             mailbox.free(mess) ;  // free the message space
00132             if(mypressEvent) {
00133                 for(int i = 0; i<5;i++){
00134                     threshold[i] = (i*volts)/6;
00135                 }// use volts to reset thresholds
00136             } 
00137             if (volts > threshold[0] ) led0 = 1 ; else led0 = 0 ;
00138             if (volts > threshold[1]) led1 = 1 ; else led1 = 0 ;
00139             if (volts > threshold[2]) led2 = 1 ; else led2 = 0 ;
00140             if (volts > threshold[3]) led3 = 1 ; else led3 = 0 ;
00141             if (volts > threshold[4]) led4 = 1 ; else led4 = 0 ;
00142             vToString(volts, vstring) ;
00143             counter++ ;
00144             if (counter == 10) {  // limit bandwidth of serial
00145                 pc.printf(vstring) ;
00146                 counter = 0 ;
00147             }
00148         }
00149     }
00150 }