Lab 3 Part 2.4

Fork of Lab3_2 by Divya Vadivel

Committer:
diviavad
Date:
Fri Feb 09 23:57:21 2018 +0000
Revision:
4:80644a19c35c
Parent:
3:b20239a6bd91
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 max_volt = false;
diviavad 2:bb80af536422 101 pollT.start(callback(polling));//start thread to check button
WilliamMarshQMUL 0:ad3cdc525b99 102 int volts = 0 ;
diviavad 2:bb80af536422 103 int threshold[6] = {55,110,165,220,275,330} ;
WilliamMarshQMUL 0:ad3cdc525b99 104 int counter = 0 ;
WilliamMarshQMUL 0:ad3cdc525b99 105 char vstring[] = "X.XX\r\n" ;
WilliamMarshQMUL 0:ad3cdc525b99 106 tick.attach_us(callback(&readA0), 10000); // ticks every 10ms
WilliamMarshQMUL 0:ad3cdc525b99 107 while (true) {
diviavad 2:bb80af536422 108
diviavad 2:bb80af536422 109 if(pressEvent)
diviavad 2:bb80af536422 110 {
diviavad 3:b20239a6bd91 111 pressEvent=0;
diviavad 3:b20239a6bd91 112 max_volt = !max_volt;
diviavad 2:bb80af536422 113 }
WilliamMarshQMUL 0:ad3cdc525b99 114 osEvent evt = mailbox.get(); // wait for mail
WilliamMarshQMUL 0:ad3cdc525b99 115 if (evt.status == osEventMail) {
WilliamMarshQMUL 0:ad3cdc525b99 116 message_t* mess = (message_t*)evt.value.p ;
WilliamMarshQMUL 0:ad3cdc525b99 117 volts = (mess->analog * 330) / 0xffff ;
WilliamMarshQMUL 0:ad3cdc525b99 118 mailbox.free(mess) ; // free the message space
diviavad 2:bb80af536422 119
diviavad 4:80644a19c35c 120 if((volts <= threshold[0]) and max_volt==false){
diviavad 2:bb80af536422 121 ledy = 0;
diviavad 2:bb80af536422 122 ledb = 0;
diviavad 2:bb80af536422 123 ledr = 0;
diviavad 2:bb80af536422 124 ledw = 0;
diviavad 2:bb80af536422 125 ledg = 0;
diviavad 2:bb80af536422 126 }
diviavad 4:80644a19c35c 127 else if(volts >=(threshold[0])&&volts <=(threshold[1] and max_volt==false)){//1.1 && volts < 1.65){
diviavad 2:bb80af536422 128 ledy = 1;
diviavad 2:bb80af536422 129 ledb = 0;
diviavad 2:bb80af536422 130 ledr = 0;
diviavad 2:bb80af536422 131 ledw = 0;
diviavad 2:bb80af536422 132 ledg = 0;
diviavad 2:bb80af536422 133 }
diviavad 4:80644a19c35c 134 else if(volts >=(threshold[1])&&volts <=(threshold[2] && max_volt==false)){
diviavad 2:bb80af536422 135 ledy = 1;
diviavad 2:bb80af536422 136 ledb = 1;
diviavad 2:bb80af536422 137 ledr = 0;
diviavad 2:bb80af536422 138 ledw = 0;
diviavad 2:bb80af536422 139 ledg = 0;
diviavad 2:bb80af536422 140 }
diviavad 4:80644a19c35c 141 else if(volts >=(threshold[2])&&volts <=(threshold[3]) and max_volt==false){
diviavad 2:bb80af536422 142 ledy = 1;
diviavad 2:bb80af536422 143 ledb = 1;
diviavad 2:bb80af536422 144 ledr = 1;
diviavad 2:bb80af536422 145 ledw = 0;
diviavad 2:bb80af536422 146 ledg = 0;
diviavad 2:bb80af536422 147 }
diviavad 4:80644a19c35c 148 else if(volts >=(threshold[3])&&volts <=(threshold[4]) and max_volt==false){
diviavad 2:bb80af536422 149 ledy = 1;
diviavad 2:bb80af536422 150 ledb = 1;
diviavad 2:bb80af536422 151 ledr = 1;
diviavad 2:bb80af536422 152 ledw = 1;
diviavad 2:bb80af536422 153 ledg = 0;
diviavad 2:bb80af536422 154 }
diviavad 4:80644a19c35c 155 else if(volts >=(threshold[4])&&volts <=(threshold[5]) and max_volt==true){
diviavad 2:bb80af536422 156 ledy = 1;
diviavad 2:bb80af536422 157 ledb = 1;
diviavad 2:bb80af536422 158 ledr = 1;
diviavad 2:bb80af536422 159 ledw = 1;
diviavad 2:bb80af536422 160 ledg = 1;
diviavad 2:bb80af536422 161 }
diviavad 2:bb80af536422 162 if(volts>=(threshold[5])){
diviavad 3:b20239a6bd91 163 max_volt=false;
diviavad 2:bb80af536422 164
diviavad 2:bb80af536422 165 }
diviavad 2:bb80af536422 166 else{}
WilliamMarshQMUL 0:ad3cdc525b99 167 vToString(volts, vstring) ;
WilliamMarshQMUL 0:ad3cdc525b99 168 counter++ ;
WilliamMarshQMUL 0:ad3cdc525b99 169 if (counter == 10) { // limit bandwidth of serial
WilliamMarshQMUL 0:ad3cdc525b99 170 pc.printf(vstring) ;
WilliamMarshQMUL 0:ad3cdc525b99 171 counter = 0 ;
WilliamMarshQMUL 0:ad3cdc525b99 172 }
WilliamMarshQMUL 0:ad3cdc525b99 173 }
WilliamMarshQMUL 0:ad3cdc525b99 174 }
WilliamMarshQMUL 0:ad3cdc525b99 175 }