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.
Diff: main.cpp
- Revision:
- 0:f3eff68b603f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Thu Sep 24 12:32:02 2015 +0000
@@ -0,0 +1,46 @@
+#include "mbed.h"
+
+//Global PWM object
+PwmOut pwmRed(D7);
+
+int T = 100; //100uS
+volatile int Tmark = 0; //0us
+volatile int delta = 1; //1us
+int n=0;
+
+//Timer
+Ticker t;
+
+//Function prototype
+void doTwinkle();
+
+int main() {
+
+ //Initial PWM state
+ pwmRed.period_us(T);
+ pwmRed.pulsewidth_us(Tmark);
+
+ //Start timer
+ t.attach(doTwinkle, 0.01);
+
+ while(1) {
+ sleep();
+
+ //Update PWM
+ pwmRed.pulsewidth_us(Tmark);
+
+ //printf("Tmark = %d\n", Tmark); //Debug
+ }
+}
+
+//ISR for Timer
+void doTwinkle()
+{
+ //Update n
+ n++;
+
+ //Calculate t
+ float t = (float)n / (float)T;
+ float y = sin(2.0f*3.1415926f*t); //Range -1...+1
+ Tmark = (int)(0.5f*(y+1.0f)*T); //Range 0..T
+}
\ No newline at end of file