PV198 – One-chip Controllers, PWM / Dávid Danaj 1 PV198 – One-chip Controllers PWM PV198 – One-chip Controllers, PWM / Dávid Danaj 2 Content 1.What is PWM 2.What is it used for 3.How does it work 4.PWM on FRDM-K66F 1.Timer/PWM Module (TPM) 5.Code 6.Application PV198 – One-chip Controllers, PWM / Dávid Danaj 3 What is PWM §PWM – Pulse Width Modulation §A method of reducing the average power delivered by an electrical signal, by effectively chopping it up into discrete parts PV198 – One-chip Controllers, PWM / Dávid Danaj 4 What is it used for §Motor control §Audio amplifiers §Digital lighting PV198 – One-chip Controllers, PWM / Dávid Danaj 5 How does it work §Switching fast enough for the application (low period) §Changing duty cycle https://en.wikipedia.org/wiki/Pulse-width_modulation#/media/File:Duty_Cycle_Examples.png CC BY-SA 4.0 PWM on FRDM-K66F §FlexTimer Module (FTM) §4 instances §2 - 8 channels §Timer/PWM Module (TPM) §2 instances §2 channels PV198 – One-chip Controllers, PWM / Dávid Danaj 6 Timer/PWM Module (TPM) §Modes of operation: §Input capture §Output compare §Edge-Aligned PWM §Center-Aligned PWM §Combine PWM §Combine Input Capture PV198 – One-chip Controllers, PWM / Dávid Danaj 7 Input capture §Detects edge in the input signal §Configurable rising/falling edge detection §Edge sets interrupt flag §Read precise time from counter §Example: ultrasonic distance sensor demo PV198 – One-chip Controllers, PWM / Dávid Danaj 8 Output compare §Generate timed pulses with programmable position, polarity, duration, and frequency PV198 – One-chip Controllers, PWM / Dávid Danaj 9 Edge-Aligned PWM §Leading edge is aligned with the beginning of the period PV198 – One-chip Controllers, PWM / Dávid Danaj 10 Center-Aligned PWM §Counts up until it reaches MOD and then counts down until it reaches zero §The pulse width center is when the TPM counter = 0 PV198 – One-chip Controllers, PWM / Dávid Danaj 11 Center-Aligned PWM PV198 – One-chip Controllers, PWM / Dávid Danaj 12 Combine PWM §Even channel (n) and adjacent odd channel (n+1) are combined to generate a PWM signal in the channel (n) output. PV198 – One-chip Controllers, PWM / Dávid Danaj 13 Combine PWM PV198 – One-chip Controllers, PWM / Dávid Danaj 14 Combine Input Capture §Measure a pulse width of the signal PV198 – One-chip Controllers, PWM / Dávid Danaj 15 Application §Create an application that turns on Blue LED with 20% intensity §Use TPM or FTM peripheral §Update your application to turn on Green and Red LED with 20% intensity (use FTM, because TPM is not available) PV198 – One-chip Controllers, PWM / Dávid Danaj 16 Code const tpm_chnl_pwm_signal_param_t TPM_edge_pwmSignalParams[] = { { .chnlNumber = kTPM_Chnl_1, .level = kTPM_LowTrue, .dutyCyclePercent = 20 } }; void TPM_edge_init(void) { TPM_Init(TPM_EDGE_PERIPHERAL, &TPM_edge_config); TPM_SetupPwm(TPM_EDGE_PERIPHERAL, TPM_edge_pwmSignalParams, sizeof(TPM_edge_pwmSignalParams) / sizeof(tpm_chnl_pwm_signal_param_t), kTPM_EdgeAlignedPwm, 24000U, TPM_EDGE_CLOCK_SOURCE); TPM_StartTimer(TPM_EDGE_PERIPHERAL, kTPM_SystemClock); } 17 PV198 – One-chip Controllers, PWM / Dávid Danaj Stepper motor demo §Which mode can we use to get 4 signals as shown in the picture? 18 PV198 – One-chip Controllers, PWM / Dávid Danaj Homework §Write 3 functions, that sets intensity for each color. §Download HSV_RGB.h from study_materials/software §All you have to do is periodically iterate over all colors by updating ‘H = (H + 1) % 360’ PV198 – One-chip Controllers, PWM / Dávid Danaj 19