#include <REGX52.H>
sbit FAN = P3^1;
unsigned int duty1,duty2,pwm_count;
//////////////////////// PWM /////////////////////
void pwm() interrupt 1 //timer 1
{
TF0=0;
TH1=100;
pwm_count++;
if(pwm_count<=duty1) FAN=1; else FAN=0; // duty1= duty cycle [dutycycle must be less than or equal to pwm_count]
if(pwm_count>100) // pwm_count = TIME PERIOD
{
pwm_count=0;
FAN=0;
}
}
}
//////////////////////////////// main ////////////////////
void main()
{
P0=0X0C; TMOD=0X33; IE=0X8A;
FAN=0;
TH0=0X0; TR0=0x1;
/////////////////////////////////MAIN LOOP ///////////////////////////////
while(1)
{
// user code duty++ ; increase the fan speed
// duty-- ; decrease the fan speed
}
}
#include <reg51.h>
sbit MTR = P1^0;
void MSDelay(unsigned int value);
void main() {
unsigned char z;
P2 = 0xFF;
z = P2;
z = z & 0x03;
MTR = 0;
while (1) {
switch(z) {
case(0):{
MTR = 1;
MSDELAY(25);
MTR = 0;
MSDelay(75);
break;
}
case(1):{
MTR = 1;
MSDELAY(50);
MTR = 0;
MSDelay(50);
break;
}
case(2):{
MTR = 1;
MSDELAY(75);
MTR = 0;
MSDelay(25);
break;
}
default:
MTR = 1;
}
}
void MSDelay(unsigned int value)
{
unsigned char x,y;
for(x=0; x<1275; x++);
for(y=0; y<value; y++);
}
#define SW P2_7_bit
#define MOTOR P1_0_bit
void main() {
unsigned int speed;
while (1) {
if(SW == 1) {
Delay_ms(20);
if(SW == 1) {
speed = speed + 1;
}
}
if(speed == 5) {
speed = 0;
}
switch(speed) {
case(0):{
MOTOR = 0;
break;
}
case(1):{
MOTOR = 1;
Delay_ms(25);
MOTOR = 0;
Delay_ms(75);
break;
}
case(2):{
MOTOR = 1;
Delay_ms(50);
MOTOR = 0;
Delay_ms(50);
break;
}
case(3):{
MOTOR = 1;
Delay_ms(75);
MOTOR = 0;
Delay_ms(25);
break;
}
case(4):{
MOTOR = 1;
break;
}
default:
;
}
}
}
;=====================================================
; 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
;=====================================================
outp equ P2
org 0h
main:mov r4,#0ffh
up0: mov r0,#100d ;Set duty cycle
mov r1,#01d
acall out ;light up LED
djnz r4,up0 ;loop for dome time
mov r4,#0ffh
up1: mov r0,#90d
mov r1,#10d
acall out
djnz r4,up1
mov r4,#0ffh
up2: mov r0,#80d
mov r1,#20d
acall out
djnz r4,up2
mov r4,#0ffh
up3: mov r0,#70d
mov r1,#30d
acall out
djnz r4,up3
mov r4,#0ffh
up4: mov r0,#60d
mov r1,#40d
acall out
djnz r4,up4
mov r4,#0ffh
up5: mov r0,#50d
mov r1,#50d
acall out
djnz r4,up5
mov r4,#0ffh
up6: mov r0,#40d
mov r1,#60d
acall out
djnz r4,up6
mov r4,#0ffh
up7: mov r0,#30d
mov r1,#70d
acall out
djnz r4,up7
mov r4,#0ffh
up8: mov r0,#20d
mov r1,#80d
acall out
djnz r4,up8
mov r4,#0ffh
up9: mov r0,#10d
mov r1,#90d
acall out
djnz r4,up9
mov r4,#0ffh
up11: mov r0,#10d
mov r1,#90d
acall out
djnz r4,up11
mov r4,#0ffh
up12: mov r0,#20d
mov r1,#80d
acall out
djnz r4,up12
mov r4,#0ffh
up13: mov r0,#30d
mov r1,#70d
acall out
djnz r4,up13
mov r4,#0ffh
up14: mov r0,#40d
mov r1,#60d
acall out
djnz r4,up14
mov r4,#0ffh
up15: mov r0,#50d
mov r1,#50d
acall out
djnz r4,up15
mov r4,#0ffh
up16: mov r0,#60d
mov r1,#40d
acall out
djnz r4,up16
mov r4,#0ffh
up17: mov r0,#70d
mov r1,#30d
acall out
djnz r4,up17
mov r4,#0ffh
up18: mov r0,#80d
mov r1,#20d
acall out
djnz r4,up18
mov r4,#0ffh
up19: mov r0,#90d
mov r1,#10d
acall out
djnz r4,up19
mov r4,#0ffh
up20: mov r0,#100d
mov r1,#01d
acall out
djnz r4,up20
ajmp main ;Loop
**************************************************
;Out subroutine
out: mov outp,#0ffh
djnz r0,out
out1:mov outp,#00h
djnz r1,out1
ret
**************************************************
end
outp equ P0
org 0h
main:mov dptr,#pwm_data ;load pointer for lookup table
clr a
movc a,@a+dptr
mov r2,a
inc dptr
up: clr a
movc a,@a+dptr
mov 40h,a ;ON period
clr a
inc dptr
movc a,@a+dptr
mov 41h,a ;OFF period
inc dptr
acall out
djnz r2,up
sjmp main
;*************************************************
out: mov r4,#02h
lp0: mov r3,#0ffh
lp: mov r0,40h
mov r1,41h
lp1: mov outp,#0ffh
djnz r0,lp1
lp2: mov outp,#00h
djnz r1,lp2
djnz r3,lp
djnz r4,lp0
ret
;*************************************************
;Lookup table
pwm_data: db 15h ;no of elements in the
db 100d,001d ;lookup table
db 090d,010d;
db 080d,020d ;each element consisting
db 070d,030d ;of 2 bytes
db 060d,040d;
db 050d,050d ;first byte is value
db 040d,060d ;for ON period
db 030d,070d ;second byte is value
db 020d,080d ;for OFF period
db 010d,090d;
db 001d,100d;
db 010d,090d;
db 020d,080d;
db 030d,071d;
db 040d,060d;
db 050d,050d;
db 060d,040d;
db 070d,030d;
db 080d,020d;
db 090d,010d;
db 100d,001d;
;*************************************************
end
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?