Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

8051 assembly code interprating

Status
Not open for further replies.

novschopin

Junior Member level 1
Junior Member level 1
Joined
Mar 1, 2006
Messages
17
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,514
can anyone please explain what are these codes all about in every single line?? thanks
; Fet@89c5x Integrated Development Environment (Assembly Code)

ORG 0000H
JMP MAIN
ORG 000BH
JMP T0ISR
ORG 0023H
JMP SISR

ORG 0030H
MOV R0, #12 ; 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
 

Code:
	ORG 	0000H			; Start of the MAIN 
	JMP 	MAIN 
	ORG	 000BH			; Timer0 interrupt address 
	JMP 	T0ISR 
	ORG 	0023H			; Serial Interrupt address 
	JMP	SISR 

ORG 0030
 
	MOV 	R0, #12			 ; motor speed on time 
	MOV 	R1, #0

MAIN:	MOV	 SCON, #52H		; These 4 lines set up UART for 9600bps .. 
	MOV 	TMOD, #21H		; assuming clock 11,059200MHz ..
	MOV 	TH1, #-3 
	SETB 	TR1 

	SETB 	TF0 
	MOV	 IE, #10010010B	; IE.7 = 1 enables all interrupts ..
				; IE.4 = 1 enables serial port interrupt ..
				; IE.1 = 1 enables Timer0 overflow interrupt
 ; Pins P3.7, P3.3, P3.4 and P3.5 are used as outputs ..

AGAIN: SETB 	P3.7		; P3.7, 3.3  = 1 ( 5V) 
	SETB	P3.3
	CLR 	P3.4		; P3.4, 3.5 = 0 (0V) 
	CLR 	P3.5 

SCAN: CJNE	 R1, #20, JJ	; Check if Timer0 interrupt occured 20 times 
	MOV	 R1, #0		; If yes, load speed reference R0 with 0 
	JMP 	AGAIN
 
JJ: 	MOV 	A, R1		; If not, check what is current R0 reference value ..
	MOV 	22H, R0		; 22H is just a memory address .. 
	CJNE 	A, 22H, J2 
	SETB 	C		; If current reference value = R1 goto SCAN .. 
J2: 	JC	SCAN 
	CLR 	P3.7		; Otherwise outputs P3.7 and P3.3 = 0V 
	CLR 	P3.3 
	JMP 	SCAN 

; ==================================
; Timer0 interrupt

T0ISR: 	CLR 	TR0 
	MOV 	TH0, #HIGH(-921)			; Reload values for TH0 and TL0 .. 
	MOV 	TL0, #LOW(-921) 
	INC 	R1					; R1 counts Timer0 interrupts .. 
	SETB 	TR0 
	RETI 

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

	SISR: 	CLR RI 
	CLR 	TI 
	MOV 	A, SBUF 
	CJNE 	A, #"F", N1			; If incoming character = 'F" load R0 with 17d .. 
	MOV 	R0, #17 
	RETI 
N1: 	CJNE 	A, #"M", N2			; If character = 'M' load R0 with 10d .. 
	MOV	 R0, #10 
	RETI 
N2: 	CJNE	 A, #"S", N3			; If character = 'S' load R0 with 04d .. 
	MOV 	R0, #4 
N3: 	RETI 

END

Regards,
IanP
 

    novschopin

    Points: 2
    Helpful Answer Positive Rating
hey Ian, thanks a lot!!!!
i have some doubts here and hope u can help once again
if i would like to control the motor speed automatically:
for example,
let motor runs in full speed for 5 seconds, then decrease to half speed for 5 second and then stop (0 speed) for 5 sec.
how to do this program by editting the previous code.??
and another problem is i would like to control the dircetion of the motor.
for the case above the motor only runs in forward direction, and i wish to make it reverse and i know it can be done by setting p3.4 and p3.5 high (5V) and clr p3.7 and p3.3 (0V). But, i need a port (anyone will do) to control the direction by using switch or something else. how can i program it??
thank you very much!!
regards,
Malcolm
 

First try to implement DIRECTION control ..
You can add a button connected to any pin, or you can, as in the attached example, use keyboard key 'D" ..

Code:
; add this declaration after MOVR0,#0 ..

	Flags		DATA	20h
	Direction		BIT	Flags.0	; If direction=0 =>CW if 1 =>CCW ..

		...
; and change the following:

Loop:		JB Direction, AgainCCW



AgainCW:		...		; as it is .. 6 and 7 =>1, 4 and 5 =>0

ScanCW:

		...
		JMP	Loop	; old jump was to Again, this time it is Loop	

AgainCCW:			; 6 and 7 =>0, 4 and 5 =>1

ScanCCW:		...
		JMP	Loop



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

SISR:		CLR	RI 
		CLR   	TI 
		MOV	A, SBUF 
		CJNE	A, #"F", N1	; If incoming character = 'F" load R0 with 17d .. 
		MOV	 R0, #17 
		RETI 
N1:		CJNE	A, #"M", N2	 ; If character = 'M' load R0 with 10d .. 
		MOV	R0, #10 
		RETI 
N2:		CJNE	A, #"S", N3	 ; If character = 'S' load R0 with 04d .. 
		MOV	R0, #4
		RETI
; added .. 
N3:		CJNE	A, #"D", N4	; If character='D' change direction ..
		CPL	Direction
N4:		RETI			

		RETI
Don't forget about renaming JJ to JJCW and JJCCW and J2 to J2CW and J2CCW in ScanCW and ScanCCW ..

How would you like to generate ≈5s? As Timer0 is already running, it can be used for that purpose ..

Regards,
IanP
 

    novschopin

    Points: 2
    Helpful Answer Positive Rating
thanks again Ian,
i wish to control the direction by adding a button connected to any pin (such as P3.1) how could i edit the program that you written to me??
how to do so??
actually this code enable the motor to run at one single speed only.
and now i wish to add a feature of speed cotrolling automatically by using programming.
the motor will first run in full speed for a length of 5 seconds then automatically decrease to half speed and run in a length of 5 seconds then stops for a legth of 5 seconds automatically.
can you please help me on this.. thanks very much
regards,
Malcolm
 

Try the follwing code:
Code:
	$NOPAGING
	$MOD252

; Pins P3.7, P3.3, P3.4 and P3.5 are used as outputs ..

	ORG	0000h				; Start of the MAIN ..
	JMP	MAIN 
	ORG	000Bh				; Timer0 interrupt address ..
	JMP	T0ISR 
	ORG	0023h				; Serial Interrupt address ..
	JMP	SISR

Flags	DATA	20h
Speed0	BIT	Flags.0		; Speed0 = stop 0
SpeedS	BIT	Flags.1		; SpeedS = slow 4 
SpeedM	BIT	Flags.2		; SpeedM = medium 10 
SpeedF	BIT	Flags.3		; SpeedF = fast 17
Any4		BIT	Flags.4
Any5		BIT	Flags.5
Any6		BIT	Flags.6
TChange	BIT	Flags.7

TimeH	Data	23h
TimeL	Data	24h

ORG 0030h

	MOV 	R0, #0			; motor speed on time .. start from 0 .. 
	MOV 	R1, #0
	MOV	TimeH, #10h			; If TimeHTimeL=1000h-> speed change ..
	MOV	TimeL, #00h
	MOV	DPTR, #0000h
	MOV	Flags, #00h

MAIN:	
	MOV	SCON, #52h			; These 4 lines set up UART for 9600bps .. 
	MOV 	TMOD, #21h			; assuming clock 11,059200MHz .. 
	MOV 	TH1, #-3 
	SETB 	TR1 

	SETB 	TF0 
	MOV	IE, #10010010B		; IE.7 = 1 enables all interrupts ..
						; IE.4 = 1 enables serial port interrupt ..
						; IE.1 = 1 enables Timer0 overflow interrupt


Main_Loop:

AGAIN: 
	JB	TChange, S5_Time		; Check if it is time to vary speed ..

	SETB 	P3.7				; P3.7, 3.3  = 1 ( 5V) 
	SETB	P3.3
	CLR 	P3.4				; P3.4, 3.5 = 0 (0V) 
	CLR 	P3.5 

SCAN: CJNE	 R1, #20, JJ		; Check if Timer0 interrupt occured 20 times 
	MOV	 R1, #0			; If yes, load speed reference R0 with 0 
	JMP 	AGAIN
 
JJ: 	MOV	A, R1				; If not, check what is current R0 reference value ..
	MOV	22h, R0			; 22h is just a memory address .. 
	CJNE	A, 22h, J2 
	SETB	C				; If current reference value <= R1 goto SCAN .. 
J2: 	JC	SCAN
	CLR	P3.7				; Otherwise outputs P3.7 and P3.3 = 0V ..
	CLR	P3.3
	JMP	SCAN

; ==================================
; Timer0 interrupt

T0ISR:
	PUSH	ACC
	PUSH	PSW
	CLR	TR0
	MOV	TH0, #HIGH(-921)		; Reload values for TH0 and TL0 .. 
	MOV	TL0, #LOW(-921) 
	INC	R1				; R1 counts Timer0 interrupts ..
	INC	DPTR				; x sec time counter ..

	CLR	C
	MOV	A, DPH			; Check if DPTR = TimeH_TimeL =1000h ..
	SUBB	A, TimeH
	JC	T0ISR_Exit			; <
	JNZ	THbg				; >
	CLR	C				; =
	MOV	A, DPL
	SUBB	A, TimeL
	JC	T0ISR_Exit			; <
	JNZ	T0ISR_Exit			; >
	CLR	C				; =			
	SETB	TChange

THbg:	MOV	DPTR, #0000h
	
T0ISR_Exit:
	POP	PSW
	POP	ACC
	SETB	TR0
	RETI

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

SISR:
	CLR	RI 
	CLR	TI 
	MOV	A, SBUF 
	CJNE	A, #'F', N1			; If incoming character = 'F" load R0 with 17d .. 
	MOV	R0, #17 
	RETI
N1:	CJNE	A, #'M', N2			; If character = 'M' load R0 with 10d .. 
	MOV	R0, #10 
	RETI
N2:	CJNE	A, #'S', N3			; If character = 'S' load R0 with 04d .. 
	MOV	R0, #4
N3:	RETI

; ====================================
; time subs ..

S5_Time:

S_Change:
	JB	SpeedS, Speed_Up1
	MOV	R0, #4			; speed slow ..
	SETB	SpeedS
	SJMP	S5_Exit
Speed_Up1:
	JB	SpeedM, Speed_Up2
	MOV	R0, #10			; speed medium ..
	SETB	SpeedM
	SJMP	S5_Exit
Speed_Up2:
	JB	SpeedF, Speed_Up3
	MOV	R0, #17			; speed fast ..
	SETB	SpeedF
	SJMP	S5_Exit

Speed_Up3:
	CLR	SpeedS
	CLR	SpeedM
	CLR	SpeedF
	MOV	R0, #0			; speed = 0

S5_Exit:
	CLR	TChange	
	RET

END
I have chosen random values of TimeH(10h) and TimeL(00h), so if time is shorter than you need you may increase these valuse at your will ..
Also, keep in mind that I have't tested this code whatsoever, so it may contain bugs ..

Regards,
IanP
 

    novschopin

    Points: 2
    Helpful Answer Positive Rating
ORG 0000H
JMP MAIN
ORG 000BH
JMP T0ISR
JMP T1ISR
ORG 0023H
JMP SISR


ORG 0030H
FLAGS DATA 20H
DIRECTION BIT FLAGS.0
MOV R0, #12 ; motor speed on time
MOV R1, #0
JNB PSW.1, $
JNB PSW.1, $
JNB PSW.1, $
JNB PSW.1, $
JNB PSW.1, $
MOV R0, #8
MOV R1, #0

MAIN: MOV SCON, #52H
MOV TMOD, #21H
MOV TH1, #-3
MOV R7, #21
SETB TR1
SETB TF0
MOV IE, #10010010B

LOOP: JB DIRECTION, AGAINCCW

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

SCANCW: CJNE R1, #20, JJCW
MOV R1, #0
JMP LOOP
JJCW: MOV A, R1
MOV 22H, R0
CJNE A, 22H, J2CW
SETB C
J2CW: JC SCANCW
CLR P3.7
CLR P3.3
JMP SCANCW

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

SCANCCW:CJNE R1, #20, JJCCW
MOV R1, #0
JMP LOOP
JJCCW: MOV A, R1
MOV 22H, R0
CJNE A, 22H, J2CCW
SETB C
J2CCW: JC SCANCCW
CLR P3.4
CLR P3.5
JMP SCANCCW

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

T1ISR: CLR TR1
MOV TH1, #HIGH(-48000)
MOV TL1, #LOW(-48000)
DJNZ R7, J1
MOV R7, #20
SETB PSW.1
J1: 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
; thanks for your code, it must have taken you quite a time to do it, really appreciate.
;this is my latest code use to control the speed, i have chosen to use 2 speed only now, #12 and #8. using timer 1 interrupt, the speed of #12 can run at 5 seconds and the speed of #8 can run unlimited after then.
Dear Ian,
I just need some of your help,
in your opinion, is this program code working??
can u please explain the T1ISR part to me??Someone just taught me that, and i am not quite understand.
can u please explain why need mov R7, #21 in this program??
Thank You.
regards,
Malcolm
 

T1ISR: CLR TR1
MOV TH1, #HIGH(-48000)
MOV TL1, #LOW(-48000)
DJNZ R7, J1
MOV R7, #20
SETB PSW.1
J1: RETI

In J1 you will need to SETB TR1 otherwise Timer 1 will not restart ..

Other major problem is in the beginning of code where you have 5x JNB PSW.1 ..

Keep in mind that the main loop of this program is within LOOP .. So, what may happen is, the program will be waiting 1s - the first JNB PSW.1 - and then will pass through the remainig four lines as there is nothing to set this bit to 0, then it will reload R0 with 8 and comfortably holding this value will go to the LOOP and will stay there for ever ..

Also, there is nothing to control the DIRECTION bit and there is missing ORG for JMP T1ISR ..

Why 21?
The input pulse to timer is Clock/12≈1µs ..
The reload value is -48000, which gives you 48000µs per overflow; if you multiply this value by 21 the time will be 48000µs * 21 ≈1second ..

Now you have something to think about ..

Regards,
IanP

BTW, The reload value for 5s in my previous post should be TimeH=15h, TimeL=34h ..
 

    novschopin

    Points: 2
    Helpful Answer Positive Rating
ok thanks Ian, i have setb TR1, so the 5x JNB PSW.1 can work well? (its main function is to let motor run in #12 speed for 5 seconds)then to the #8 speed.

for the ORG of T1ISR, what is the value that i should put? Can you please guide me?

from the previous guide that you given to me, is about the direction control, you told me that if character = 'D', it changes direction. how can i set the character "D" using external input to control the direction??previously, you also mentioned that it can be done by adding a button to any pin. Let's say the pin is P3.1, how can i achieve this and how to write the assembly code?
thank you Ian!!

regards,
Malcolm
 

from the previous guide that you given to me, is about the direction control, you told me that if character = 'D', it changes direction. how can i set the character "D" using external input to control the direction??previously, you also mentioned that it can be done by adding a button to any pin. Let's say the pin is P3.1, how can i achieve this and how to write the assembly code?
This can be added at any time, but for now you have to address more important issue, which is related to:
i have setb TR1, so the 5x JNB PSW.1 can work well? (its main function is to let motor run in #12 speed for 5 seconds)then to the #8 speed.
Counting 5s time has to occur "behind" the main loop and as a result a bit is set/clear to tell the main loop that the time has passed ..
As you have it now, even before you enter the main loop you will spend time doing nothing ( 5 x JNB PSW.1 ) ..
Take a look at my previous suggestion on putting the time count into Timer 0 interrupt sub ..
Code:
; Timer0 interrupt 

T0ISR: 
   PUSH   ACC 
   PUSH   PSW 
   CLR   TR0 
   MOV   TH0, #HIGH(-921)      ; Reload values for TH0 and TL0 .. 
   MOV   TL0, #LOW(-921) 
   INC   R1            ; R1 counts Timer0 interrupts .. 
   INC   DPTR            ; x sec time counter .. 

   CLR   C 
   MOV   A, DPH         ; Check if DPTR = TimeH_TimeL =1000h .. 
   SUBB   A, TimeH 
   JC   T0ISR_Exit         ; < 
   JNZ   THbg            ; > 
   CLR   C            ; = 
   MOV   A, DPL 
   SUBB   A, TimeL 
   JC   T0ISR_Exit         ; < 
   JNZ   T0ISR_Exit         ; > 
   CLR   C            ; =          
   [u][b]SETB   TChange[/b][/u] 

THbg:   MOV   DPTR, #0000h 
    
T0ISR_Exit: 
   POP   PSW 
   POP   ACC 
   SETB   TR0 
   RETI
Then in the main loop:
Code:
Main_Loop: 

AGAIN: 
   JB   TChange, S5_Time 
 ...
And finally:
Code:
S5_Time: 

S_Change: 
   JB   SpeedS, Speed_Up1 
   MOV   R0, #4         ; speed slow .. 
   SETB   SpeedS 
   SJMP   S5_Exit 
Speed_Up1: 
   JB   SpeedM, Speed_Up2 
   MOV   R0, #10         ; speed medium .. 
   SETB   SpeedM 
   SJMP   S5_Exit 
Speed_Up2: 
   JB   SpeedF, Speed_Up3 
   MOV   R0, #17         ; speed fast .. 
   SETB   SpeedF 
   SJMP   S5_Exit 

Speed_Up3: 
   CLR   SpeedS 
   CLR   SpeedM 
   CLR   SpeedF 
   MOV   R0, #0         ; speed = 0 

S5_Exit: 
   CLR   TChange    
   LJMP  Main_Loop
And you go back to the main loop with new R0 value ..

Also, keep in mind that Timer1 is used to generate baud rates, so if you want to use it that way, don't use it for anything else ..

Once this is sorted out a short sub can be added to change direction, but let's talk about it later ..

Regards,
IanP

Btw. When you attach a code, would you mind using
Code:
 ..[code] option ..
It makes the code easier to read ..
 

hi Ian,
i have compiled the code u given and its working fine, but the only thing is that i can only test on tuesday due to public holiday and my hardware is in the lab and my due date is on wednesday!!! swt...
So, i hope u keep in touch with this post and i looking forward for your help.

you mean the code that i have done
Code:
5x JNB PSW.1

the motor won't run and will stop running in that 5 seconds, is that what you meant?

thank you
regards,
Malcolm
 

Code:
 MOV R0, #12 ; motor speed on time 
 MOV R1, #0 
 JNB PSW.1, $ ; 1s delay .. 
 JNB PSW.1, $
 JNB PSW.1, $ 
 JNB PSW.1, $ 
 JNB PSW.1, $
Up till now the motor will not start running ..
Now you load the R0 with:
Code:
 MOV R0, #8 
 MOV R1, #0
and motor will be running at that speed for ever ..

Regards,
IanP

Btw:
Code:
S5_Exit: 
   CLR   TChange    
   RET
RET is wrong .. it should be LJMP Main_Loop ..
 

    novschopin

    Points: 2
    Helpful Answer Positive Rating
Code:
ORG   0000h            ; Start of the MAIN .. 
   JMP   MAIN 
   ORG   000Bh            ; Timer0 interrupt address .. 
   JMP   T0ISR 
   ORG   0023h            ; Serial Interrupt address .. 
   JMP   SISR 

Flags   DATA   20h 
Speed0   BIT   Flags.0      ; Speed0 = stop 0 
SpeedS   BIT   Flags.1      ; SpeedS = slow 8
SpeedM   BIT   Flags.2      ; SpeedM = medium 10 
SpeedF   BIT   Flags.3      ; SpeedF = fast 17 
Any4      BIT   Flags.4 
Any5      BIT   Flags.5 
Any6      BIT   Flags.6 
TChange   BIT   Flags.7 

TimeH   Data   23h 
TimeL   Data   24h 

ORG 0030h 

   MOV    R0, #0         ; motor speed on time .. start from 0 .. 
   MOV    R1, #0 
   MOV   TimeH, #10h         ; If TimeHTimeL=1000h-> speed change .. 
   MOV   TimeL, #00h 
   MOV   DPTR, #0000h 
   MOV   Flags, #00h 

MAIN:    
   MOV   SCON, #52h         ; These 4 lines set up UART for 9600bps .. 
   MOV    TMOD, #21h         ; assuming clock 11,059200MHz .. 
   MOV    TH1, #-3 
   SETB    TR1 

   SETB    TF0 
   MOV   IE, #10010010B      ; IE.7 = 1 enables all interrupts .. 
                  ; IE.4 = 1 enables serial port interrupt .. 
                  ; IE.1 = 1 enables Timer0 overflow interrupt 


Main_Loop: 

AGAINCW: 
   JB   TChange, S5_Time      ; Check if it is time to vary speed .. 

   SETB    P3.7            ; P3.7, 3.3  = 1 ( 5V) 
   SETB   P3.3 
   CLR    P3.4            ; P3.4, 3.5 = 0 (0V) 
   CLR    P3.5 

SCANCW: CJNE    R1, #20, JJ      ; Check if Timer0 interrupt occured 20 times 
   MOV    R1, #0         ; If yes, load speed reference R0 with 0 
   JMP    AGAINCW 
  
JJCW:    MOV   A, R1            ; If not, check what is current R0 reference value .. 
   MOV   22h, R0         ; 22h is just a memory address .. 
   CJNE   A, 22h, J2CW 
   SETB   C            ; If current reference value <= R1 goto SCAN .. 
J2CW:    JC   SCANCW 
   CLR   P3.7            ; Otherwise outputs P3.7 and P3.3 = 0V .. 
   CLR   P3.3 
   JMP   SCANCW

AGAINCCW: 
   JB   TChange, S5_Time      ; Check if it is time to vary speed .. 

   SETB    P3.4            ; P3.7, 3.3  = 1 ( 5V) 
   SETB   P3.5 
   CLR    P3.7             ; P3.4, 3.5 = 0 (0V) 
   CLR    P3.3 

SCANCCW: CJNE    R1, #20, JJ      ; Check if Timer0 interrupt occured 20 times 
   MOV    R1, #0         ; If yes, load speed reference R0 with 0 
   JMP    AGAINCCW 
  
JJCCW:    MOV   A, R1            ; If not, check what is current R0 reference value .. 
   MOV   22h, R0         ; 22h is just a memory address .. 
   CJNE   A, 22h, J2CCW 
   SETB   C            ; If current reference value <= R1 goto SCAN .. 
J2CCW:    JC   SCANCCW 
   CLR   P3.7            ; Otherwise outputs P3.7 and P3.3 = 0V .. 
   CLR   P3.3 
   JMP   SCANCCW 
 

; ================================== 
; Timer0 interrupt 

T0ISR: 
   PUSH   ACC 
   PUSH   PSW 
   CLR   TR0 
   MOV   TH0, #HIGH(-921)      ; Reload values for TH0 and TL0 .. 
   MOV   TL0, #LOW(-921) 
   INC   R1            ; R1 counts Timer0 interrupts .. 
   INC   DPTR            ; x sec time counter .. 

   CLR   C 
   MOV   A, DPH         ; Check if DPTR = TimeH_TimeL =1000h .. 
   SUBB   A, TimeH 
   JC   T0ISR_Exit         ; < 
   JNZ   THbg            ; > 
   CLR   C            ; = 
   MOV   A, DPL 
   SUBB   A, TimeL 
   JC   T0ISR_Exit         ; < 
   JNZ   T0ISR_Exit         ; > 
   CLR   C            ; =          
   SETB   TChange 

THbg:   MOV   DPTR, #0000h 
    
T0ISR_Exit: 
   POP   PSW 
   POP   ACC 
   SETB   TR0 
   RETI 

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

SISR: 
   CLR   RI 
   CLR   TI 
   MOV   A, SBUF 
   CJNE   A, #'F', N1         ; If incoming character = 'F" load R0 with 17d .. 
   MOV   R0, #17 
   RETI 
N1:   CJNE   A, #'M', N2         ; If character = 'M' load R0 with 10d .. 
   MOV   R0, #10 
   RETI 
N2:   CJNE   A, #'S', N3         ; If character = 'S' load R0 with 04d .. 
   MOV   R0, #4 
N3:   RETI 

; ==================================== 
; time subs .. 

S5_Time: 

S_Change: 
   JB   SpeedS, Speed_Up1 
   MOV   R0, #8        ; speed slow .. 
   SETB   SpeedS 
   SJMP   S5_Exit 
Speed_Up1: 
   JB   SpeedM, Speed_Up2 
   MOV   R0, #10         ; speed medium .. 
   SETB   SpeedM 
   SJMP   S5_Exit 
Speed_Up2: 
   JB   SpeedF, Speed_Up3 
   MOV   R0, #17         ; speed fast .. 
   SETB   SpeedF 
   SJMP   S5_Exit 

Speed_Up3: 
   CLR   SpeedS 
   CLR   SpeedM 
   CLR   SpeedF 
   MOV   R0, #0         ; speed = 0 

S5_Exit: 
   CLR   TChange    
   RET 

END

hi Ian, this is the code that i got from you, please feel free to check if there is any mistake done by me, thanks!!!

as for the direction control i wish that you can give me the guide as soon as pssible because there is not enough time for me.
thank you.



regards,
Malcolm
 

Check out this version:
Code:
                       1       $NOPAGING
                       2       $MOD252
                       3    
                       4    
0000                   5       ORG    0000h              ; Start of the MAIN .. 
0000 020040            6       JMP    MAIN 
000B                   7       ORG    000Bh              ; Timer0 interrupt address .. 
000B 020095            8       JMP    T0ISR 
0023                   9       ORG    0023h              ; Serial Interrupt address .. 
0023 0200CD           10       JMP    SISR 
                      11    
  0020                12    Flags     DATA  20h 
  0000                13    Speed0    BIT   Flags.0      ; Speed0 = stop 0 
  0001                14    SpeedS    BIT   Flags.1      ; SpeedS = slow 8 
  0002                15    SpeedM    BIT   Flags.2      ; SpeedM = medium 10 
  0003                16    SpeedF    BIT   Flags.3      ; SpeedF = fast 17 
  0004                17    Any4      BIT   Flags.4 
  0005                18    Any5      BIT   Flags.5 
  0006                19    Direction BIT   Flags.6      ; Direction=0 ->CW .. Direction=1 ->CCW
  0007                20    TChange   BIT   Flags.7      ; 1=5secs has elapsed
  0022                21    Tmp       DATA   22h
  0023                22    TimeH     Data   23h 
  0024                23    TimeL     Data   24h 
                      24    
0030                  25       ORG    0030h 
                      26    
0030 7800             27       MOV    R0, #0              ; motor speed on time .. start from 0 .. 
0032 7900             28       MOV    R1, #0 
0034 752315           29       MOV    TimeH, #15h         ; If TimeHTimeL=1534h-> speed change .. 
0037 752434           30       MOV    TimeL, #34h 
003A 900000           31       MOV    DPTR, #0000h 
003D 752000           32       MOV    Flags, #00h 
                      33    
0040                  34    MAIN:
                      35        
0040 759852           36       MOV    SCON, #52h          ; These 4 lines set up UART for 9600bps .. 
0043 758921           37       MOV    TMOD, #21h          ; assuming clock 11,059200MHz .. 
0046 758DFD           38       MOV    TH1, #-3 
0049 D28E             39       SETB   TR1 
                      40    
004B D28D             41       SETB   TF0 
004D 75A892           42       MOV    IE, #10010010b      ; IE.7 = 1 enables all interrupts .. 
                      43                                  ; IE.4 = 1 enables serial port interrupt .. 
                      44                                  ; IE.1 = 1 enables Timer0 overflow interrupt ..
                      45    
                      46    ; ==========
                      47    
0050                  48    Main_Loop:
                      49    
0050 200621           50       JB     Direction, Again_CCW
                      51    
                      52    ; ==========
                      53    
0053                  54    AGAIN_CW:
0053 1200E5           55       CALL   S5_Time             ; Check if it is time to vary speed .. 
                      56    
0056 D2B7             57       SETB   P3.7                ; P3.7, 3.3  = 1 ( 5V) 
0058 D2B3             58       SETB   P3.3 
005A C2B4             59       CLR    P3.4                ; P3.4, 3.5 = 0 (0V) 
005C C2B5             60       CLR    P3.5 
                      61    
005E                  62    SCAN_CW: 
005E B91404           63       CJNE   R1, #20, JJ_CW      ; Check if Timer0 interrupt occured 20 times 
0061 7900             64       MOV    R1, #0              ; If yes, load speed reference R0 with 0 
0063 80EB             65       JMP    Main_Loop 
                      66      
0065                  67    JJ_CW:
0065 E9               68       MOV    A, R1               ; If not, check current R1 reference value .. 
0066 8822             69       MOV    Tmp, R0 
0068 B52201           70       CJNE   A, Tmp, J2_CW 
006B D3               71       SETB   C                   ; If current reference value <= R0 goto SCAN .. 
006C                  72    J2_CW:
006C 40F0             73       JC     SCAN_CW 
006E C2B7             74       CLR    P3.7                ; Otherwise outputs P3.7 and P3.3 = 0V .. 
0070 C2B3             75       CLR    P3.3 
0072 80EA             76       JMP    SCAN_CW
                      77    
                      78    ; ========== 
                      79    
0074                  80    AGAIN_CCW: 
0074 1200E5           81       CALL  S5_Time              ; Check if it is time to vary speed .. 
                      82    
0077 D2B4             83       SETB   P3.4                ; P3.7, 3.3  = 1 ( 5V) 
0079 D2B5             84       SETB   P3.5 
007B C2B7             85       CLR    P3.7                ; P3.4, 3.5 = 0 (0V) 
007D C2B3             86       CLR    P3.3 
                      87    
007F                  88    SCAN_CCW:
007F B91404           89       CJNE   R1, #20, JJ_CCW     ; Check if Timer0 interrupt occured 20 times 
0082 7900             90       MOV    R1, #0              ; If yes, load speed reference R0 with 0 
0084 80CA             91       JMP    Main_Loop 
                      92      
0086                  93    JJ_CCW:
0086 E9               94       MOV    A, R1               ; If not, check current R0 reference value .. 
0087 8822             95       MOV    Tmp, R0
0089 B52201           96       CJNE   A, Tmp, J2_CCW 
008C D3               97       SETB   C                   ; If current reference value <= R1 goto SCAN .. 
008D                  98    J2_CCW:
008D 40F0             99       JC     SCAN_CCW 
008F C2B7            100       CLR    P3.7                ; Otherwise outputs P3.7 and P3.3 = 0V .. 
0091 C2B3            101       CLR    P3.3 
0093 80EA            102       JMP    SCAN_CCW 
                     103      
                     104    
                     105    ; ================================== 
                     106    ; Timer0 interrupt .. 
                     107    
0095                 108    T0ISR: 
0095 C0E0            109       PUSH   ACC 
0097 C0D0            110       PUSH   PSW 
0099 C28C            111       CLR    TR0 
009B 758CFC          112       MOV    TH0, #HIGH(-921)     ; Reload values for TH0 and TL0 .. 
009E 758A67          113       MOV    TL0, #LOW(-921) 
00A1 09              114       INC    R1                   ; R1 counts Timer0 interrupts .. 
00A2 A3              115       INC    DPTR                 ; x sec time counter .. 
                     116    
00A3 C3              117       CLR    C 
00A4 E583            118       MOV    A, DPH               ; Check if DPTR = TimeH_TimeL =1534h .. 
00A6 9523            119       SUBB   A, TimeH 
00A8 4011            120       JC     T0_Exit              ; < 
00AA 700C            121       JNZ    THbg                 ; > 
00AC C3              122       CLR    C                    ; = 
00AD E582            123       MOV    A, DPL 
00AF 9524            124       SUBB   A, TimeL 
00B1 4008            125       JC     T0_Exit              ; < 
00B3 7006            126       JNZ    T0_Exit              ; > 
00B5 C3              127       CLR    C                    ; =          
00B6 D207            128       SETB   TChange              ; DPTR=1534h .. 
                     129    
00B8                 130    THbg:
00B8 900000          131       MOV    DPTR, #0000h 
                     132    
00BB                 133    T0_Exit:
00BB A2B1            134       MOV    C, P3.1              ; Test if P3.1 SW closed ..
00BD 4005            135       JC     Set_Dir              ; If closed (0V) -> CW ..     
00BF C206            136       CLR    Direction
00C1 0200C6          137       JMP    T0ISR_Exit
00C4                 138    Set_Dir:
00C4 D206            139       SETB   Direction            ; If open (5V) -> CCW .. 
                     140        
00C6                 141    T0ISR_Exit: 
00C6 D0D0            142       POP    PSW 
00C8 D0E0            143       POP    ACC 
00CA D28C            144       SETB   TR0 
00CC 32              145       RETI 
                     146    
                     147    ; =================================== 
                     148    ; Serial port interrupt 
                     149    ; F, M or S will load R0 (speed reference) with selected values .. 
                     150    
00CD                 151    SISR: 
00CD C298            152       CLR   RI 
00CF C299            153       CLR   TI 
00D1 E599            154       MOV   A, SBUF 
00D3 B44603          155       CJNE  A, #'F', N1         ; If incoming character = 'F" load R0 with 17d .. 
00D6 7811            156       MOV   R0, #17 
00D8 32              157       RETI 
00D9                 158    N1:
00D9 B44D03          159       CJNE  A, #'M', N2         ; If character = 'M' load R0 with 10d .. 
00DC 780A            160       MOV   R0, #10 
00DE 32              161       RETI 
00DF                 162    N2:
00DF B45302          163       CJNE  A, #'S', N3         ; If character = 'S' load R0 with 04d .. 
00E2 7804            164       MOV   R0, #4 
00E4                 165    N3:
00E4 32              166       RETI 
                     167    
                     168    ; ==================================== 
                     169    ; time subs .. 
                     170    
00E5                 171    S5_Time: 
00E5 300723          172       JNB   TChange, S5_Exit  
00E8                 173    S_Change: 
00E8 200106          174       JB    SpeedS, Speed_Up1 
00EB 7808            175       MOV   R0, #8              ; speed slow .. 
00ED D201            176       SETB  SpeedS 
00EF 801A            177       SJMP  S5_Exit 
00F1                 178    Speed_Up1: 
00F1 200206          179       JB    SpeedM, Speed_Up2 
00F4 780A            180       MOV   R0, #10             ; speed medium .. 
00F6 D202            181       SETB  SpeedM 
00F8 8011            182       SJMP  S5_Exit 
00FA                 183    Speed_Up2: 
00FA 200306          184       JB    SpeedF, Speed_Up3 
00FD 7811            185       MOV   R0, #17             ; speed fast .. 
00FF D203            186       SETB  SpeedF 
0101 8008            187       SJMP  S5_Exit 
                     188    
0103                 189    Speed_Up3: 
0103 C201            190       CLR   SpeedS 
0105 C202            191       CLR   SpeedM 
0107 C203            192       CLR   SpeedF 
0109 7800            193       MOV   R0, #0              ; speed = 0 .. 
                     194    
010B                 195    S5_Exit: 
010B C207            196       CLR   TChange    
010D 22              197       RET 
                     198    
                     199    END

VERSION 1.2h ASSEMBLY COMPLETE, 0 ERRORS FOUND

Regards,
IanP
 

    novschopin

    Points: 2
    Helpful Answer Positive Rating
thanks Ian, nice one, compiled -> no errors, hope it works.
btw, may i know which port pin of my 89c2051 microcotroller should be used to control the direction (manually), is it pin 1.6 ??
Thank You.

regards,
Malcolm
 

Code:
T0_Exit:
   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 ..

This will be the matter of your choice .. Just change P3.1 to anythig you like ..

Regards,
IanP
 

    novschopin

    Points: 2
    Helpful Answer Positive Rating
oh ya, i just observed that.
btw, their purpose is to declare?? may i know how do these codes function??
Code:
Flags     DATA  20h 
Speed0    BIT   Flags.0      ; Speed0 = stop 0 
SpeedS    BIT   Flags.1      ; SpeedS = slow 8 
SpeedM    BIT   Flags.2      ; SpeedM = medium 10 
SpeedF    BIT   Flags.3      ; SpeedF = fast 17 
Any4      BIT   Flags.4 
Any5      BIT   Flags.5 
Direction BIT   Flags.6      ; Direction=0 ->CW .. Direction=1 ->CCW
TChange   BIT   Flags.7      ; 1=5secs has elapsed

thank you

regrads,
Malcolm
 

Let's take into account bit PSW.1, which is a general purpose bit; what does it mean to you? .. The answer is NOTHING .. if you forget that this is user defined bit you may be under impression that it has something to do with Program Status ..
Now, look at bit named Direction (Here it is Flags.6, but it can be anything else) .. In this case you immediately associate it with certain function, in this case CW or CCW ..
In many cases you don't need to declare/name bits (or Bytes as DATA), but it makes your code more transparent and easier to follow ..

Regards,
IanP
 

    novschopin

    Points: 2
    Helpful Answer Positive Rating
hi Ian,
hope u will be able to check my post in the next 15 hours time haha
thanks.

regards,
Malcolm
 

hi Ian,
another type of more simple feature is needed here.
run the motor in only one speed, and more emphasize on direction control
pls check my code and help to edit it

Code:
	ORG	0000H
	JMP	MAIN
	ORG	000BH
	JMP	T0ISR
	ORG	0023H
	JMP	SISR

	
	ORG	0030H
	FLAGS	DATA 20H
	DIRECTION BIT FLAGS.0
	MOV	R0, #10		; motor speed on time
	MOV	R1, #0
	

MAIN:	MOV	SCON, #52H
	MOV	TMOD, #21H
	MOV	TH1, #-3
	MOV	R7, #21
	SETB	TR1
	SETB	TF0
	MOV	IE, #10010010B

LOOP:	JB DIRECTION, AGAINCCW

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

SCANCW:	CJNE	R1, #20, JJCW
	MOV	R1, #0
	JMP	LOOP
JJCW:	MOV	A, R1
	MOV	22H, R0
	CJNE	A, 22H, J2CW
	SETB	C
J2CW:	JC 	SCANCW
	CLR	P3.7
	CLR	P3.3
	JMP	SCANCW

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

SCANCCW:CJNE	R1, #20, JJCCW
	MOV	R1, #0
	JMP	LOOP
JJCCW:	MOV	A, R1
	MOV	22H, R0
	CJNE	A, 22H, J2CCW
	SETB	C
J2CCW:	JC 	SCANCCW
	CLR	P3.4
	CLR	P3.5
	JMP	SCANCCW	

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

thank you,
regards,
Malcolm
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top