Lab 3 Part 2.2

Fork of ADCandticker_sample by William Marsh

Committer:
diviavad
Date:
Fri Feb 09 23:42:36 2018 +0000
Revision:
3:b20239a6bd91
Parent:
2:bb80af536422
Child:
4:d9666f1c135b
version 1

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
diviavad 2:bb80af536422 10 DigitalIn b1(D15, PullUp);
diviavad 2:bb80af536422 11 //DigitalOut led1(LED_RED); // Red LED
diviavad 2:bb80af536422 12 DigitalOut ledy(PTD5);
diviavad 2:bb80af536422 13 DigitalOut ledb(PTA13);
diviavad 2:bb80af536422 14 DigitalOut ledr(PTD0);
diviavad 2:bb80af536422 15 DigitalOut ledw(PTD2);
diviavad 2:bb80af536422 16 DigitalOut ledg(PTD3);
diviavad 2:bb80af536422 17 volatile int pressEvent = 0 ;
diviavad 2:bb80af536422 18 Serial pc(USBTX, USBRX); // tx, rx, for debugging
diviavad 2:bb80af536422 19
diviavad 2:bb80af536422 20
WilliamMarshQMUL 0:ad3cdc525b99 21
diviavad 2:bb80af536422 22 enum buttonPos { up, down, bounce }; // Button positions
diviavad 2:bb80af536422 23 void polling() {
diviavad 2:bb80af536422 24 buttonPos pos = up ;//chose positing in enum
diviavad 2:bb80af536422 25 int bcounter = 0 ;
diviavad 2:bb80af536422 26 while (true) {
diviavad 2:bb80af536422 27 switch (pos) {
diviavad 2:bb80af536422 28 case up :
diviavad 2:bb80af536422 29 if (!b1.read()) { // now down
diviavad 2:bb80af536422 30 pressEvent = 1 ; // transition occurred
diviavad 2:bb80af536422 31 pos = down ;
diviavad 2:bb80af536422 32 }
diviavad 2:bb80af536422 33 break ;
diviavad 2:bb80af536422 34 case down :
diviavad 2:bb80af536422 35 if (b1 == 1) { // no longer down
diviavad 2:bb80af536422 36 bcounter = 3 ; // wait four cycles
diviavad 2:bb80af536422 37 pos = bounce ;
diviavad 2:bb80af536422 38 }
diviavad 2:bb80af536422 39 break ;
diviavad 2:bb80af536422 40 case bounce :
diviavad 2:bb80af536422 41 if (b1 == 0) { // down again - button has bounced
diviavad 2:bb80af536422 42 pos = down ; // no event
diviavad 2:bb80af536422 43 } else if (bcounter == 0) {
diviavad 2:bb80af536422 44 pos = up ; // delay passed - reset to up
diviavad 2:bb80af536422 45 } else {
diviavad 2:bb80af536422 46 bcounter-- ; // continue waiting
diviavad 2:bb80af536422 47 }
diviavad 2:bb80af536422 48 break ;
diviavad 2:bb80af536422 49 }
diviavad 2:bb80af536422 50 Thread::wait(30);
diviavad 2:bb80af536422 51 }
diviavad 2:bb80af536422 52 }
WilliamMarshQMUL 0:ad3cdc525b99 53
WilliamMarshQMUL 0:ad3cdc525b99 54 // Message type
WilliamMarshQMUL 0:ad3cdc525b99 55 typedef struct {
WilliamMarshQMUL 0:ad3cdc525b99 56 uint16_t analog; /* Analog input value */
WilliamMarshQMUL 0:ad3cdc525b99 57 } message_t;
diviavad 2:bb80af536422 58 Thread pollT ; // thread to poll
diviavad 2:bb80af536422 59 void buttonCallback(){
diviavad 2:bb80af536422 60 pressEvent = 1 ;
diviavad 2:bb80af536422 61 }
WilliamMarshQMUL 0:ad3cdc525b99 62 // Mail box
WilliamMarshQMUL 0:ad3cdc525b99 63 Mail<message_t, 2> mailbox;
WilliamMarshQMUL 0:ad3cdc525b99 64
WilliamMarshQMUL 0:ad3cdc525b99 65 // Function called every 10ms to read ADC
WilliamMarshQMUL 0:ad3cdc525b99 66 // Low pass filter
WilliamMarshQMUL 0:ad3cdc525b99 67 // Every 10th value is sent to mailbox
WilliamMarshQMUL 0:ad3cdc525b99 68 volatile int samples = 0 ;
WilliamMarshQMUL 0:ad3cdc525b99 69 volatile uint16_t smoothed = 0 ;
diviavad 2:bb80af536422 70
WilliamMarshQMUL 0:ad3cdc525b99 71 void readA0() {
WilliamMarshQMUL 0:ad3cdc525b99 72 smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ;
WilliamMarshQMUL 0:ad3cdc525b99 73 samples++ ;
WilliamMarshQMUL 0:ad3cdc525b99 74 if (samples == 10) {
WilliamMarshQMUL 0:ad3cdc525b99 75 // send to thread
WilliamMarshQMUL 0:ad3cdc525b99 76 message_t *mess = mailbox.alloc() ; // may fail but does not block
WilliamMarshQMUL 0:ad3cdc525b99 77 if (mess) {
WilliamMarshQMUL 0:ad3cdc525b99 78 mess->analog = smoothed ;
WilliamMarshQMUL 0:ad3cdc525b99 79 mailbox.put(mess); // fails but does not block if full
WilliamMarshQMUL 0:ad3cdc525b99 80 }
WilliamMarshQMUL 0:ad3cdc525b99 81 samples = 0;
WilliamMarshQMUL 0:ad3cdc525b99 82 }
WilliamMarshQMUL 0:ad3cdc525b99 83 }
WilliamMarshQMUL 0:ad3cdc525b99 84
WilliamMarshQMUL 0:ad3cdc525b99 85 // Write voltage digits
WilliamMarshQMUL 0:ad3cdc525b99 86 // v Voltage as scale int, e.g. 3.30 is 330
WilliamMarshQMUL 0:ad3cdc525b99 87 void vToString(int v, char* s) {
WilliamMarshQMUL 0:ad3cdc525b99 88 s[3] = '0' + (v % 10) ;
WilliamMarshQMUL 0:ad3cdc525b99 89 v = v / 10 ;
WilliamMarshQMUL 0:ad3cdc525b99 90 s[2] = '0' + (v % 10) ;
WilliamMarshQMUL 0:ad3cdc525b99 91 v = v / 10 ;
WilliamMarshQMUL 0:ad3cdc525b99 92 s[0] = '0' + (v % 10) ;
WilliamMarshQMUL 0:ad3cdc525b99 93 }
WilliamMarshQMUL 0:ad3cdc525b99 94
WilliamMarshQMUL 0:ad3cdc525b99 95 // Main program
WilliamMarshQMUL 0:ad3cdc525b99 96 // Initialise variables
WilliamMarshQMUL 0:ad3cdc525b99 97 // Attach ISR for ticker
WilliamMarshQMUL 0:ad3cdc525b99 98 // Procss messages from mailbox
WilliamMarshQMUL 0:ad3cdc525b99 99 int main() {
diviavad 2:bb80af536422 100 bool change_detect = false;
diviavad 2:bb80af536422 101 bool max_volt = false;
diviavad 2:bb80af536422 102 pollT.start(callback(polling));//start thread to check button
WilliamMarshQMUL 0:ad3cdc525b99 103 int volts = 0 ;
diviavad 2:bb80af536422 104 int threshold[6] = {55,110,165,220,275,330} ;
WilliamMarshQMUL 0:ad3cdc525b99 105 int counter = 0 ;
WilliamMarshQMUL 0:ad3cdc525b99 106 char vstring[] = "X.XX\r\n" ;
WilliamMarshQMUL 0:ad3cdc525b99 107 tick.attach_us(callback(&readA0), 10000); // ticks every 10ms
WilliamMarshQMUL 0:ad3cdc525b99 108 while (true) {
diviavad 2:bb80af536422 109
diviavad 2:bb80af536422 110 if(pressEvent)
diviavad 2:bb80af536422 111 {
diviavad 3:b20239a6bd91 112 pressEvent=0;
diviavad 3:b20239a6bd91 113 max_volt = !max_volt;
diviavad 2:bb80af536422 114 change_detect=false;
diviavad 2:bb80af536422 115 }
WilliamMarshQMUL 0:ad3cdc525b99 116 osEvent evt = mailbox.get(); // wait for mail
WilliamMarshQMUL 0:ad3cdc525b99 117 if (evt.status == osEventMail) {
WilliamMarshQMUL 0:ad3cdc525b99 118 message_t* mess = (message_t*)evt.value.p ;
WilliamMarshQMUL 0:ad3cdc525b99 119 volts = (mess->analog * 330) / 0xffff ;
WilliamMarshQMUL 0:ad3cdc525b99 120 mailbox.free(mess) ; // free the message space
diviavad 2:bb80af536422 121
diviavad 2:bb80af536422 122 if(volts <=(threshold[0]) and max_volt=false){
diviavad 2:bb80af536422 123 ledy = 0;
diviavad 2:bb80af536422 124 ledb = 0;
diviavad 2:bb80af536422 125 ledr = 0;
diviavad 2:bb80af536422 126 ledw = 0;
diviavad 2:bb80af536422 127 ledg = 0;
diviavad 2:bb80af536422 128 }
diviavad 2:bb80af536422 129 else if(volts >=(threshold[0])&&volts <=(threshold[1] and max_volt=false)){//1.1 && volts < 1.65){
diviavad 2:bb80af536422 130 ledy = 1;
diviavad 2:bb80af536422 131 ledb = 0;
diviavad 2:bb80af536422 132 ledr = 0;
diviavad 2:bb80af536422 133 ledw = 0;
diviavad 2:bb80af536422 134 ledg = 0;
diviavad 2:bb80af536422 135 }
diviavad 3:b20239a6bd91 136 else if(volts >=(threshold[1])&&volts <=(threshold[2] && max_volt=false)){
diviavad 2:bb80af536422 137 ledy = 1;
diviavad 2:bb80af536422 138 ledb = 1;
diviavad 2:bb80af536422 139 ledr = 0;
diviavad 2:bb80af536422 140 ledw = 0;
diviavad 2:bb80af536422 141 ledg = 0;
diviavad 2:bb80af536422 142 }
diviavad 2:bb80af536422 143 else if(volts >=(threshold[2])&&volts <=(threshold[3]) and max_volt=false){
diviavad 2:bb80af536422 144 ledy = 1;
diviavad 2:bb80af536422 145 ledb = 1;
diviavad 2:bb80af536422 146 ledr = 1;
diviavad 2:bb80af536422 147 ledw = 0;
diviavad 2:bb80af536422 148 ledg = 0;
diviavad 2:bb80af536422 149 }
diviavad 2:bb80af536422 150 else if(volts >=(threshold[3])&&volts <=(threshold[4]) and max_volt=false){
diviavad 2:bb80af536422 151 ledy = 1;
diviavad 2:bb80af536422 152 ledb = 1;
diviavad 2:bb80af536422 153 ledr = 1;
diviavad 2:bb80af536422 154 ledw = 1;
diviavad 2:bb80af536422 155 ledg = 0;
diviavad 2:bb80af536422 156 }
diviavad 2:bb80af536422 157 else if(volts >=(threshold[4])&&volts <=(threshold[5]) and max_volt=true){
diviavad 2:bb80af536422 158 ledy = 1;
diviavad 2:bb80af536422 159 ledb = 1;
diviavad 2:bb80af536422 160 ledr = 1;
diviavad 2:bb80af536422 161 ledw = 1;
diviavad 2:bb80af536422 162 ledg = 1;
diviavad 2:bb80af536422 163 }
diviavad 2:bb80af536422 164 if(volts>=(threshold[5])){
diviavad 3:b20239a6bd91 165 max_volt=false;
diviavad 2:bb80af536422 166
diviavad 2:bb80af536422 167 }
diviavad 2:bb80af536422 168 else{}
WilliamMarshQMUL 0:ad3cdc525b99 169 vToString(volts, vstring) ;
WilliamMarshQMUL 0:ad3cdc525b99 170 counter++ ;
WilliamMarshQMUL 0:ad3cdc525b99 171 if (counter == 10) { // limit bandwidth of serial
WilliamMarshQMUL 0:ad3cdc525b99 172 pc.printf(vstring) ;
WilliamMarshQMUL 0:ad3cdc525b99 173 counter = 0 ;
WilliamMarshQMUL 0:ad3cdc525b99 174 }
WilliamMarshQMUL 0:ad3cdc525b99 175 }
WilliamMarshQMUL 0:ad3cdc525b99 176 }
WilliamMarshQMUL 0:ad3cdc525b99 177 }