Lecture 16: Digital to Analog Converter - PWM Implementation Lecturers: Professor John Devlin Mr Robert Ross
Overview Implementation of a DAC using PWM Class Demonstration Worked example – Software Loops Worked example – Timer Based
DAC PWM Implementation Choose an operating frequency (this specifies the period of the DAC) Create a filter circuit to operate at the desired frequency Write software to vary on/off time When on – drive output high When off – drive output low
PWM software loop implementation PWM can be generated in software simply by using loops This is generally bad design (timers use less CPU resources and are more accurate) A useful stepping stone to understanding DAC’s
PWM software loop implementation MOV #100, R5 ; Period MOV #50, R6 ; ON Time setup MOV #0, R7 ; Comparison register BIS.b #000000001b, &P1OUT ; bit 1 = high on_time INC R7 CMP R6,R7 ; has on time expired JNE on_time BIC.b #00000001b, &P1OUT ;on time expired, bit 1 = low off_time CMP R5, R7 ; has off time expired JNE off_time JMP setup ; off time has expired
Timer based PWM implementation Timers can be used to perform PWM with less CPU overhead – allowing processing for other tasks Design the following PWM timer: Period = 16.38ms F = 61kHz OFF Time climbs from 0 to 16.38ms and repeats from 0 Timers will be covered in detail in lecture 19
Timer based PWM implementation SetupP1 BIS.B #0x0f,&P1DIR ; Set P1.0-P1.7 as outputs BIS.B #00010100b,&P1SEL ; P1.2 and P1.4 TA/SMCLK options SetupP2 BIS.B #11000000b,&P2DIR ; Set P2.6 and P2.7 as outputs Set_clock ; Set to calibrated 1MHz Clock MOV.B &CALBC1_1MHZ,&BCSCTL1 ; Set range; DCO = 1 MHz MOV.B &CALDCO_1MHZ,&DCOCTL ; Set DCO step + modulation setup_timer MOV.W #02000h,&TACCR0 ; CCR0 = PWM Period/2 MOV.W #0,&TACCR1 ; CCR1 = PWM_OFF_Time/2 MOV.W #00C0h,&TACCTL1 ; Output=Toggle/Set MOV.W #0230h, &TACTL ; CLK = SMCLK(1MHz), MODE = ; UP/DOWN
Timer based PWM implementation MOV.W #0, R5 ; Initialise OFF_Time reset_countdown MOV.W #0FFh, R4 ; Initialise countdown main DEC R4 ; Decrement countdown JNZ main ; If countdown != 0, loop INC R5 ; Increment the OFF_time CMP &TACCR0, R5 ; Does OFF_Time = Period? JEQ reset_r5 ; If OFF_Time = Period, jump MOV.W R5,&TACCR1 ; Load OFF_Time into register JMP reset_countdown ; Loop again reset_r5 MOV.W #0, R5 ; Reset OFF time
Circuit Diagram Add a low pass filter to reduce switching noise and return an analog signal C should be small – otherwise DAC response will be low MSP430 R = 330k Analog Signal P1.2 C=100nF
Class Demonstration – Timer PWM Without filtering circuitry With filtering circuitry:
Summary Digital to Analog converters allow digital electronics to output signals which are similar to real world continuous signals Pulse Width Modulation is a simple and widely used form of DAC