My forked repository. DISCO_F407VG, DISCO_F303VC, DISCO_F051R8 and DISCO_F100RB maybe added.
Dependents: FastPWM-DISCO-test
Fork of FastPWM by
FastPWM_common.cpp@6:0f57969697b6, 2014-03-17 (annotated)
- Committer:
- Sissors
- Date:
- Mon Mar 17 22:12:58 2014 +0000
- Revision:
- 6:0f57969697b6
- Parent:
- 4:a7b9f778c4b4
- Child:
- 12:4600daab8a83
Changing period now keeps pulsewidth the same instead of duty cycle
;
; KLXX now doesn't give glitches when changing pulsewidth
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Sissors | 4:a7b9f778c4b4 | 1 | #include "FastPWM.h" |
Sissors | 4:a7b9f778c4b4 | 2 | |
Sissors | 4:a7b9f778c4b4 | 3 | FastPWM::FastPWM(PinName pin, int prescaler) : PwmOut(pin) { |
Sissors | 4:a7b9f778c4b4 | 4 | initFastPWM(); |
Sissors | 4:a7b9f778c4b4 | 5 | this->prescaler(prescaler); |
Sissors | 4:a7b9f778c4b4 | 6 | |
Sissors | 4:a7b9f778c4b4 | 7 | //Set duty cycle on 0%, period on 20ms |
Sissors | 4:a7b9f778c4b4 | 8 | period(0.02); |
Sissors | 6:0f57969697b6 | 9 | write(0); |
Sissors | 4:a7b9f778c4b4 | 10 | |
Sissors | 4:a7b9f778c4b4 | 11 | |
Sissors | 4:a7b9f778c4b4 | 12 | } |
Sissors | 4:a7b9f778c4b4 | 13 | |
Sissors | 4:a7b9f778c4b4 | 14 | void FastPWM::period(double seconds) { |
Sissors | 4:a7b9f778c4b4 | 15 | if (dynamicPrescaler) |
Sissors | 4:a7b9f778c4b4 | 16 | calcPrescaler((uint64_t)(seconds * (double) SystemCoreClock)); |
Sissors | 4:a7b9f778c4b4 | 17 | |
Sissors | 4:a7b9f778c4b4 | 18 | period_ticks(seconds * dticks + 0.5); |
Sissors | 4:a7b9f778c4b4 | 19 | } |
Sissors | 4:a7b9f778c4b4 | 20 | |
Sissors | 4:a7b9f778c4b4 | 21 | void FastPWM::period_ms(int ms) { |
Sissors | 4:a7b9f778c4b4 | 22 | if (dynamicPrescaler) |
Sissors | 4:a7b9f778c4b4 | 23 | calcPrescaler(ms * (SystemCoreClock / 1000)); |
Sissors | 4:a7b9f778c4b4 | 24 | |
Sissors | 4:a7b9f778c4b4 | 25 | period_ticks(ms * iticks_ms); |
Sissors | 4:a7b9f778c4b4 | 26 | } |
Sissors | 4:a7b9f778c4b4 | 27 | |
Sissors | 4:a7b9f778c4b4 | 28 | void FastPWM::period_us(int us) { |
Sissors | 4:a7b9f778c4b4 | 29 | if (dynamicPrescaler) |
Sissors | 4:a7b9f778c4b4 | 30 | calcPrescaler(us * (SystemCoreClock / 1000000)); |
Sissors | 4:a7b9f778c4b4 | 31 | |
Sissors | 4:a7b9f778c4b4 | 32 | period_ticks(us * iticks_us); |
Sissors | 4:a7b9f778c4b4 | 33 | } |
Sissors | 4:a7b9f778c4b4 | 34 | |
Sissors | 4:a7b9f778c4b4 | 35 | void FastPWM::period_us(double us) { |
Sissors | 4:a7b9f778c4b4 | 36 | if (dynamicPrescaler) |
Sissors | 4:a7b9f778c4b4 | 37 | calcPrescaler((uint64_t)(us * (double)(SystemCoreClock / 1000000))); |
Sissors | 4:a7b9f778c4b4 | 38 | |
Sissors | 4:a7b9f778c4b4 | 39 | period_ticks(us * dticks_us + 0.5); |
Sissors | 4:a7b9f778c4b4 | 40 | } |
Sissors | 4:a7b9f778c4b4 | 41 | |
Sissors | 4:a7b9f778c4b4 | 42 | void FastPWM::pulsewidth(double seconds) { |
Sissors | 4:a7b9f778c4b4 | 43 | pulsewidth_ticks(seconds * dticks + 0.5); |
Sissors | 4:a7b9f778c4b4 | 44 | } |
Sissors | 4:a7b9f778c4b4 | 45 | |
Sissors | 4:a7b9f778c4b4 | 46 | void FastPWM::pulsewidth_ms(int ms) { |
Sissors | 4:a7b9f778c4b4 | 47 | pulsewidth_ticks(ms * iticks_ms); |
Sissors | 4:a7b9f778c4b4 | 48 | } |
Sissors | 4:a7b9f778c4b4 | 49 | |
Sissors | 4:a7b9f778c4b4 | 50 | void FastPWM::pulsewidth_us(int us) { |
Sissors | 4:a7b9f778c4b4 | 51 | pulsewidth_ticks(us * iticks_us); |
Sissors | 4:a7b9f778c4b4 | 52 | } |
Sissors | 4:a7b9f778c4b4 | 53 | |
Sissors | 4:a7b9f778c4b4 | 54 | void FastPWM::pulsewidth_us(double us) { |
Sissors | 4:a7b9f778c4b4 | 55 | pulsewidth_ticks(us * dticks_us + 0.5); |
Sissors | 4:a7b9f778c4b4 | 56 | } |
Sissors | 4:a7b9f778c4b4 | 57 | |
Sissors | 4:a7b9f778c4b4 | 58 | void FastPWM::write(double duty) { |
Sissors | 4:a7b9f778c4b4 | 59 | _duty=duty; |
Sissors | 4:a7b9f778c4b4 | 60 | pulsewidth_ticks(duty*getPeriod()); |
Sissors | 4:a7b9f778c4b4 | 61 | } |
Sissors | 4:a7b9f778c4b4 | 62 | |
Sissors | 4:a7b9f778c4b4 | 63 | double FastPWM::read( void ) { |
Sissors | 4:a7b9f778c4b4 | 64 | return _duty; |
Sissors | 4:a7b9f778c4b4 | 65 | } |
Sissors | 4:a7b9f778c4b4 | 66 | |
Sissors | 4:a7b9f778c4b4 | 67 | FastPWM & FastPWM::operator= (double value) { |
Sissors | 4:a7b9f778c4b4 | 68 | write(value); |
Sissors | 4:a7b9f778c4b4 | 69 | return(*this); |
Sissors | 4:a7b9f778c4b4 | 70 | } |
Sissors | 4:a7b9f778c4b4 | 71 | |
Sissors | 4:a7b9f778c4b4 | 72 | FastPWM::operator double() { |
Sissors | 4:a7b9f778c4b4 | 73 | return _duty; |
Sissors | 4:a7b9f778c4b4 | 74 | } |
Sissors | 4:a7b9f778c4b4 | 75 | |
Sissors | 4:a7b9f778c4b4 | 76 | int FastPWM::prescaler(int value) { |
Sissors | 4:a7b9f778c4b4 | 77 | int retval; |
Sissors | 4:a7b9f778c4b4 | 78 | if (value == -1) { |
Sissors | 4:a7b9f778c4b4 | 79 | dynamicPrescaler = true; |
Sissors | 4:a7b9f778c4b4 | 80 | value = 0; |
Sissors | 4:a7b9f778c4b4 | 81 | } |
Sissors | 4:a7b9f778c4b4 | 82 | else |
Sissors | 4:a7b9f778c4b4 | 83 | dynamicPrescaler = false; |
Sissors | 4:a7b9f778c4b4 | 84 | |
Sissors | 4:a7b9f778c4b4 | 85 | retval = setPrescaler(value); |
Sissors | 4:a7b9f778c4b4 | 86 | updateTicks(retval); |
Sissors | 4:a7b9f778c4b4 | 87 | return retval; |
Sissors | 4:a7b9f778c4b4 | 88 | } |
Sissors | 4:a7b9f778c4b4 | 89 | |
Sissors | 4:a7b9f778c4b4 | 90 | void FastPWM::updateTicks( uint32_t prescaler ) { |
Sissors | 4:a7b9f778c4b4 | 91 | dticks = SystemCoreClock / (double)prescaler; |
Sissors | 4:a7b9f778c4b4 | 92 | dticks_us = SystemCoreClock / (double)prescaler / 1000000.0f; |
Sissors | 4:a7b9f778c4b4 | 93 | iticks_ms = SystemCoreClock / prescaler / 1000; |
Sissors | 4:a7b9f778c4b4 | 94 | iticks_us = SystemCoreClock / prescaler / 1000000; |
Sissors | 4:a7b9f778c4b4 | 95 | } |
Sissors | 4:a7b9f778c4b4 | 96 | |
Sissors | 4:a7b9f778c4b4 | 97 | int FastPWM::calcPrescaler(uint64_t clocks) { |
Sissors | 4:a7b9f778c4b4 | 98 | uint32_t scale = (clocks >> bits) + 1; |
Sissors | 4:a7b9f778c4b4 | 99 | uint32_t retval = setPrescaler(scale); |
Sissors | 4:a7b9f778c4b4 | 100 | updateTicks(retval); |
Sissors | 4:a7b9f778c4b4 | 101 | return retval; |
Sissors | 4:a7b9f778c4b4 | 102 | } |