Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Thread Tools Display Modes
Old 14th May 2008, 03:35 PM   (permalink)
Experienced Member
danuke is on a distinguished road
Default MPLab variables

I'm trying to write a program that will keep a count going using variables, but have discovered something about my code causes the variable to return to the original set value when a jump occurs. I'm using a variable because I couldn't get the if statements to work with a memory location variable.

The loop part of my code looks like:
Code:
NXTCue				;Start the show
	call	Time	; Get delay time
	TDelay			; Delay for time called for in Time table
	Cu				; Set pins to advance firing
	movlw	d'5'	; Hold the pin high for
	TDelay			; WREG x 100ms
	clrout			; Clear Outputs
CN set	CN+1		; increment Cue number by 1
	goto	NXTCue	; Return to start and do it again
and the Cu command is a macro containing:
Code:
Cu	MACRO 
	if CN == 1
		bsf	S1
		bsf	S3
	endif
	if CN == 2
		bsf	S7
		bsf S18
	endif
	if CN == 3
		bsf S9
	endif
	endm
I'm stuck as to how I can accomplish the if statement using a method that doesn't reset upon the goto command at the end of the time sequence. I would use a table for the project, but it will be a different number of commands for each loop that I want carried out, and the only way I could fill them would be nop's -- which I feel would be a waste of memory for a long table.

Does anybody have any suggestions how how I can get such code to work without being a HUGE waste of memory in the PIC?

Thanks,
Joe
danuke is offline   Reply With Quote
Old 14th May 2008, 03:42 PM   (permalink)
Super Moderator
 
Nigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to beholdNigel Goodwin is a splendid one to behold
Default

I feel you're confusing assembler instructions and PIC instructions, IF and CN+1 are instructions to the assembler at assemble time, and have no effect at run time.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline   Reply With Quote
Old 14th May 2008, 03:43 PM   (permalink)
Experienced Member
 
Blog Entries: 4
blueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to beholdblueroomelectronics is a splendid one to behold
Send a message via Skype™ to blueroomelectronics
Default

"goto PC+W"
W is loaded with the jump location offset is often popular. Terminate with a "retlw"
How large is the table?
__________________
Bill
Smart Kits build Smart People

http://www.blueroomelectronics.com
blueroomelectronics is offline   Reply With Quote
Old 14th May 2008, 03:54 PM   (permalink)
Experienced Member
danuke is on a distinguished road
Default

Quote:
Originally Posted by Nigel Goodwin
I feel you're confusing assembler instructions and PIC instructions, IF and CN+1 are instructions to the assembler at assemble time, and have no effect at run time.
I'm not trying to use the if or CN+1 as a runtime command. Correct me if I misunderstand this, but the if command works sort of like a macro. When the statement is true for the if command, it will replace the series of if commands with the statements inside the true if command while not including the other that are not true. Is that correct?

If so, it's acting as intended for that part of the program. I'm having trouble with the CN variable. I'm using the CN variable inside the assembler to dictate which if command is true. I tested it before putting it in the loop as just a series of Cu commands and it ran through all the series without any trouble. When I put it inside the loop it resets to the original value set at the beginning of the code where I was declaring memory location variables. I added the code below and found it is incrementing fine and then resets after the goto statement.

Code:
NXTCue				;Start the show
	movlw	CN
	movwf	TEMP
	call	Time	; Get delay time
	TDelay			; Delay for time called for in Time table
	Cu				; Set pins to advance firing
	movlw	d'5'	; Hold the pin high for
	TDelay			; WREG x 100ms
	clrout			; Clear Outputs
CN set	CN+1		; increment Cue number by 1
	movlw	CN
	movwf	TEMP
	goto	NXTCue	; Return to start and do it again
Quote:
Originally Posted by blueroomelectronics
"goto PC+W"
W is loaded with the jump location offset is often popular. Terminate with a "retlw"
How large is the table?

Are you saying to replace the " goto NXTCue ; Return to start and do it again" line with the "goto PC+W"?

The table may be as short as 30 table locations or as long as 300 locations. Each containing anywhere from 1 bsf command to 15 bsf commands.
danuke is offline   Reply With Quote
Old 14th May 2008, 05:32 PM   (permalink)
Experienced Member
 
mike50 has a spectacular aura about
Default

Nigel is right, you are confused about macro variables.

The value of CN is always zero at NXTCue and is always 1 after the "CN set CN+1". CN does not get incremented at run time.

Mike
mike50 is offline   Reply With Quote
Old 14th May 2008, 06:59 PM   (permalink)
Experienced Member
danuke is on a distinguished road
Default

After taking a break from it, it's sinking in more why it performs the way it does at run time. Is there a way to use a memory variable with "if" statements or something similar? I'm trying to keep from using a table if possible because of the variance of how many instructions are performed on each call/command. I may end up being better off to setup each command in a macro and call them from the table like that.

I also played with the while statement a little bit and never could get it to follow the count, but that was earlier. I haven't really put much thought into how this works since I stepped away from the project for a while.

Thanks again for the help. Everyones responses make it clearer how the PICs work with the assembler.
danuke is offline   Reply With Quote
Old 14th May 2008, 07:16 PM   (permalink)
Experienced Member
 
futz is a name known to allfutz is a name known to allfutz is a name known to allfutz is a name known to allfutz is a name known to allfutz is a name known to all
Default

Quote:
Originally Posted by danuke
After taking a break from it, it's sinking in more why it performs the way it does at run time. Is there a way to use a memory variable with "if" statements or something similar? I'm trying to keep from using a table if possible because of the variance of how many instructions are performed on each call/command. I may end up being better off to setup each command in a macro and call them from the table like that.

I also played with the while statement a little bit and never could get it to follow the count, but that was earlier. I haven't really put much thought into how this works since I stepped away from the project for a while.
It appears that you're kind of trying to make assembler into something it's not. It's not a high level language. There are no IF or WHILE statements. But you can make your own very easily.

Look in the datasheet for your PIC at the Instruction Set Summary. Almost every instruction with an F in it can be applied to a variable. Those are the commands you use. There are lots of tests and compares and conditional jumps you can combine in a myriad of different ways to accomplish what you want to do.

Use a cblock to define your variables:
Code:
	cblock	0x20
	d1,d2,d3
	endc
The number is the start address of the block of RAM where you're putting those d1, d2 and d3 variables. They're one byte each. This definition goes in your code before the main program and ISR orgs (outside program memory).

If you're just starting out, leave macros alone for now. Learn the basics first.

If you're having trouble grasping the low level-ness of assembler (it's not terribly difficult - keep trying), you could switch to C. There are several good free C compilers available. All your favorite higher level instructions are there. I recommend SourceBoost BoostC. A good useable demo, and when you hit the limit on that, a full license is around $75. Good compiler.
__________________
=========================
Futz's Microcontrollers & Robotics
=========================

Last edited by futz; 14th May 2008 at 07:22 PM.
futz is online now   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Latest
Help with ICD2 clone!!!!!!!!!!!! juanbbv Micro Controllers 17 5th April 2008 05:07 AM
Assembly Variables for PICs! Peter_wadley Micro Controllers 9 22nd June 2007 05:04 AM
Debugging 877A with MPLAB williB Micro Controllers 10 7th May 2007 03:53 PM
Need help badly on Inchworm and MPLAB thushy Micro Controllers 14 11th March 2007 06:05 PM
Inchworm project started williB Micro Controllers 69 5th March 2007 06:55 PM



All times are GMT. The time now is 05:51 AM.


Electronic Circuits  |  Radio Controlled
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.