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
Revision 0:5307f49cd305, committed 2017-02-01
- Comitter:
- WilliamMarshQMUL
- Date:
- Wed Feb 01 13:38:02 2017 +0000
- Child:
- 1:284229960d6a
- Commit message:
- First version
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Wed Feb 01 13:38:02 2017 +0000
@@ -0,0 +1,62 @@
+// 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
+
+#include "mbed.h"
+#include "rtos.h"
+#include "sineTable.h"
+
+Ticker tick ; // Creates periodic interrupt
+AnalogOut ao(PTE30) ; // Analog output
+
+// --- following code for debugging ---
+Thread debugT ;
+Serial pc(USBTX, USBRX); // tx, rx, useful for debugging
+
+// 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') ;
+}
+
+volatile int index = 0 ; // this variable is not just for debugging!!
+
+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
+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
+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
+ }
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-rtos.lib Wed Feb 01 13:38:02 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed-rtos/#58563e6cba1e
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Feb 01 13:38:02 2017 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/ad3be0349dc5 \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/sineTable.h Wed Feb 01 13:38:02 2017 +0000
@@ -0,0 +1,15 @@
+// Look up table for a sine wave as 16 bits
+// Note that it is possible to construct the full period from
+// the first quarter, but the code is more complex
+//
+// These number were calcuated using a spreadsheet
+const uint16_t sine[] = {
+ 32768, 35980, 39161, 42280, 45308, 48215, 50973, 53556,
+ 55938, 58098, 60014, 61667, 63042, 64125, 64906, 65378,
+ 65535, 65378, 64906, 64125, 63042, 61667, 60014, 58098,
+ 55938, 53556, 50973, 48215, 45308, 42280, 39161, 35980,
+ 32768, 29556, 26375, 23256, 20228, 17321, 14563, 11980,
+ 9598, 7438, 5522, 3869, 2494, 1411, 630, 158,
+ 0, 158, 630, 1411, 2494, 3869, 5522, 7438,
+ 9598, 11980, 14563, 17321, 20228, 23256, 26375, 29556
+ } ;
\ No newline at end of file
