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.
main.cpp@2:df9c87ac7704, 2018-11-12 (annotated)
- Committer:
- noutram
- Date:
- Mon Nov 12 14:14:47 2018 +0000
- Revision:
- 2:df9c87ac7704
- Parent:
- 0:277f12eec770
- Child:
- 3:134bd19c5f04
Updated for FZ429ZI
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| noutram | 0:277f12eec770 | 1 | #include "mbed.h" |
| noutram | 0:277f12eec770 | 2 | |
| noutram | 0:277f12eec770 | 3 | //Global PWM object |
| noutram | 2:df9c87ac7704 | 4 | PwmOut pwmRed(D6); |
| 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 | } |