Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

If Statement in assembly (for PIC)

Status
Not open for further replies.

adrianvon

Member
Hi all,

I have the following assembly program which is an ADC. It is monitoring the analogue input at AN0 and output its respective binary value to port D.

Code:
;REGISTER DECLERATIONS

REG1 EQU 60H ;;HEARE WE ARE NAMING THE GENERAL PURPOSE REGISTORS
ADC_H EQU 61H
ADC_L EQU 62H
COUNTER EQU 63H

ORG 0000H
	

	ORG 0004H				

	ORG 00008H				;HIGH PRIORITY VECTOR


	ORG 000018H				;LOW PRIORITY INTERRUPT VECTOR

	

ORG 0020H


;ALL INTIALIZATION HERE OF ALL PORTS AND PERIPHERALS HERE

MOVLW B'00001110' ;;SET RAO AS AN0
MOVWF ADCON1

MOVLW B'00101100' ;LEFT JUSTIFIED
MOVWF ADCON2 ;;ACQUSITION TIME
				;;12TAD
				;;CLOCK FREQ FOSC/4
MOVLW B'00000001' ;;CHONNEL 0
MOVWF ADCON0 ;;ADC ON

MOVLW B'111'
MOVWF TRISE

MOVLW B'111111' ;;DECLARING PORT AS INPUT
MOVWF TRISA

CLRF PORTD
CLRF TRISD


;START MAIN PROGRAM HERE
MAIN

		CALL ACQ_DELAY ;CALL DELAY
		BSF ADCON0,1 ;SET ADCON0 bit 1 high
		LINE1
		BTFSC ADCON0,1 ;check if ADCON0 bit 1 if clear,if yes skip the next instruction
		BRA LINE1
		MOVFF ADRESH, ADC_H ;copying the value from ARESH into ADC_H
		MOVFF ADRESH, PORTD ;copying the value from ARESH into portD
		MOVFF ADRESL, ADC_L ;copying the value from ARESH into ADC_L
		GOTO MAIN

ACQ_DELAY

	MOVLW .3
	MOVWF COUNTER
	ACQ1
	DECFSZ COUNTER
	BRA ACQ1
	RETURN

Now i want to save the analogue input value to Register X, compare it to a value in register Y and if the value of Resister X is smaller, PORT B,0 will go to high, else it will remain low.

Can someone help me in doing this please.

BTW im using an 18F4550 PIC micro-controller.

Thanks in advance...
 
Check the PICList for all kinds of software routines (such as the conditional one you need), although it's mostly 16F series - but simple to convert for 18F.
 
Hi,

You just use simple 18F assembler instructions such as this.


; to compare values

movf YVALUE, W
cpfseq XVALUE
goto notequal

; is equal so skip to here;

See the Intruction Set at the back of the 4550s datasheet for details of CPFSEQ

Is that code your own or some borrowed stuff, it looks rather hacked about and incomplete, are you trying to run it in a chip or simulator ?
 
Thanks for your reply Wp100.

Do i have to declare register W ?

I did the following coding where two switches are used to increment and decrement a value which is then saved in REG1 and the respective value is displayed at port D.

I also used your coding so that when the value in REG1 is greater than 00110000, an LED at port C,2 turns on, else it remains off.

Code:
;REGISTER DECLERATIONS

REG1 EQU 60H
REG2 EQU 61H
REG3 EQU 62H
REG4 EQU 63H
REG5 EQU 64H

ORG 0000H
	

	ORG 0004H				

	ORG 00008H				;HIGH PRIORITY VECTOR


	ORG 000018H				;LOW PRIORITY INTERRUPT VECTOR

	

ORG 0020H


;ALL INTIALIZATION HERE OF ALL PORTS AND PERIPHERALS HERE



MOVLW B'00000000' 
MOVWF TRISD

MOVLW B'11111111' 
MOVWF TRISB

MOVLW B'000000' 
MOVWF TRISC

;START MAIN PROGRAM HERE

CLRF PORTD

MAIN

BTFSC PORTB,6
GOTO INC
LINE5
BTFSC PORTB,7
GOTO DEC
LINE6
MOVFF REG1, PORTD


MOVF B'00110000', W
CPFSGT REG1
BSF PORTC,2
BCF PORTC,2

BRA MAIN

INC
CALL DEBOUNCE_DELAY
INCF REG1
CALL DELAY
GOTO LINE5

DEC
CALL DEBOUNCE_DELAY
DECF REG1
CALL DELAY
GOTO LINE6

DEBOUNCE_DELAY
CLRF REG2
LINE1_DEBOUNCE_DELAY
DECFSZ REG2
BRA LINE1_DEBOUNCE_DELAY
RETURN

DELAY
CLRF REG3
CLRF REG4
MOVLW 15
MOVWF REG5
LINE1
DECFSZ REG3
BRA LINE1
DECFSZ REG4
BRA LINE1
DECFSZ REG5
BRA LINE1
RETURN

Now my problem is with the below coding since the LED at portC,2 is not working as expected...

Code:
MOVF B'00110000', W
CPFSGT REG1
BSF PORTC,2
BCF PORTC,2

If you can please help me in this i will appreciate it...

And by the way, i have seen the data sheet but i cannot understand it :( It gives an example (the one below) but i cannot understand what some of the statements are referring to (such as HERE) ??

Code:
Example: HERE CPFSGT REG, 0
NGREATER :
GREATER :

Before Instruction:
PC = Address (HERE)
W = ?

After Instruction:
If REG > W;
PC = Address (GREATER)
If REG ≤ W;
PC = Address (NGREATER)

Is that code your own or some borrowed stuff, it looks rather hacked about and incomplete, are you trying to run it in a chip or simulator ?

The code was given to me by a lecturer and yes i am trying it on a chip.

Thanks in advance...
 
Last edited:
Hi,


The CPFSEQ instruction is similar to the BTFSS in that the instruction immediately following it should normally be a GOTO instruction.
You are simply setting that Port On then Off with the next instruction.


I would be interesting to see the whole code you have been given.

Without telling you all what needs doing all I can suggest is you follow a few tutorials in Assembler.

While the Beginners Sticky at the top of the forum suggests many assembler tutorial sites most are for the 16F range.
The 18Fs, though 90% similar, do have some important differences.

Have a look for 18F tutorials that may help you - here one, using the 4550.
https://www.pic18f.com/tutorial/2007/12/06/tutorial-2-hello-world/
 
Hi,

Ok i managed to do it :) i used the following code:

Code:
MOVFF REG1, F                 
MOVLW .5                   
CPFSGT REG1
BSF PORTE,0
BCF PORTE,0

But i have another problem... the LED which is connected to PORTE,0 is very dim . I checked the output voltage and its showing olny 0.3V. I also changed to port and the same is happening.

Do you know what may be causing this problem please??

Thanks
 
Hi,

Think you have missed the point I mentioned - try this

Code:
	movlw	.5       ; load W with decimal 5
	cpfseq	REG1     ; compare the value in REG1 with the value in W  - if they are the same, skip the next instruction
	goto	unequal	 ; if they are not the same goto routine unequal

	bsf		PORTE,0	; they are equal so turn on port
					; REST OF THE CODE
;;
;

unequal  bcf PORTE,0  ; turn off port

					; REST OF THE CODE
;;
;


Before you can Output to PORTE,0 you must turn off its default Analogue setting and configure it as an Output - there is some example assembler code in the Datasheet in the PORT E section.
 
Yes i have set port E as output as shown in the coding below (in the INTIALIZATIONS section). [ MOVLW B'000' ....... MOVWF TRISE ]

Code:
;REGISTER DECLERATIONS

REG1 EQU 60H
REG2 EQU 61H
REG3 EQU 62H
REG4 EQU 63H
REG5 EQU 64H
F EQU 65H

ORG 0000H
	

	ORG 0004H				

	ORG 00008H				;HIGH PRIORITY VECTOR


	ORG 000018H				;LOW PRIORITY INTERRUPT VECTOR

	

ORG 0020H


;INTIALIZATIONS 


MOVLW B'00000000' 
MOVWF TRISD

MOVLW B'11111111' 
MOVWF TRISB

MOVLW B'000' 
MOVWF TRISE

;START MAIN PROGRAM HERE

CLRF PORTD
CLRF PORTE

MAIN

BTFSC PORTB,6
GOTO INC
LINE5
BTFSC PORTB,7
GOTO DEC
LINE6
MOVFF REG1, PORTD

MOVFF REG1, F                 
MOVLW .5                 
CPFSGT REG1
BSF PORTE,0
BCF PORTE,0

BRA MAIN

INC
CALL DEBOUNCE_DELAY
INCF REG1
CALL DELAY
GOTO LINE5

DEC
CALL DEBOUNCE_DELAY
DECF REG1
CALL DELAY
GOTO LINE6

DEBOUNCE_DELAY
CLRF REG2
LINE1_DEBOUNCE_DELAY
DECFSZ REG2
BRA LINE1_DEBOUNCE_DELAY
RETURN

DELAY
CLRF REG3
CLRF REG4
MOVLW 15
MOVWF REG5
LINE1
DECFSZ REG3
BRA LINE1
DECFSZ REG4
BRA LINE1
DECFSZ REG5
BRA LINE1
RETURN

By the way im using CPFSGT not CPFSEQ.

Thanks in advance
 
OK, problem solved :)

The problem was that you cannot use BSF or BCF after CPFSGT, so i did the coding as the one below and it worked fine:

Code:
MOVFF REG1, F                 
MOVLW .5                 
CPFSGT REG1
GOTO LINE9
GOTO LINE8

LINE9
BSF PORTE,0
GOTO LINE7

LINE8
BCF PORTE,0
LINE7

Thanks alot for your help.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top