Digital To Analogue Converter

Fork of ADCandticker_sample by William Marsh

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 // LAB 3 SAMPLE PROGRAM 1
00003 //   Revised for mbed 5
00004 
00005 #include "mbed.h"
00006 #include "sineTable.h"
00007 
00008 
00009 
00010 Ticker tick;                // Ticker for reading analog
00011 AnalogIn ain(A0) ;          // Analog input
00012 DigitalOut led1(PTA12);
00013 DigitalOut led2(PTA4);
00014 DigitalOut led3(PTA5);
00015 DigitalOut led4(PTC8);
00016 DigitalOut led5(PTC9);
00017 Serial pc(USBTX, USBRX); // tx, rx, for debugging
00018 
00019 DigitalIn b1(PTD0, PullUp);
00020 
00021 volatile int pressEvent = 0 ;  // Variabe set by the polling thread
00022 
00023 AnalogOut ao(PTE30) ;  // Analog output
00024 
00025 // Function called periodically
00026 // Write new value to AnalogOut 
00027 volatile int index = 0 ; // index into array of sin values
00028 void writeAout() {
00029     ao.write_u16(sine[index]) ;
00030     index = (index + 1) % 64 ;   
00031 }
00032 
00033 enum buttonPos { up, down, bounce }; // Button positions
00034 void polling()
00035 {
00036     buttonPos pos = up ;
00037     int bcounter = 0 ;
00038 
00039     switch (pos) {
00040         case up :
00041             if (!b1.read()) {    // now down
00042                 pressEvent = 1 ;  // transition occurred
00043                 pos = down ;
00044             }
00045             break ;
00046         case down :
00047             if (b1 == 1) { // no longer down
00048                 bcounter = 3 ; // wait four cycles
00049                 pos = bounce ;
00050             }
00051             break ;
00052         case bounce :
00053             if (b1 == 0) { // down again - button has bounced
00054                 pos = down ;   // no event
00055             } else if (bcounter == 0) {
00056                 pos = up ;     // delay passed - reset to up
00057             } else {
00058                 bcounter-- ;   // continue waiting
00059             }
00060             break ;
00061 
00062     }
00063 }
00064 
00065 // Message type
00066 typedef struct {
00067     uint16_t analog; /* Analog input value */
00068 } message_t;
00069 
00070 // Mail box
00071 Mail<message_t, 2> mailbox;
00072 
00073 // Function called every 10ms to read ADC
00074 // Low pass filter
00075 // Every 10th value is sent to mailbox
00076 volatile int samples = 0 ;
00077 volatile uint16_t smoothed = 0 ;
00078 void readA0()
00079 {
00080     polling();
00081     smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ; // divided by 2 - reduce the signal noise
00082     samples++ ;
00083     if (samples == 10) {
00084         // send to thread
00085         message_t *mess = mailbox.alloc() ; // may fail but does not block
00086         if (mess) {
00087             mess->analog = smoothed ;
00088             mailbox.put(mess); // fails but does not block if full
00089         }
00090         samples = 0;
00091     }
00092 }
00093 
00094 // Write voltage digits
00095 //   v  Voltage as scale int, e.g. 3.30 is 330
00096 void vToString(int v, char* s)
00097 {
00098     s[3] = '0' + (v % 10) ;
00099     v = v / 10 ;
00100     s[2] = '0' + (v % 10) ;
00101     v = v / 10 ;
00102     s[0] = '0' + (v % 10) ;
00103 }
00104 
00105 // Main program
00106 //   Initialise variables
00107 //   Attach ISR for ticker
00108 //   Procss messages from mailbox
00109 int main()
00110 {
00111     led1 = 1 ; // turn off
00112     int volts = 0 ;
00113     int threshold = 100 ; // 1 vol
00114     int counter = 0 ;
00115     char vstring[] = "X.XX\r\n" ;
00116     
00117     int update_us = 1000 ; // 1ms
00118 
00119     tick.attach_us(callback(&readA0), 10000); // ticks every 10ms -> 10000 micro second
00120 
00121 
00122     while (true) {
00123         
00124 
00125         
00126         osEvent evt = mailbox.get(); // wait for mail
00127 
00128         if (pressEvent) {
00129             pressEvent = 0 ; // clear the event variable
00130             threshold = volts;
00131 
00132         }
00133 
00134         // every 100 ms this loop operates
00135         if (evt.status == osEventMail) {
00136             message_t* mess = (message_t*)evt.value.p ;
00137             volts = (mess->analog * 330) / 0xffff ; // 2 ^ 16
00138             mailbox.free(mess) ;  // free the message space
00139 
00140             int f = (1+49*volts/threshold);
00141             if(f<1)
00142                 f =1;
00143             else if(f>50)
00144                 f=50;
00145             
00146             update_us = 1/(64*f);
00147             tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut
00148             
00149             
00150             if(volts < (threshold / 6)) {
00151                 led1 = 1;
00152                 led2 = 1;
00153                 led3 = 1;
00154                 led4 = 1;
00155                 led5 = 1;
00156             }
00157             if((volts > (threshold * 1/ 6))&&(volts < (threshold * 2/ 6))) {
00158                 led1 = 0;
00159                 led2 = 1;
00160                 led3 = 1;
00161                 led4 = 1;
00162                 led5 = 1;
00163             }
00164             if((volts > (threshold * 2/ 6))&&(volts < (threshold * 3/ 6))) {
00165                 led1 = 0;
00166                 led2 = 0;
00167                 led3 = 1;
00168                 led4 = 1;
00169                 led5 = 1;
00170             }
00171             if((volts > (threshold * 3/ 6))&&(volts < (threshold * 4/ 6))) {
00172                 led1 = 0;
00173                 led2 = 0;
00174                 led3 = 0;
00175                 led4 = 1;
00176                 led5 = 1;
00177             }
00178             if((volts > (threshold * 4/ 6))&&(volts < (threshold * 5/ 6))) {
00179                 led1 = 0;
00180                 led2 = 0;
00181                 led3 = 0;
00182                 led4 = 0;
00183                 led5 = 1;
00184             }
00185             if(volts > (threshold * 5/ 6)) {
00186                 led1 = 0;
00187                 led2 = 0;
00188                 led3 = 0;
00189                 led4 = 0;
00190                 led5 = 0;
00191             }
00192             vToString(volts, vstring) ;
00193             counter++ ;
00194 
00195             // every 1 s this loop will operate
00196             if (counter == 10) {  // limit bandwidth of serial
00197                 pc.printf(vstring) ;
00198                 counter = 0 ;
00199             }
00200         }
00201 
00202     }
00203 }