Searching \ for '[PIC]: How to PROPERLY define and use variables?' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/devices.htm?key=pic
Search entire site for: 'How to PROPERLY define and use variables?'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: How to PROPERLY define and use variables?'
2001\09\17@170403 by Allen Mahurin

picon face
First off, hello all!  I'm new to the piclist, but
have read SEVERAL pages over these last few weeks.
This place seems like a GREAT resource for resolving
PIC problem ... but I haven't seen anything addressing
my situation (maybe I'm strange).

What I'm working on is multiplexing 5 7-segment
displays using a 16F84.  I'm using 7 I/O pins for the
segments, and 5 more pins to enable each (through a
transistors, to tie the common cathode to ground).  My
problem is that I'm trying to do it as follows:

I have a variable defined in the top of the program:

DispNum EQU             0x10            ; selected display variable

Of course, I have lines of code to set the output
pins, clear them all, etc.  Down in the body of the
code (where I want to use the variable), I have this
within a loop:

movwf PortB                     ; Now, display the new
                                 digit
bsf PortA,DispNum               ; Enable the next
                                 display
call Delay1                     ; Execute a delay
bcf PortA,DispNum               ; Disable the display

My reasoning for doing this is because I'm more
familiar with BASIC than Assembly (although I am
learning).  I don't want to use a constant for the
pin number because the pin number changes each loop
cycle.  So PortA,1 will light on one cycle, PortA,2 on
the next, etc.  Each time the loop count increments, I
want the DispNum variable to increment, and address
the pin number to be active.

When I assemble it, I get no errors.  But when I
simulate it, PortA pin one is the only one that goes
high (not sure why).  I've tried changing the
declaration statement to #DEFINE and #DECLARE, but
haven't gotten either to work.  Can anyone tell me how
to declare a variable, so that I can use the
value stored IN the variable in the above lines of
code?  Please? ...

Thanks for your time,


Allen T. Mahurin (ATM)

__________________________________________________
Terrorist Attacks on U.S. - How can you help?
Donate cash, emergency relief information
http://dailynews.yahoo.com/fc/US/Emergency_Information/

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email spam_OUTlistservTakeThisOuTspammitvma.mit.edu with SET PICList DIGEST in the body


2001\09\17@175117 by Brandon Fosdick

flavicon
face
Allen Mahurin wrote:
<snip>
> When I assemble it, I get no errors.  But when I
> simulate it, PortA pin one is the only one that goes
> high (not sure why).  I've tried changing the
> declaration statement to #DEFINE and #DECLARE, but
> haven't gotten either to work.  Can anyone tell me how
> to declare a variable, so that I can use the
> value stored IN the variable in the above lines of
> code?  Please? ...

You don't really declare variables in PIC assembly. Instead you define
the RAM location the variable is stored in. Your "DispNum EQU 0x10" line
tells the assembler to associate the number 0x10 with the label
"DispNum", its not a variable in the Basic or C sense. #define and
#declare are the same way. When you do something like "movf DispNum, w"
you're telling the assembler to emit an instruction that moves the byte
at address 0x10 into the W register.

Unfortunately, the bit oriented instructions don't take addresses for
the second argument, only constants. For your application you can use a
RAM location to store the bit number, then when you want to modify a
specified bit, convert the bit number to a bitmask and use that to
modify the appropriate register (PORTA in the above case, but beware of
read-modify-write issues with the PORTs). The source code library on
piclist.org has some examples of just how to do this.

Good luck

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email .....listservKILLspamspam@spam@mitvma.mit.edu with SET PICList DIGEST in the body


2001\09\17@181952 by Allen Mahurin

picon face
Hmmm ...
Thanks for the quick reply!  I'll go check for what
you refered to.  I didn't see anything on my situation
there, but maybe because I wasn't too sure of what to
look for.  Even now I'm not _entirely_ sure of what
you're describing ... but I've got an idea.  Like I
said previously, I'm new to PICs.  The last time I
used assembly was on a 6805 9 years ago.

I guess since there are only 5 displays I could do
this with 5 separate routines to make it easy (no
variable needed, just address each pin in each
routine).  Anyway, I'll check int your recommendation.
If anyone else has any ideas, I'm still open to
suggestions!   ;)

Thanks again,

ATM
--- Brandon Fosdick <bfozspamKILLspamGLUE.UMD.EDU> wrote:
{Quote hidden}

__________________________________________________
Terrorist Attacks on U.S. - How can you help?
Donate cash, emergency relief information
http://dailynews.yahoo.com/fc/US/Emergency_Information/

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email .....listservKILLspamspam.....mitvma.mit.edu with SET PICList DIGEST in the body


2001\09\17@210758 by Olin Lathrop

face picon face
> You don't really declare variables in PIC assembly. Instead you define
> the RAM location the variable is stored in. Your "DispNum EQU 0x10" line
> tells the assembler to associate the number 0x10 with the label
> "DispNum", its not a variable in the Basic or C sense. #define and
> #declare are the same way. When you do something like "movf DispNum, w"
> you're telling the assembler to emit an instruction that moves the byte
> at address 0x10 into the W register.

By the way, you don't have to code hard addresses for variables or starting
addresses for blocks of variables.  If you use the linker, you can allocate
variables with the RES directive.


********************************************************************
Olin Lathrop, embedded systems consultant in Littleton Massachusetts
(978) 742-9014, EraseMEolinspam_OUTspamTakeThisOuTembedinc.com, http://www.embedinc.com

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email listservspamspam_OUTmitvma.mit.edu with SET PICList DIGEST in the body


2001\09\17@210811 by Olin Lathrop

face picon face
> DispNum EQU             0x10            ; selected display variable
>
> bsf PortA,DispNum               ; Enable the next
>                                   display
> call Delay1                     ; Execute a delay
> bcf PortA,DispNum               ; Disable the display

I haven't read your whole post, but something is definitely wrong here.  The
top line sets the assembler contant DISPNUM to 16.  You then try to use that
as a bit number in BSF and BCF instructions.  Bit numbers go from 0 to 7,
since this is an 8 bit machine.

I think you were trying to create the variable DISPNUM, then later use its
value to access a specific bit.  The BSF and BCF instructions only work on
fixed bit numbers.  The bit number is coded into the instruction.  There is
no single instruction like BSF to access a bit selected at run time.  You
have to do that by creating a mask for the bit and using AND, OR, or XOR,
depending on what you want to do.


********************************************************************
Olin Lathrop, embedded systems consultant in Littleton Massachusetts
(978) 742-9014, @spam@olinKILLspamspamembedinc.com, http://www.embedinc.com

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email KILLspamlistservKILLspamspammitvma.mit.edu with SET PICList DIGEST in the body


2001\09\17@214009 by Allen Mahurin

picon face
Thanks for the reply.  This, along with the previous
reply, make more sense to me no.  Should have seen
that one ...  ;)

Oh, and you were correct about what I was _trying_ to
do.

Thanks again,

ATM

--- Olin Lathrop <RemoveMEolin_piclistTakeThisOuTspamEMBEDINC.COM> wrote:
{Quote hidden}

********************************************************************
{Quote hidden}

__________________________________________________
Terrorist Attacks on U.S. - How can you help?
Donate cash, emergency relief information
http://dailynews.yahoo.com/fc/US/Emergency_Information/

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email RemoveMElistservspamTakeThisOuTmitvma.mit.edu with SET PICList DIGEST in the body


2001\09\17@214357 by Heinz Czychun

flavicon
face
Hi Allen,

       You sound somewhat confused by Brandon's answer. While I'm
sure other's could explain this better I'll have a go. There was
actually quite a lot thereto take in one gulp.

       You don't need to write 5 seperate subroutines, just change
the way to get the results you want.

       Instead of using the Bit set/clear instructions, move a
variable to PortA that sets the bits the way you want. For example:


if you want     bit 0   set     move 0x01
               bit 1   set     move 0x02
               .
               .
               bit 4   set     move 0x10

       That's what Brandon meant by bitmasking. So first mov 0x01 to
PortA,  and then left shift the variable, and mov it to PortA, for
each successive display.
       Moving a variable manipulated in a seperate register and the
moving it to the output port is called register shadowing.
       Actually you could try moving 0x01 to PortA and then left
shift the portA register, but then you would have to worry about the
read modify/write issue as Brandon mentioned. This, and I'm not
entirely clear here, means that when you write the port it actually
reads the port in the same instruction, so if anything external holds
the pin high or low, or even if there is not enough time to allow the
pin to get to the desired state (same thing I guess) then this state
is read in and acted on. Internal registers (those not exposed to
external pins) don't suffer from this effect.

Hope this clears things up a little,
Heinz



At 3:17 PM -0700 9/17/01, Allen Mahurin wrote:
{Quote hidden}

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email EraseMElistservspammitvma.mit.edu with SET PICList DIGEST in the body


2001\09\17@215746 by Bob Barr

picon face
Olin Lathrop wrote:
>
>By the way, you don't have to code hard addresses for variables or starting
>addresses for blocks of variables.  If you use the linker, you can allocate
>variables with the RES directive.
>

As an aside, even if you aren't using the linker, there is another way to
allocate variables. I find it preferable.

The CBLOCK directive works similarly to the RES directive to set aside the
variable space without having to hard-code addresses.

(Next step for me - get up to speed on relocatable coding and linking.
Thanks, Olin, for posting all the information that you have on your
website.)

Regards, Bob


_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email RemoveMElistservEraseMEspamEraseMEmitvma.mit.edu with SET PICList DIGEST in the body


2001\09\17@221914 by Bob Barr

picon face
Olin Lathrop wrote:
{Quote hidden}

I would have expected an assembly error but I just assembled that code and
didn't get an error.

I did get a warning (#202, 'out of range' or some such). This is  another
example of ignoring warnings that would tell exactly where a potential
problem was located.


To the original poster (Sorry, I've lost your name.):

Don't ignore warnings! They may not always be entirely clear but they almost
always indicate the potential for an error in your program.

Regards, Bob

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp

--
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email RemoveMElistservspam_OUTspamKILLspammitvma.mit.edu with SET PICList DIGEST in the body


2001\09\18@024120 by Gennette, Bruce

flavicon
face
--- Brandon Fosdick <RemoveMEbfozTakeThisOuTspamspamGLUE.UMD.EDU> wrote:
> <snip>
> Unfortunately, the bit oriented instructions don't
> take addresses for
> the second argument, only constants. For your
> application you can use a
> RAM location to store the bit number, then when you
> want to modify a
> specified bit, convert the bit number to a bitmask
> and use that to
> modify the appropriate register (PORTA in the above
> case, but beware of
> read-modify-write issues with the PORTs). The source
> code library on
> piclist.org has some examples of just how to do
> this.

Try this technique -

Create a variable that points to the current display by having just 1 bit
set in it.  This becomes a type of flag for holding the bit position.
When you need to use it you load it into w, clear the carry bit, then rotate
the variable to the right (or left), check that it is within bounds, then
move (copy) it to the port.
Other parts of your program can inspect or modify this variable as required
to do various (clever) things.

For bounds checking you just inspect the carry bit after each rotate if
using the full range of bits 0-->7. If using less than the full width then
you have to check carry after a rotate right and a mask (containing a 1 in
the bit after your highest bit) after a rotate left.

Bye.

__________________________________________________
http://www.piclist.com#nomail Going offline? Don't AutoReply us!
email EraseMElistservspamspamspamBeGonemitvma.mit.edu with SET PICList DIGEST in the body

--
http://www.piclist.com hint: To leave the PICList
RemoveMEpiclist-unsubscribe-requestKILLspamspammitvma.mit.edu


2001\09\18@082401 by Allen Mahurin

picon face
Bob, Heinz, and others:

Thanks again for all your quick replies.  I think I'm
going to like this place.   ;)

I didn't ignore the warning ... just didn't know
exactly what it meant.  And since the lower portion of
the address is all I was concerned with (or o I would
think), it seems like it would be OK.  My next step is
to find and memorize what each warning message means
(I've already been looking over error messages).

I understood what most of y'all said ... Heinz mostly.
I think I'm going to give it a go.  I already thought
about the rotate command, but wasn't looking at doing
it to a secondary address - just directly to PortA
(which would make my display deviate from its correct
pattern, and is why I didn't do it).  I'm going to
give that secondary address a shot, manipulate it,
then dump it into PortA.

Thanks!

Allen T. Mahurin (ATM)
--- Bob Barr <bob_barrSTOPspamspamspam_OUTHOTMAIL.COM> wrote:
> To the original poster (Sorry, I've lost your
> name.):
>
> Don't ignore warnings! They may not always be
> entirely clear but they almost
> always indicate the potential for an error in your
> program.
>
> Regards, Bob

__________________________________________________
Terrorist Attacks on U.S. - How can you help?
Donate cash, emergency relief information
http://dailynews.yahoo.com/fc/US/Emergency_Information/

--
http://www.piclist.com hint: To leave the PICList
spamBeGonepiclist-unsubscribe-requestSTOPspamspamEraseMEmitvma.mit.edu


2001\09\18@082701 by Allen Mahurin

picon face
Thanks Brandon (didn't want to miss anyone),

Part of my program which I didn't post does include a
"bounds" or range check, to see if I've hit the end of
my useable range.  That part works, because I'm
checking the variable ... it just wouldn't update
PortA, and now I see why.  Your suggestion is similar
to what Heinz said, and is the route I'm gonna try
next.  Funny how the answer is sometimes SO obvious,
and easy to see, unless you're the one looking
directly at the problem (that would be me).

Thanks,

ATM
--- "Gennette, Bruce" <KILLspambruce.gennettespamBeGonespamTAFE.NSW.EDU.AU>
wrote:
{Quote hidden}

__________________________________________________
Terrorist Attacks on U.S. - How can you help?
Donate cash, emergency relief information
http://dailynews.yahoo.com/fc/US/Emergency_Information/

--
http://www.piclist.com hint: To leave the PICList
EraseMEpiclist-unsubscribe-requestspamEraseMEmitvma.mit.edu


2001\09\18@102049 by Bob Barr

picon face
Allen Mahurin wrote:
>
>I didn't ignore the warning ... just didn't know
>exactly what it meant.  And since the lower portion of
>the address is all I was concerned with (or o I would
>think), it seems like it would be OK.  My next step is
>to find and memorize what each warning message means
>(I've already been looking over error messages).
>

Sorry, my response may have sounded a bit more harsh than I meant it to be.
:=)

There's probably no need to memorize what each warning means (unless you get
a *lot* of warnings). Just be aware that the assembler is telling you that
you're doing something that it thinks is weird.

I may be paranoid about warnings, but I make it my standard practice to
avoid having any warnings at all. I was taught many years ago (more than I
think I'd care to admit to) that warnings should be treated as if they were
errors.

I've never been comfortable with the 'Just ignore it, it's only a warning.'
approach. If you're used to getting a dozen warnings every time you assemble
your code, are you really likely to notice when one new warning appears?
Probably not.

Regards, Bob



_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp

--
http://www.piclist.com hint: To leave the PICList
@spam@piclist-unsubscribe-request@spam@spamspam_OUTmitvma.mit.edu


2001\09\18@141436 by Harold M Hallikainen

picon face
On Tue, 18 Sep 2001 05:20:54 -0700 Allen Mahurin <spamBeGonewild_wirespamKILLspamYAHOO.COM>
writes:
> pattern, and is why I didn't do it).  I'm going to
> give that secondary address a shot, manipulate it,
> then dump it into PortA.
>
>

       And watch out for RA4! It's an open drain output on most (all?) PICs.

Harold



FCC Rules Online at http://hallikainen.com/FccRules
Lighting control for theatre and television at http://www.dovesystems.com

________________________________________________________________
GET INTERNET ACCESS FROM JUNO!
Juno offers FREE or PREMIUM Internet access for less!
Join Juno today!  For your FREE software, visit:
dl.http://www.juno.com/get/tagj.

--
http://www.piclist.com hint: To leave the PICList
.....piclist-unsubscribe-requestspam_OUTspammitvma.mit.edu


2001\09\18@141645 by Harold M Hallikainen

picon face
       As mentioned previously, the bit number in the BSF and BCF instructions
is coded into the instruction, so you can't change it at run time. One
thing you COULD do is to set the carry bit or a bit in a rotate byte of
RAM, then do a rotate instruction each time around the loop. Copy the
rotating byte to the port to set the appropriate bit.
       On defining variables, Microchip tends to make use of EQU in their
sample code. This makes YOU responsible for assigning addresses of
variables, a job better suited to a machine.  I instead use CBLOCK.  At
the top of my code, I do something like

       cblock          0x20    ; Set start of user ram at 0x20
       endc

       Then, whenever I need another variable, I do stuff like

 cblock
       ThisVariable
       ThatVariable
       ThisArray:0x20
 endc

       I typically put these cblocks at the beginning of each subroutine,
emulating the encapsulation of higher level languages. This is far easier
than defining all variables in one location, then trying to keep track of
them through all your code.

Harold



On Mon, 17 Sep 2001 15:17:13 -0700 Allen Mahurin <TakeThisOuTwild_wire.....spamTakeThisOuTYAHOO.COM>
writes:
{Quote hidden}

FCC Rules Online at http://hallikainen.com/FccRules
Lighting control for theatre and television at http://www.dovesystems.com

________________________________________________________________
GET INTERNET ACCESS FROM JUNO!
Juno offers FREE or PREMIUM Internet access for less!
Join Juno today!  For your FREE software, visit:
dl.http://www.juno.com/get/tagj.

--
http://www.piclist.com hint: To leave the PICList
TakeThisOuTpiclist-unsubscribe-requestKILLspamspamspammitvma.mit.edu


2001\09\18@194550 by Allen Mahurin

picon face
No problem ... I didn't take it in a harsh way.  :)  I
appreciate any and all advice I can get. After all,
I'm the newbie.  ;)

And I fully agree about heading the warnings.  I'm
hoping to "learn the trade" as quick as possible, and
find ways around them.

Thanks again,

ATM

--- Bob Barr <.....bob_barrspamRemoveMEHOTMAIL.COM> wrote:
> Sorry, my response may have sounded a bit more harsh
> than I meant it to be.
> :=)


__________________________________________________
Terrorist Attacks on U.S. - How can you help?
Donate cash, emergency relief information
http://dailynews.yahoo.com/fc/US/Emergency_Information/

--
http://www.piclist.com hint: To leave the PICList
RemoveMEpiclist-unsubscribe-requestspamspamBeGonemitvma.mit.edu


More... (looser matching)
- Last day of these posts
- In 2001 , 2002 only
- Today
- New search...