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

Slides:



Advertisements
Similar presentations
ECE/CS-352: Embedded Microcontroller Systems The Silicon Laboratories C8051F020 Enhanced 8051 Part 5 Other Subsystems.
Advertisements

More fun with Timer/Counters
4-1 Timers Timers can be used for  timing  event counting  pulse width measurement  pulse generation  frequency multiplication There are 8 Timers.
555 Timer ©Paul Godin Updated February Oscillators ◊We have looked at simple oscillator designs using an inverter, and had a brief look at crystal.
1 ADC – Analog to Digital Converter and PWM Waveform Generation with EZDSP2812 Professor: Chen, Pei-Chung 陳 沛 仲 Student: Thanh-Nhan Nguyen 阮 誠 仁 2008,
Introduction of Holtek HT-46 series MCU
Infineon Technologies Corp. June 99 1 HOT167-1 Version 2.0 F PWMmax = x 50ns = 78 kHz Pulse Width Modulation Unit (PWM) (20 MHz)  4 independent.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
Digital I/O Connecting to the Outside World
1 Timing System Timing System Applications. 2 Timing System components Counting mechanisms Input capture mechanisms Output capture mechanisms.
Department of Electrical Engineering, National Taiwan Ocean University NuMicro Learning Board Introduction 1/29/2015 Richard Kuo Assistant.
Pulse Width Modulation
8254 Programmable Interval Timer
ENEE 440 Chapter Timer 8254 Register Select The 8254 timer is actually 3 timers in one. It is an upgraded version of the 8253 timer which was.
7/23 Timers in Coldfire Processor Computer Science & Engineering Department Arizona State University Tempe, AZ Dr. Yann-Hang Lee (480)
Lecture 29: LM3S9B96 Microcontroller – Pulse Width Modulator (PWM)
PIT: Programmable Interval Timer
2/26/2015 Richard Kuo Assistant Professor
ARM Timers.
System Clocks.
Universal Asynchronous RX/TX
M Semiconductor Products Sector Computer Operating Properly Module Detail Slide #1 of 7 Tutorial Introduction PURPOSE -To explain how to configure and.
Department of Electrical Engineering, National Taiwan Ocean University NuMicro MCU Learning Board SDK Installation 3/7/2013 Richard.
16F877A. Timer 0 The Timer0 module timer/counter has the following features: –8-bit timer/counter –Readable and writable –8-bit software programmable.
Timers ELEC 330 Digital Systems Engineering Dr. Ron Hayne
Unit 10.2 Timer Examples. Example – Music Generation Channel 6 – Set up as a timer Output to Generate Square Waves Channel 4 – Set up as a timer Output.
Timer Peripherals.
Department of Electrical Engineering, National Taiwan Ocean University Motor Control 5/2/2013 Richard Kuo Assistant Professor.
Department of Electrical Engineering, National Taiwan Ocean University C Programming 3/14/2013 Richard Kuo Assistant Professor.
Department of Electrical Engineering, National Taiwan Ocean University Analog-to-Digital Converter (ADC) 4/18/2013 Richard Kuo Assistant.
Department of Electrical Engineering, National Taiwan Ocean University LCD Display 3/28/2013 Richard Kuo Assistant Professor.
UART: Universal Asynchronous RX/TX
Cortex-M0 MCU Interface and Driver Design - Course Overview
Department of Electrical Engineering, National Taiwan Ocean University Basic Electronics 3/7/2013 Richard Kuo Assistant Professor.
Lecture 11: TI MSP430 Timers Compare Modes
PWM UNIT 17 로봇 SW 교육원 조용수. 학습 목표 PWM PWM Register 2.
Department of Electrical Engineering, National Taiwan Ocean University ARM Cortex-M0 Introduction 3/1/2012 Richard Kuo Assistant Professor.
Department of Electrical Engineering, National Taiwan Ocean University Universal Serial Bus & SD card 5/30/2013 Richard Kuo Assistant.
Charles Kime & Thomas Kaminski © 2004 Pearson Education, Inc. Terms of Use (Hyperlinks are active in View Show mode) Terms of Use ECE/CS 352: Digital Systems.
© 2009, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction  Purpose This course provides an introduction to the peripheral functions.
Department of Electrical Engineering, National Taiwan Ocean University Timer/WatchDog /RTC 4/11/2013 Richard Kuo Assistant Professor.
ELE22MIC Lecture 18 The AVR Sleep Modes The ATMEGA128’s Timer System
CCP MODULES  The CCP module (Capture/Compare/PWM) is a peripheral which allows the user to time and control different events.  Capture Mode provides.
1 68HC11 Timer Chapter HC11 Timer Subsystem Several timing functions: Basic timing Basic timing Real time interrupts Real time interrupts Output.
© 2008, Renesas Technology America, Inc., All Rights Reserved 1 Module Introduction Purpose  This training module provides an overview of the analog interfaces.
Lecture 12 Multi-Function Timer Pulse Unit 2 (MTU2a)
Embedded Systems Design: A Unified Hardware/Software Introduction 1 Chapter 4 Standard Single Purpose Processors: Peripherals.
Serial Peripheral Interface
Oracle Fusion Applications 11gR1 ( ) Functional Overview (L2) Manage Inbound Logistics (L3) Manage Supplier Returns.
© 2009, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction  Purpose This course provides an introduction to the peripheral functions.
General Purpose Input/Output
Chapter 12 Some PIC Microcontroller Advances The aims of this chapter are to introduce: To introduce in overview two microcontrollers which show some enhanced.
© 2008, Renesas Technology America, Inc., All Rights Reserved 1 Course Introduction Purpose  This course provides an introduction to the peripheral functions.
#1 of 10 Tutorial Introduction PURPOSE -To explain how to configure and use the Timer Interface Module in common applications OBJECTIVES: -Identify the.
Department of Electrical Engineering, National Taiwan Ocean University I2C Bus 5/16/2013 Richard Kuo Assistant Professor.
ADC 1 Analog to Digital Converter. ADC 2 ADC Features n General Features -Supports 8 or 10-bit resolution Modes, -Track period fully programmable up to.
Pulse-Width Modulation: Simulating variable DC output
Application Case Study Christmas Lights Controller
16F877A.
Why are Timer Functions Important?
Connectivity to bank and sample account structure
V.V.P. ENGINEERING COLLEGE,RAJKOT
Pulse Width Modulation
Cortex-M0 MCU Clocks & Pins
Programmable Interval timer 8253 / 8254
Programmable Interval timer 8253 / 8254
UNIT 19 PWM 로봇 SW 교육원 조용수.
Digital Fundamentals Floyd Chapter 1 Tenth Edition
Lecture 12 Multi-Function Timer Pulse Unit 2 (MTU2a)
Presentation transcript:

Department of Electrical Engineering, National Taiwan Ocean University Pulse Width Modulation 4/25/2013 Richard Kuo Assistant Professor

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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.

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University PWM clock source selection

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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

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

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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).

Department of Electrical Engineering, National Taiwan Ocean University 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 = ; 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);

Department of Electrical Engineering, National Taiwan Ocean University 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) g_u8PWMCount PWM output

Department of Electrical Engineering, National Taiwan Ocean University 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 = ( ) / (100+8) = 44.4mA

Department of Electrical Engineering, National Taiwan Ocean University Smpl_PWM_Tone

Department of Electrical Engineering, National Taiwan Ocean University 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 }

Department of Electrical Engineering, National Taiwan Ocean University 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 = ; // Clock source = MHz 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 / ;

Department of Electrical Engineering, National Taiwan Ocean University 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 }

Department of Electrical Engineering, National Taiwan Ocean University 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 = ; // 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();

Department of Electrical Engineering, National Taiwan Ocean University 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); }

Department of Electrical Engineering, National Taiwan Ocean University Smpl_PWM_Tone_Keypad

Department of Electrical Engineering, National Taiwan Ocean University 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();

Department of Electrical Engineering, National Taiwan Ocean University 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; }

Department of Electrical Engineering, National Taiwan Ocean University 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( ); // delay between each note }

Department of Electrical Engineering, National Taiwan Ocean University Smpl_PWM_Tone_Stereo

Department of Electrical Engineering, National Taiwan Ocean University 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 = ; // 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();

Department of Electrical Engineering, National Taiwan Ocean University 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 }

Department of Electrical Engineering, National Taiwan Ocean University 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

Department of Electrical Engineering, National Taiwan Ocean University 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