lab3 solution

Fork of ADCandticker_sample by William Marsh

Committer:
cvmanuel
Date:
Fri Feb 09 21:27:18 2018 +0000
Revision:
2:4348f9a6c179
Parent:
1:126dd2f5fc2d
lab3 solution

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WilliamMarshQMUL 0:ad3cdc525b99 1
WilliamMarshQMUL 0:ad3cdc525b99 2 // LAB 3 SAMPLE PROGRAM 1
WilliamMarshQMUL 1:126dd2f5fc2d 3 // Revised for mbed 5
WilliamMarshQMUL 0:ad3cdc525b99 4
WilliamMarshQMUL 0:ad3cdc525b99 5 #include "mbed.h"
WilliamMarshQMUL 1:126dd2f5fc2d 6
WilliamMarshQMUL 0:ad3cdc525b99 7
WilliamMarshQMUL 0:ad3cdc525b99 8 Ticker tick; // Ticker for reading analog
WilliamMarshQMUL 0:ad3cdc525b99 9 AnalogIn ain(A0) ; // Analog input
cvmanuel 2:4348f9a6c179 10 DigitalOut led1(D8); // Red LED
cvmanuel 2:4348f9a6c179 11 DigitalOut led2(D9); // Red LED
cvmanuel 2:4348f9a6c179 12 DigitalOut led3(D10); // Red LED
cvmanuel 2:4348f9a6c179 13 DigitalOut led4(D11); // Red LED
cvmanuel 2:4348f9a6c179 14 DigitalOut led5(D12); // Red LED
cvmanuel 2:4348f9a6c179 15
cvmanuel 2:4348f9a6c179 16 DigitalIn b1(PTE1,PullUp);
WilliamMarshQMUL 0:ad3cdc525b99 17
WilliamMarshQMUL 0:ad3cdc525b99 18 Serial pc(USBTX, USBRX); // tx, rx, for debugging
WilliamMarshQMUL 0:ad3cdc525b99 19
cvmanuel 2:4348f9a6c179 20 Thread pollT ; // thread to poll
cvmanuel 2:4348f9a6c179 21 volatile int pressEvent = 0 ; // Variabe set by the polling thread
cvmanuel 2:4348f9a6c179 22 int maxvolt = 330;
cvmanuel 2:4348f9a6c179 23 int threshold =55;
cvmanuel 2:4348f9a6c179 24
cvmanuel 2:4348f9a6c179 25
cvmanuel 2:4348f9a6c179 26 enum buttonPos { up, down, bounce }; // Button positions
cvmanuel 2:4348f9a6c179 27 void polling() {
cvmanuel 2:4348f9a6c179 28 buttonPos pos = up ;
cvmanuel 2:4348f9a6c179 29 int bcounter = 0 ;
cvmanuel 2:4348f9a6c179 30 while (true) {
cvmanuel 2:4348f9a6c179 31 switch (pos) {
cvmanuel 2:4348f9a6c179 32 case up :
cvmanuel 2:4348f9a6c179 33 if (!b1.read()) { // now down
cvmanuel 2:4348f9a6c179 34 pressEvent = 1 ; // transition occurred
cvmanuel 2:4348f9a6c179 35 pos = down ;
cvmanuel 2:4348f9a6c179 36 }
cvmanuel 2:4348f9a6c179 37 break ;
cvmanuel 2:4348f9a6c179 38 case down :
cvmanuel 2:4348f9a6c179 39 if (b1 == 1) { // no longer down
cvmanuel 2:4348f9a6c179 40 bcounter = 3 ; // wait four cycles
cvmanuel 2:4348f9a6c179 41 pos = bounce ;
cvmanuel 2:4348f9a6c179 42 }
cvmanuel 2:4348f9a6c179 43 break ;
cvmanuel 2:4348f9a6c179 44 case bounce :
cvmanuel 2:4348f9a6c179 45 if (b1 == 0) { // down again - button has bounced
cvmanuel 2:4348f9a6c179 46 pos = down ; // no event
cvmanuel 2:4348f9a6c179 47 } else if (bcounter == 0) {
cvmanuel 2:4348f9a6c179 48 pos = up ; // delay passed - reset to up
cvmanuel 2:4348f9a6c179 49 } else {
cvmanuel 2:4348f9a6c179 50 bcounter-- ; // continue waiting
cvmanuel 2:4348f9a6c179 51 }
cvmanuel 2:4348f9a6c179 52 break ;
cvmanuel 2:4348f9a6c179 53 }
cvmanuel 2:4348f9a6c179 54 Thread::wait(30);
cvmanuel 2:4348f9a6c179 55 }
cvmanuel 2:4348f9a6c179 56 }
cvmanuel 2:4348f9a6c179 57
cvmanuel 2:4348f9a6c179 58
cvmanuel 2:4348f9a6c179 59
cvmanuel 2:4348f9a6c179 60
cvmanuel 2:4348f9a6c179 61
cvmanuel 2:4348f9a6c179 62
cvmanuel 2:4348f9a6c179 63
cvmanuel 2:4348f9a6c179 64
cvmanuel 2:4348f9a6c179 65
cvmanuel 2:4348f9a6c179 66
cvmanuel 2:4348f9a6c179 67
cvmanuel 2:4348f9a6c179 68
cvmanuel 2:4348f9a6c179 69
cvmanuel 2:4348f9a6c179 70
cvmanuel 2:4348f9a6c179 71
cvmanuel 2:4348f9a6c179 72 // Message type
WilliamMarshQMUL 0:ad3cdc525b99 73 typedef struct {
WilliamMarshQMUL 0:ad3cdc525b99 74 uint16_t analog; /* Analog input value */
WilliamMarshQMUL 0:ad3cdc525b99 75 } message_t;
WilliamMarshQMUL 0:ad3cdc525b99 76
WilliamMarshQMUL 0:ad3cdc525b99 77 // Mail box
WilliamMarshQMUL 0:ad3cdc525b99 78 Mail<message_t, 2> mailbox;
WilliamMarshQMUL 0:ad3cdc525b99 79
WilliamMarshQMUL 0:ad3cdc525b99 80 // Function called every 10ms to read ADC
WilliamMarshQMUL 0:ad3cdc525b99 81 // Low pass filter
WilliamMarshQMUL 0:ad3cdc525b99 82 // Every 10th value is sent to mailbox
WilliamMarshQMUL 0:ad3cdc525b99 83 volatile int samples = 0 ;
WilliamMarshQMUL 0:ad3cdc525b99 84 volatile uint16_t smoothed = 0 ;
WilliamMarshQMUL 0:ad3cdc525b99 85 void readA0() {
WilliamMarshQMUL 0:ad3cdc525b99 86 smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ;
WilliamMarshQMUL 0:ad3cdc525b99 87 samples++ ;
WilliamMarshQMUL 0:ad3cdc525b99 88 if (samples == 10) {
WilliamMarshQMUL 0:ad3cdc525b99 89 // send to thread
WilliamMarshQMUL 0:ad3cdc525b99 90 message_t *mess = mailbox.alloc() ; // may fail but does not block
WilliamMarshQMUL 0:ad3cdc525b99 91 if (mess) {
WilliamMarshQMUL 0:ad3cdc525b99 92 mess->analog = smoothed ;
WilliamMarshQMUL 0:ad3cdc525b99 93 mailbox.put(mess); // fails but does not block if full
WilliamMarshQMUL 0:ad3cdc525b99 94 }
WilliamMarshQMUL 0:ad3cdc525b99 95 samples = 0;
WilliamMarshQMUL 0:ad3cdc525b99 96 }
WilliamMarshQMUL 0:ad3cdc525b99 97 }
WilliamMarshQMUL 0:ad3cdc525b99 98
WilliamMarshQMUL 0:ad3cdc525b99 99 // Write voltage digits
WilliamMarshQMUL 0:ad3cdc525b99 100 // v Voltage as scale int, e.g. 3.30 is 330
WilliamMarshQMUL 0:ad3cdc525b99 101 void vToString(int v, char* s) {
WilliamMarshQMUL 0:ad3cdc525b99 102 s[3] = '0' + (v % 10) ;
WilliamMarshQMUL 0:ad3cdc525b99 103 v = v / 10 ;
WilliamMarshQMUL 0:ad3cdc525b99 104 s[2] = '0' + (v % 10) ;
WilliamMarshQMUL 0:ad3cdc525b99 105 v = v / 10 ;
WilliamMarshQMUL 0:ad3cdc525b99 106 s[0] = '0' + (v % 10) ;
WilliamMarshQMUL 0:ad3cdc525b99 107 }
WilliamMarshQMUL 0:ad3cdc525b99 108
WilliamMarshQMUL 0:ad3cdc525b99 109 // Main program
WilliamMarshQMUL 0:ad3cdc525b99 110 // Initialise variables
WilliamMarshQMUL 0:ad3cdc525b99 111 // Attach ISR for ticker
WilliamMarshQMUL 0:ad3cdc525b99 112 // Procss messages from mailbox
WilliamMarshQMUL 0:ad3cdc525b99 113 int main() {
cvmanuel 2:4348f9a6c179 114 led1 = 0 ; // turn off
cvmanuel 2:4348f9a6c179 115 led2 = 0 ;
cvmanuel 2:4348f9a6c179 116 led3 = 0 ;
cvmanuel 2:4348f9a6c179 117 led4 = 0 ;
cvmanuel 2:4348f9a6c179 118 led5 = 0 ;
WilliamMarshQMUL 0:ad3cdc525b99 119 int volts = 0 ;
cvmanuel 2:4348f9a6c179 120 //const int threshold = 55 ;
cvmanuel 2:4348f9a6c179 121
cvmanuel 2:4348f9a6c179 122 pollT.start(callback(polling));
cvmanuel 2:4348f9a6c179 123
WilliamMarshQMUL 0:ad3cdc525b99 124 int counter = 0 ;
WilliamMarshQMUL 0:ad3cdc525b99 125 char vstring[] = "X.XX\r\n" ;
WilliamMarshQMUL 0:ad3cdc525b99 126
cvmanuel 2:4348f9a6c179 127 threshold = maxvolt/6;
cvmanuel 2:4348f9a6c179 128
WilliamMarshQMUL 0:ad3cdc525b99 129 tick.attach_us(callback(&readA0), 10000); // ticks every 10ms
WilliamMarshQMUL 0:ad3cdc525b99 130 while (true) {
WilliamMarshQMUL 0:ad3cdc525b99 131 osEvent evt = mailbox.get(); // wait for mail
WilliamMarshQMUL 0:ad3cdc525b99 132 if (evt.status == osEventMail) {
WilliamMarshQMUL 0:ad3cdc525b99 133 message_t* mess = (message_t*)evt.value.p ;
WilliamMarshQMUL 0:ad3cdc525b99 134 volts = (mess->analog * 330) / 0xffff ;
WilliamMarshQMUL 0:ad3cdc525b99 135 mailbox.free(mess) ; // free the message space
cvmanuel 2:4348f9a6c179 136 if(pressEvent)
cvmanuel 2:4348f9a6c179 137 {
cvmanuel 2:4348f9a6c179 138 pressEvent=0;
cvmanuel 2:4348f9a6c179 139 maxvolt = volts;
cvmanuel 2:4348f9a6c179 140 threshold= maxvolt / 6;
cvmanuel 2:4348f9a6c179 141 led1 = 1;
cvmanuel 2:4348f9a6c179 142 led2 = 1;
cvmanuel 2:4348f9a6c179 143 led3 = 1;
cvmanuel 2:4348f9a6c179 144 led4 = 1;
cvmanuel 2:4348f9a6c179 145 led5 = 1;
cvmanuel 2:4348f9a6c179 146 }
cvmanuel 2:4348f9a6c179 147
cvmanuel 2:4348f9a6c179 148
cvmanuel 2:4348f9a6c179 149 if(volts >=threshold*5 && volts <= threshold*6)
cvmanuel 2:4348f9a6c179 150 {
cvmanuel 2:4348f9a6c179 151 led1 = 1;
cvmanuel 2:4348f9a6c179 152 led2 = 1;
cvmanuel 2:4348f9a6c179 153 led3 = 1;
cvmanuel 2:4348f9a6c179 154 led4 = 1;
cvmanuel 2:4348f9a6c179 155 led5 = 1;
cvmanuel 2:4348f9a6c179 156 }else
cvmanuel 2:4348f9a6c179 157 if(volts >=threshold*4 && volts <= threshold*5)
cvmanuel 2:4348f9a6c179 158 {
cvmanuel 2:4348f9a6c179 159 led1 = 1;
cvmanuel 2:4348f9a6c179 160 led2 = 1;
cvmanuel 2:4348f9a6c179 161 led3 = 1;
cvmanuel 2:4348f9a6c179 162 led4 = 1;
cvmanuel 2:4348f9a6c179 163 led5 = 0;
cvmanuel 2:4348f9a6c179 164 }else if(volts>=threshold*3 && volts <= threshold*4)
cvmanuel 2:4348f9a6c179 165 {
cvmanuel 2:4348f9a6c179 166 led1 = 1;
cvmanuel 2:4348f9a6c179 167 led2 = 1;
cvmanuel 2:4348f9a6c179 168 led3 = 1;
cvmanuel 2:4348f9a6c179 169 led4 = 0;
cvmanuel 2:4348f9a6c179 170 led5 = 0;
cvmanuel 2:4348f9a6c179 171 }else
cvmanuel 2:4348f9a6c179 172 if( volts>=threshold*2 && volts <= threshold*3)
cvmanuel 2:4348f9a6c179 173 {
cvmanuel 2:4348f9a6c179 174 led1 = 1;
cvmanuel 2:4348f9a6c179 175 led2 = 1;
cvmanuel 2:4348f9a6c179 176 led3 = 0;
cvmanuel 2:4348f9a6c179 177 led4 = 0;
cvmanuel 2:4348f9a6c179 178 led5 = 0;
cvmanuel 2:4348f9a6c179 179 }else
cvmanuel 2:4348f9a6c179 180 if( volts >threshold && volts <=threshold*2)
cvmanuel 2:4348f9a6c179 181 {
cvmanuel 2:4348f9a6c179 182 led1 = 1;
cvmanuel 2:4348f9a6c179 183 led2 = 0;
cvmanuel 2:4348f9a6c179 184 led3 = 0;
cvmanuel 2:4348f9a6c179 185 led4 = 0;
cvmanuel 2:4348f9a6c179 186 led5 = 0;
cvmanuel 2:4348f9a6c179 187 }
cvmanuel 2:4348f9a6c179 188 else if(volts < threshold){
cvmanuel 2:4348f9a6c179 189 led1 = 0 ;
cvmanuel 2:4348f9a6c179 190 led2 = 0 ;
cvmanuel 2:4348f9a6c179 191 led3 = 0 ;
cvmanuel 2:4348f9a6c179 192 led5 = 0 ;
cvmanuel 2:4348f9a6c179 193 led5 = 0 ;
cvmanuel 2:4348f9a6c179 194 }
cvmanuel 2:4348f9a6c179 195
WilliamMarshQMUL 0:ad3cdc525b99 196 vToString(volts, vstring) ;
WilliamMarshQMUL 0:ad3cdc525b99 197 counter++ ;
WilliamMarshQMUL 0:ad3cdc525b99 198 if (counter == 10) { // limit bandwidth of serial
WilliamMarshQMUL 0:ad3cdc525b99 199 pc.printf(vstring) ;
WilliamMarshQMUL 0:ad3cdc525b99 200 counter = 0 ;
WilliamMarshQMUL 0:ad3cdc525b99 201 }
WilliamMarshQMUL 0:ad3cdc525b99 202 }
WilliamMarshQMUL 0:ad3cdc525b99 203
WilliamMarshQMUL 0:ad3cdc525b99 204 }
WilliamMarshQMUL 0:ad3cdc525b99 205 }