mình viết chương trình điều chế PWM bằng 8051, sử dụng ngắt timer và ngắt ngoài INT0 , khi mình tác đọng vào ngắt ngoài INT0 thì chương trình không nhảy đến địa chỉ ngắt, bài này mình viết cho con robot do đường đơn giản, và khi gặp vật cản thì quay đầu, cảm biến vật cản đưa vào chân P3.2, còn timer thì dùng để điều xung
các bác giúp em xem sai ở đâu với
;=============================
count_timer equ 31h
;============PWM===============
PWM1 equ 32h
PWM2 equ 33h
#define PWM_PORT1 P1.4 ;motor RIGHT
#define DAO_MOTORR P1.5
#define PWM_PORT2 P1.6 ;motor LEFT
#define DAO_MOTORL P1.7
;===========define sensor=======
#define SENSOR0 P2.7
#define SENSOR1 P2.6
#define SENSOR2 P2.5
#define SENSOR3 P2.4
#define SENSOR4 P2.3
#define SENSOR5 P2.2
#define SENSOR6 P2.1
#define SENSOR7 P2.0
#define SENSOR P2
;==============================
org 0000h
ljmp main
org 0Bh
ljmp EXtimer0
org 03h
ljmp ngat_ngoai0
org 40h
main:
lcall khaibaothanhghi
mainloop:
lcall kiemtracambien
sjmp mainloop
;==============================
;chuong trinh phuc vu ngat ngoai0 de xua ly vat can
ngat_ngoai0:
jb p3.2,thoat_ngat_ngoai0
lcaal delay_nhieu
jb p3.2,thoat_ngat_ngoai0
;xu ly quay dau
mov PWM1,#50
setb DAO_MOTORL
jb SENSOR3,thoat_ngat_ngoai0
jb SENSOR4,thoat_ngat_ngoai0
thoat_ngat_ngoai0:
reti
;==============================
khaibaothanhghi:
mov ie,#10000000b
setb ex0 ;cho phep ngat ngoai 0 de phat hien vat can
setb it0 ;ngat ngoai 0 o canh xung
mov IP,#04h ; cho phep ngat ngoai uu tien hon ngat trong
mov sp,#5fh
MOV TMOD,#00000010b
mov th0,#(-100)
setb EA
setb ET0
setb TR0
;======================
mov count_timer,#0
mov PWM1,#50
mov PWM2,#50
ret
;==============================
kiemtracambien:
jb SENSOR4,nhan1
lcall tocdo1
nhan1:
jb SENSOR3,nhan2
lcall tocdo2
nhan2:
jb SENSOR5,nhan3
lcall tocdo3
nhan3:
jb SENSOR2,nhan4
lcall tocdo4
nhan4:
jb SENSOR6,nhan5
lcall tocdo5
nhan5:
jb SENSOR1,nhan6
lcall tocdo6
nhan6:
jb SENSOR7,nhan7
lcall tocdo7
nhan7:
jb SENSOR0,nhan8
lcall tocdo8
nhan8:
ret
;==============================
tocdo1:
mov PWM1,#50
MOV PWM2,#50
ret
;==============================
tocdo2:
mov PWM1,#50
MOV PWM2,#50
ret
;==============================
tocdo3:
mov PWM1,#50
MOV PWM2,#40
ret
;==============================
tocdo4:
mov PWM1,#40
MOV PWM2,#50
ret
;==============================
tocdo5:
mov PWM1,#50
MOV PWM2,#25
ret
;==============================
tocdo6:
mov PWM1,#25
MOV PWM2,#50
ret
;==============================
tocdo7:
mov PWM1,#50
MOV PWM2,#10
ret
;==============================
tocdo8:
mov PWM1,#10
MOV PWM2,#50
ret
;==============================
EXtimer0:
PUSH acc
inc count_timer
mov a,count_timer
cjne a,#100,EXt1
mov count_timer,#0
EXt1:
mov a,count_timer
;=====================
clr c
cjne a,PWM1,EXt11
EXt11:
jnc EXt111
setb PWM_PORT1
sjmp EXt2
EXt111:
clr PWM_PORT1
EXt2:
clr c
cjne a,PWM2,EXt22
EXt22:
jnc EXt222
setb PWM_PORT2
sjmp end_EXtimer0
EXt222:
clr PWM_PORT2
end_EXtimer0:
POP acc
reti
;==============================
delay_nhieu:
mov r7,#255
djnz r7,$
ret
;==============================
delay500ms:
mov r0,#200
d2: mov r1,#255
d3:
djnz r1,d3
djnz r0,d2
ret
end
count_timer equ 31h
;============PWM===============
PWM1 equ 32h
PWM2 equ 33h
#define PWM_PORT1 P1.4 ;motor RIGHT
#define DAO_MOTORR P1.5
#define PWM_PORT2 P1.6 ;motor LEFT
#define DAO_MOTORL P1.7
;===========define sensor=======
#define SENSOR0 P2.7
#define SENSOR1 P2.6
#define SENSOR2 P2.5
#define SENSOR3 P2.4
#define SENSOR4 P2.3
#define SENSOR5 P2.2
#define SENSOR6 P2.1
#define SENSOR7 P2.0
#define SENSOR P2
;==============================
org 0000h
ljmp main
org 0Bh
ljmp EXtimer0
org 03h
ljmp ngat_ngoai0
org 40h
main:
lcall khaibaothanhghi
mainloop:
lcall kiemtracambien
sjmp mainloop
;==============================
;chuong trinh phuc vu ngat ngoai0 de xua ly vat can
ngat_ngoai0:
jb p3.2,thoat_ngat_ngoai0
lcaal delay_nhieu
jb p3.2,thoat_ngat_ngoai0
;xu ly quay dau
mov PWM1,#50
setb DAO_MOTORL
jb SENSOR3,thoat_ngat_ngoai0
jb SENSOR4,thoat_ngat_ngoai0
thoat_ngat_ngoai0:
reti
;==============================
khaibaothanhghi:
mov ie,#10000000b
setb ex0 ;cho phep ngat ngoai 0 de phat hien vat can
setb it0 ;ngat ngoai 0 o canh xung
mov IP,#04h ; cho phep ngat ngoai uu tien hon ngat trong
mov sp,#5fh
MOV TMOD,#00000010b
mov th0,#(-100)
setb EA
setb ET0
setb TR0
;======================
mov count_timer,#0
mov PWM1,#50
mov PWM2,#50
ret
;==============================
kiemtracambien:
jb SENSOR4,nhan1
lcall tocdo1
nhan1:
jb SENSOR3,nhan2
lcall tocdo2
nhan2:
jb SENSOR5,nhan3
lcall tocdo3
nhan3:
jb SENSOR2,nhan4
lcall tocdo4
nhan4:
jb SENSOR6,nhan5
lcall tocdo5
nhan5:
jb SENSOR1,nhan6
lcall tocdo6
nhan6:
jb SENSOR7,nhan7
lcall tocdo7
nhan7:
jb SENSOR0,nhan8
lcall tocdo8
nhan8:
ret
;==============================
tocdo1:
mov PWM1,#50
MOV PWM2,#50
ret
;==============================
tocdo2:
mov PWM1,#50
MOV PWM2,#50
ret
;==============================
tocdo3:
mov PWM1,#50
MOV PWM2,#40
ret
;==============================
tocdo4:
mov PWM1,#40
MOV PWM2,#50
ret
;==============================
tocdo5:
mov PWM1,#50
MOV PWM2,#25
ret
;==============================
tocdo6:
mov PWM1,#25
MOV PWM2,#50
ret
;==============================
tocdo7:
mov PWM1,#50
MOV PWM2,#10
ret
;==============================
tocdo8:
mov PWM1,#10
MOV PWM2,#50
ret
;==============================
EXtimer0:
PUSH acc
inc count_timer
mov a,count_timer
cjne a,#100,EXt1
mov count_timer,#0
EXt1:
mov a,count_timer
;=====================
clr c
cjne a,PWM1,EXt11
EXt11:
jnc EXt111
setb PWM_PORT1
sjmp EXt2
EXt111:
clr PWM_PORT1
EXt2:
clr c
cjne a,PWM2,EXt22
EXt22:
jnc EXt222
setb PWM_PORT2
sjmp end_EXtimer0
EXt222:
clr PWM_PORT2
end_EXtimer0:
POP acc
reti
;==============================
delay_nhieu:
mov r7,#255
djnz r7,$
ret
;==============================
delay500ms:
mov r0,#200
d2: mov r1,#255
d3:
djnz r1,d3
djnz r0,d2
ret
end
Comment