Assembly code for DC Motor - PWM Pulse width Modulation


Sample code for DC Motor - PWM Pulse width Modulation.
Following code can be using for 8051 and 8052 ICs like: 89c51, 89c52, 89s51, 89s52, 89c2051.

 

;===================================================== ; Pin Definations ;===================================================== mot1for equ a0h ;address of Port 2.0 mot1rev equ a1h ;address of port 2.1 mot2for equ a2h ;address of port 2.2 mot2rev equ a3h ;address of port 2.3 ;===================================================== ; Main Program starts from here ;===================================================== call slowstart call slowstop here jmp here ;===================================================== ; Sub routines starts from here ;===================================================== slowstart: jnb 21h,mot2 ;goto mot2 if motor 2 is selected jnb 20h,rev_rise_on1 ;goto rev_rise_on1 if reverse direction is selected jnb 27h,slowstop_invert_dir ;if motor is in reverse dir stop motor&rotate forward jnb 26h,slowstart_ret ;if motor is in forward dir don't rotate forward again call mot1_for_start slowstart_ret ret slowstop_invert_dir call mot1_rev_stop call mot1_for_start ret rev_rise_on1 jnb 26h,slowstart_invert ;don't start if motor is in forward direction jnb 27h,slowstart_ret1 ;don't start if motor is in reverse direction call mot1_rev_start slowstart_ret1 ret slowstart_invert call mot1_for_stop call mot1_rev_start ret mot2 jnb 20h,mot2_rev jnb 29h,slowstart_inv jnb 28h,slowstart_ret2 call mot2_for_start slowstart_ret2 ret slowstart_inv call mot2_rev_stop call mot2_for_start ret mot2_rev jnb 28h,slowstart_inve jnb 29h,slowstart_ret3 call mot2_rev_start slowstart_ret3 ret slowstart_inve call mot2_for_stop call mot2_rev_start ret ;========================================================= slowstop jnb 21h,for_rise_off_mot2 jnb 20h,rev_rise_off_mot1 jb 22h,slowstop_ret call mot1_for_stop slowstop_ret ret rev_rise_off_mot1 jb 23h,slowstop_ret1 call mot1_rev_stop slowstop_ret1 ret for_rise_off_mot2 jnb 20h,rev_rise_off_mot2 jb 24h,slowstop_ret2 call mot2_for_stop slowstop_ret2 ret rev_rise_off_mot2 jb 25h,slowstop_ret3 call mot2_rev_stop slowstop_ret3 ret ;========================================================= mot1_for_start clr mot1for ;on period of pwm mov r2,30H djnz r2,$ setb mot1for ;off period of pwm mov r2,31H djnz r2,$ djnz r3,mot1_for_start ;repeat the loop 10 times mov r3,#10 mov a,30H add a,# 5 ;increment on period by 10 microsec mov 30H,a mov a,31H subb a,# 5 ;decrement off period by 10 microsec mov 31H,a djnz r4,mot1_for_start ;repeat the loop 20 times mov r4,#20 clr mot1for clr 22h ;sense stop key as mot1+for clr 26h ;motor1+rev ret ;========================================================= mot1_rev_start clr mot1rev ;on period of pwm mov r2,30H djnz r2,$ setb mot1rev ;off period of pwm mov r2,31H djnz r2,$ djnz r3,mot1_rev_start ;repeat the loop 10 times mov r3,#10 mov a,30H add a,# 5 ;increment on period by 10 microsec mov 30H,a mov a,31H subb a,# 5 ;decrement off period by 10 microsec mov 31H,a djnz r4,mot1_rev_start ;repeat the loop 20 times mov r4,#20 clr mot1rev clr 23h ;sense stop key as mot1+rev clr 27h ;mot1+for ret ;========================================================= mot2_for_start clr mot2for ;on period of pwm mov r2,30H djnz r2,$ setb mot2for ;off period of pwm mov r2,31H djnz r2,$ djnz r3,mot2_for_start ;repeat the loop 10 times mov r3,#10 mov a,30H add a,# 5 ;increment on period by 10 microsec mov 30H,a mov a,31H subb a,# 5 ;decrement off period by 10 microsec mov 31H,a djnz r4,mot2_for_start ;repeat the loop 20 times mov r4,#20 clr mot2for clr 24h ;sense stop key as mot2+for clr 28h ;mot2+rev ret ;========================================================= mot2_rev_start clr mot2rev ;on period of pwm mov r2,30H djnz r2,$ setb mot2rev ;off period of pwm mov r2,31H djnz r2,$ djnz r3,mot2_rev_start ;repeat the loop 10 times mov r3,#10 mov a,30H add a,# 5 ;increment on period by 10 microsec mov 30H,a mov a,31H subb a,# 5 ;decrement off period by 10 microsec mov 31H,a djnz r4,mot2_rev_start ;repeat the loop 20 times mov r4,#20 clr mot2rev clr 25h ;sense stop key as mot2+rev clr 29h ;mot2+for ret ;========================================================= mot1_for_stop clr mot1for ;on period of pwm mov r2,31H djnz r2,$ setb mot1for ;off period of pwm mov r2,30H djnz r2,$ djnz r3,mot1_for_stop ;repeat the loop 10 times mov r3,#10 mov a,30H add a,# 5 ;increment off period by 10 microsec mov 30h,a mov a,31h subb a,# 5 ;decrement on period by 10 microsec mov 31h,a djnz r4,mot1_for_stop ;repeat the loop 20 times mov r4,#20 setb mot1for setb 22h ;don't sense stop key if moh1+for is not started setb 26h ;if mot1+for then don't start mot1+rev ret ;========================================================= mot1_rev_stop clr mot1rev ; on period of pwm mov r2,31h djnz r2,$ setb mot1rev ;off period of pwm mov r2,30h djnz r2,$ djnz r3,mot1_rev_stop ;repeat the loop 10 times mov r3,#10 mov a,30h add a,# 5 ;increment off period by 10 microsec mov 30h,a mov a,31h subb a,# 5 ;decrement on period by 10 microsec mov 31h,a djnz r4,mot1_rev_stop ;repeat the loop 20 times mov r4,#20 setb mot1rev setb 23h ;don't sense stop key if mot1+rev is not started setb 27h ;if mot1+rev then don't start mot1+for ret ;========================================================= mot2_for_stop clr mot2for ;on period of pwm mov r2,31h djnz r2,$ setb mot2for ;off period of pwm mov r2,30h djnz r2,$ djnz r3,mot2_for_stop ;repeat the loop 10 times mov r3,#10 mov a,30h add a,# 5 ;increment off period by 10 microsec mov 30h,a mov a,31h subb a,# 5 ;decrement on period by 10 microsec mov 31h,a djnz r4,mot2_for_stop ;repeat the loop 20 times mov r4,#20 setb mot2for setb 24h ;don't sense stop key if mot2+for is not started setb 28h ;if mot2+for then don't start mor2+rev ret ;========================================================= mot2_rev_stop clr mot2rev ;on period of pwm mov r2,31h djnz r2,$ setb mot2rev ;off period of pwm mov r2,30h djnz r2,$ djnz r3,mot2_rev_stop ;repeat the loop 10 times mov r3,#10 mov a,30h ;increment off period by 10 microsec add a,# 5 mov 30h,a mov a,31h subb a,# 5 ;decrement on period by 10 microsec mov 31h,a djnz r4,mot2_rev_stop ;repeat the loop 20 times mov r4,#20 setb mot2rev setb 25h ;don't sense stop key if mot2+rev is not started setb 29h ;if mot2+rev then don't start mot2+for ret ;===================================================== ; Program ENDS here ;=====================================================