Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of DACandticker_sample_with_debug by
Diff: main.cpp
- Revision:
- 3:fcf87401c1b2
- Parent:
- 2:b88c9b2c4c34
- Child:
- 4:78ec71fcb981
--- a/main.cpp Fri Feb 09 23:31:23 2018 +0000
+++ b/main.cpp Wed Feb 14 15:43:28 2018 +0000
@@ -1,64 +1,101 @@
-// Lab 3 Example Program 2
-// -----------------------
-// Periodically write to the AnalogOut to create a sine wave
-// Alternate between two fixed frequencies every 5 sec
-//
-// THIS VERSION HAS DEBUGGING CODE USING THE SERIAL PORT
-
-// Revised for mbed 5
+//LAB 3 SAMPLE PROGRAM 2
+// Revised for mbed 5
#include "mbed.h"
#include "sineTable.h"
-Ticker tick ; // Creates periodic interrupt
+Ticker tickout ; // Creates periodic interrupt
AnalogOut ao(PTE30) ; // Analog output
-// --- following code for debugging ---
-Thread debugT ;
-Serial pc(USBTX, USBRX); // tx, rx, useful for debugging
+Ticker tick; // Ticker for reading analog
+AnalogIn ain(A0) ; // Analog input
+DigitalOut led1(LED_RED);
+
+
+Serial pc(USBTX, USBRX); // tx, rx, for debugging
+
+// Message type
+typedef struct {
+ uint16_t analog; /* Analog input value */
+} message_t;
+
+// Mail box
+Mail<message_t, 2> mailbox;
-// Put a simple reprsentation of the sine wave
-// to the serial output. ONLY at low frequency
-void debug(int index) {
- int sine4 = sine[index] >> 11 ; // get top 5 bits
- pc.putc('*') ;
- while (sine4--) pc.putc('*') ;
- pc.putc('\n') ;
- //pc.putc('\r') ;
+// Function called every 10ms to read ADC
+// Low pass filter
+// Every 10th value is sent to mailbox
+volatile int samples = 0 ;
+volatile uint16_t smoothed = 0 ;
+void readA0() {
+ smoothed = (smoothed >> 1) + (ain.read_u16() >> 1) ;
+ samples++ ;
+ if (samples == 10) {
+ // send to thread
+ message_t *mess = mailbox.alloc() ; // may fail but does not block
+ if (mess) {
+ mess->analog = smoothed ;
+ mailbox.put(mess); // fails but does not block if full
+ }
+ samples = 0;
+ }
}
-
-volatile int index = 0 ; // this variable is not just for debugging!!
+// Write voltage digits
+// v Voltage as scale int, e.g. 3.30 is 330
+void vToString(int v, char* s) {
+ s[3] = '0' + (v % 10) ;
+ v = v / 10 ;
+ s[2] = '0' + (v % 10) ;
+ v = v / 10 ;
+ s[0] = '0' + (v % 10) ;
+}
-void debugCallback() {
- while (true) {
- Thread::signal_wait(0x1) ;
- debug(index) ; // there is a race condition here
- }
-}
-// ---- end of debugging code ---------
-
-// Function called every periodically
-// Write new value to AnalogOut
+volatile int index = 0 ; // index into array of sin values
void writeAout() {
ao.write_u16(sine[index]) ;
- debugT.signal_set(0x1) ; // DEBUGGING low frequency only
index = (index + 1) % 64 ;
}
-// Control the frequency of updates
-// Alternative between two frequencies
+// Main program
+// Initialise variables
+// Attach ISR for ticker
+// Procss messages from mailbox
int main() {
- int update_us = 100000 ; // 100ms
- debugT.start(&debugCallback) ;
- while (true) {
- pc.printf("Update at 64 x 100ms giving about 0.15Hz\n");
- tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut
- Thread::wait(30000) ; // wait 30 sec - 30000ms
- update_us = 150000 ; // 150ms
- pc.printf("Update at 64 x 150ms giving about 0.1Hz\n");
- tick.attach_us(callback(&writeAout), update_us); // setup ticker to write to AnalogOut
- Thread::wait(30000) ; // wait 30 sec - 30000ms
- update_us = 100000 ; // 100ms
+
+ int update_us = 1000 ;
+ float tempupdate = 0.0;
+ int volts = 0;
+ led1 = 1 ; // turn off
+ int counter = 0 ;
+ char vstring[] = "X.XX\r\n" ;
+
+ tick.attach_us(callback(&readA0), 10000); // ticks every 10ms
+
+
+ while (true)
+ {
+ osEvent evt = mailbox.get(); // wait for mail
+ if (evt.status == osEventMail)
+ {
+ message_t* mess = (message_t*)evt.value.p ;
+ volts = (mess->analog * 330) / 0xffff ;
+ mailbox.free(mess) ; // free the message space
+
+ update_us = 1000000/((1+(49*volts/330))*64) ;
+ tickout.detach() ;
+ tickout.attach_us(callback(&writeAout), update_us);
+
+ // vToString(volts, vstring) ;
+
+ /* counter++ ;
+ if (counter == 10) { // limit bandwidth of
+ pc.printf(vstring);
+ pc.printf("%d\n",update_us);
+ counter = 0 ;
+ }
+ */
+ }
+
}
-}
+}
\ No newline at end of file
