ADCandticker_lab-3

Fork of ADCandticker_lab-3_edwin by Edwin Kadavy

Committer:
WilliamMarshQMUL
Date:
Tue Jan 31 23:02:02 2017 +0000
Revision:
0:ad3cdc525b99
Child:
1:126dd2f5fc2d
First complete version

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 0:ad3cdc525b99 3
WilliamMarshQMUL 0:ad3cdc525b99 4 #include "mbed.h"
WilliamMarshQMUL 0:ad3cdc525b99 5 #include "rtos.h"
WilliamMarshQMUL 0:ad3cdc525b99 6
WilliamMarshQMUL 0:ad3cdc525b99 7 Ticker tick; // Ticker for reading analog
WilliamMarshQMUL 0:ad3cdc525b99 8 AnalogIn ain(A0) ; // Analog input
WilliamMarshQMUL 0:ad3cdc525b99 9 DigitalOut led1(LED_RED); // Red LED
WilliamMarshQMUL 0:ad3cdc525b99 10
WilliamMarshQMUL 0:ad3cdc525b99 11 Serial pc(USBTX, USBRX); // tx, rx, for debugging
WilliamMarshQMUL 0:ad3cdc525b99 12
WilliamMarshQMUL 0:ad3cdc525b99 13 // Message type
WilliamMarshQMUL 0:ad3cdc525b99 14 typedef struct {
WilliamMarshQMUL 0:ad3cdc525b99 15 uint16_t analog; /* Analog input value */
WilliamMarshQMUL 0:ad3cdc525b99 16 } message_t;
WilliamMarshQMUL 0:ad3cdc525b99 17
WilliamMarshQMUL 0:ad3cdc525b99 18 // Mail box
WilliamMarshQMUL 0:ad3cdc525b99 19 Mail<message_t, 2> mailbox;
WilliamMarshQMUL 0:ad3cdc525b99 20
WilliamMarshQMUL 0:ad3cdc525b99 21 // Function called every 10ms to read ADC
WilliamMarshQMUL 0:ad3cdc525b99 22 // Low pass filter
WilliamMarshQMUL 0:ad3cdc525b99 23 // Every 10th value is sent to mailbox
WilliamMarshQMUL 0:ad3cdc525b99 24 volatile int samples = 0 ;
WilliamMarshQMUL 0:ad3cdc525b99 25 volatile uint16_t smoothed = 0 ;
WilliamMarshQMUL 0:ad3cdc525b99 26 void readA0() {
WilliamMarshQMUL 0:ad3cdc525b99 27 smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ;
WilliamMarshQMUL 0:ad3cdc525b99 28 samples++ ;
WilliamMarshQMUL 0:ad3cdc525b99 29 if (samples == 10) {
WilliamMarshQMUL 0:ad3cdc525b99 30 // send to thread
WilliamMarshQMUL 0:ad3cdc525b99 31 message_t *mess = mailbox.alloc() ; // may fail but does not block
WilliamMarshQMUL 0:ad3cdc525b99 32 if (mess) {
WilliamMarshQMUL 0:ad3cdc525b99 33 mess->analog = smoothed ;
WilliamMarshQMUL 0:ad3cdc525b99 34 mailbox.put(mess); // fails but does not block if full
WilliamMarshQMUL 0:ad3cdc525b99 35 }
WilliamMarshQMUL 0:ad3cdc525b99 36 samples = 0;
WilliamMarshQMUL 0:ad3cdc525b99 37 }
WilliamMarshQMUL 0:ad3cdc525b99 38 }
WilliamMarshQMUL 0:ad3cdc525b99 39
WilliamMarshQMUL 0:ad3cdc525b99 40 // Write voltage digits
WilliamMarshQMUL 0:ad3cdc525b99 41 // v Voltage as scale int, e.g. 3.30 is 330
WilliamMarshQMUL 0:ad3cdc525b99 42 void vToString(int v, char* s) {
WilliamMarshQMUL 0:ad3cdc525b99 43 s[3] = '0' + (v % 10) ;
WilliamMarshQMUL 0:ad3cdc525b99 44 v = v / 10 ;
WilliamMarshQMUL 0:ad3cdc525b99 45 s[2] = '0' + (v % 10) ;
WilliamMarshQMUL 0:ad3cdc525b99 46 v = v / 10 ;
WilliamMarshQMUL 0:ad3cdc525b99 47 s[0] = '0' + (v % 10) ;
WilliamMarshQMUL 0:ad3cdc525b99 48 }
WilliamMarshQMUL 0:ad3cdc525b99 49
WilliamMarshQMUL 0:ad3cdc525b99 50 // Main program
WilliamMarshQMUL 0:ad3cdc525b99 51 // Initialise variables
WilliamMarshQMUL 0:ad3cdc525b99 52 // Attach ISR for ticker
WilliamMarshQMUL 0:ad3cdc525b99 53 // Procss messages from mailbox
WilliamMarshQMUL 0:ad3cdc525b99 54 int main() {
WilliamMarshQMUL 0:ad3cdc525b99 55 led1 = 1 ; // turn off
WilliamMarshQMUL 0:ad3cdc525b99 56 int volts = 0 ;
WilliamMarshQMUL 0:ad3cdc525b99 57 const int threshold = 100 ;
WilliamMarshQMUL 0:ad3cdc525b99 58 int counter = 0 ;
WilliamMarshQMUL 0:ad3cdc525b99 59 char vstring[] = "X.XX\r\n" ;
WilliamMarshQMUL 0:ad3cdc525b99 60
WilliamMarshQMUL 0:ad3cdc525b99 61 tick.attach_us(callback(&readA0), 10000); // ticks every 10ms
WilliamMarshQMUL 0:ad3cdc525b99 62 while (true) {
WilliamMarshQMUL 0:ad3cdc525b99 63 osEvent evt = mailbox.get(); // wait for mail
WilliamMarshQMUL 0:ad3cdc525b99 64 if (evt.status == osEventMail) {
WilliamMarshQMUL 0:ad3cdc525b99 65 message_t* mess = (message_t*)evt.value.p ;
WilliamMarshQMUL 0:ad3cdc525b99 66 volts = (mess->analog * 330) / 0xffff ;
WilliamMarshQMUL 0:ad3cdc525b99 67 mailbox.free(mess) ; // free the message space
WilliamMarshQMUL 0:ad3cdc525b99 68 if (volts > threshold) led1 = 0 ; else led1 = 1 ;
WilliamMarshQMUL 0:ad3cdc525b99 69 vToString(volts, vstring) ;
WilliamMarshQMUL 0:ad3cdc525b99 70 counter++ ;
WilliamMarshQMUL 0:ad3cdc525b99 71 if (counter == 10) { // limit bandwidth of serial
WilliamMarshQMUL 0:ad3cdc525b99 72 pc.printf(vstring) ;
WilliamMarshQMUL 0:ad3cdc525b99 73 counter = 0 ;
WilliamMarshQMUL 0:ad3cdc525b99 74 }
WilliamMarshQMUL 0:ad3cdc525b99 75 }
WilliamMarshQMUL 0:ad3cdc525b99 76
WilliamMarshQMUL 0:ad3cdc525b99 77 }
WilliamMarshQMUL 0:ad3cdc525b99 78 }