final

Committer:
ex19397
Date:
Fri Feb 28 09:32:06 2020 +0000
Revision:
6:28a86a76010b
Parent:
4:ebd00f94455a
Final

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WilliamMarshQMUL 0:ad3cdc525b99 1
WilliamMarshQMUL 0:ad3cdc525b99 2 // LAB 3 SAMPLE PROGRAM 1
ex19397 6:28a86a76010b 3
WilliamMarshQMUL 0:ad3cdc525b99 4
WilliamMarshQMUL 0:ad3cdc525b99 5 #include "mbed.h"
WilliamMarshQMUL 1:126dd2f5fc2d 6
WilliamMarshQMUL 0:ad3cdc525b99 7
WilliamMarshQMUL 0:ad3cdc525b99 8 AnalogIn ain(A0) ; // Analog input
ex19397 6:28a86a76010b 9 DigitalOut led1(D3); // Red LED
ex19397 6:28a86a76010b 10 DigitalOut led2(D4);
ex19397 6:28a86a76010b 11 DigitalOut led3(D5);
ex19397 6:28a86a76010b 12 DigitalOut led4(D6);
ex19397 6:28a86a76010b 13 DigitalOut led5(D7);
ex19397 6:28a86a76010b 14 InterruptIn b1(PTD0);
ex19397 6:28a86a76010b 15
ex19397 6:28a86a76010b 16
WilliamMarshQMUL 4:ebd00f94455a 17 EventQueue queue; // creates an event queue, to call read ADC
WilliamMarshQMUL 0:ad3cdc525b99 18
WilliamMarshQMUL 0:ad3cdc525b99 19 Serial pc(USBTX, USBRX); // tx, rx, for debugging
WilliamMarshQMUL 0:ad3cdc525b99 20
ex19397 6:28a86a76010b 21 volatile int pressEvent = 1 ;
ex19397 6:28a86a76010b 22 void buttonCallback(){
ex19397 6:28a86a76010b 23 pressEvent = 1 ;
ex19397 6:28a86a76010b 24 }
ex19397 6:28a86a76010b 25
WilliamMarshQMUL 4:ebd00f94455a 26 // This thread runs the event queue
WilliamMarshQMUL 4:ebd00f94455a 27 Thread eventThread ;
WilliamMarshQMUL 3:f5d7fddeef39 28
WilliamMarshQMUL 0:ad3cdc525b99 29 // Message type
WilliamMarshQMUL 0:ad3cdc525b99 30 typedef struct {
WilliamMarshQMUL 0:ad3cdc525b99 31 uint16_t analog; /* Analog input value */
WilliamMarshQMUL 0:ad3cdc525b99 32 } message_t;
WilliamMarshQMUL 0:ad3cdc525b99 33
WilliamMarshQMUL 0:ad3cdc525b99 34 // Mail box
WilliamMarshQMUL 0:ad3cdc525b99 35 Mail<message_t, 2> mailbox;
WilliamMarshQMUL 0:ad3cdc525b99 36
WilliamMarshQMUL 0:ad3cdc525b99 37 // Function called every 10ms to read ADC
WilliamMarshQMUL 4:ebd00f94455a 38 // Average using a low pass filter
WilliamMarshQMUL 0:ad3cdc525b99 39 // Every 10th value is sent to mailbox
WilliamMarshQMUL 4:ebd00f94455a 40 volatile int samples = 0 ;
WilliamMarshQMUL 4:ebd00f94455a 41 volatile uint16_t smoothed = 0 ;
WilliamMarshQMUL 4:ebd00f94455a 42
WilliamMarshQMUL 0:ad3cdc525b99 43 void readA0() {
WilliamMarshQMUL 4:ebd00f94455a 44 smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ;
WilliamMarshQMUL 4:ebd00f94455a 45 samples++ ;
WilliamMarshQMUL 4:ebd00f94455a 46 if (samples == 10) {
WilliamMarshQMUL 4:ebd00f94455a 47 // send to thread
WilliamMarshQMUL 4:ebd00f94455a 48 message_t *mess = mailbox.alloc() ; // may fail but does not block
WilliamMarshQMUL 4:ebd00f94455a 49 if (mess) {
WilliamMarshQMUL 4:ebd00f94455a 50 mess->analog = smoothed ;
WilliamMarshQMUL 4:ebd00f94455a 51 mailbox.put(mess); // fails but does not block if full
WilliamMarshQMUL 0:ad3cdc525b99 52 }
WilliamMarshQMUL 4:ebd00f94455a 53 samples = 0;
WilliamMarshQMUL 4:ebd00f94455a 54 }
WilliamMarshQMUL 0:ad3cdc525b99 55 }
WilliamMarshQMUL 0:ad3cdc525b99 56
WilliamMarshQMUL 0:ad3cdc525b99 57 // Write voltage digits
WilliamMarshQMUL 0:ad3cdc525b99 58 // v Voltage as scale int, e.g. 3.30 is 330
WilliamMarshQMUL 0:ad3cdc525b99 59 void vToString(int v, char* s) {
WilliamMarshQMUL 0:ad3cdc525b99 60 s[3] = '0' + (v % 10) ;
WilliamMarshQMUL 0:ad3cdc525b99 61 v = v / 10 ;
WilliamMarshQMUL 0:ad3cdc525b99 62 s[2] = '0' + (v % 10) ;
WilliamMarshQMUL 0:ad3cdc525b99 63 v = v / 10 ;
WilliamMarshQMUL 0:ad3cdc525b99 64 s[0] = '0' + (v % 10) ;
WilliamMarshQMUL 0:ad3cdc525b99 65 }
WilliamMarshQMUL 0:ad3cdc525b99 66
WilliamMarshQMUL 0:ad3cdc525b99 67 // Main program
WilliamMarshQMUL 0:ad3cdc525b99 68 // Initialise variables
WilliamMarshQMUL 0:ad3cdc525b99 69 // Attach ISR for ticker
WilliamMarshQMUL 0:ad3cdc525b99 70 // Procss messages from mailbox
WilliamMarshQMUL 0:ad3cdc525b99 71 int main() {
ex19397 6:28a86a76010b 72 led1 = 0; // turn off
ex19397 6:28a86a76010b 73 led2 = 0;// turn off
ex19397 6:28a86a76010b 74 led3 = 0;// turn off
ex19397 6:28a86a76010b 75 led4 = 0;// turn off
ex19397 6:28a86a76010b 76 led5 = 0; // turn off
WilliamMarshQMUL 0:ad3cdc525b99 77 int volts = 0 ;
ex19397 6:28a86a76010b 78 int max = 0 ;
WilliamMarshQMUL 0:ad3cdc525b99 79 const int threshold = 100 ;
WilliamMarshQMUL 0:ad3cdc525b99 80 int counter = 0 ;
WilliamMarshQMUL 0:ad3cdc525b99 81 char vstring[] = "X.XX\r\n" ;
ex19397 6:28a86a76010b 82 b1.mode(PullUp);
ex19397 6:28a86a76010b 83 b1.fall(&buttonCallback);
WilliamMarshQMUL 3:f5d7fddeef39 84
WilliamMarshQMUL 4:ebd00f94455a 85 // Start the event queue
WilliamMarshQMUL 4:ebd00f94455a 86 eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
WilliamMarshQMUL 4:ebd00f94455a 87
WilliamMarshQMUL 4:ebd00f94455a 88 // call the readA0 function every 10ms
WilliamMarshQMUL 4:ebd00f94455a 89 queue.call_every(10, readA0) ;
WilliamMarshQMUL 0:ad3cdc525b99 90
WilliamMarshQMUL 0:ad3cdc525b99 91 while (true) {
WilliamMarshQMUL 0:ad3cdc525b99 92 osEvent evt = mailbox.get(); // wait for mail
WilliamMarshQMUL 0:ad3cdc525b99 93 if (evt.status == osEventMail) {
WilliamMarshQMUL 0:ad3cdc525b99 94 message_t* mess = (message_t*)evt.value.p ;
ex19397 6:28a86a76010b 95 if (pressEvent) {
ex19397 6:28a86a76010b 96 pressEvent = 0;
ex19397 6:28a86a76010b 97 max = (mess->analog * 330) / 0xffff ;
ex19397 6:28a86a76010b 98 }
WilliamMarshQMUL 0:ad3cdc525b99 99 volts = (mess->analog * 330) / 0xffff ;
WilliamMarshQMUL 0:ad3cdc525b99 100 mailbox.free(mess) ; // free the message space
ex19397 6:28a86a76010b 101 //if (volts > threshold) led1 = 0 ; else led1 = 1 ;
ex19397 6:28a86a76010b 102 if (volts > 0 && volts < max/6) {
ex19397 6:28a86a76010b 103 led1 = 0;
ex19397 6:28a86a76010b 104 led2 = 0;
ex19397 6:28a86a76010b 105 led3 = 0;
ex19397 6:28a86a76010b 106 led4 = 0;
ex19397 6:28a86a76010b 107 led5 = 0;
ex19397 6:28a86a76010b 108 }
ex19397 6:28a86a76010b 109 if (volts > max/6 && volts < 2*max/6) {
ex19397 6:28a86a76010b 110 led1 = 1;
ex19397 6:28a86a76010b 111 led2 = 0;
ex19397 6:28a86a76010b 112 led3 = 0;
ex19397 6:28a86a76010b 113 led4 = 0;
ex19397 6:28a86a76010b 114 led5 = 0;
ex19397 6:28a86a76010b 115 }
ex19397 6:28a86a76010b 116 if (volts > 2*max/6 && volts < 3*max/6) {
ex19397 6:28a86a76010b 117 led1 = 1;
ex19397 6:28a86a76010b 118 led2 = 1;
ex19397 6:28a86a76010b 119 led3 = 0;
ex19397 6:28a86a76010b 120 led4 = 0;
ex19397 6:28a86a76010b 121 led5 = 0;
ex19397 6:28a86a76010b 122 }
ex19397 6:28a86a76010b 123 if (volts > 3*max/6 && volts < 4*max/6) {
ex19397 6:28a86a76010b 124 led1 = 1;
ex19397 6:28a86a76010b 125 led2 = 1;
ex19397 6:28a86a76010b 126 led3 = 1;
ex19397 6:28a86a76010b 127 led4 = 0;
ex19397 6:28a86a76010b 128 led5 = 0;
ex19397 6:28a86a76010b 129 }
ex19397 6:28a86a76010b 130 if (volts > 4*max/6 && volts < 5*max/6) {
ex19397 6:28a86a76010b 131 led1 = 1;
ex19397 6:28a86a76010b 132 led2 = 1;
ex19397 6:28a86a76010b 133 led3 = 1;
ex19397 6:28a86a76010b 134 led4 = 1;
ex19397 6:28a86a76010b 135 led5 = 0;
ex19397 6:28a86a76010b 136 }
ex19397 6:28a86a76010b 137 if (volts > 5*max/6 && volts < 6*max/6) {
ex19397 6:28a86a76010b 138 led1 = 1;
ex19397 6:28a86a76010b 139 led2 = 1;
ex19397 6:28a86a76010b 140 led3 = 1;
ex19397 6:28a86a76010b 141 led4 = 1;
ex19397 6:28a86a76010b 142 led5 = 1;
ex19397 6:28a86a76010b 143 }
ex19397 6:28a86a76010b 144
ex19397 6:28a86a76010b 145
WilliamMarshQMUL 0:ad3cdc525b99 146 vToString(volts, vstring) ;
WilliamMarshQMUL 0:ad3cdc525b99 147 counter++ ;
WilliamMarshQMUL 0:ad3cdc525b99 148 if (counter == 10) { // limit bandwidth of serial
WilliamMarshQMUL 0:ad3cdc525b99 149 pc.printf(vstring) ;
WilliamMarshQMUL 0:ad3cdc525b99 150 counter = 0 ;
WilliamMarshQMUL 0:ad3cdc525b99 151 }
WilliamMarshQMUL 0:ad3cdc525b99 152 }
WilliamMarshQMUL 0:ad3cdc525b99 153 }
WilliamMarshQMUL 0:ad3cdc525b99 154 }