Nicholas Outram / Mbed OS Task523Solution
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 //Global PWM object
00004 PwmOut pwmRed(D7);
00005 
00006 int T = 100;    //100uS
00007 volatile int Tmark = 0; //0us
00008 volatile int delta = 1; //1us
00009 
00010 //Timer
00011 Ticker t;
00012 
00013 //Function prototype 
00014 void doTwinkle();
00015 
00016 int main() {
00017     
00018     //Initial PWM state
00019     pwmRed.period_us(T);
00020     pwmRed.pulsewidth_us(Tmark);
00021     
00022     //Start timer
00023     t.attach(doTwinkle, 0.01);
00024     
00025     while(1) {
00026         sleep();
00027         
00028         //Update PWM
00029         pwmRed.pulsewidth_us(Tmark);
00030         
00031         //printf("Tmark = %d\n", Tmark);    //Debug
00032     }
00033 }
00034 
00035 //ISR for Timer
00036 void doTwinkle()
00037 {
00038     //Update mark time
00039     Tmark += delta;
00040     
00041     //Check bounds - and swap direction
00042     if ((Tmark >= 99) || (Tmark <= 0)) {
00043         delta = -delta;
00044     }
00045 }