Pulse Width Modulation

Slides:



Advertisements
Similar presentations
More fun with Timer/Counters
Advertisements

Basic DC Motor Circuits
555 Timer ©Paul Godin Updated February Oscillators ◊We have looked at simple oscillator designs using an inverter, and had a brief look at crystal.
Introduction of Holtek HT-46 series MCU
PWM ON SAMSUNG's S3C2410X. Building Embedded LINUX SYSTEM Outline SAMSUNG's S3C2410X components CLOCK & POWER MANAGEMENT PWM TIMER I/O PORTS Finally Project.
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.
HT46 A/D Type MCU Series Data Memory (Byte) Program Memory HT46R22 (OTP) HT46C22 (Mask) 2Kx Kx16 4Kx HT46R23 (OTP) HT46C23 (Mask) HT46R24.
Timers and Interrupts Shivendu Bhushan Summer Camp ‘13.
1 ECE 263 Embedded System Design Lessons 2, 3 68HC12 Hardware Overview, Subsystems, and memory System.
Department of Electrical Engineering Southern Taiwan University of Science and Technology Robot and Servo Drive Lab. 2015/7/2 Digital Control Strategy.
Engineering 1040: Mechanisms & Electric Circuits Fall 2011 Introduction to Embedded Systems.
16-Bit Timer/Counter 1 and 3 Counter/Timer 1,3 (TCNT1, TCNT3) are identical in function. Three separate comparison registers exist. Thus, three separate.
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.
8254 Programmable Interval Timer
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)
2/26/2015 Richard Kuo Assistant Professor
ARM Timers.
September, 2007ENGR Low-Level Robot Control Mechatronics: Motors, sensors & embedded controls.
System Clocks.
PWM ECE 4437-EMBEDDED SYSTEMS DR. GLOVER CHRISTOPHER AHN & KHOA VAN.
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.
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 Motor Control 5/2/2013 Richard Kuo Assistant Professor.
AT91 Embedded Peripherals
Department of Electrical Engineering, National Taiwan Ocean University Analog-to-Digital Converter (ADC) 4/18/2013 Richard Kuo Assistant.
UART: Universal Asynchronous RX/TX
Department of Electrical Engineering, National Taiwan Ocean University Appendix – NUC140 pin assignment 9/29/2012 Richard Kuo Assistant.
Cortex-M0 MCU Interface and Driver Design - Course Overview
PWM UNIT 17 로봇 SW 교육원 조용수. 학습 목표 PWM PWM Register 2.
ELE22MIC Lecture 18 The AVR Sleep Modes The ATMEGA128’s Timer System
Microprocessoren lesson 6. PWM /motor control/servo.
CCP MODULES  The CCP module (Capture/Compare/PWM) is a peripheral which allows the user to time and control different events.  Capture Mode provides.
8114A Overview. 8114A Overview 10-Feb-04 Page A Overview 1) Specifications and Applications 2) Operational Overview 3) Block Diagram.
1 68HC11 Timer Chapter HC11 Timer Subsystem Several timing functions: Basic timing Basic timing Real time interrupts Real time interrupts Output.
3/23/2015 Richard Kuo Assistant Professor
Example 12 Pulse-Width Modulation (PWM): Motors and Servos Lecture L8.1.
Serial Peripheral Interface
Automatic accident avoiding system PROJECT MEMBERS MUTHUKUMAR.K (05ME33) SAKTHIDHASAN.S (05ME39) SAKTHIVEL.N (05ME40) VINOTH.S (05ME56) PROJECT GUIDE:
CS224 I/O. Timers Timer1 is 16-bit timer 2^16-1 or 65,535 SYSCLK = 40MHz, Prescalers – 1:1, 1:8, 1:64, and 1:256 – Use T1CONbits.TCKPS=3 for 1:256 scaling.
The Cortex-M3 Embedded Systems: LM3S9B96 Microcontroller – Pulse Width Modulator (PWM) Refer to Chapter 21 in the reference book “Stellaris® LM3S9B96 Microcontroller.
8254 Timer.
1 68HC11 Timer HC11 or HC12: Chapter HC11 Timer Subsystem  Several timing functions: Basic timing Basic timing Real time interrupts Real time.
DsPIC30F4011 Fall DIP Switches  The upper four switches of SW1 are used to enable LEDs connected to PORTB/C, PORTA/D, PORTE and PORTF. For example,
Network and Systems Laboratory nslab.ee.ntu.edu.tw.
PWM Test 2 UNIT 20 로봇 SW 교육원 조용수. 학습 목표 PWM Servo 제어 2.
CSE466 - Fall What is an Embedded System  Its not a desktop system  Fixed or semi-fixed functionality (not user programmable)  Lacks some or.
#1 of 10 Tutorial Introduction PURPOSE -To explain how to configure and use the Timer Interface Module in common applications OBJECTIVES: -Identify the.
[Lab10] Output PWM  Output PWM Signal  Example  Exercise(Optional)
Module 8 Tutorial  An 8086 system is used for controlling the speed of a motor. The motor can operate at 5 different speeds (1- 5).  The speed.
PWM Test UNIT 20 로봇 SW 교육원 조용수. 학습 목표 PWM Init PWM LED 제어 2.
Application Case Study Christmas Lights Controller
Why are Timer Functions Important?
Serial Peripheral Interface
Application Case Study Security Camera Controller
V.V.P. ENGINEERING COLLEGE,RAJKOT
Outline Analog to digital conversion (ADC) of NuMaker TRIO
Pulse Width Modulation
Cortex-M0 MCU Clocks & Pins
Analog-to-Digital Converter (ADC)
NuMicro MCU New Project Express Creation
CS4101 Introduction to Embedded Systems Lab 9: NuMaker ADC and LCD
AVR Addressing Modes Subject: Microcontoller & Interfacing
UNIT 19 PWM 로봇 SW 교육원 조용수.
Timer/Counter Timer/Counter 0 Timer/Counter 1 Timer/Counter 2 8 bit
Prof. Chung-Ta King Department of Computer Science
Robotics System Lecture 11_12: DC Motor
Presentation transcript:

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

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

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

Pulse Width Modulation http://commons.wikimedia.org/wiki/File:Pwm.png

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

NUC140 PWM clock source selection HIRC HCLK LXT HXT

Nano102 PWM clock source selection

PWM Block Diagram

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)

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

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(32000000); // 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 }

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

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

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 = (5 - 0.2) / (100+8) = 44.4mA

smpl_PWM_Music

Note_Freq.h //note_freq.h #define NULL 0 // null = 0Hz #define C0 16 // C0 = 16.35Hz #define C0u 17 // C0#/D0b = 17.32Hz #define D0b 17 #define D0 18 // D0 = 18.35Hz #define D0u 19 // D0#/E0b = 19.45Hz #define E0b 19 #define E0 21 // E0 = 20.60Hz #define F0 22 // F0 = 21.83Hz #define F0u 23 // F0#/G0b = 23.12Hz #define G0b 23 #define G0 24 // G0 = 24.50Hz #define G0u 26 // G0#/A0b = 25.96Hz #define A0b 26 #define A0 27 // A0 = 27.50Hz #define A0u 29 // A0#/B0b = 29.14Hz #define B0b 29 #define B0 31 // B0 = 30.87Hz #define C1 33 // C1 = 32.70Hz #define C1u 35 // C1#/D1b = 34.65Hz #define D1b 35 #define D1 37 // D1 = 36.71Hz #define D1u 39 // D1#/E1b = 38.89Hz

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 125000 #define P250ms 250000 #define P500ms 500000 #define P1S 1000000

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(32000000); // 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 }

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

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]); }

SG5010

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

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

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

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(50000000); // 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(); }

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); }

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

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

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(50000000); // 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 }

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);

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

smpl_PWM_DeadZone

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); }

PWM Capture timing diagram

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

Home Projects 伺服機控制 https://youtu.be/qJVVuDI2_ng 自製水族自動餵食器 https://youtu.be/MUo_31xR45E 寵物飼料自動餵食器 https://youtu.be/sHUtfkwyOi4 兩足機器人 https://youtu.be/O3vZtCXRAKE 六足機器人 https://youtu.be/GDaBIaSpDl0 低週波電療機電路原理http://www.shs.edu.tw/works/essay/2010/11/2010111215341386.pdf 超音波指向性喇叭 Ultrasonic Directive Speaker

Ultrasonic Directive Speaker

Ultrasonic Directive Speaker

Ultrasonic Directive Speaker

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.