Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pulse Width Modulation

Similar presentations


Presentation on theme: "Pulse Width Modulation"— Presentation transcript:

1 Pulse Width Modulation
3/19/2015 Richard Kuo Assistant Professor

2 Lesson 8. Pulse Width Modulation
8.NuMicro_PWM.ppt Exercise : PWM frequency output Lab. Keypad to select tone to speaker (smpl_PWM_Tone_Keypad) Lab. using PWM to generate music (smpl_PWM_Music) Exercise : PWM control DC Servo Motor Lab. PWM control angular DC servo (smpl_PWM_SG5010) Lab. PWM control rotatory DC servo (smpl_PWM_DS04) Exercise : PWM DeadZone Lab. PWM control DeadZone of output waveform Homework : Electrotherapy Device Homework : Ultrasonic Directive Speaker

3 PWM pins Based on NUC140 LQFP 100pin PWM0 / GPA12 : pin 65
PWM4 / GPB11 : pin 48 PWM5 / GPE5 : pin 47 PWM6 / GPE0 : pin 55 PWM7 / GPE1 : pin 54 Based on Nano102 LQFP 64pin PWM0_CH0 / PC0 / PD12 PWM0_CH1 / PC1 / PD11 PWM0_CH2 / PC2 / PD10 PWM0_CH3 / PC3 / PD9

4 Pulse Width Modulation

5 PWM Generator Four PWM Generators, each generator supports
One 8-bit prescaler One clock divider Two PWM-timers for two outputs, each timer includes A 16-bit PWM down-counter A 16-bit PWM reload value register (CNR) A 16-bit PWM compare register (CMR) One dead-zone generator Two PWM outputs 8 PWM channels or 4 PWM paired channels 16 bits resolution PWM Interrupt synchronized with PWM period Single-shot or Continuous mode PWM

6 NUC140 PWM clock source selection
HIRC HCLK LXT HXT

7 Nano102 PWM clock source selection

8 PWM Block Diagram

9 PWM Frequency & Duty Cycle
CMRx+1 >= CNR: PWM output high. CMRx+1 < CNR: PWM output low PWM Frequency = PWMxy_CLK/(prescale+1)*(clock divider)/(CNR+1); where xy = 01, 23, 45 or 67, the selected PWM channel. Duty ratio = (CMR+1)/(CNR+1)

10 PWM.c (Driver function calls)
frequency PWM_ConfigOutputChannel(PWM0, 0, , 50); PWM_EnableOutput(PWM0, 0x3); PWM_Start(PWM0, 0x3); duty cycle PWM group channel no. PWM channel output enable enable PWM

11 PWM initialization void SYS_Init(void) { SYS_UnlockReg(); // Unlock protected registers CLK_EnableXtalRC(CLK_PWRCTL_HXT_EN_Msk); // Enable HXT external 12MHz crystal CLK_SetCoreClock( ); // set HCLK clock to 32MHz // Select IP clock source CLK_SetModuleClock(PWM0_CH01_MODULE, CLK_CLKSEL1_PWM0_CH01_S_HCLK, 0); // Enable IP clock CLK_EnableModuleClock(PWM0_CH01_MODULE); // Init I/O Multi-function // Set PC multi-function pins for PWM SYS->PC_L_MFP = SYS->PC_L_MFP & ~(SYS_PC_L_MFP_PC0_MFP_Msk) | SYS_PC_L_MFP_PC0_MFP_PWM0_CH0; SYS->PC_L_MFP = SYS->PC_L_MFP & ~(SYS_PC_L_MFP_PC1_MFP_Msk) | SYS_PC_L_MFP_PC1_MFP_PWM0_CH1; SYS_LockReg(); // Lock protected registers }

12 smpl_PWM int32_t main (void) { SYS_Init(); // PWM0 frequency is 125KHz, duty 50%, PWM_ConfigOutputChannel(PWM0, 0, , 50); // Enable output of all PWM channels PWM_EnableOutput(PWM0, 0x3); // Start PWM_Start(PWM0, 0x3); while(1); }

13 PWM double buffering scheme
S/W write new period (CNR) And new duty (CMR) First cycle Second cycle

14 Driving an external Speaker
C to +5V B to PWM0 E to GND +5V 2N2222 Vceo = 40V collector current = 600mA Total dissipation = 625mW PWM0 (GPA12) PWM1 (GPA13) Loud Speaker 8 ohm Gnd collector current = ( ) / (100+8) = 44.4mA

15 smpl_PWM_Music

16 Note_Freq.h //note_freq.h #define NULL 0 // null = 0Hz
#define C // C = Hz #define C0u // C0#/D0b = Hz #define D0b 17 #define D // D = Hz #define D0u // D0#/E0b = Hz #define E0b 19 #define E // E = Hz #define F // F = Hz #define F0u // F0#/G0b = Hz #define G0b 23 #define G // G = Hz #define G0u // G0#/A0b = Hz #define A0b 26 #define A // A = Hz #define A0u // A0#/B0b = Hz #define B0b 29 #define B // B = Hz #define C // C = Hz #define C1u // C1#/D1b = Hz #define D1b 35 #define D // D = Hz #define D1u // D1#/E1b = Hz

17 main.c // // smpl_PWM : PWM0 Channel0 output to speaker // PC0 /PWM0_CH0 (Nano102SC2AN LQFP-64 pin7) #include <stdio.h> #include "Nano1X2Series.h" #include "Note_Freq.h" #define P125ms #define P250ms #define P500ms #define P1S

18 SYS_Init void SYS_Init(void) { SYS_UnlockReg(); // Unlock protected registers CLK_EnableXtalRC(CLK_PWRCTL_HXT_EN_Msk); // Enable HXT external 12MHz crystal CLK_SetCoreClock( ); // set HCLK clock to 32MHz // Select IP clock source CLK_SetModuleClock(PWM0_CH01_MODULE, CLK_CLKSEL1_PWM0_CH01_S_HXT, 0); // Enable IP clock CLK_EnableModuleClock(PWM0_CH01_MODULE); // Init I/O Multi-function // Set PC multi-function pins for PWM SYS->PC_L_MFP = SYS->PC_L_MFP & ~(SYS_PC_L_MFP_PC0_MFP_Msk) | SYS_PC_L_MFP_PC0_MFP_PWM0_CH0; SYS_LockReg(); // Lock protected registers }

19 main.c Tone array Pitch array
int32_t main (void) { uint8_t i; uint16_t music[72] = { E6 ,D6u,E6 ,D6u,E6 ,B5 ,D6 ,C6 ,A5 ,A5 , 0 , 0 , C5 ,E5 ,A5 ,B5 ,B5 , 0 ,C5 ,A5 ,B5 ,C6 ,C6 , 0 , C5 ,E5 ,A5 ,B5 ,B5 , 0 ,E5 ,C6 ,B5 ,A5 ,A5 , 0 , B5 ,C6 ,D6 ,E6 ,E6 , 0 ,G5 ,F6 ,E6 ,D6 ,D6 , 0 , F5 ,E6 ,D6 ,C6 ,C6 , 0 ,E5 ,D6 ,C6 ,B5 ,B5 , 0 }; uint32_t pitch[72] = { P250ms, P250ms, P250ms, P250ms, P250ms, P250ms, P250ms, P250ms, P250ms, P250ms, P250ms, P250ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms, P500ms }; Tone array Pitch array

20 main.c SYS_Init(); PWM_EnableOutput(PWM0, 0x1); PWM_Start(PWM0, 0x1); for (i=0; i<72; i++) { PWM_ConfigOutputChannel(PWM0, 0, music[i], 50); CLK_SysTickDelay(pitch[i]); }

21 SG5010

22 DC Servo Motor Driving Method
DC Servo (SG-5010): remote controlled airplane/car/robot Interface: pin1 = PWM, pin2 = Vcc, pin3 = Gnd Driving Method PWM pulse duration is 20ms High width = 0.5 ~ 2.5ms to control motor rotation Exercise: Smpl_PWM_SG5010 PWM controller generate 20ms pulse ADC read from VR to control PWM high pulse width ADC input variable resistance to control gripper open/close

23 smpl_PWM_SG5010 SG5010 with a gripper
PWM clock at 10us & generate 50Hz(20ms) pulse output to DC Servo signal pin high-time between 0.5 and 2.5ms

24 smpl_PWM_SG5010 // // Smpl_PWM_DCservo_SG5010 // using PWM to generate 50Hz (20ms) pulse to DC Servo signal pin // 0.5 ~ 2.5ms high time (PWM clock at 10us per count) // SG5010 DC servo // pin1 : signal to PWM0/GPA12 (NUC140VE3CN LQFP100 pin65) // pin2 : Vcc // pin3 : Gnd #include <stdio.h> #include "NUC100Series.h" #define SERVO_CYCTIME 2000 // 20ms = 50Hz #define SERVO_HITIME_MIN 50 // minimum Hi width = 0.5ms #define SERVO_HITIME_MAX 250 // maximum Hi width = 2.5ms #define SERVO_STEPTIME 10 // incremental time = 1.0ms

25 SYS_Init (NUC140) void SYS_Init(void) { SYS_UnlockReg(); // Unlock protected registers CLK_EnableXtalRC(CLK_PWRCTL_HXT_EN_Msk); // Enable HXT external 12MHz crystal CLK_SetCoreClock( ); // set HCLK clock to 50MHz // Select IP clock source CLK_SetModuleClock(PWM01_MODULE, CLK_CLKSEL1_PWM01_S_HCLK, 0); // Enable IP clock CLK_EnableModuleClock(PWM01_MODULE); SYS_ResetModule(PWM03_RST); // // Init I/O Multi-function // Set PA multi-function pins for PWM SYS->GPA_MFP = SYS->GPA_MFP & ~(SYS_PA_H_MFP_PA12_MFP_Msk) | SYS_PA_H_MFP_PA12_MFP_PWM_CH0; SYS_LockReg(); }

26 smpl_PWM_SG5010 int32_t main(void) { uint16_t i; SYS_Init(); PWM_EnableOutput(PWMA, 0x1); PWM_Start(PWMA, 0x1); while(1) { for (i=SERVO_HITIME_MIN; i<SERVO_HITIME_MAX; i+=SERVO_STEPTIME) PWM_ConfigOutputChannel(PWMA, 0, 50, i*100/SERVO_CYCTIME); CLK_SysTickDelay(500000); }

27 smpl_ADC_PWM LCD Panel ADC7 Flash Cortex-M0 SPI ADC Value: 277 PWM0
LED NUC140V3AN Function: adjusting VR1 to change PWM frequency and control LED flashing

28 smpl_ADC_PWM use ADC value to control duty cycle //
// smpl_ADC_PWM : read VR1 (ADC7) and set PWM0 to dim blue-LED (GPA12) #include <stdio.h> #include "NUC100Series.h" void ADC_IRQHandler(void) { uint32_t u32Flag; uint32_t u32ADCvalue; // Get ADC conversion finish interrupt flag u32Flag = ADC_GET_INT_FLAG(ADC, ADC_ADF_INT); if(u32Flag & ADC_ADF_INT) { u32ADCvalue = ADC_GET_CONVERSION_DATA(ADC, 7); PWM_ConfigOutputChannel(PWMA, PWM_CH0, 100, u32ADCvalue*100/4096); } ADC_CLR_INT_FLAG(ADC, u32Flag); use ADC value to control duty cycle

29 SYS_Init (set ADC7 & PWM0)
void SYS_Init(void) { SYS_UnlockReg(); // Unlock protected registers CLK_EnableXtalRC(CLK_PWRCTL_HXT_EN_Msk); // Enable HXT external 12MHz crystal CLK_SetCoreClock( ); // Set HCLK clock to 32MHz // Select IP clock source CLK_SetModuleClock(ADC_MODULE,CLK_CLKSEL1_ADC_S_HXT,CLK_ADC_CLK_DIVIDER(1)); CLK_SetModuleClock(PWM01_MODULE, CLK_CLKSEL1_PWM01_S_HXT, 0); // Enable IP clock CLK_EnableModuleClock(ADC_MODULE); CLK_EnableModuleClock(PWM01_MODULE); // Init I/O Multi-function SYS->GPA_MFP &= ~SYS_PA_L_MFP_PA7_MFP_Msk ; // Set PA multi-function pins for ADC SYS->GPA_MFP |= SYS_PA_L_MFP_PA7_MFP_ADC_CH7; // Set PA7 to ADC7 // Disable digital input path //PA->OFFD = PA->OFFD | (ADC_CH_7_MASK<< 16); GPIO_DISABLE_DIGITAL_PATH(PA, ADC_CH_7_MASK); // Configure the PA12 as PWM0 output pin SYS->GPA_MFP &= ~SYS_PA_H_MFP_PA12_MFP_Msk; SYS->GPA_MFP |= SYS_PA_H_MFP_PA12_MFP_PWM_CH0; SYS_LockReg(); // Lock protected registers }

30 Init_ADC, Init_PWM, main()
void Init_ADC(void) { ADC_Open(ADC, ADC_INPUT_MODE_SINGLE_END, ADC_OPERATION_MODE_CONTINUOUS, ADC_CH_7_MASK); ADC_POWER_ON(ADC); ADC_EnableInt(ADC, ADC_ADF_INT); NVIC_EnableIRQ(ADC_IRQn); ADC_START_CONV(ADC); } void Init_PWM(void) PWM_ConfigOutputChannel(PWMA, PWM_CH0, 100, 50); PWM_EnableOutput(PWMA, 0x1); PWM_Start(PWMA, 0x1); int32_t main (void) SYS_Init(); Init_ADC(); Init_PWM(); while(1);

31 Dead Zone Generator PWM controller is implemented with Dead Zone generator. They are built for power device protection. This function generates a programmable time gap to delay PWM rising output. User can program PPRx.DZI to determine the Dead Zone interval. It provides 8-bit dead-zone timer by PWM clock

32 smpl_PWM_DeadZone

33 smpl_PWM_DeadZone int main(void) { SYS_Init();
// PWM0 frequency is 100Hz, duty 30%, PWM_ConfigOutputChannel(PWMA, PWM_CH0, 100, 30); PWM_EnableDeadZone(PWMA, PWM_CH0, 400); // PWM2 frequency is 300Hz, duty 50% PWM_ConfigOutputChannel(PWMA, PWM_CH2, 300, 50); PWM_EnableDeadZone(PWMA, PWM_CH2, 200); // Enable output of all PWMA channels PWM_EnableOutput(PWMA, 0xF); // Enable PWMA channel 0 period interrupt, use channel 0 to measure time. PWM_EnablePeriodInt(PWMA, PWM_CH0, 0); NVIC_EnableIRQ(PWMA_IRQn); // Start PWM_Start(PWMA, 0xF); while(1); }

34 PWM Capture timing diagram

35 smpl_Capture LCD Panel Capture demo High:683usec SPI Low: 683usec
Flash Capture0 Cortex-M0 PWM1 PWM2 NUC140V3AN Function : PWM pins perform to capture input

36 Home Projects 伺服機控制 https://youtu.be/qJVVuDI2_ng
自製水族自動餵食器 寵物飼料自動餵食器 兩足機器人 六足機器人 低週波電療機電路原理 超音波指向性喇叭 Ultrasonic Directive Speaker

37 Ultrasonic Directive Speaker

38 Ultrasonic Directive Speaker

39 Ultrasonic Directive Speaker

40 Important Notice ! This educational material are neither intended nor warranted for usage in systems or equipment, any malfunction or failure of which may cause loss of human life, bodily injury or severe property damage. Such applications are deemed, “Insecure Usage”. Insecure usage includes, but is not limited to: equipment for surgical implementation, atomic energy control instruments, airplane or spaceship instruments, the control or operation of dynamic, brake or safety systems designed for vehicular use, traffic signal instruments, all types of safety devices, and other applications intended to support or sustain life. All Insecure Usage shall be made at user’s own risk, and in the event that third parties lay claims to the author as a result of customer’s Insecure Usage, the user shall indemnify the damages and liabilities thus incurred by using this material. Please note that all lecture and sample codes are subject to change without notice. All the trademarks of products and companies mentioned in this material belong to their respective owners.


Download ppt "Pulse Width Modulation"

Similar presentations


Ads by Google