sample

Fork of ADCandticker_sample by William Marsh

Committer:
Ivone
Date:
Thu Feb 08 17:44:38 2018 +0000
Revision:
2:36c7ac9b8598
Parent:
1:126dd2f5fc2d
adc Lab3

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