Potmeter FastPWM.h

Dependencies:   mbed

Committer:
vd
Date:
Tue Sep 19 16:52:01 2017 +0000
Revision:
0:16be67f4d9ac
Potmeter FastPWM.h

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vd 0:16be67f4d9ac 1 //For targets which use the SCT
vd 0:16be67f4d9ac 2 #if defined(TARGET_LPC81X) || defined(TARGET_LPC82X)
vd 0:16be67f4d9ac 3
vd 0:16be67f4d9ac 4 #ifdef TARGET_LPC82X
vd 0:16be67f4d9ac 5 #define CTRL_U CTRL
vd 0:16be67f4d9ac 6 #endif
vd 0:16be67f4d9ac 7
vd 0:16be67f4d9ac 8 #include "FastPWM.h"
vd 0:16be67f4d9ac 9
vd 0:16be67f4d9ac 10 void FastPWM::initFastPWM( void ) {
vd 0:16be67f4d9ac 11 //Mbed uses the timer as a single unified 32-bit timer, who are we to argue with this, and it is easier
vd 0:16be67f4d9ac 12 bits = 32;
vd 0:16be67f4d9ac 13
vd 0:16be67f4d9ac 14 #ifdef TARGET_LPC82X
vd 0:16be67f4d9ac 15 //The mbed lib uses the PWM peripheral slightly different, which is irritating. This sets it bck to the LPC81X
vd 0:16be67f4d9ac 16 _pwm.pwm->EVENT[_pwm.pwm_ch + 1].CTRL = (1 << 12) | (_pwm.pwm_ch + 1); // Event_n on Match_n
vd 0:16be67f4d9ac 17 _pwm.pwm->EVENT[_pwm.pwm_ch + 1].STATE = 0xFFFFFFFF; // All states
vd 0:16be67f4d9ac 18 _pwm.pwm->OUT[_pwm.pwm_ch].SET = (1 << 0); // All PWM channels are SET on Event_0
vd 0:16be67f4d9ac 19 _pwm.pwm->OUT[_pwm.pwm_ch].CLR = (1 << (_pwm.pwm_ch + 1)); // PWM ch is CLRed on Event_(ch+1)
vd 0:16be67f4d9ac 20 #endif
vd 0:16be67f4d9ac 21
vd 0:16be67f4d9ac 22 //With 32-bit we fix prescaler to 1
vd 0:16be67f4d9ac 23 _pwm.pwm->CTRL_U |= (1 << 2) | (1 << 3);
vd 0:16be67f4d9ac 24 _pwm.pwm->CTRL_U &= ~(0x7F << 5);
vd 0:16be67f4d9ac 25 _pwm.pwm->CTRL_U &= ~(1 << 2);
vd 0:16be67f4d9ac 26
vd 0:16be67f4d9ac 27 }
vd 0:16be67f4d9ac 28
vd 0:16be67f4d9ac 29 void FastPWM::pulsewidth_ticks( uint32_t ticks ) {
vd 0:16be67f4d9ac 30 #ifdef TARGET_LPC81X
vd 0:16be67f4d9ac 31 _pwm.pwm->MATCHREL[_pwm.pwm_ch + 1].U = ticks;
vd 0:16be67f4d9ac 32 #else
vd 0:16be67f4d9ac 33 _pwm.pwm->MATCHREL[_pwm.pwm_ch + 1] = ticks;
vd 0:16be67f4d9ac 34 #endif
vd 0:16be67f4d9ac 35 }
vd 0:16be67f4d9ac 36
vd 0:16be67f4d9ac 37 void FastPWM::period_ticks( uint32_t ticks ) {
vd 0:16be67f4d9ac 38 #ifdef TARGET_LPC81X
vd 0:16be67f4d9ac 39 _pwm.pwm->MATCHREL[0].U = ticks;
vd 0:16be67f4d9ac 40 #else
vd 0:16be67f4d9ac 41 _pwm.pwm->MATCHREL[0] = ticks;
vd 0:16be67f4d9ac 42 #endif
vd 0:16be67f4d9ac 43 }
vd 0:16be67f4d9ac 44
vd 0:16be67f4d9ac 45 uint32_t FastPWM::getPeriod( void ) {
vd 0:16be67f4d9ac 46 #ifdef TARGET_LPC81X
vd 0:16be67f4d9ac 47 return _pwm.pwm->MATCHREL[0].U;
vd 0:16be67f4d9ac 48 #else
vd 0:16be67f4d9ac 49 return _pwm.pwm->MATCHREL[0];
vd 0:16be67f4d9ac 50 #endif
vd 0:16be67f4d9ac 51 }
vd 0:16be67f4d9ac 52
vd 0:16be67f4d9ac 53 //Maybe implemented later, but needing to change the prescaler for a 32-bit
vd 0:16be67f4d9ac 54 //timer used in PWM mode is kinda unlikely.
vd 0:16be67f4d9ac 55 //If you really need to do it, rejoice, you can make it run so slow a period is over 40,000 year
vd 0:16be67f4d9ac 56 uint32_t FastPWM::setPrescaler(uint32_t reqScale) {
vd 0:16be67f4d9ac 57 //Disable dynamic prescaling
vd 0:16be67f4d9ac 58 dynamicPrescaler = false;
vd 0:16be67f4d9ac 59
vd 0:16be67f4d9ac 60 return 1;
vd 0:16be67f4d9ac 61 }
vd 0:16be67f4d9ac 62
vd 0:16be67f4d9ac 63 #endif