University of Plymouth - Stages 1, 2 and 3 / Mbed OS Task523Solution
Committer:
noutram
Date:
Fri Sep 20 13:39:27 2019 +0000
Revision:
3:134bd19c5f04
Parent:
2:df9c87ac7704
2019

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 3:134bd19c5f04 4 #ifdef TARGET_NUCLEO_F429ZI
noutram 3:134bd19c5f04 5 PwmOut pwmRed(D6); //D7 is not a PWM output on the F429, so use D6 instead
noutram 3:134bd19c5f04 6 #else
noutram 3:134bd19c5f04 7 PwmOut pwmRed(D7);
noutram 3:134bd19c5f04 8 #endif
noutram 0:277f12eec770 9
noutram 0:277f12eec770 10 int T = 100; //100uS
noutram 0:277f12eec770 11 volatile int Tmark = 0; //0us
noutram 0:277f12eec770 12 volatile int delta = 1; //1us
noutram 0:277f12eec770 13
noutram 0:277f12eec770 14 //Timer
noutram 0:277f12eec770 15 Ticker t;
noutram 0:277f12eec770 16
noutram 0:277f12eec770 17 //Function prototype
noutram 0:277f12eec770 18 void doTwinkle();
noutram 0:277f12eec770 19
noutram 0:277f12eec770 20 int main() {
noutram 0:277f12eec770 21
noutram 0:277f12eec770 22 //Initial PWM state
noutram 0:277f12eec770 23 pwmRed.period_us(T);
noutram 0:277f12eec770 24 pwmRed.pulsewidth_us(Tmark);
noutram 0:277f12eec770 25
noutram 0:277f12eec770 26 //Start timer
noutram 0:277f12eec770 27 t.attach(doTwinkle, 0.01);
noutram 0:277f12eec770 28
noutram 0:277f12eec770 29 while(1) {
noutram 0:277f12eec770 30 sleep();
noutram 0:277f12eec770 31
noutram 0:277f12eec770 32 //Update PWM
noutram 0:277f12eec770 33 pwmRed.pulsewidth_us(Tmark);
noutram 0:277f12eec770 34
noutram 0:277f12eec770 35 //printf("Tmark = %d\n", Tmark); //Debug
noutram 0:277f12eec770 36 }
noutram 0:277f12eec770 37 }
noutram 0:277f12eec770 38
noutram 0:277f12eec770 39 //ISR for Timer
noutram 0:277f12eec770 40 void doTwinkle()
noutram 0:277f12eec770 41 {
noutram 0:277f12eec770 42 //Update mark time
noutram 0:277f12eec770 43 Tmark += delta;
noutram 0:277f12eec770 44
noutram 0:277f12eec770 45 //Check bounds - and swap direction
noutram 0:277f12eec770 46 if ((Tmark >= 99) || (Tmark <= 0)) {
noutram 0:277f12eec770 47 delta = -delta;
noutram 0:277f12eec770 48 }
noutram 0:277f12eec770 49 }