You are on page 1of 4

Embedded Systems IE403

Lab Work

Lab #8

Objective:
To Control the Brightness of LED using PWM technique with Timers

Theory
To control the brightness of an LED you can vary the power which is sent to the LED, for example using a potentiometer (variable resistor), the more power the LED receives the brighter it is, the less power it receives the dimmer it is. Microcontrollers are digital, meaning they only have two power states, on and off. Although it is possible to supply a varying power from a microcontroller (using a Digital to Analogue Converter (DAC)) this usually requires an additional chip. PWM provides the ability to simulate varying levels of power by oscillating the output from the microcontroller. If, over a short duration of time, we turn the LED on for 50% and off for 50%, the LED will appear half as bright since the total light output over the time duration is only half as much as 100% on. This is illustrated by PWM waveform shown in figure 7.1. By varying (or modulating) the pulsing width we can effectively control the light output from the LED, hence the term PWM or Pulse Width Modulation. Minimum Speed of LED Oscillation When using PWM its important to consider how slowly we can flash the LED so that the viewer does not perceive the oscillation. The eyes inability to see rapid oscillations of light is caused by our persistence of vision which means, in very simple terms, we see the light as on even after it has turned off. This technique is how televisions display a seemingly Figure 8.1: PWM Wave with 50% duty-cycle moving picture which is actually made up of a number of different still frames displayed one after the other very rapidly. The minimum speed of an LED oscillating which can be seen by the human eye varies from person to person. Duty-Cycle and PWM Resolution Duty-cycle refers to the percentage of on-time with respect to the total period of the PWM cycle. The greater the PWM, the greater is the average voltage and consequently a brighter LED results. The accuracy with which we can control the duty-cycle is known as the PWM resolution. The higher our PWM resolution is, the more levels of brightness we can display. The faster the microcontroller, the smaller durations it can time. Thus, the higher the resolution, the more timing accuracy and processing overhead is required. Advantages and Disadvantages of PWM Led Control The obvious advantages are no power dissipation in controlling, eliminates the need of DAC chips, very accurate and flexible, inexpensive method. However, PWM can be a source of radio frequency interference (RFI) on the global circuit, because PWM is a periodic signal.

Group Members:

Ali Asad (1936)

Safdar Abbasi (1926)

Embedded Systems IE403

Lab Work

Lab #8

Flow Chart

Group Members:

Ali Asad (1936)

Safdar Abbasi (1926)

Embedded Systems IE403

Lab Work

Lab #8

Source Code
LOC OBJ LINE 1 2 3 Period of PWM Wave 0000 0000 0000 758911 timer) 0003 78FF Timer-0 0005 79FF Timer-0 0007 0007 7D0A cycle 0009 0009 758B66 000C 758DFC 000F 888A 0011 898C 0013 D290 0015 D28E 0017 D28C 0019 0019 308DFD 001C C28C 001E C28D 0020 C290 0022 0022 308FFD 0025 C28E 0027 C28F 0029 DDDE 002B 18 002C E8 002D B4FF01 0030 19 0031 0031 E9 0032 B4FCD2 timer-1 become equal 0035 E8 0036 B466CE 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 0039 78FF 003B 79FF 003D 80C8 42 43 ORG 0000H MAIN: MOV TMOD,#11H MOV R0,#0FFH MOV R1,#0FFH REPEAT: MOV R5,#10 ; Timer-0 and Timer-1 in Mode-1(16-bit ; This Variable holds low-byte(TL0) of ; This Variable holds high-byte(TH0) of ; Repeats wave-form for current dutySOURCE ;PWM Generation using timers ;Timer-0 is a VARIABLE duration timer corresponding to ;Timer-1 is a FIXED duration timer corresponding to

ON-Time

LOOP: MOV TL1,#66H ; Low-byte of Timer-1 MOV TH1,#0FCH ; High-byte of Timer-1, [FC66H = 1 ms] MOV TL0,R0 ; Update low-byte of timer-0 MOV TH0,R1 ; Update high-byte of timer-1 SETB P1.0 ; Pulse High SETB TR1 ; Start Timer-1 SETB TR0 ; Start Timer-0 WAIT_ON: JNB TF0,WAIT_ON ; Wait here until timer-0 overflows CLR TR0 ; Stop timer-0 CLR TF0 ; Clear timer-0 overflow flag CLR P1.0 ; Pulse Low WAIT_OFF: JNB TF1,WAIT_OFF ; Wait here until timer-1 overflows CLR TR1 ; Stop timer-1 CLR TF1 ; Clear timer-1 overflow flag DJNZ R5,LOOP ; Repeat PWM-wave of current duty-cycle DEC R0 ; Increase on-time by one time-unit MOV A,R0 ; Prepare for CJNE instruction CJNE A,#0FFH,CONT ; If R0 = FFH DEC R1 ; then decrease R1 CONT: MOV A,R1 ; Prepare for CJNE instruction CJNE A,#0FCH,REPEAT ; If high-byte of timer-0 and MOV A,R0 ; Prepare for CJNE instruction CJNE A,#066H,REPEAT ; also if low-byte of timer-0 and timer-1 become equal ; then On-time has become equal to total period (100%) MOV R0,#0FFH MOV R1,#0FFH ; Reset duty-cycle back to 1% ; by resetting timer-0 update variables R0 and R1 ; Repeat this program forever

44 SJMP REPEAT 45 END Code 8.1: ASM program listing for LED brightness control

Group Members:

Ali Asad (1936)

Safdar Abbasi (1926)

Embedded Systems IE403

Lab Work

Lab #8

Simulations
20% Duty-Cycle:

Figure 8.2: PWM Wave with 10% duty-cycle

50% Duty-Cycle:

Figure 8.3: PWM Wave with 50% duty-cycle

80% Duty-Cycle:

Figure 8.4: PWM Wave with 80% duty-cycle

Conclusion:
PWM wave-form of varying duty-cycle was genererated successfully using 8051 timers. Observations of PWM wave-forms were taken oscilloscope and the results were found to very accurate as compared to previous PWM generation technique using loops.

Group Members:

Ali Asad (1936)

Safdar Abbasi (1926)