V D
/
di19sept2017
Potmeter FastPWM.h
FastPWM_common.cpp@0:16be67f4d9ac, 2017-09-19 (annotated)
- Committer:
- vd
- Date:
- Tue Sep 19 16:52:01 2017 +0000
- Revision:
- 0:16be67f4d9ac
Potmeter FastPWM.h
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vd | 0:16be67f4d9ac | 1 | #include "FastPWM.h" |
vd | 0:16be67f4d9ac | 2 | |
vd | 0:16be67f4d9ac | 3 | FastPWM::FastPWM(PinName pin, int prescaler) : PwmOut(pin) { |
vd | 0:16be67f4d9ac | 4 | fast_obj = NULL; |
vd | 0:16be67f4d9ac | 5 | initFastPWM(); |
vd | 0:16be67f4d9ac | 6 | this->prescaler(prescaler); |
vd | 0:16be67f4d9ac | 7 | |
vd | 0:16be67f4d9ac | 8 | //Set duty cycle on 0%, period on 20ms |
vd | 0:16be67f4d9ac | 9 | period(0.02); |
vd | 0:16be67f4d9ac | 10 | write(0); |
vd | 0:16be67f4d9ac | 11 | |
vd | 0:16be67f4d9ac | 12 | |
vd | 0:16be67f4d9ac | 13 | } |
vd | 0:16be67f4d9ac | 14 | |
vd | 0:16be67f4d9ac | 15 | FastPWM::~FastPWM( void ) { |
vd | 0:16be67f4d9ac | 16 | if (fast_obj != NULL) |
vd | 0:16be67f4d9ac | 17 | delete(fast_obj); |
vd | 0:16be67f4d9ac | 18 | } |
vd | 0:16be67f4d9ac | 19 | |
vd | 0:16be67f4d9ac | 20 | void FastPWM::period(double seconds) { |
vd | 0:16be67f4d9ac | 21 | if (dynamicPrescaler) |
vd | 0:16be67f4d9ac | 22 | calcPrescaler((uint64_t)(seconds * (double) SystemCoreClock)); |
vd | 0:16be67f4d9ac | 23 | |
vd | 0:16be67f4d9ac | 24 | period_ticks(seconds * dticks + 0.5); |
vd | 0:16be67f4d9ac | 25 | } |
vd | 0:16be67f4d9ac | 26 | |
vd | 0:16be67f4d9ac | 27 | void FastPWM::period_ms(int ms) { |
vd | 0:16be67f4d9ac | 28 | if (dynamicPrescaler) |
vd | 0:16be67f4d9ac | 29 | calcPrescaler(ms * (SystemCoreClock / 1000)); |
vd | 0:16be67f4d9ac | 30 | |
vd | 0:16be67f4d9ac | 31 | period_ticks(ms * iticks_ms); |
vd | 0:16be67f4d9ac | 32 | } |
vd | 0:16be67f4d9ac | 33 | |
vd | 0:16be67f4d9ac | 34 | void FastPWM::period_us(int us) { |
vd | 0:16be67f4d9ac | 35 | if (dynamicPrescaler) |
vd | 0:16be67f4d9ac | 36 | calcPrescaler(us * (SystemCoreClock / 1000000)); |
vd | 0:16be67f4d9ac | 37 | |
vd | 0:16be67f4d9ac | 38 | period_ticks(us * iticks_us); |
vd | 0:16be67f4d9ac | 39 | } |
vd | 0:16be67f4d9ac | 40 | |
vd | 0:16be67f4d9ac | 41 | void FastPWM::period_us(double us) { |
vd | 0:16be67f4d9ac | 42 | if (dynamicPrescaler) |
vd | 0:16be67f4d9ac | 43 | calcPrescaler((uint64_t)(us * (double)(SystemCoreClock / 1000000))); |
vd | 0:16be67f4d9ac | 44 | |
vd | 0:16be67f4d9ac | 45 | period_ticks(us * dticks_us + 0.5); |
vd | 0:16be67f4d9ac | 46 | } |
vd | 0:16be67f4d9ac | 47 | |
vd | 0:16be67f4d9ac | 48 | void FastPWM::pulsewidth(double seconds) { |
vd | 0:16be67f4d9ac | 49 | pulsewidth_ticks(seconds * dticks + 0.5); |
vd | 0:16be67f4d9ac | 50 | } |
vd | 0:16be67f4d9ac | 51 | |
vd | 0:16be67f4d9ac | 52 | void FastPWM::pulsewidth_ms(int ms) { |
vd | 0:16be67f4d9ac | 53 | pulsewidth_ticks(ms * iticks_ms); |
vd | 0:16be67f4d9ac | 54 | } |
vd | 0:16be67f4d9ac | 55 | |
vd | 0:16be67f4d9ac | 56 | void FastPWM::pulsewidth_us(int us) { |
vd | 0:16be67f4d9ac | 57 | pulsewidth_ticks(us * iticks_us); |
vd | 0:16be67f4d9ac | 58 | } |
vd | 0:16be67f4d9ac | 59 | |
vd | 0:16be67f4d9ac | 60 | void FastPWM::pulsewidth_us(double us) { |
vd | 0:16be67f4d9ac | 61 | pulsewidth_ticks(us * dticks_us + 0.5); |
vd | 0:16be67f4d9ac | 62 | } |
vd | 0:16be67f4d9ac | 63 | |
vd | 0:16be67f4d9ac | 64 | void FastPWM::write(double duty) { |
vd | 0:16be67f4d9ac | 65 | _duty=duty; |
vd | 0:16be67f4d9ac | 66 | pulsewidth_ticks(duty*getPeriod()); |
vd | 0:16be67f4d9ac | 67 | } |
vd | 0:16be67f4d9ac | 68 | |
vd | 0:16be67f4d9ac | 69 | double FastPWM::read( void ) { |
vd | 0:16be67f4d9ac | 70 | return _duty; |
vd | 0:16be67f4d9ac | 71 | } |
vd | 0:16be67f4d9ac | 72 | |
vd | 0:16be67f4d9ac | 73 | FastPWM & FastPWM::operator= (double value) { |
vd | 0:16be67f4d9ac | 74 | write(value); |
vd | 0:16be67f4d9ac | 75 | return(*this); |
vd | 0:16be67f4d9ac | 76 | } |
vd | 0:16be67f4d9ac | 77 | |
vd | 0:16be67f4d9ac | 78 | FastPWM::operator double() { |
vd | 0:16be67f4d9ac | 79 | return _duty; |
vd | 0:16be67f4d9ac | 80 | } |
vd | 0:16be67f4d9ac | 81 | |
vd | 0:16be67f4d9ac | 82 | int FastPWM::prescaler(int value) { |
vd | 0:16be67f4d9ac | 83 | int retval; |
vd | 0:16be67f4d9ac | 84 | if (value == -1) { |
vd | 0:16be67f4d9ac | 85 | dynamicPrescaler = true; |
vd | 0:16be67f4d9ac | 86 | value = 0; |
vd | 0:16be67f4d9ac | 87 | } |
vd | 0:16be67f4d9ac | 88 | else |
vd | 0:16be67f4d9ac | 89 | dynamicPrescaler = false; |
vd | 0:16be67f4d9ac | 90 | |
vd | 0:16be67f4d9ac | 91 | retval = setPrescaler(value); |
vd | 0:16be67f4d9ac | 92 | updateTicks(retval); |
vd | 0:16be67f4d9ac | 93 | return retval; |
vd | 0:16be67f4d9ac | 94 | } |
vd | 0:16be67f4d9ac | 95 | |
vd | 0:16be67f4d9ac | 96 | void FastPWM::updateTicks( uint32_t prescaler ) { |
vd | 0:16be67f4d9ac | 97 | dticks = SystemCoreClock / (double)prescaler; |
vd | 0:16be67f4d9ac | 98 | dticks_us = dticks / 1000000.0f; |
vd | 0:16be67f4d9ac | 99 | iticks_us = (int)(dticks_us + 0.5); |
vd | 0:16be67f4d9ac | 100 | iticks_ms = (int)(dticks_us * 1000.0 + 0.5); |
vd | 0:16be67f4d9ac | 101 | } |
vd | 0:16be67f4d9ac | 102 | |
vd | 0:16be67f4d9ac | 103 | int FastPWM::calcPrescaler(uint64_t clocks) { |
vd | 0:16be67f4d9ac | 104 | uint32_t scale = (clocks >> bits) + 1; |
vd | 0:16be67f4d9ac | 105 | uint32_t retval = setPrescaler(scale); |
vd | 0:16be67f4d9ac | 106 | updateTicks(retval); |
vd | 0:16be67f4d9ac | 107 | return retval; |
vd | 0:16be67f4d9ac | 108 | } |