the code for Lab3

Fork of ADCandticker_sample by William Marsh

Committer:
WilliamMarshQMUL
Date:
Wed Jan 24 21:55:43 2018 +0000
Revision:
1:126dd2f5fc2d
Parent:
0:ad3cdc525b99
Child:
2:f7c24790b1ad
Updated for mbed 5

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