Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pulse Width Modulation (PWM)

Similar presentations


Presentation on theme: "Pulse Width Modulation (PWM)"— Presentation transcript:

1 Pulse Width Modulation (PWM)
Simple method of using rectangular digital waveform to control an analog variable Used in a variety of applications, from communications to automatic control Usually keep period constant, and vary the pulse width, or “on” time The duty cycle is the proportion of time that the pulse is “on”, expressed as a percentage

2 Pulse Width Modulation (PWM)
The average value of the signal will be determined by the duty cycle By controlling the duty cycle, can control the average value If “on” time is small, average value is low; if “on” time is large, average value is higher The average value can be extracted from the PWM signal with a low pass filter – then Vout is an analog output

3 Pulse Width Modulation (PWM)
On the mbed, there are 6 PWM outputs, on pins 21-26 In the lab, you generated various PWM signals. 20% duty cycle 80% duty cycle Can do quite a few other things with PWM………..

4 Pulse Width Modulation (PWM)
Controlling LED brightness by changing PWM duty cycle, controlled from host computer keyboard – also shows the use of host terminal emulator. #include "mbed.h" Serial pc(USBTX, USBRX); //sets up communication with PC PwmOut led(LED1); float brightness=0.0; int pc.printf("Control LED brightness from PC \n\r"); main(){ pc.printf("Press 'u' or 'd' to change brightness while(1){ char c=pc.getc(); wait(0.001); if((c=='u') && (brightness <=1.0)){ brightness+=0.05; led=brightness; } if((c=='d') && (brightness>0)){ brightness-=0.05; led=brightness; pc.printf("%c %1.3f \n \r",c,brightness); \n\r"); }

5 Pulse Width Modulation (PWM)
Controlling stepper motor from a potentiometer Vs #include "mbed.h” AnalogIn A2(p20); float period; float duty; PwmOut PWM(p21); “Freewheeling” Motor coil int main(){ while(1) { period duty = diode Inductance L - cannot get + Resistance R = 0.01; instant change (period * A2); PWM.period(period); PWM.pulsewidth(duty); wait(0.1); in current thru L, so when transistor switches off, current } } has somewhere to go PWM output from mbed MOSFET essentially a switch MOSFET – Metal-Oxide-Semiconductor Field Effect Transistor

6 Pulse Width Modulation (PWM)
Driving a speaker Setting duty cycle to 50%, results in a square wave whose frequency can be varied by changing the PWM period You will learn later that any periodic waveform can be constructed from a combination of sinusoidal signals – Fourier Series Square wave with frequency fo can be synthesised from sinusoidal signals at frequencies nfo, where n is an odd integer (ie n=1,3,5 etc) ie a square wave of frequency fo “contains” sin waves at frequencies fo, 3fo, 5fo etc

7 Pulse Width Modulation (PWM)
Higher “harmonics” of the “fundamental” frequency f0 have lower amplitude, and as a speaker has resistance and capacitance which make it look a bit like a low pass filter, the higher frequency, lower amplitude harmonics are attenuated and so the speaker is effectively driven by something approaching a sin wave at the PWM frequency. By changing the PWM period (and hence frequency) and duration of the PWM signal, we can make sweet(ish) music !

8 Pulse Width Modulation (PWM)
Could of course, just generate PWM from a digital output by switching it on and off, and changing the time for which the digital signal is “high” or “low”, but that would take up the time and effort of the CPU. The advantage of handling over responsibility of the PWM signal generation to the specific peripheral in the microcontroller chip, is that the CPU is then available to do other stuff. This is one of the powerful aspects of a highly functionalised microcontroller – devolving responsibility for things low PWM, D/A and A/D to the peripherals frees up the time of the CPU.

9 Serial Communication As mentioned before, the mbed has a lot of serial communications capability SPI; I2C, CAN, USB For communicating externally and to control other hardware Lets take a tour around SPI to get a feel for how these types of protocol work. Further details can be found on the mbed website. Serial Peripheral Interface (SPI) bus operates with a master/slave configuration SPI is a synchronous serial data communication protocol

10 Clock Bit 0 Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 Bit 7 Serial Data Data is synchronised to the clock When there is no data being sent, there is no signal on the clock line Every time the clock pulses, one bit is output by the transmitter and should be read by the receiver The receiver synchronises its reading of the data with an edge of the clock. In the picture above, this is to the rising edge. Parallel Read/write Parallel Read/write SDO SDI SDO – serial data out SDI – serial data in SCLK – serial clock Shift Register Shift Register SDI SDO msb lsb msb lsb SCLK SCLK Clock Master Slave

11 Each SDO is connected to the lsb of the other shift register (SDI)
Parallel Read/write Parallel Read/write SDO SDI SDO – serial data out SDI – serial data in SCLK – serial clock Shift Register Shift Register SDI SDO msb lsb msb lsb SCLK SCLK Clock Master Slave Assume the shift registers in the Master and Slave are 8-bit – each has 8 D-type flip-flops Each register is loaded with a new word via the parallel I/O lines The Master then produces 8 clock pulses. For each clock pulse, data is output from the msb of each register (SDO) Each SDO is connected to the lsb of the other shift register (SDI) As each bit is clocked out of one register, it is clocked into the other. After 8 clock cycles, the word that was in the master shift register as been transferred to the slave shift register, and vice versa.

12 The SPI protocol uses 4 signals clock (SCLK)
master data output/slave data input (MOSI) master data input/slave data output (MISO) slave chip select (CS) – usually active low When multiple slave devices exist, the master must output a unique CS signal for each slave. MOSI

13 SPI The master device starts an SPI data transfer by doing the following : Configures to SPI clock (SCLK) to be a frequency that the recipient slave device can handle (max is 70 MHz) Sets the chip select line (CS) of the intended recipient to 0 V Starts the clock pulses on SCLK to indicate that that data is to be transferred Simultaneously sends data as consecutive bits on MOSI Number of bits in each data frame can be configured, but is usually between 4 and 16 bits The slave returns data in the same manner on MISO

14 SPI /* Sets up the a single byte mbed as SPI master, and continuously
sends */ #include "mbed.h" SPI ser_port(p11, p12, p13); // mosi, miso, sclk char switch_word ; //word we will send int main() { ser_port.format(8,0); Mode 0 operation ser_port.frequency( ); while (1){ switch_word=0xA1; // Setup the SPI for 8 bit // Clock frequency is 1MHz data, //set up word to be transmitted //send switch_word ser_port.write(switch_word); wait_us(50); }

15 CLK DATA 1 1 1 A 1


Download ppt "Pulse Width Modulation (PWM)"

Similar presentations


Ads by Google