Lab3 part2 with DAC

Committer:
anair12345
Date:
Thu Feb 14 23:23:10 2019 +0000
Revision:
5:1ab4ad65d5cf
Parent:
4:75ad475aff41
lab3p2;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WilliamMarshQMUL 0:5307f49cd305 1 // Lab 3 Example Program 2
WilliamMarshQMUL 0:5307f49cd305 2 // -----------------------
WilliamMarshQMUL 0:5307f49cd305 3 // Periodically write to the AnalogOut to create a sine wave
WilliamMarshQMUL 0:5307f49cd305 4 // Alternate between two fixed frequencies every 5 sec
WilliamMarshQMUL 4:75ad475aff41 5 //
WilliamMarshQMUL 4:75ad475aff41 6 // This code uses the Ticker API to create a periodic interrupt
WilliamMarshQMUL 4:75ad475aff41 7 // Devices used in this way must not call locks; a variant of
WilliamMarshQMUL 4:75ad475aff41 8 // the AnalogOut device w/o locking is created for this.
WilliamMarshQMUL 0:5307f49cd305 9 //
WilliamMarshQMUL 2:e27fd3b65155 10 // Updated for mbed 5
WilliamMarshQMUL 2:e27fd3b65155 11
WilliamMarshQMUL 1:18e0f8aef32f 12 // THIS VERSION HAS NO DEBUGGING CODE
WilliamMarshQMUL 0:5307f49cd305 13
WilliamMarshQMUL 0:5307f49cd305 14 #include "mbed.h"
WilliamMarshQMUL 0:5307f49cd305 15 #include "sineTable.h"
WilliamMarshQMUL 0:5307f49cd305 16
WilliamMarshQMUL 4:75ad475aff41 17 // --------------------------
WilliamMarshQMUL 4:75ad475aff41 18 // This declaration introduces a derived version of the mbed AnalogOut
WilliamMarshQMUL 4:75ad475aff41 19 // class with the locking removed.
WilliamMarshQMUL 4:75ad475aff41 20 // You do NOT NEED TO UNDERSTAND this
WilliamMarshQMUL 4:75ad475aff41 21 class AnalogOut_unsafe : public AnalogOut {
WilliamMarshQMUL 4:75ad475aff41 22 public:
WilliamMarshQMUL 4:75ad475aff41 23 AnalogOut_unsafe (PinName pin) : AnalogOut (pin) {}
WilliamMarshQMUL 4:75ad475aff41 24 protected:
WilliamMarshQMUL 4:75ad475aff41 25 virtual void lock() {}
WilliamMarshQMUL 4:75ad475aff41 26 virtual void unlock() {}
WilliamMarshQMUL 4:75ad475aff41 27 };
WilliamMarshQMUL 4:75ad475aff41 28 // --------------------------
WilliamMarshQMUL 4:75ad475aff41 29
WilliamMarshQMUL 0:5307f49cd305 30 Ticker tick ; // Creates periodic interrupt
WilliamMarshQMUL 4:75ad475aff41 31 AnalogOut_unsafe ao(PTE30) ; // Analog output
WilliamMarshQMUL 0:5307f49cd305 32
anair12345 5:1ab4ad65d5cf 33 AnalogIn ain(A0) ; // Analog input
anair12345 5:1ab4ad65d5cf 34
anair12345 5:1ab4ad65d5cf 35 DigitalOut led0(PTA13);
anair12345 5:1ab4ad65d5cf 36 DigitalOut led1(PTD5);
anair12345 5:1ab4ad65d5cf 37 DigitalOut led2(PTD0);
anair12345 5:1ab4ad65d5cf 38 DigitalOut led3(PTD2);
anair12345 5:1ab4ad65d5cf 39 DigitalOut led4(PTD3);
anair12345 5:1ab4ad65d5cf 40
anair12345 5:1ab4ad65d5cf 41 DigitalIn b1(PTA1, PullUp);
anair12345 5:1ab4ad65d5cf 42
anair12345 5:1ab4ad65d5cf 43 EventQueue queue; // creates an event queue, to call read ADC
anair12345 5:1ab4ad65d5cf 44
anair12345 5:1ab4ad65d5cf 45 Serial pc(USBTX, USBRX); // tx, rx, for debugging
anair12345 5:1ab4ad65d5cf 46
anair12345 5:1ab4ad65d5cf 47 // This thread runs the event queue
anair12345 5:1ab4ad65d5cf 48 Thread eventThread ;
anair12345 5:1ab4ad65d5cf 49
anair12345 5:1ab4ad65d5cf 50 typedef struct {
anair12345 5:1ab4ad65d5cf 51 uint16_t analog; /* Analog input value */
anair12345 5:1ab4ad65d5cf 52 int button;
anair12345 5:1ab4ad65d5cf 53 } message_t;
anair12345 5:1ab4ad65d5cf 54
anair12345 5:1ab4ad65d5cf 55 Mail<message_t, 2> mailbox;
anair12345 5:1ab4ad65d5cf 56
anair12345 5:1ab4ad65d5cf 57 volatile int samples = 0 ;
anair12345 5:1ab4ad65d5cf 58 volatile uint16_t smoothed = 0 ;
anair12345 5:1ab4ad65d5cf 59
anair12345 5:1ab4ad65d5cf 60 volatile int pressEvent = 0 ;
anair12345 5:1ab4ad65d5cf 61
anair12345 5:1ab4ad65d5cf 62 enum buttonPos { up, down, bounce }; // Button positions
anair12345 5:1ab4ad65d5cf 63
anair12345 5:1ab4ad65d5cf 64 buttonPos pos = up ;
anair12345 5:1ab4ad65d5cf 65 int bcounter = 0 ;
anair12345 5:1ab4ad65d5cf 66
anair12345 5:1ab4ad65d5cf 67 void readA0() {
anair12345 5:1ab4ad65d5cf 68 smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ;
anair12345 5:1ab4ad65d5cf 69 samples++ ;
anair12345 5:1ab4ad65d5cf 70
anair12345 5:1ab4ad65d5cf 71 switch (pos) {
anair12345 5:1ab4ad65d5cf 72 case up :
anair12345 5:1ab4ad65d5cf 73 if (!b1.read()) { // now down
anair12345 5:1ab4ad65d5cf 74 pressEvent = 1 ; // transition occurred
anair12345 5:1ab4ad65d5cf 75 pos = down ;
anair12345 5:1ab4ad65d5cf 76 }
anair12345 5:1ab4ad65d5cf 77 break ;
anair12345 5:1ab4ad65d5cf 78 case down :
anair12345 5:1ab4ad65d5cf 79 if (b1 == 1) { // no longer down
anair12345 5:1ab4ad65d5cf 80 bcounter = 3 ; // wait four cycles
anair12345 5:1ab4ad65d5cf 81 pos = bounce ;
anair12345 5:1ab4ad65d5cf 82 }
anair12345 5:1ab4ad65d5cf 83 break ;
anair12345 5:1ab4ad65d5cf 84 case bounce :
anair12345 5:1ab4ad65d5cf 85 if (b1 == 0) { // down again - button has bounced
anair12345 5:1ab4ad65d5cf 86 pos = down ; // no event
anair12345 5:1ab4ad65d5cf 87 } else if (bcounter == 0) {
anair12345 5:1ab4ad65d5cf 88 pos = up ; // delay passed - reset to up
anair12345 5:1ab4ad65d5cf 89 } else {
anair12345 5:1ab4ad65d5cf 90 bcounter-- ; // continue waiting
anair12345 5:1ab4ad65d5cf 91 }
anair12345 5:1ab4ad65d5cf 92 break ;
anair12345 5:1ab4ad65d5cf 93 }
anair12345 5:1ab4ad65d5cf 94 wait(0.03);
anair12345 5:1ab4ad65d5cf 95
anair12345 5:1ab4ad65d5cf 96 if (samples == 10) {
anair12345 5:1ab4ad65d5cf 97 // send to thread
anair12345 5:1ab4ad65d5cf 98 message_t *mess = mailbox.alloc() ; // may fail but does not block
anair12345 5:1ab4ad65d5cf 99 if (mess) {
anair12345 5:1ab4ad65d5cf 100 mess->analog = smoothed ;
anair12345 5:1ab4ad65d5cf 101 mess->button = pressEvent ;
anair12345 5:1ab4ad65d5cf 102 mailbox.put(mess); // fails but does not block if full
anair12345 5:1ab4ad65d5cf 103 pressEvent = 0;// clear press
anair12345 5:1ab4ad65d5cf 104 }
anair12345 5:1ab4ad65d5cf 105 samples = 0;
anair12345 5:1ab4ad65d5cf 106
anair12345 5:1ab4ad65d5cf 107 }
anair12345 5:1ab4ad65d5cf 108 }
anair12345 5:1ab4ad65d5cf 109
WilliamMarshQMUL 1:18e0f8aef32f 110 // Function called periodically
WilliamMarshQMUL 1:18e0f8aef32f 111 // Write new value to AnalogOut
WilliamMarshQMUL 2:e27fd3b65155 112 volatile int index = 0 ; // index into array of sin values
WilliamMarshQMUL 0:5307f49cd305 113 void writeAout() {
WilliamMarshQMUL 0:5307f49cd305 114 ao.write_u16(sine[index]) ;
WilliamMarshQMUL 0:5307f49cd305 115 index = (index + 1) % 64 ;
WilliamMarshQMUL 0:5307f49cd305 116 }
WilliamMarshQMUL 0:5307f49cd305 117
WilliamMarshQMUL 0:5307f49cd305 118 // Control the frequency of updates
WilliamMarshQMUL 0:5307f49cd305 119 // Alternative between two frequencies
WilliamMarshQMUL 0:5307f49cd305 120 int main() {
anair12345 5:1ab4ad65d5cf 121 led0 = 0 ; // turn off
anair12345 5:1ab4ad65d5cf 122 led1 = 0 ;
anair12345 5:1ab4ad65d5cf 123 led2 = 0 ;
anair12345 5:1ab4ad65d5cf 124 led3 = 0 ;
anair12345 5:1ab4ad65d5cf 125 led4 = 0 ;
anair12345 5:1ab4ad65d5cf 126
anair12345 5:1ab4ad65d5cf 127 int volts = 0 ;
anair12345 5:1ab4ad65d5cf 128 int threshold[] = {55,110,165,220,275,330} ;
anair12345 5:1ab4ad65d5cf 129 int counter = 0 ;
anair12345 5:1ab4ad65d5cf 130
anair12345 5:1ab4ad65d5cf 131 eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
anair12345 5:1ab4ad65d5cf 132
anair12345 5:1ab4ad65d5cf 133 // call the readA0 function every 10ms
anair12345 5:1ab4ad65d5cf 134 queue.call_every(10, readA0) ;
anair12345 5:1ab4ad65d5cf 135
anair12345 5:1ab4ad65d5cf 136
WilliamMarshQMUL 1:18e0f8aef32f 137 int update_us = 1000 ; // 1ms
anair12345 5:1ab4ad65d5cf 138 int freq = 1;
WilliamMarshQMUL 0:5307f49cd305 139 while (true) {
anair12345 5:1ab4ad65d5cf 140 osEvent evt = mailbox.get(); // wait for mail
anair12345 5:1ab4ad65d5cf 141 if (evt.status == osEventMail) {
anair12345 5:1ab4ad65d5cf 142 message_t* mess = (message_t*)evt.value.p ;
anair12345 5:1ab4ad65d5cf 143 volts = (mess->analog * 330) / 0xffff ;
anair12345 5:1ab4ad65d5cf 144 int mypressEvent = mess->button;
anair12345 5:1ab4ad65d5cf 145 mailbox.free(mess) ; // free the message space
anair12345 5:1ab4ad65d5cf 146 if(mypressEvent) {
anair12345 5:1ab4ad65d5cf 147 for(int i = 0; i<5;i++){
anair12345 5:1ab4ad65d5cf 148 threshold[i] = (i*volts)/6;
anair12345 5:1ab4ad65d5cf 149 }// use volts to reset thresholds
anair12345 5:1ab4ad65d5cf 150 }
anair12345 5:1ab4ad65d5cf 151 if (volts > threshold[0] ) led0 = 1 ; else led0 = 0 ;
anair12345 5:1ab4ad65d5cf 152 if (volts > threshold[1]) led1 = 1 ; else led1 = 0 ;
anair12345 5:1ab4ad65d5cf 153 if (volts > threshold[2]) led2 = 1 ; else led2 = 0 ;
anair12345 5:1ab4ad65d5cf 154 if (volts > threshold[3]) led3 = 1 ; else led3 = 0 ;
anair12345 5:1ab4ad65d5cf 155 if (volts > threshold[4]) led4 = 1 ; else led4 = 0 ;
anair12345 5:1ab4ad65d5cf 156 //vToString(volts, vstring) ;
anair12345 5:1ab4ad65d5cf 157 counter++ ;
anair12345 5:1ab4ad65d5cf 158 }
anair12345 5:1ab4ad65d5cf 159 //tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut
anair12345 5:1ab4ad65d5cf 160 //wait(30.0) ; // wait 30 sec
anair12345 5:1ab4ad65d5cf 161 freq = 1+((49*volts)/330);
anair12345 5:1ab4ad65d5cf 162 update_us = 1000000/(freq*64);
anair12345 5:1ab4ad65d5cf 163 //update_us = 2000 ; // 2ms
WilliamMarshQMUL 0:5307f49cd305 164 tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut
anair12345 5:1ab4ad65d5cf 165 //wait(30.0) ; // wait 30 sec
anair12345 5:1ab4ad65d5cf 166 //update_us = 1000 ; // 1ms
WilliamMarshQMUL 0:5307f49cd305 167 }
WilliamMarshQMUL 0:5307f49cd305 168 }