University of Plymouth - Stages 1, 2 and 3
/
Task524Solution
main.cpp
- Committer:
- noutram
- Date:
- 2019-09-20
- Revision:
- 2:1261bfd4b52d
- Parent:
- 0:f3eff68b603f
File content as of revision 2:1261bfd4b52d:
#include "mbed.h" //Global PWM object #ifdef TARGET_NUCLEO_F429ZI PwmOut pwmRed(D6); //D7 is not a PWM output on the F429, so use D6 instead #else PwmOut pwmRed(D7); #endif 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 }