Searching \ for '[PIC]: Easy way to convert ' 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: 'Easy way to convert'.

No exact or substring matches. trying for part
PICList Thread
'[PICLIST] [PIC] Easy way to convert # to bit posit'
2001\02\10@152337 by Robert Shady

picon face
I'm pretty sure this is a stupid question, but is
there an easy way to convert a number into it's
appropriate bit position?

For example, say I have 3 stored in the variable
BLAH.  How can I turn on bit #3 on PORTB?
4=4, 5=5, etc...

I've tried
     bsf PORTB,BLAH

and that obviously isn't correct.. (It appears to
set PORTB = address of BLAH..

-- Rob


__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35
a year!  http://personal.mail.yahoo.com/

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


'[PIC]: Easy way to convert # to bit position?'
2001\02\10@162446 by James Newton

face picon face
Its not a stupid question, its just an FAQ (Frequently Asked Question)...

...and that being the case, it is answered in the FAQ at
http://www.piclist.com/faq
specifically at
www.piclist.com/techref/microchip/math/bit/setbit.htm
which is the second item that appears if you type "set bit" into the search
field at the FAQ site or the archive site or right at piclist.com which is
referenced in the welcome email you got when you subscribed as being the
place to go look for things before you post a question to the list.

If that seemed harsh in any way it is not meant to be...

...I'm just proud of the site and it's ability to help answer questions.

P.S. its [pic]: rather than [pic] in the subject line which is also in the
welcome email, list FAQ, etc.

James Newton, PICList Admin #3
.....jamesnewtonKILLspamspam@spam@piclist.com
1-619-652-0593 phone
http://www.piclist.com

{Original Message removed}

2001\02\14@044557 by D Lloyd

flavicon
face
part 1 1200 bytes content-type:text/plain; charset=us-ascii
Hi,

Not sure what the FAQ says but you can do it this way:

#define SET_BIT(n)              (unsigned char)(1L<<(n))

Dan




(Embedded     Robert Shady <picnewbiespamKILLspamYAHOO.COM>spamKILLspamMITVMA.MIT.EDU>> image moved   10/02/2001 16:22
to file:
pic28164.pcx)





Please respond to pic microcontroller discussion list
     <
.....PICLISTKILLspamspam.....MITVMA.MIT.EDU>
Sent by:  pic microcontroller discussion list <EraseMEPICLISTspam_OUTspamTakeThisOuTMITVMA.MIT.EDU>


To:   PICLISTspamspam_OUTMITVMA.MIT.EDU
cc:
Subject:  [PIC] Easy way to convert # to bit position?

Security Level:?         Internal


I'm pretty sure this is a stupid question, but is
there an easy way to convert a number into it's
appropriate bit position?

For example, say I have 3 stored in the variable
BLAH.  How can I turn on bit #3 on PORTB?
4=4, 5=5, etc...

I've tried
     bsf PORTB,BLAH

and that obviously isn't correct.. (It appears to
set PORTB = address of BLAH..

-- Rob


__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35
a year!  http://personal.mail.yahoo.com/

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






part 2 165 bytes content-type:application/octet-stream; (decode)

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


2001\02\14@092943 by Mitch Miller

picon face
>> I'm pretty sure this is a stupid question, but is
>> there an easy way to convert a number into it's
>> appropriate bit position?

> Not sure what the FAQ says but you can do it this way:
> #define SET_BIT(n)              (unsigned char)(1L<<(n))

I've also been keeping an eye for a similar solution to the original poster
of this question.  There are two problems I have with this particular
solution (in my situation).  1) This solution is C compiler based, and I do
all my SX/PIC stuff in ASM.  2) This solution is compile time dependent, and
does not provide a runtime solution.  Even though this solution doesn't work
for me, thanks for the suggestion.

-- Mitch

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


2001\02\14@095242 by imenez Yamasaki

flavicon
face
   Check this out:

http://www.piclist.com/techref/microchip/math/bit/mask.htm

   It was perfect for me...

   Gonzalo

----- Mensaje original -----
De: "Mitch Miller" <spamBeGonemdmiller2spamBeGonespamHOME.COM>
Para: <TakeThisOuTPICLISTEraseMEspamspam_OUTMITVMA.MIT.EDU>
Enviado: Miircoles 14 de Febrero de 2001 9:31 AM
Asunto: Re: [PIC] Easy way to convert # to bit position?


| >> I'm pretty sure this is a stupid question, but is
| >> there an easy way to convert a number into it's
| >> appropriate bit position?
|

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


2001\02\14@101730 by Robert Shady

picon face
I was the original poster of this question, I've
had the answer since that day, I'm sorry I didn't
post my results... This is what I've come up with
which seems to work.

--- cut here ---

Have your table located somewhere in your code...

;;
;; Convert a number into a bit offset
;;
BitToMask
       addwf   PCL,F
       retlw   1
       retlw   2
       retlw   4
       retlw   8
       retlw   16
       retlw   32
       retlw   64
       retlw   128

Then this does the actual conversion...

       movlw   PORTB           ;; Load the address of PORTB into W
       movwf   FSR             ;; and save that in our FSR
       movf    Servo_Offset,W  ;; Load the # we want to convert into W
       call    BitToMask       ;; convert it into a binary position...

And then to turn the bit on, do:
       iorwf   INDF,F          ;; and turn it ON!

Or to turn the bit off, do:
       xorlw   0xFF            ;;
       andwf   INDF,F          ;; and turn it OFF!

--- cut here ---

--- Mitch Miller <mdmiller2EraseMEspam.....HOME.COM> wrote:
{Quote hidden}

__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35
a year!  http://personal.mail.yahoo.com/

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


2001\02\14@130558 by Bill Westfield

face picon face
   >> I'm pretty sure this is a stupid question, but is
   >> there an easy way to convert a number into it's
   >> appropriate bit position?

   > Not sure what the FAQ says but you can do it this way:
   > #define SET_BIT(n)              (unsigned char)(1L<<(n))

Shifting a single bit N positions is the standard solution.

   There are two problems I have with this particular solution...

   1) This solution is C compiler based, and I do all my SX/PIC
      stuff in ASM.

Most assemblers have a shift operator as well.  Looks to me like in mpasm,
the shift operator is "<<", just like C.  You can use a C pre-processor with
an assembler, if you want to.  (Hmm.  I wonder how compiler vendors with
freeware "small" compilers feel about people using their compiler as a macro
processor for mpasm?)  CPP macros have the advantage of not needing to be on
their own line in the source code...


   2) This solution is compile time dependent, and does not
      provide a runtime solution.

In C, an "adequate" compiler will do the shift either at compile time or
at execution time, depending on whether "b" is a constant or a variable.
Making that dertmination at assembly time is more difficult, but then it's
also usually more obvious to the programmer...

At runtime, you either loop and shift, or do a table lookup.

BillW

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


2001\02\14@150402 by Douglas Wood

picon face
Try this:

#define   mask(bit)        (1<<bit)

(This is for MPASM).

Douglas Wood
Software Enigneer
RemoveMEdbwoodTakeThisOuTspamspamkc.rr.com

Home of the EPICIS Development System for the PIC and SX
http://www.piclist.com/techref/member/DW--RA4

{Original Message removed}

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