Searching \ for ' Bit operation quiz...' 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/index.htm?key=bit+operation+quiz
Search entire site for: 'Bit operation quiz...'.

No exact or substring matches. trying for part
PICList Thread
'[PICLIST] Bit operation quiz...'
2002\03\08@222322 by Pic Dude

flavicon
face
Anyone up for a pop quiz?...

I have all Port C outputs going to 8 LED's.  To save PCB space, I can re-route
the 4 lowest lines to the LED's, but the last 4 bits get reversed.  What's the fastest
way, in code, to reverse just those 4 lowest bits, and leave the 4 highest bits
untouched?  Fastest = smallest # of steps.

Assume input = ABCDEFGH, then output should be ABCDHGFE.

So far I've come up with this (untested), but it's seems like a very
"sledgehammer" way to do it.  There's gotta be something slicker.  I
remember learning about the XOR swap trick many years ago, and
thought it was the coolest thing ever (at the time).  Any thing like
that exist for doing this partial reverse?


movf    DATAIN,TEMP
rrf    TEMP,F
movf    TEMP,W
andlw    H'02'
movwf    DATAOUT    ; Bit 1 done
rrf    TEMP,F
rrf    TEMP,W
andlw    H'01'
andwf    DATAOUT,F    ; Bit 0 done
movf    DATAIN,TEMP
rlf    TEMP,F
movf    TEMP,W
andlw    H'04'
andwf    DATAOUT,F    ; Bit 2 done
rlf    TEMP,F
rlf    TEMP,W
andlw    H'08'
andwf    DATAOUT,F    ; Bit 3 done
movf    DATAIN,W
andlw    H'F0'        ; Strip out low nibble
andwf    DATAOUT,F    ; Merge with result


Cheers,
-Neil.

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2002\03\08@225537 by Jinx

face picon face
How's about this -

movf  leds,w          ;led drive pattern
andlw 0xf0
movwf data            ;data = 7654 0000

rrf data,f                  ;data = x765 4000 (x=carry state , irrelevant)

btfsc data,0x06      ;leave at 0
bsf   data,0x00       ;or change to 1

btfsc data,0x05
bsf   data,0x01

btfsc data,0x04
bsf   data,0x02      ;data = x765 4567

movlw 0x0f
andwf data,f          ;data = 0000 4567

swapf data,f          ;data = 4567 0000

then AND it with the low nybble

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2002\03\08@230749 by Bob Ammerman

picon face
a simple technique, perhaps not the best

movf    DATAIN,W        ; Get the data
andlw  0xF0                    ; Keep 4 high bits

btfsc    DATAIN,0          ; Low bit set?
iorlw    1 << 3                 ; Put in new place

btfsc    DATAIN,1
iorlw    1 << 2

btfsc    DATAIN,2
iorlw    1 << 1

btfsc    DATAIN,3
iorlw    1 << 0

result is left in W

Bob  Ammerman
RAm Systems

{Original Message removed}

2002\03\08@233943 by Tom Messenger

flavicon
face
At 09:29 PM 3/8/02 -0600, you wrote:
>Anyone up for a pop quiz?...

Sure. Been designing switching power supplies all week. Let's see just how
bent my mind is today.

Here's my first cut. BTW, I *never* submit code to this list; there are too
many people *far* better than me at this. Plus, I don't know what I'm doing.

*UNTESTED CODE FOLLOWS*
input is in datain

clrf    temp
clc
rrf     datain
rlf     temp
rrf     datain
rlf     temp
rrf     datain
rlf     temp
rrf     datain
rlf     temp
swapf datain
movfw temp
iorwf datain,f   13 words.


Faster method: use a jump table. Wastes lots of room but maybe you have
room to waste.

Fastest method I know of: waste an entire port. Wire the high nibble from,
say, port b direct to port c. Wire the low nibble in reverse fashion from
port b to port c.

movfw   datain  ; move word to w
movwf   portb   ; move w to portb - it immediately shows up twisted around at
portc
movfw   portc   ; read portc - desired manipulation now in w
movwf   datain  ; back in original register with low nibble reversed

Only four steps.
Ok, I'll go back to my power supply now.


Tom M.

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2002\03\09@054013 by NDuckworth

flavicon
face
This is how I'd do it in C;

(assumes H is LSB of DATAIN)

TEMP = DATAIN & 0xf0;        // copy DATAIN and strip off reversed bits

if(DATAIN & 0x01) TEMP += 8; // test bit 0 of DATAIN, update bit 3 of TEMP if
set
if(DATAIN & 0x02) TEMP += 4; // test bit 1 of DATAIN, update bit 2 of TEMP if
set
if(DATAIN & 0x04) TEMP += 2; // test bit 2 of DATAIN, update bit 1 of TEMP if
set
if(DATAIN & 0x08) TEMP += 1; // test bit 3 of DATAIN, update bit 0 of TEMP if
set

DATAOUT = TEMP;



On Saturday, March 09, 2002 3:29 AM, Pic Dude [SMTP:spam_OUTpicdudeTakeThisOuTspamAVN-TECH.COM]
wrote:
> Anyone up for a pop quiz?...
>
> I have all Port C outputs going to 8 LED's.  To save PCB space, I can re-
> route
> the 4 lowest lines to the LED's, but the last 4 bits get reversed.  What's
the
{Quote hidden}

--
http://www.piclist.com hint: The list server can filter out subtopics
(like ads or off topics) for you. See http://www.piclist.com/#topics


2002\03\09@225027 by Dmitriy A. Kiryashov

picon face
Hi Bob and guys.

John Payson would be offered this.

movfw   DATAIN          ;abcd efgh > abcd hgfe
btfsc   DATAIN,0        ;exchange e and h
xorlw   0x09
btfsc   DATAIN,3
xorlw   0x09
btfsc   DATAIN,1        ;exchange f anf g
xorlw   0x06
btfsc   DATAIN,2
xorlw   0x06

1 clock less, result in W as well.

WBR Dmitry.



Bob Ammerman wrote:
{Quote hidden}

> {Original Message removed}

'[PICLIST] [PIC}: Re: Bit operation quiz...'
2002\03\10@005419 by Pic Dude

flavicon
face
Ack! forgot the [PIC]: prefix.  Anyway, been looking thru the
code snippets that were posted, and my resulting thought is that
I need to stop making life so complicated on myself.  In my
past life, when I coded assembly on the TRS-80, I remember
something about conditionals being very expensive, so I tend
to avoid them.  Gotta get into the mindset that things are not
the same anymore.  As Bob Ammerman demonstrated, the
solution with the fewest steps, was the most brute force method
possible -- to test each of the 4 lowest bits and set the
corresponding bit in the result.  Only 11 steps!

That said, I must say that I like the way Tom Messenger thinks
-- really slick dataflow.  Sequentially squeeze the 4 lowest bits
out the right and push it into the result from the right (to the left).
Tom, as for the other solutions, I am actually running out of
space on the F872.  I'm sure however, that I can do a lot of
cleanup on my code -- this is still my first PIC code ever.  As
for wiring one port into the other -- nice thought, but I started
this bit-reversal thing to clean up wires, and the other ports are
in use.

Jinx, your solution had the top 4 bits swapped.  With some
code adjustments I managed to get the result in 14 steps.

Nigel unfortunately I'm not using C, but your code flow is
pretty much the same as Bob's -- check each bit and set the
output bits accordingly.

This was fun!  Must do this again sometime! :-)

Thanks much guys!
-Neil.



{Original Message removed}

2002\03\10@010151 by Pic Dude

flavicon
face
Egads!  As I was typing up that message, there was a fresh
entry from Dmitriy -- very slick solution, that now takes the
top spot with the fewest # of steps.  I haven't actually tried it,
but a mental walk-thru tells me it should work.  And it actually
uses a neat XOR swap technique.  My hat's off to you sir.

Cheers,
-Neil.



{Original Message removed}

'[PICLIST] [PIC}: Bit operation quiz...'
2002\03\10@060830 by Jinx

face picon face
> Jinx, your solution had the top 4 bits swapped.  With some
> code adjustments I managed to get the result in 14 steps.

Ah well. I have to admit to a little confusion over which nybble
was to be reversed.

"I can re-route the 4 lowest lines to the LED's, but the last 4
bits get reversed.  What's the fastest way, in code, to reverse
just those 4 lowest bits, and leave the 4 highest bits untouched?

Assume input = ABCDEFGH, then output should be ABCDHGFE"

I guessed wrong, as I would normally put the bit order HGFEDCBA.
Maybe I'd have gone a different way

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\03\10@112409 by Pic Dude

flavicon
face
So you're saying you'll take the challenge to beat 10 steps?  :-)


----- Original Message -----
From: "Jinx" <.....joecolquittKILLspamspam@spam@CLEAR.NET.NZ>
To: <PICLISTspamKILLspamMITVMA.MIT.EDU>
Sent: Sunday, March 10, 2002 5:07 AM
Subject: Re: [PIC}: Bit operation quiz...


{Quote hidden}

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\03\10@125014 by Sergio Masci

picon face
----- Original Message -----
From: Pic Dude <.....picdudeKILLspamspam.....AVN-TECH.COM>
To: <EraseMEPICLISTspam_OUTspamTakeThisOuTMITVMA.MIT.EDU>
Sent: Sunday, March 10, 2002 4:29 PM
Subject: Re: [PIC}: Bit operation quiz...


> So you're saying you'll take the challenge to beat 10 steps?  :-)
>

challenge eh?

movwf tmp
andlw 15
addwf pcl
goto lab0
goto lab1
goto lab2
goto lab3
goto lab4
goto lab5
goto lab6
goto lab7
goto lab8
goto lab9
goto lab10
goto lab11
goto lab12
goto lab13
goto lab14
goto lab14

lab0 movf tmp,w
goto labend

lab1 movf tmp,w
xorlw 9  ;1
goto labend

lab2 movf tmp,w
xorlw 6  ;2
goto labend

lab3 movf tmp,w
xorlw 15  ;3
goto labend

lab4 movf tmp,w
xorlw 6  ;4
goto labend

lab5 movf tmp,w
xorlw 15  ;5
goto labend

lab6 movf tmp,w
goto labend

lab7 movf tmp,w
xorlw 9  ;7
goto labend

lab8 movf tmp,w
xorlw 9  ;8
goto labend

lab9 movf tmp,w
goto labend

lab10 movf tmp,w
xorlw 15  ;10
goto labend

lab11 movf tmp,w
xorlw 5  ;11
goto labend

lab12 movf tmp,w
xorlw 15  ;12
goto labend

lab13 movf tmp,w
xorlw 6  ;13
goto labend

lab14 movf tmp,w
xorlw 9  ;14
goto labend

lab15 movf tmp,w
goto labend


Regards
Sergio

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\03\10@163541 by Bob Ammerman

picon face
Excellent!

Bob Ammerman
RAm Systems

----- Original Message -----
From: "Dmitriy A. Kiryashov" <vze27bymspamspam_OUTVERIZON.NET>
To: <@spam@PICLISTKILLspamspamMITVMA.MIT.EDU>
Sent: Saturday, March 09, 2002 11:36 AM
Subject: Re: Bit operation quiz...


{Quote hidden}

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\03\10@180327 by Jinx

face picon face
> So you're saying you'll take the challenge to beat 10 steps?  :-)

movf   datain,w    ;if datain is already in W, cut this ( = 10 steps)

andlw 0xf0           ;send upper nybble to port
movwf portc        ;portc = abcd 0000, datain = abcd efgh

btfsc datain,3      ;set lower nybble bit pattern
bsf   portc,0
btfsc datain,2
bsf   portc,1
btfsc datain,1
bsf   portc,2
btfsc datain,0
bsf   portc,3         ;portc = abcd hgfe

Dmitriy's method is mathematically elegant, although (and
this will sound terribly ingracious but it's not meant to be), it
needs MOVWF PORTC to complete it = 10 steps

Unfortunately I'm not wired mathematically, try as I may

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\03\10@185147 by Dwayne Reid

flavicon
face
At 10:36 PM 3/9/02 +0300, Dmitriy A. Kiryashov wrote:
>Hi Bob and guys.
>
>John Payson would be offered this.

Anyone heard from John?  Is he doing OK?  I sure wish he would visit us again!

dwayne


Dwayne Reid   <KILLspamdwaynerKILLspamspamplanet.eon.net>
Trinity Electronics Systems Ltd    Edmonton, AB, CANADA
(780) 489-3199 voice          (780) 487-6397 fax

Celebrating 18 years of Engineering Innovation (1984 - 2002)
 .-.   .-.   .-.   .-.   .-.   .-.   .-.   .-.   .-.   .-
    `-'   `-'   `-'   `-'   `-'   `-'   `-'   `-'   `-'
Do NOT send unsolicited commercial email to this email address.
This message neither grants consent to receive unsolicited
commercial email nor is intended to solicit commercial email.

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\03\10@192132 by Tom Handley

picon face
  Dmitriy, outstanding! I wish John would come back but it's
good to know you are still around ;-)

  - Tom

PSBS: I think of John everytime I use his BCD routine. Heck of a way
to remember a guy ;-)

At 22:36 09-03-02 +0300, Dmitriy A. Kiryashov wrote:
{Quote hidden}

------------------------------------------------------------------------
Tom Handley
New Age Communications
Since '75 before "New Age" and no one around here is waiting for UFOs ;-)

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\03\10@233625 by Dmitriy A. Kiryashov

picon face
Hi Jinx.

Jinx wrote:
> > So you're saying you'll take the challenge to beat 10 steps?  :-)
> movf   datain,w    ;if datain is already in W, cut this ( = 10 steps)
> andlw 0xf0           ;send upper nybble to port
> movwf portc        ;portc = abcd 0000, datain = abcd efgh

What about this particular movwf portc ? ;)

{Quote hidden}

That was Payson's idea initially ;)

Actually you should be very carefull using bsf operation under port
which is direct driving bunch of LED's. Read-modify-write is a hint.
As well as shadow port register.

WBR Dmitry.

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2002\03\11@025306 by Jinx

face picon face
> Actually you should be very carefull using bsf operation
> under port which is direct driving bunch of LED's. Read-
> modify-write is a hint. As well as shadow port register

Yes, I know what you mean, but there's no modification
is there ? The bits being read are in DATAIN, not the
port (which is only being written to), so it should be OK

As they're only LEDs it shouldn't matter, but I wouldn't
use this method with port pins that are clock lines for
example, might be unpredictable

I think you still have me beat by a cycle

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


2002\03\11@075345 by Dmitriy A. Kiryashov

picon face
Good morning/day Jinx.

After you put byte into port you are making bsf manipulation.
In reality PIC _is_ reading port value than modify the bit than
writes it back to port. In your example modified data is stored
exactly in port itself.

What I was saying that depending from load situation or delays
because of capacitance if you write "0"/"1" you probably won't
get it back two cycles later for say on 20MHz clocking.

I stepped onto this trouble couple times in the past. After that
if I doubt a little bit I better use shadow register technique.
All manipulations are performed under some memory cells than that
value is copied to requied port.


WBR Dmitry.

PS. Nobody beat anybody, especially by the cycle ;)



Jinx wrote:
{Quote hidden}

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


'[PICLIST] Re [PIC}: Bit operation quiz...'
2002\03\11@155934 by Jinx

face picon face
> Good morning/day Jinx.
>
And good day to you

> After you put byte into port you are making bsf manipulation.
> In reality PIC _is_ reading port value than modify the bit than
> writes it back to port. In your example modified data is stored
> exactly in port itself.

I mistakenly "thought" that mov instructions are r-m-w but bsf/bcf
aren't. It says in the manual that "All write operations are r-m-w.
So a write to a port implies that that the port pins are first read,
then this value is modified and written to the port latch". I'm
aware of the bsf-nop-bcf of course and use that in my own s/w
and so shouldn't have made such an erroneous statement

The btfsc instructions between the bsf instructions should be
enough to avoid a bad r-m-w situation with LEDs

> What I was saying that depending from load situation or delays
> because of capacitance if you write "0"/"1" you probably won't
> get it back two cycles later for say on 20MHz clocking.

That's certainly something to watch out for, perhaps more so
in analogue circuits. Digital interfaces should be alright shouldn't
they ? Are you thinking of an instance where you'd drive a
logic chip with one pin and expect to see a result on another ?
>
> I stepped onto this trouble couple times in the past. After that
> if I doubt a little bit I better use shadow register technique.
> All manipulations are performed under some memory cells
> than that value is copied to requied port

That's a sensible thing to do. Is r-m-w a peculiarity of the PIC ?
The AVR, for example, is quite different. There's no mention
of r-m-w with regard to any port operation. In fact you can
perform two distinct reads of a port - you can either read the
latches or use the PIN instruction to read the logical values
present on the pins

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


2002\03\12@035041 by Michael Rigby-Jones

flavicon
face
> That's a sensible thing to do. Is r-m-w a peculiarity of the PIC ?
> The AVR, for example, is quite different. There's no mention
> of r-m-w with regard to any port operation. In fact you can
> perform two distinct reads of a port - you can either read the
> latches or use the PIN instruction to read the logical values
> present on the pins
>
Most micro's use RMW, the peculiarity with the PIC is that when such an
operation is performed on a PORT, the value read is not that in the data
latch, but rather the values on the pins.  The 18C/18F series fixed this
problem as you can read either the pins or the data latch.

Mike

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


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