Lab3 part2 with DAC

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Lab 3 Example Program 2
00002 // -----------------------
00003 // Periodically write to the AnalogOut to create a sine wave
00004 // Alternate between two fixed frequencies every 5 sec
00005 // 
00006 // This code uses the Ticker API to create a periodic interrupt
00007 // Devices used in this way must not call locks; a variant of 
00008 //   the AnalogOut device w/o locking is created for this.
00009 //
00010 // Updated for mbed 5
00011 
00012 // THIS VERSION HAS NO DEBUGGING CODE
00013 
00014 #include "mbed.h"
00015 #include "sineTable.h"
00016 
00017 // --------------------------
00018 // This declaration introduces a derived version of the mbed AnalogOut
00019 //   class with the locking removed.
00020 // You do NOT NEED TO UNDERSTAND this 
00021 class AnalogOut_unsafe : public AnalogOut {
00022   public:
00023     AnalogOut_unsafe (PinName pin) : AnalogOut (pin) {} 
00024   protected: 
00025     virtual void lock() {}
00026     virtual void unlock() {}
00027 };
00028 // --------------------------
00029 
00030 Ticker tick ;          // Creates periodic interrupt
00031 AnalogOut_unsafe ao(PTE30) ;  // Analog output
00032 
00033 AnalogIn ain(A0) ;          // Analog input
00034 
00035 DigitalOut led0(PTA13);
00036 DigitalOut led1(PTD5);
00037 DigitalOut led2(PTD0);
00038 DigitalOut led3(PTD2);
00039 DigitalOut led4(PTD3);
00040 
00041 DigitalIn b1(PTA1, PullUp);
00042 
00043 EventQueue queue;  // creates an event queue, to call read ADC
00044 
00045 Serial pc(USBTX, USBRX); // tx, rx, for debugging
00046 
00047 // This thread runs the event queue
00048 Thread eventThread ;
00049 
00050 typedef struct {
00051   uint16_t analog; /* Analog input value */
00052   int button;
00053 } message_t;
00054 
00055 Mail<message_t, 2> mailbox;
00056 
00057 volatile int samples = 0 ;
00058 volatile uint16_t smoothed = 0 ; 
00059 
00060 volatile int pressEvent = 0 ;
00061 
00062 enum buttonPos { up, down, bounce }; // Button positions
00063 
00064 buttonPos pos = up ;
00065 int bcounter = 0 ;
00066 
00067 void readA0() {   
00068     smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ;
00069     samples++ ;
00070     
00071     switch (pos) {
00072             case up :
00073                 if (!b1.read()) {    // now down 
00074                     pressEvent = 1 ;  // transition occurred
00075                     pos = down ;
00076                 }
00077                 break ;
00078             case down : 
00079                 if (b1 == 1) { // no longer down
00080                     bcounter = 3 ; // wait four cycles
00081                     pos = bounce ;
00082                 }
00083                 break ;
00084             case bounce :
00085                 if (b1 == 0) { // down again - button has bounced
00086                     pos = down ;   // no event
00087                 } else if (bcounter == 0) {
00088                     pos = up ;     // delay passed - reset to up
00089                 } else {
00090                     bcounter-- ;   // continue waiting
00091                 }
00092                 break ;
00093         }
00094         wait(0.03);
00095     
00096     if (samples == 10) {
00097         // send to thread
00098         message_t *mess = mailbox.alloc() ; // may fail but does not block
00099         if (mess) {
00100             mess->analog = smoothed ;
00101             mess->button = pressEvent ;
00102             mailbox.put(mess); // fails but does not block if full
00103             pressEvent = 0;// clear press
00104         }
00105         samples = 0;
00106         
00107     }
00108 }
00109 
00110 // Function called periodically
00111 // Write new value to AnalogOut 
00112 volatile int index = 0 ; // index into array of sin values
00113 void writeAout() {
00114     ao.write_u16(sine[index]) ;
00115     index = (index + 1) % 64 ;   
00116 }
00117 
00118 // Control the frequency of updates
00119 //   Alternative between two frequencies      
00120 int main() {
00121     led0 = 0 ; // turn off 
00122     led1 = 0 ;
00123     led2 = 0 ;
00124     led3 = 0 ;
00125     led4 = 0 ;
00126     
00127     int volts = 0 ;
00128     int threshold[] = {55,110,165,220,275,330} ;
00129     int counter = 0 ;
00130     
00131     eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
00132 
00133     // call the readA0 function every 10ms 
00134     queue.call_every(10, readA0) ;
00135     
00136     
00137     int update_us = 1000 ; // 1ms
00138     int freq = 1;
00139     while (true) {
00140         osEvent evt = mailbox.get(); // wait for mail 
00141         if (evt.status == osEventMail) {
00142             message_t* mess = (message_t*)evt.value.p ;
00143             volts = (mess->analog * 330) / 0xffff ;
00144             int mypressEvent = mess->button;
00145             mailbox.free(mess) ;  // free the message space
00146             if(mypressEvent) {
00147                 for(int i = 0; i<5;i++){
00148                     threshold[i] = (i*volts)/6;
00149                 }// use volts to reset thresholds
00150             } 
00151             if (volts > threshold[0] ) led0 = 1 ; else led0 = 0 ;
00152             if (volts > threshold[1]) led1 = 1 ; else led1 = 0 ;
00153             if (volts > threshold[2]) led2 = 1 ; else led2 = 0 ;
00154             if (volts > threshold[3]) led3 = 1 ; else led3 = 0 ;
00155             if (volts > threshold[4]) led4 = 1 ; else led4 = 0 ;
00156             //vToString(volts, vstring) ;
00157             counter++ ;
00158         }
00159         //tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut
00160         //wait(30.0) ; // wait 30 sec 
00161         freq = 1+((49*volts)/330);
00162         update_us = 1000000/(freq*64);
00163         //update_us = 2000 ; // 2ms
00164         tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut
00165         //wait(30.0) ; // wait 30 sec 
00166         //update_us = 1000 ; // 1ms
00167     }
00168 }