[SOLVED]pwm with 8051(confused)

Status
Not open for further replies.

ucsam

hello everyone!
i know that many has already posted posts on PWM control but i am still confused about the THING!
What does PWM depend upon? i know that PWM is obtained when we have delay between input and output..but can't we that simply by:

void pwm(unsigned char value)
{
out=0;
delay(value)
out=1;
}

:?:

why do we have to use timer? i know that timer is meant to create some frequency to turn on and off the port, but once we keep some delay (from the above code) don't we automatically get frequency(as there is delay between on and off)? :shock:

I went through the following site:
Pulse Width Modulation (PWM) tutorial: 8051 example : 8051 Microcontroller Projects AVR PIC Projects Tutorials Ebooks Libraries codes

but could not get some parts:

1.What is the use of "pwm_width" in the code?
2.How do i vary the pwm signals in it?
3.Does changing the frequency vary the PWM?
4.How can i use it more easily?

Thanks

sankar.m8

PWM is an effective tool to control DC motor speed, controlling the position of servo motor even the LED brightness control.
While giving a continuous signal(DC) to an LED, u wil get a full brightness....but if u giv the pulses, the LED will be ON for a particular part of the signal....In the overall view, the brightness will be reduced. The width of the PWM signal is nothing but the time duration at which the LED is suppose to be ON.

For example, you have a 50Hz signal (20ms time period). If you make a PWM signal with 5ms duty-cycle(ON time), the LED will On for 5ms and OFF for 15ms...this will continues...
So if you increase the dutycyle, you can increase the ON time of the LED.

Like this you can control the speed of a DC motor.

Remember, while you making an LED brightness control, you should make the pulses with more than 100Hz. else, the flickering can be seen.

Frequency will be the same for a PWM signal...only the dutycycle (ON time of the pulse) will change.

you can make a simple PWM signal using delays...

i will tell you to interface a servo motor(180degree)....it usually needs 50Hz signals.....If ur PWM signal having 1ms dutycycle, the shaft of the servo will align to left end.
If dutycycle is 1.5ms, it will align in the center ...and for 2ms...the right end........so you can control the position of the shaft by giving the PWM with dutycycle 1ms(5%) to 2ms (10%).

to move a servo shaft to left end,

loop:
bsf servo
call 1msdelay
bcf servo
call 19msdelay
goto loop

(It is in ASM code...servo is the command pin to the servo motor)

Points: 2

Jigar 4 Electronics

Points: 2

ucsam

thank you for your kind reply. I still have a query, If i want to have PWM from a particular port then,how can i put on all together all these data and combine it to form a single function, can you please guide me to a algorithm for PWM control? Please help me with the codes understanding.

( I mostly use C but i can read assembly as well, But i prefer C)

sankar.m8

If suppose, you want to get a PWM signal,50Hz(20ms)

init:
movlw B'11111111'
movwf TRISB (Make all the port B are inputs)
return

pwm:
movlw H'FF'
movwf PORTB
call 1msdelay
movlw H'00'
movwf PORTB
goto pwm

main:
call init

if SWITCH1 is pressed,
movlw B'11111110'
movwf TRISB
call pwm (PORTB,0 will get PWM signal)

if SW2 is pressed,
movlw B'11111101'
movwf TRISB
call pwm (PORTB,1 will get PWM signal)

etc

you can also customize ur PWM duty cycle by getting one (delay) with a keypad and calculating (20-delay) for the other

ucsam

i use 8051 and and 8051 does not have instructions like movlw and movwf..can u please just provide the C code?

thanks

sankar.m8

Sorry i am not well with c code....atleast you got the idea right?.....just fed the pwm output to all the ports and select the required port by making it output....(using TRIS register)

ucsam

nope! i cannot read that assembly codes if u can please provide me the link where i can see the algorithm of PWM

thanks

hongphuc118

Member level 2
You should use timer because while timer is running,8051 can run another function.
But you use delay.8051 can't do anything.

yashwanth450

Newbie level 4
i use 8051 and and 8051 does not have instructions like movlw and movwf..can u please just provide the C code?

thanks

hai here iam sending pwm control dc motor code in assembly this may help you much

Code ASM - [expand]1
2
3
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
42
43
44
org 0000h
sjmp start
org 0050h
start:
mov a,#00h
setb p0.0
x:jnb p1.0,is1
jnb p1.1,ds2
cjne a,#00h,x11

sjmp x

x11:   clr p0.0
mov r2,a

x1: lcall delay
djnz r2,x1

setb p0.0
mov r1,a
mov a,#64h
subb a,r1  ; a = a - r1
mov r2,a   ; result into r2 reg
mov a,r1 ; a old value from r1

x12:
lcall delay
djnz r2,x12
sjmp x
is1:
loop: jnb p1.0,loop
sjmp x11
ds2:
subb a,#10h
loop1: jnb p1.1,loop1
sjmp x11

delay:
mov r3,#0ffh
q:djnz r3,q
ret
end

Last edited by a moderator:

ucsam

thanks yashwanth450 i will try to study it :grin:

shamito88

Newbie level 6
if to control the dc motor using pwm... which port we should use.? or it just any output can be use? if i am using l293d ic for an example... which port should i program to give an ouput of pwm towards the dc motor?

hongphuc118

Member level 2
You can uses all port.
But I often use port P1 or P2

raja555

Junior Member level 1
if i want to use 12v dc motor by using a single key with changing its speed in 4 different speed like,slow,medium,fast,very fast throw 8051. how is it possible???????? plz reply me sir

internetuser2k12

Banned
With switch you can use a counter. On every press of the button the counter is incremented. depending upon the value of the counter the pwm duty can be varied.

If counter = 0 then pwm = off
If counter = 1 then pwm = slow
If counter = 2 then pwm = medium
If counter = 3 then pwm = fast
If counter = 4 then pwm = very fast

again on clicking the button if counter = 4 then counter = 0

raja555

Junior Member level 1
i want to use normally the delay program not counter or timer and interrupt also ,,,,,so tell me the delay program how i use in it in proper way which it will work properly.tell me the normal way and give me the assembly code for it,,for which i can study it properly.....plz its urgent sir

thank you sir

internetuser2k12

Banned
If the program is executing a delay it cannot do any other work. I don't know asm. Can you explain how a delay can be used to vary the speed of a motor?

raja555

Junior Member level 1
i think it will work but i m nor sure....so plz you tell me about the pwm method and its initialisation..

internetuser2k12

Banned
There is no initialization in pwm. you have to generate pulses of different widths. If the on time of the pulse is 25ms and off time is 75 ms it produces a pwm of 25%. For motors the switching speed of pulses should be in tens of kilohertz. If the on time of the pulse is more the speed is more, if the on time of the pulses is less the speed is less. you have to generate 3 kinds of pulses. 0 means motor is off. 1 means motor is very fast. 3 different pwm for 25%, 50%, 75% speeds have to be used. Timers are used to generate the high freq pulses.

raja555

Junior Member level 1
i want to know how i write these pwm method in assembly code...........

sankar.m8

Hi raja
Here is a model code for controlling a dc motor with single key using assembly (this is for PIC micro controller but logic you can use with different instructions).

Here switch is connected in PORTB,1 with active high connection.
Motor is connected in PORTB,0

Code ASM - [expand]1
2
3
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
main:
btfsc PORTB,1
call mode_change
bsf PORTB,0
call delay_1
bcf PORTB,0
call delay_2
goto main

mode_change:
incf delay_temp
movlw D'04'
subwf delay_temp,0
btfss STATUS,Z
clrf delay_temp
return

delay_1:
movf delay_temp,W
sublw D'0'
btfsc STATUS,Z
delay of 25msec with return instruction (Depending on speed you want for ON time for 100msec)

movf delay_temp,W
sublw D'1'
btfsc STATUS,Z
delay of 50msec  with return instruction (Depending on speed you want for ON time for 100msec)

movf delay_temp,W
sublw D'2'
btfsc STATUS,Z
delay of 75msec  with return instruction (Depending on speed you want for ON time for 100msec)

movf delay_temp,W
sublw D'3'
btfsc STATUS,Z
delay of 100msec  with return instruction (Depending on speed you want for ON time for 100msec)

delay_2:
movf delay_temp,W
sublw D'0'
btfsc STATUS,Z
delay of 75msec  with return instruction (Depending on speed you want for ON time for 100msec)

movf delay_temp,W
sublw D'1'
btfsc STATUS,Z
delay of 50msec  with return instruction (Depending on speed you want for ON time for 100msec)

movf delay_temp,W
sublw D'2'
btfsc STATUS,Z
delay of 25msec  with return instruction (Depending on speed you want for ON time for 100msec)

movf delay_temp,W
sublw D'3'
btfsc STATUS,Z
delay of 0msec  with return instruction (Depending on speed you want for ON time for 100msec)

Last edited by a moderator:

Status
Not open for further replies.