Presentation is loading. Please wait.

Presentation is loading. Please wait.

Www.ee.ntou.edu.tw Department of Electrical Engineering, National Taiwan Ocean University Pulse Width Modulation 4/25/2013 Richard Kuo Assistant Professor.

Similar presentations


Presentation on theme: "Www.ee.ntou.edu.tw Department of Electrical Engineering, National Taiwan Ocean University Pulse Width Modulation 4/25/2013 Richard Kuo Assistant Professor."— Presentation transcript:

1 www.ee.ntou.edu.tw Department of Electrical Engineering, National Taiwan Ocean University Pulse Width Modulation 4/25/2013 Richard Kuo Assistant Professor

2 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Outline ► Pulse Width Modulation Introduction –8.NuMicro_PWM.ppt ► Exercise : using PWM to generate tone (Smpl_PWM_Tone) –To use oscilloscope to observe the output waveform ► Exercise : using PWM to output to speaker (Smpl_PWM_stereo) –Add a transistor to driver external speaker ► Exercise : using PWM with Keypad to select different tone to output to speaker (Smpl_PWM_Tone_Keypad) –Use ScanKey driver to get input from 3x3 keypad –Use PWM to generate tone

3 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Pulse Width Modulation ► PWM (Pulse Width Modulation) is a powerful technique for controlling analog circuits with a microcontroller’s digital output. ► PWM is employed in a wide variety of applications, ranging from measurement and communications to power control and conversion adjustable frequency, duty cycle

4 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Pulse Width Modulation ► PWM waveforms are "on-off" digital waveforms that are specified by two values: the period and the duty cycle, as shown below.

5 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw NuMicro 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 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw PWM clock source selection

7 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw PWM Block Diagram Select proper PWM clock input Set PWM frequency( CNR) Set PWM duty (CMR) Configure PWM setting (PCR) Configure PWM output channel Select Paired-PWM Insert dead-zone for paired-PWM PWM Interrupt

8 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw PWM Frequency & Duty Cycle  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) CMRx+1 >= CNR: PWM output high. CMRx+1 < CNR: PWM output low

9 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw PWM double buffering scheme New period (CNR) New duty (CMR) S/W write new period (CNR) And new duty (CMR) First cycle Second cycle

10 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw 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

11 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Capture Input ► Captured by Rising or Falling Edge ► Measure Pulse Width –Rising to Falling (W2) –Falling to Rising (W1) –Rising to Rising (W1+W2) –Falling to Falling (W1+W2) ► Capture Input Channel –How to set and change input channel? ► Sampling Frequency –How to set and change frequency? ► Capture Function Interrupt ► Captured pulse width data –Rising Latch Register (CRLR) –Falling Latch Register (CFLR) Sampling Interrupt

12 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Capture Input - Timing Diagram The PWM counter will be reloaded with CNRx when a capture interrupt flag (CAPIFx) is set The channel low pulse width is (CNR - CRLR). The channel high pulse width is (CNR - CFLR).

13 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Sample Code: Toggle mode, 250Hz, duty =33% uint8_t volatile g_u8PWMCount = 100; int32_t main (void) { S_DRVPWM_TIME_DATA_T sPt; UNLOCKREG(); SYSCLK->PWRCON.XTL12M_EN = 1; SysTimerDelay(5000); SystemFrequency = 12000000; DrvSYS_SetHCLKSource(0); LOCKREG(); /* Enable PWM clock */ DrvPWM_Open(); /* Set PWM pins */ DrvGPIO_InitFunction(FUNC_PWM01); /* PWM Timer property */ sPt.u8Mode = DRVPWM_TOGGLE_MODE; sPt.u32Frequency =250; /* High Pulse peroid : Total Pulse peroid = 33 : 100 */ sPt.u8HighPulseRatio = 33; sPt.i32Inverter = 0; /* Select PWM engine clock */ DrvPWM_SelectClockSource(DRVPWM_TIMER0, DRVPWM_HCLK);

14 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Sample Code: Toggle mode, 250Hz, duty =33% /* Set PWM Timer0 Configuration */ DrvPWM_SetTimerClk(DRVPWM_TIMER0, &sPt); /* Enable Output for PWM Timer0 */ DrvPWM_SetTimerIO(DRVPWM_TIMER0, 1); /* Enable Interrupt Sources of PWM Timer0 and install call back function */ DrvPWM_EnableInt(DRVPWM_TIMER0, 0, DRVPWM_PwmIRQHandler); /* Enable the PWM Timer 0 */ DrvPWM_Enable(DRVPWM_TIMER0, 1); while (g_u8PWMCount); /* Disable the PWM Timer 0 */ DrvPWM_Enable(DRVPWM_TIMER0, 0); } void DRVPWM_PwmIRQHandler() { if (g_u8PWMCount==1 ) { DrvPWM_SetTimerCounter(DRVPWM_TIMER0, 0); } g_u8PWMCount--; } 14 4 ms 33% (250Hz) 10001999897 g_u8PWMCount PWM output

15 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw External Speaker Circuit +5V PWM0 (GPA12) PWM1 (GPA13) Gnd 100ohm 2A673 collector current = -500mA collector power = 400mW Ice saturation current = -150mA Vce saturation voltage = -0.2V Loud Speaker 8 ohm collector current = (5 - 0.2) / (100+8) = 44.4mA

16 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_PWM_Tone

17 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw InitPWM() void InitPWM0(void) { /* Step 1. GPIO initial */ SYS->GPAMFP.PWM0_AD13=1; /* Step 2. Enable and Select PWM clock source*/ SYSCLK->APBCLK.PWM01_EN = 1;//Enable PWM0 & PWM1 clock SYSCLK->CLKSEL1.PWM01_S = 0;//Select 12Mhz for PWM clock source PWMA->PPR.CP01=1;//Prescaler 0~255, Setting 0 to stop output clock PWMA->CSR.CSR0=0;//PWM clock = clock source/(Prescaler + 1)/divider /* Step 3. Select PWM Operation mode */ PWMA->PCR.CH0MOD=1;//0:One-shot mode, 1:Auto-load mode //CNR and CMR will be auto-cleared after setting CH0MOD form 0 to 1. PWMA->CNR0=0xFFFF; PWMA->CMR0=0xFFFF; PWMA->PCR.CH0INV=0;//Inverter->0:off, 1:on PWMA->PCR.CH0EN=1;//PWM function->0:Disable, 1:Enable PWMA->POE.PWM0=1;//Output to pin->0:Diasble, 1:Enable }

18 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw PWM_Freq(freq,duty) void PWM0_Freq(uint32_t PWM_frequency, uint8_t PWM_duty) { uint32_t PWM_Clock; uint8_t PWM_PreScaler; uint16_t PWM_ClockDivider; uint16_t CNR0, CMR0; if (PWM_frequency==0) PWMA->POE.PWM0=0; else PWMA->POE.PWM0=1; if (PWM_frequency!=0) { // PWM setting PWM_Clock = 12000000; // Clock source = 22.1184MHz PWM_PreScaler = 5; // clock is divided by (PreScaler + 1) PWM_ClockDivider = 2; // 0: 1/2, 1: 1/4, 2: 1/8, 3: 1/16, 4: 1 //PWM_FreqOut = PWM_Clock / (PWM_PreScaler + 1) / PWM_ClockDivider / (PWM_CNR0 + 1); CNR0 = PWM_Clock / PWM_frequency / (PWM_PreScaler + 1) / PWM_ClockDivider - 1; // Duty Cycle = (CMR0+1) / (CNR0+1) CMR0 = (CNR0 +1) * PWM_duty /100 - 1;

19 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw PWM_Freq(freq,duty) //printf("PWM frequency = %d\n", PWM_frequency); //printf("CNR0 = %d, CMR0 = %d\n", CNR0, CMR0); //printf("Duty Cycle = %d\n",PWM_duty); //PWM setting PWMA->CSR.CSR0 = 0; // 0: 1/2, 1: 1/4, 2: 1/8, 3: 1/16, 4: 1 PWMA->PPR.CP01 = PWM_PreScaler; // set PreScaler PWMA->CNR0 = CNR0; // set CNR0 PWMA->CMR0 = CMR0; // set CMR0 }

20 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_PWM_Tone int32_t main (void) { uint8_t i, duty_cycle; uint32_t note_duration; //Enable 12Mhz and set HCLK->12Mhz char TEXT0[16]="PWM Musical Note"; char TEXT1[16]="Tone : "; char TEXT2[16]="Freq : Hz"; char TEXT3[16]="Duty : % "; duty_cycle = 50; // Duty = 50% note_duration = 1000000; // number used for delay loop UNLOCKREG(); SYSCLK->PWRCON.XTL12M_EN = 1; SYSCLK->CLKSEL0.HCLK_S = 0; LOCKREG(); Initial_panel();//call initial pannel function clr_all_panel();

21 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_PWM_Tone print_lcd(0, TEXT0);// print TEXT on LCD print_lcd(1, TEXT1); print_lcd(2, TEXT2); print_lcd(3, TEXT3); InitPWM0(); // initialize PWM0, output pin = GPA12 while(1) { for (i=49; i<61; i++) { PWM0_Freq(Tone_Freq(i),duty_cycle); // set Tone's frequency, duty cycle, duration into PWM sprintf(TEXT1+10,"%d",i); sprintf(TEXT2+10,"%d",Tone_Freq(i)); sprintf(TEXT3+10,"%d",duty_cycle); print_lcd(1, TEXT1); print_lcd(2, TEXT2); print_lcd(3, TEXT3); Delay(note_duration); }

22 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_PWM_Tone_Keypad

23 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_Tone_Keypad – main (1/3) int32_t main (void) { uint8_t i, duty_cycle; uint8_t number; uint16_t tone_freq; //Enable 12Mhz and set HCLK->12Mhz char TEXT0[16]="PWM Musical Note"; char TEXT1[16]="Tone : "; char TEXT2[16]="Freq : Hz"; char TEXT3[16]="Duty : % "; duty_cycle = 50; // Duty = 50% UNLOCKREG(); SYSCLK->PWRCON.XTL12M_EN = 1; SYSCLK->CLKSEL0.HCLK_S = 0; LOCKREG(); Initial_panel();//call initial pannel function clr_all_panel();

24 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_Tone_Keypad – main (2/3) print_lcd(0, TEXT0);// print TEXT on LCD print_lcd(1, TEXT1); print_lcd(2, TEXT2); print_lcd(3, TEXT3); InitPWM0(); // initialize PWM0, output pin = GPA12 OpenKeyPad();// initialize KeyPad while(1) { number = Scankey(); switch(number) { case 1: i = 36; tone_freq = 131; break; case 2: i = 38; tone_freq = 147; break; case 3: i = 40; tone_freq = 165; break; case 4: i = 41; tone_freq = 175; break; case 5: i = 43; tone_freq = 196; break; case 6: i = 45; tone_freq = 220; break; case 7: i = 47; tone_freq = 247; break; case 8: i = 48; tone_freq = 262; break; case 9: i = 50; tone_freq = 294; break; default:i = 0; tone_freq = 0; break; }

25 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_Tone_Keypad – main (1/3) PWM0_Freq(Tone_Freq(i),duty_cycle); // set PWM0 with frequency & duty cycle sprintf(TEXT1+10,"%d",number); sprintf(TEXT2+10,"%d",tone_freq); sprintf(TEXT3+10,"%d",duty_cycle); print_lcd(1, TEXT1); print_lcd(2, TEXT2); print_lcd(3, TEXT3); // Delay(1000000); // delay between each note }

26 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_PWM_Tone_Stereo

27 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_Tone_stereo – main (1/2) int32_t main (void) { uint8_t i; uint8_t note_number = 16; uint8_t duty_cycle = 50; // Duty = 50% uint32_t note_duration = 1500000; // Pachelbel's Canon in D major (two violins) uint8_t volin1[16] = {79,77,75,74, 72,70,72,74, 75,74,72,70, 68,66,68,65}; uint8_t volin2[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 79,77,75,74, 72,70,72,74}; //Enable 12Mhz and set HCLK->12Mhz char TEXT0[16]="PWM Music "; char TEXT1[16]="Canon in D major"; char TEXT2[16]="Volin1: Hz"; char TEXT3[16]="Volin2: Hz"; UNLOCKREG(); SYSCLK->PWRCON.XTL12M_EN = 1; SYSCLK->CLKSEL0.HCLK_S = 0; LOCKREG(); Initial_panel();//call initial pannel function clr_all_panel();

28 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw Smpl_Tone_stereo – main (2/2) print_lcd(0, TEXT0);// print TEXT on LCD print_lcd(1, TEXT1); print_lcd(2, TEXT2); print_lcd(3, TEXT3); InitPWM0(); // initialize PWM0, output pin = GPA12 InitPWM1(); // initialize PWM1, output pin = GPA13 i = 0; while(i < note_number) { PWM0_Freq(Tone_Freq(volin1[i]),duty_cycle); // set PWM0 with frequency & duty cycle PWM1_Freq(Tone_Freq(volin2[i]),duty_cycle); // set PWM1 with frequency & duty cycle sprintf(TEXT2+8,"%d",Tone_Freq(volin1[i])); sprintf(TEXT3+8,"%d",Tone_Freq(volin2[i])); print_lcd(2, TEXT2); print_lcd(3, TEXT3); Delay(note_duration); // delay loop for note duration i++; } PWMA->POE.PWM0=0; // Mute PWM0 PWMA->POE.PWM1=0; // Mute PWM1 }

29 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw General Disclaimer The Lecture is strictly used for educational purpose. MAKES NO GUARANTEE OF VALIDITY ► The lecture cannot guarantee the validity of the information found here. The lecture may recently have been changed, vandalized or altered by someone whose opinion does not correspond with the state of knowledge in the relevant fields. Note that most other encyclopedias and reference works also have similar disclaimers.similar disclaimers No formal peer review ► The lecture is not uniformly peer reviewed; while readers may correct errors or engage in casual peer review, they have no legal duty to do so and thus all information read here is without any implied warranty of fitness for any purpose or use whatsoever. Even articles that have been vetted by informal peer review or featured article processes may later have been edited inappropriately, just before you view them.peer reviewfeatured article No contract; limited license ► Please make sure that you understand that the information provided here is being provided freely, and that no kind of agreement or contract is created between you and the owners or users of this site, the owners of the servers upon which it is housed, the individual Wikipedia contributors, any project administrators, sysops or anyone else who is in any way connected with this project or sister projects subject to your claims against them directly. You are being granted a limited license to copy anything from this site; it does not create or imply any contractual or extracontractual liability on the part of Wikipedia or any of its agents, members, organizers or other users. ► There is no agreement or understanding between you and the content provider regarding your use or modification of this information beyond the Creative Commons Attribution-Sharealike 3.0 Unported License (CC-BY-SA) and the GNU Free Documentation License (GFDL);Creative Commons Attribution-Sharealike 3.0 Unported License GNU Free Documentation License

30 Department of Electrical Engineering, National Taiwan Ocean University www.ee.ntou.edu.tw General Disclaimer Trademarks ► Any of the trademarks, service marks, collective marks, design rights or similar rights that are mentioned, used or cited in the lectures are the property of their respective owners. Their use here does not imply that you may use them for any purpose other than for the same or a similar informational use as contemplated by the original authors under the CC-BY- SA and GFDL licensing schemes. Unless otherwise stated, we are neither endorsed by nor affiliated with any of the holders of any such rights and as such we cannot grant any rights to use any otherwise protected materials. Your use of any such or similar incorporeal property is at your own risk. Personality rights ► The lecture may portray an identifiable person who is alive or deceased recently. The use of images of living or recently deceased individuals is, in some jurisdictions, restricted by laws pertaining to personality rights, independent from their copyright status. Before using these types of content, please ensure that you have the right to use it under the laws which apply in the circumstances of your intended use. You are solely responsible for ensuring that you do not infringe someone else's personality rights.personality rights


Download ppt "Www.ee.ntou.edu.tw Department of Electrical Engineering, National Taiwan Ocean University Pulse Width Modulation 4/25/2013 Richard Kuo Assistant Professor."

Similar presentations


Ads by Google