8051 assembly code interprating

Status
Not open for further replies.
Implementing direction control involves the following modifications to the T0ISR:

Code:
; Timer0 interrupt .. 
T0ISR: 

   CLR    TR0 
   MOV    TH0, #HIGH(-921)
   MOV    TL0, #LOW(-921) 
   INC    R1
 
   MOV    C, P3.1              ; Test if P3.1 SW closed ..
   JC     Set_Dir              ; If closed (0V) -> CW ..     
   CLR    Direction
   JMP    T0ISR_Exit

Set_Dir:

   SETB   Direction            ; If open (5V) -> CCW .. 
    
T0ISR_Exit: 
   SETB   TR0 
   RETI

Regards,
IanP
 

    novschopin

    Points: 2
    Helpful Answer Positive Rating
hi Ian,
the code that u previously gave me is working but the speed is not changing.
but never mind, because now i am going to do the demo separately.
I am going to make it forward in high speed. Another one is make it reverse in low speed. So, these are my codes
forward high speed
Code:
ORG	0000H
	JMP	MAIN
	ORG	000BH
	JMP	T0ISR
	ORG	0023H
	JMP	SISR

	ORG	0030H
	MOV	R0, #15		; motor speed on time
	MOV	R1, #0
MAIN:	MOV	SCON, #52H
	MOV	TMOD, #21H
	MOV	TH1, #-3
	SETB	TR1
	SETB	TF0
	MOV	IE, #10010010B

AGAIN:	SETB	P3.7
	SETB	P3.3
	CLR	P3.4
	CLR	P3.5

SCAN:	CJNE	R1, #20, JJ
	MOV	R1, #0
	JMP	AGAIN
JJ:	MOV	A, R1
	MOV	22H, R0
	CJNE	A, 22H, J2
	SETB	C
J2:	JC 	SCAN
	CLR	P3.7
	CLR	P3.3
	JMP	SCAN

T0ISR:	CLR	TR0
	MOV	TH0, #HIGH(-921)
	MOV	TL0, #LOW(-921)
	INC	R1
	SETB	TR0
	RETI

SISR:	CLR	RI
	CLR	TI
	MOV	A, SBUF
	CJNE	A, #"F", N1
	MOV	R0, #17
	RETI
N1:	CJNE	A, #"M", N2
	MOV	R0, #10
	RETI
N2:	CJNE	A, #"S", N3
	MOV	R0, #4
N3:	RETI

	END
reverse low speed
Code:
ORG	0000H
	JMP	MAIN
	ORG	000BH
	JMP	T0ISR
	ORG	0023H
	JMP	SISR

	ORG	0030H
	MOV	R0, #8		; motor speed on time
	MOV	R1, #0
MAIN:	MOV	SCON, #52H
	MOV	TMOD, #21H
	MOV	TH1, #-3
	SETB	TR1
	SETB	TF0
	MOV	IE, #10010010B

AGAIN:	SETB	P3.4
	SETB	P3.5
	CLR	P3.7
	CLR	P3.3

SCAN:	CJNE	R1, #20, JJ
	MOV	R1, #0
	JMP	AGAIN
JJ:	MOV	A, R1
	MOV	22H, R0
	CJNE	A, 22H, J2
	SETB	C
J2:	JC 	SCAN
	CLR	P3.4
	CLR	P3.5
	JMP	SCAN

T0ISR:	CLR	TR0
	MOV	TH0, #HIGH(-921)
	MOV	TL0, #LOW(-921)
	INC	R1
	SETB	TR0
	RETI

SISR:	CLR	RI
	CLR	TI
	MOV	A, SBUF
	CJNE	A, #"F", N1
	MOV	R0, #17
	RETI
N1:	CJNE	A, #"M", N2
	MOV	R0, #10
	RETI
N2:	CJNE	A, #"S", N3
	MOV	R0, #4
N3:	RETI

	END
just want confirm with you that my code is right or not. Thank you.
regards,
Malcolm
 

You don't need to ask - both codes look OK ..

Also, I have attached a code (tested with an oscilloscope) tha has everything included: speed change every 3-4s, direction change with P1.3 (latched switch OFF = CW, switch ON = CCW) ..

Regards,
IanP
 

    novschopin

    Points: 2
    Helpful Answer Positive Rating
alright Ian,
i will use ur code for demo if its working, but may i know how to change the direction other than using the switch, i mean a simple way without switch, for example using wire to short the p3.1 to ground or to 5v for demo purpose in direction changing. I have tried this method using the code u give to me last time but it cant work. I might have not enough time to get a latch switch tomorrow. mayb this direction function not working??
any suggestion for controlling the direction without shopping?
thank you,
regards,
Malcolm
 

Another option to change direction is through the serial port .. that is, if you have a PC connected to your circuit via the RS-232 ..
The serial port interrupt routine is already runing in your code - it just needs a small modification:

Code:
; =================================== 
; Serial port interrupt 
; F, M or S will load R0 (speed reference) with selected values .. 

SISR:
   PUSH  ACC
   PUSH  PSW 
   CLR   RI 
   CLR   TI 
   MOV   A, SBUF 
   CJNE  A, #'F', N1         ; If incoming character = 'F" load R0 with 17d .. 
   MOV   R0, #17 
   JMP SI_Exit
N1:
   CJNE  A, #'M', N2         ; If character = 'M' load R0 with 10d .. 
   MOV   R0, #10 
   JMP  SI_Exit
N2:
   CJNE  A, #'S', N3         ; If character = 'S' load R0 with 04d .. 
   MOV   R0, #4
   JMP  SI_Exit 
N3:
   CJNE A, #'d', N4
   CPL  Direction
N4:
SI_Exit:
   POP  PSW
   POP  ACC
   RETI 
; ====================================

But to make it working you will need to remove the section of the code that reads P1.3:

Code:
;   MOV    C, P1.3              ; Test if P3.1 SW closed ..
;   JC     Set_Dir              ; If closed (0V) -> CW ..     
;   CLR    Direction
;   JMP    T0ISR_Exit
;Set_Dir:
;   SETB   Direction            ; If open (5V) -> CCW ..

Otherwise attach a wire (or a latched switch) to P1.3 and connect it by hand to 0V or 5V to chage the direction ..

Regards,
IanP
 

hi Ian.
I have tried using wire to connect the p1.3 to 5V or 0V but it wont change the direction
how come??? is it some mistake in the code??
thank you
regards,
malcolm
 

I don't think there is a mistake in the code ..
The P1.3 pin is read by the code several times per second, in fact, each time the code branches to the T0 interrupt .. So, if you need to change direction you have to simulate "latched" action: touch and hold, not just touch ..
Also, make sure that in the code uses P1.3 and not P3.1 or something like that, and that the wire is connected to P1.3 ..

Regards,
IanP
 

hi Ian,
I wonder why the output pin of my microcontroller is not consistent. sometimes high and sometimes low, and causes short circuit happens and burnt yhe mosfet.
swt...i am using at89c2051...
btw, its all over now, thank you for your supports and precious guides.
a million thanks to you my friend.
thank you
regards,
Malcolm
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…