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.

Assembly Variables for PICs!

Status
Not open for further replies.

Peter_wadley

New Member
Hello y'all,

I get so much great help from the members of this forum I would just like to take the time to say.. THANKS!!

I just started programming the Snake game for my PIC16F84a game module (see signature)

I need a bit of help understanding variable use of PICs.

Such as:

Cblock variables..

and EQU variables..

I need to make variables which act as boolean variables.. kind of like digital variables as the PORTS do on the PIC.

What registers of the PIC16F84a can I use as variables?

This is what I want to do:

The snake can either go RIGHT - LEFT - UP or DOWN

There are two buttons one turns the snake LEFT and the other RIGHT

Lets say the snake is moving RIGHT and I press RIGHT ( the snake will move down)

I want to use 4 variables (one for each direction)

The PIC will check these variables to see which way the snake is moving and then change to the next appriopriate postion..

Making sense?

So if its moving RIGHT..

Right_Boolean = 1

Left_Boolean = 0

Up_Boolean = 0

Down_Boolean = 0

I want to be able to say:

Btfsc Right_Boolean
Call Move_Down ; IT IS WAS MOVING Right NOW MOVE Down

Btfsc Left_Boolean
Call Move_Up ; IT IS WAS MOVING Left NOW MOVE UP

Btfsc Down_Boolean
Call Move_Left ; IT IS WAS MOVING Down NOW MOVE Left

Btfsc Up_Boolean
Call Move_Right ; IT IS WAS MOVING Up NOW MOVE Right

Is it possible to do this?

Take Care,
PW
 
cblock and EQU can both used to define your user registers, although cblock is much easier to use if you want to define several.
You just have to specify the start address of the block of regs eg.

cblock 0x20
myreg
myreg1
myreg2
endc

This is the same as:
myreg equ 0x20
myreg1 equ 0x21
myreg2 equ 0x22

It saves you having to specify the address for each reg defined.

For your direction variables, I think you just need to read individual bits of a reg eg.

direction equ 0x20 ; setup a reg to hold the direction bits

; *** variable EQUATES ***
up equ 0 ; bits for direction
down equ 1
left equ 2
right equ 3


then in your program you test the appropriate bit in the direction reg:

Btfsc direction, right
Call Move_Down ; IT IS WAS MOVING Right NOW MOVE Down

Btfsc direction, left
Call Move_Up ; IT IS WAS MOVING Left NOW MOVE UP

Btfsc direction, down
Call Move_Left ; IT IS WAS MOVING Down NOW MOVE Left

Btfsc direction, up
Call Move_Right ; IT IS WAS MOVING Up NOW MOVE Right
 
Peter_wadley said:
Oh! Thank you so much picasm!

I will try this out then let you know if it works or not!

It does! - it's a standard technique.

I commonly declare a variable 'flags', and then declare the various bits within the register as I need them.

You may not be aware?, but all the equ line does is a simple text substitution during assembly of the program - so all occurances of 'up' will be replaced by '0', 'down' will be replaced by '1' etc.
 
Quick question regarding the same subject...

Does an equ make a variable equal to that contents of the address or just value. For example if i said
test equ 5
Does that mean test is actually equal to the number five or is it equal to the contents of resister address 5 ?

Also lets say i want to create a variable equal to a number but i want it to be modifiable thru the program so that it can be used as a counter for example, how would one do that ?

Thanks
pete
 
amdkicksass said:
Does an equ make a variable equal to that contents of the address or just value. For example if i said
test equ 5
Does that mean test is actually equal to the number five or is it equal to the contents of resister address 5 ?

05H is already reserved for the PORTA in 84A.

If you want to equal 05H to the TEST registor simply can do like this
Code:
	movlw	05H
	movwf	TEST
Now the actual value in TEST variable is 5.
TEST variable you must equal or must put as a GP registor in the begining. (Don't equ or use 05H its already reserved)

Code:
	TEST	equ	20H

For more details you can see in the .INC file.
 
Last edited:
The easy way to think about equs is to just imagine that it is a text replacement.

so Test equ 5 will tell the assembler that whenever it finds test it is to replace it with 5.

so,
Code:
	movlw	Test		;	move 5 into W
	org	Test		;	start assembling code at location 5
	call	0x200+Test	;	call location 0x205
	movfw	Test		;	move the value on port a into W

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top