Solution for the FZ429

Dependencies:   mbed

Committer:
noutram
Date:
Thu Sep 24 12:31:48 2015 +0000
Revision:
0:277f12eec770
Child:
3:dd6456275904
Initial version 24-09-2015

Who changed what in which revision?

UserRevisionLine numberNew contents of line
noutram 0:277f12eec770 1 #include "mbed.h"
noutram 0:277f12eec770 2
noutram 0:277f12eec770 3 //Global PWM object
noutram 0:277f12eec770 4 PwmOut pwmRed(D7);
noutram 0:277f12eec770 5
noutram 0:277f12eec770 6 int T = 100; //100uS
noutram 0:277f12eec770 7 volatile int Tmark = 0; //0us
noutram 0:277f12eec770 8 volatile int delta = 1; //1us
noutram 0:277f12eec770 9
noutram 0:277f12eec770 10 //Timer
noutram 0:277f12eec770 11 Ticker t;
noutram 0:277f12eec770 12
noutram 0:277f12eec770 13 //Function prototype
noutram 0:277f12eec770 14 void doTwinkle();
noutram 0:277f12eec770 15
noutram 0:277f12eec770 16 int main() {
noutram 0:277f12eec770 17
noutram 0:277f12eec770 18 //Initial PWM state
noutram 0:277f12eec770 19 pwmRed.period_us(T);
noutram 0:277f12eec770 20 pwmRed.pulsewidth_us(Tmark);
noutram 0:277f12eec770 21
noutram 0:277f12eec770 22 //Start timer
noutram 0:277f12eec770 23 t.attach(doTwinkle, 0.01);
noutram 0:277f12eec770 24
noutram 0:277f12eec770 25 while(1) {
noutram 0:277f12eec770 26 sleep();
noutram 0:277f12eec770 27
noutram 0:277f12eec770 28 //Update PWM
noutram 0:277f12eec770 29 pwmRed.pulsewidth_us(Tmark);
noutram 0:277f12eec770 30
noutram 0:277f12eec770 31 //printf("Tmark = %d\n", Tmark); //Debug
noutram 0:277f12eec770 32 }
noutram 0:277f12eec770 33 }
noutram 0:277f12eec770 34
noutram 0:277f12eec770 35 //ISR for Timer
noutram 0:277f12eec770 36 void doTwinkle()
noutram 0:277f12eec770 37 {
noutram 0:277f12eec770 38 //Update mark time
noutram 0:277f12eec770 39 Tmark += delta;
noutram 0:277f12eec770 40
noutram 0:277f12eec770 41 //Check bounds - and swap direction
noutram 0:277f12eec770 42 if ((Tmark >= 99) || (Tmark <= 0)) {
noutram 0:277f12eec770 43 delta = -delta;
noutram 0:277f12eec770 44 }
noutram 0:277f12eec770 45 }