Searching \ for '[PIC]: bit high code challenge' 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: 'bit high code challenge'.

Exact match. Not showing close matches.
PICList Thread
'[PIC]: bit high code challenge'
2001\02\19@104624 by Roman Black

flavicon
face
Hi, is anyone interested in a quick code challenge?
I have a quick polling loop on a 16F84 that needs to
check if either of 2 input pins has gone high.

It starts with the two pin values in w,
just read from the port and the other 6bits masked to 0.
The two bits are next to each other in bits 4,5
and won't change places ever due to hardware.

The last state of the 2 bits is is last_inputs reg.
The bit positions line up in 4,5 again. The other 6
bits can be masked to 0 or dont-care.

* absolute quickest way to detect if EITHER pin
has changed from 0 to 1. Critical.

* quickest ways to exit if no change, and to
store the new last_inputs value and exit if
there is any other changes (1s to 0s etc)

Any suggestions? My code is clumsy and first
checks for any change, 5 cycles including the
portb read and mask. Then I am left checking to see
if either pin has gone from 0 to 1 but it's
very messy since the test I need the fastest
is the 0 to 1 tests. It's probably one of those real
simple things that the code geniuses do without
any effort?? :o)
-Roman

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


2001\02\19@113900 by Drew Vassallo

picon face
>It starts with the two pin values in w,
>just read from the port and the other 6bits masked to 0.
>The two bits are next to each other in bits 4,5
>and won't change places ever due to hardware.
>
>The last state of the 2 bits is is last_inputs reg.
>The bit positions line up in 4,5 again. The other 6
>bits can be masked to 0 or dont-care.

;; Untested
;; 5 cycles if bit5 went 0->1
;; 7 cycles if bit4 only went 0->1
;; 8 cycles if neither bit changed from 0->1
 btfss last_inputs, 5
 btfss PORTB, 5
 goto Check_bit4
 goto Bit_Changed

Check_bit4
 btfss last_inputs, 4
 btfss PORTB, 4
 goto Both_Set

Bit_Changed
;; At least one bit changed from 0->1
 movf PORTB, 0
 movwf  last_inputs
 .
 .
 .
Both_Set
;; Both bits of "last_inputs" were set initially, so exit
 movf PORTB, 0
 movwf  last_inputs
 .
 .
 .

There seems to be a more elegant way, but I couldn't get it below 9 cycles
using 3 other different methods.

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

--
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\02\19@115314 by Scott Dattalo

face
flavicon
face
On Fri, 26 Nov 1999, Roman Black wrote:

{Quote hidden}

Here's a version that saves the changes and then checks if wither input went
high (but doesn't check high to lows):

   movf   PORTB,w         ;get current state
   xorwf  last_in,w       ;compare to previous
   xorwf  last_in,f       ;save changes
   andwf  last_in,w       ;did either go high?

   andlw  (1<<4)||(1<<5)  ;only want bits 4 and 5
   skpnz
    goto  nolowtohighs

; either bit4 or bit5 went from 0 to 1
...

nolowtohighs


Here's one that does a check and then saves the changes (and will allow you to
test high to lows if you want)

   movf   PORTB,w         ;get current state
   movwf  temp
   xorwf  last_in,w       ;compare to previous
   andwf  temp,w          ;did either go high?

   andlw  (1<<4)||(1<<5)  ;only want bits 4 and 5

   skpnz
    goto  save changes

; low to high change - do whatever

   btfsc  temp,4
    do whatever for bit 4 going high
   btfsc  temp,5
    do whatever for bit 5 going high

save_changes:
   movf   temp,w
   xorwf  last_in,w
   xorwf  last_in,f

Scott

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


2001\02\19@115318 by Drew Vassallo

picon face
(don't mind the "." in front of each line... my email client seems not to
recognize tabs or spaces in front of text)

Hmm, how about this.  6 cycles regardless of which bit changed.  7 cycles if
neither changed 0->1.

.   movf PORTB, w
.   andlw 0x30  ; mask out 4,5
.   xorwf last_input, f
.   btfss last_input, 5
.   btfss last_input, 4
.   goto error_path
;; Either 4 or 5 changed 0->1
.   movf PORTB, w
.   movwf last_input
.
.   etc...
error_path
;; Otherwise, neither changed
;   do error stuff here
;

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

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


2001\02\19@115731 by Roman Black

flavicon
face
Drew Vassallo wrote:
{Quote hidden}

Wow Andrew, that's pretty clever and seems functional.
Much faster than anything I had. So it always saves
the port bits to the last_inputs register for any
NON 0-1 condition. That seems clumsy at first compared
to having 3 distinct conditions,
0-1 change
1-0 change
no change

but it works very fast! And I really don't mind saving
the port again for no-change conditions as it never gets
worse than 9 cycles. It would be nice to know which
input was the one that changed (or if both changed)...
-Roman

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


2001\02\19@121421 by promero

flavicon
face
part 0 44 bytes
his is a multi-part message in MIME format.
part 1 169 bytes content-type:text/plain; charset=iso-8859-1 (decoded quoted-printable)

why don´t you try:

xorwf    lastinputs,w
andwf    port,w
bnz    change  ;mask of changed bits is left in w
goto    exit


part 2 422 bytes content-type:text/x-vcard; charset=us-ascii;
(decoded quoted-printable)

begin:vcard n:Romero Plaza;Pável Ernesto
tel;cell:5489528
tel;fax:6-7444829
tel;home:6-7464233
tel;work:6-7444829
x-mozilla-html:TRUE
url:http://www.insitel.com.co
org:Insitel Ltda.;Research & Development
adr:;;Calle 21 # 16 - 46 Piso 7;Armenia;Quindío;;Colombia
version:2.1
email;internet:promerospamspam_OUTinsitel.com.co
title:Hardware Engineer
fn:Pável Ernesto Romero Plaza
end:vcard


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


2001\02\19@124159 by imenez Yamasaki

flavicon
face
   What about this:

       movlw    b'00110000'
       andwf    portb,w
       movwf   temp                ;need an auxiliar reg.
       xorwf     last_inputs,w
       bz          no_changes    ; quick enought when no changes?
       movfw   temp
       movwf   last_inputs
no_changes
   ...

   Trying to do it nearest to what you wanted...

| * absolute quickest way to detect if EITHER pin
| has changed from 0 to 1. Critical.
|
| * quickest ways to exit if no change, and to
| store the new last_inputs value and exit if
| there is any other changes (1s to 0s etc)

   Regards,

   Gonzalo

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


2001\02\19@133458 by Drew Vassallo

picon face
> > ;; Untested
> > ;; 5 cycles if bit5 went 0->1
> > ;; 7 cycles if bit4 only went 0->1
> > ;; 8 cycles if neither bit changed from 0->1

>Wow Andrew, that's pretty clever and seems functional.
>Much faster than anything I had. So it always saves

The second one I sent works in 6 cycles regardless of which pin changes, and
I think 7 cycles on non-change.  If you really need to prioritize one pin
check over the other, you can switch them to save two cycles using this
method.  Or go with the 6 cycle both ways if you want it isynchronous.

The 6-cycle method seems to be similar to what most people are proposing,
but you wanted the LEAST number, so I sent this one :)

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

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


2001\02\19@133709 by Drew Vassallo

picon face
>.   movf PORTB, w
>.   andlw 0x30  ; mask out 4,5
>.   xorwf last_input, f

If you only want to test EITHER bit changing 0->1, then you can just use:
.    btfss STATUS, Z
.    goto error_path

This will save one cycle, bringing this one to 5 cycles, regardless of which
bit changed, 6 cycles if neither changed.  However, you won't know WHICH one
changed, of course.

{Quote hidden}

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

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


2001\02\19@184729 by Bob Ammerman

picon face
; direct computation of ~last_inputs & new_value

   comf    last_inputs,W
   andwf  PORTB,W
   andwf  (1<<4)|(1<<5)
   skpz
   goto    got_a_0_to_1


Bob Ammerman
RAm Systems
(contract development of high performance, high function, low-level
software)

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


2001\02\19@193432 by rich+piclist

flavicon
face
Here's a stab at it...

       movf port,w
       xorwf current,w
       andlw interestingbits
       ; could 'bz no_changes' here
       xorwf current,f
       andwf current,w
       bz done
       ; here, an interesting bit was just set

On Fri, 26 Nov 1999, Roman Black wrote:

{Quote hidden}

--
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\20@043622 by Roman Black

flavicon
face
Drew Vassallo wrote:
{Quote hidden}

Hi Andrew (and thanks everyone else re this challenge!).

But now I'm getting confused. Doesn't the xorwf
produce a 1 for every bit that has changed, and a 0
for every unchanged bit??

So this is just a test for change?? How does it
just detect a 0-1 change but not a 1-0 change?

I already had a fast routine to detect any change:

subwf last_input,w
skpz

so did I miss something? :o)

I forgot to mention before too that after it detects
a 0-1 going input I need to know which input it was
to get to that routine asap. :o)
-Roman

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


2001\02\20@082833 by Drew Vassallo

picon face
>But now I'm getting confused. Doesn't the xorwf
>produce a 1 for every bit that has changed, and a 0
>for every unchanged bit??
>so did I miss something? :o)

Hmmm, no, it appears that everyone else missed something :)

If last_input had bits 4 and 5 set, and the PORT readings were zeroes, it
would still return 1s.

The first version I sent (which doesn't use xorwf) should be closer to what
you wanted, I think.  Maybe that's why I didn't use it at first!

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

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


2001\02\20@084917 by Drew Vassallo

picon face
>     comf    last_inputs,W     andwf  PORTB,W          andwf  (1<<4)|(1<<5)
.     ^^^^^  this should be "andlw"

>     skpz
>     goto    got_a_0_to_1

Good idea.

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

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


2001\02\20@084927 by Scott Dattalo

face
flavicon
face
On Tue, 20 Feb 2001, Roman Black wrote:

{Quote hidden}

Another way to approach this problem is with state machines. As I see it you
have these conditions which are of interest:

   bit5    bit4
SA:  low     low
SB:  low    high
SC: high     low
SD: high    high


Then the low to high transitions may be checked slightly faster:

while in state SA:

   movf   PORTB,W
   andlw  MASK
   skpnz
 ...


However, this assumes your in a tight polling loop. If you're really
multitasking, then you have the overhead of determining which state to switch
to.
Just a thought.

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


2001\02\20@121603 by Roman Black

flavicon
face
Scott Dattalo wrote:

{Quote hidden}

Hi Scott. I hadn't thought about approaching it from
a "4 states" perspective. I had been hung up on doing
tests in a minimum number of cycles.

Maybe I could use a table read of the 4 states,
with the table organised as a "goto table" that
jumps straight to optimised code for each possible
state...

That might lead to a good solution... Need to think
about that. :o)
-Roman

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


2001\02\21@084213 by Roman Black

flavicon
face
Thanks to everyone who offered ideas for detecting
2 inputs going 0-1 in the fastest time. I learned a
lot and it was a good help.

Basically now I'm torn between using int on change
feature, which has advantages but sucks when you
need to rely on it to detect two different inputs.
The other option is polling the two inputs but I
really need to record accurate period (freq) of
both input signals. Polling around the other
processes will add varying delays before the input
0-1 change is detected. Jitter problems.

My third option is to upgrade to a more expensive
chip like a 16F873 which has two capture pins.
Unfortunately the board is very tight already with
a SOIC 18 pin chip. Going to 28pin will be very
hard and also involves extra costs...

Searching the Mchip web site doesn't reveal any
18pin flash PICs with 2 capture pins. Darn.
-Roman

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


2001\02\21@102528 by Dwayne Reid

flavicon
face
At 12:43 AM 2/22/01 +1100, Roman Black wrote:
>Thanks to everyone who offered ideas for detecting
>2 inputs going 0-1 in the fastest time. I learned a
>lot and it was a good help.
>
>Basically now I'm torn between using int on change
>feature, which has advantages but sucks when you
>need to rely on it to detect two different inputs.

Are you using TMR0 during the time when you need to monitor for those pin
changes?  If not, you can use the T0CK input as another interrupt
input.  You simply change TMR0 clock input to external, set the polarity
for the edge that you want (rising or falling), pre-load TMR0 with 0xFF,
clear T0IF, and set T0IE.  Next correct edge on pin T0CK causes an
interrupt.  That, coupled with the INT pin gives you 2 distinct interrupt
inputs.

Another choice is to add one of those SOT 23-5 single gate logic packages -
use a XOR gate feeding the INT pin.  You could fit that chip on the other
side of the board under the PIC - no extra board space required.  Downside
is that you need 3 PIC pins - the 2 inputs (paralleled with the XOR gate
inputs) plus the INT pin.

INT on change feature is not reliable if you are accessing (reading or
writing) port B in any fashion while you want to monitor for pin
changes.  If you can arrange your pin assignments so that you NEVER read or
write port B while looking for those pin changes, INT on port change is usable.

dwayne



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

Celebrating 17 years of Engineering Innovation (1984 - 2001)

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
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: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads


2001\02\21@150103 by Scott Dattalo

face
flavicon
face
On Thu, 22 Feb 2001, Roman Black wrote:

>                                            but I
> really need to record accurate period (freq) of
> both input signals.

Oh.

How about a hybrid solution? You can use the hardware capture for one of the
signals and software polling for the other. I've posted a software period
measurer that had 3-instruction cycle resolution (for both sampling and time
tagging):

http://www.piclist.com/techref/microchip/pulsewidth-sd.htm?key=pulse%20width&from=

The one NOP could check the capture interrupt state to see if an edge has been
caught by the hardware.

Scott

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


2001\02\21@230504 by Roman Black

flavicon
face
Dwayne Reid wrote:
{Quote hidden}

Thanks Dwayne, most of this I found out. Unfortunately
there are another 3 inputs that have to be on PORTB
and I need to check them regularly. Also I need TIMER0
as the main circular timer so I can get a count when
the pins change state.

All in all I'm going to have some problems to deal
with no matter which way I do it.

Interesting about the 5-pin logic package.. But my board
is really tight. Took me hours to finally get it to
route with the 18 pin SOIC chip, it will be very hard
to use a larger chip or add parts. :o(
-Roman

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


2001\02\21@233816 by Roman Black

flavicon
face
Scott Dattalo wrote:
>
> On Thu, 22 Feb 2001, Roman Black wrote:
>
> >                                            but I
> > really need to record accurate period (freq) of
> > both input signals.
>
> Oh.
>
> How about a hybrid solution? You can use the hardware capture for one of the
> signals and software polling for the other. I've posted a software period
> measurer that had 3-instruction cycle resolution (for both sampling and time
> tagging):
>
> http://www.piclist.com/techref/microchip/pulsewidth-sd.htm?key=pulse%20width&from=


Thanks Scott! I have existing code that does a similar thing
detecting change on either of the 2 pins within a few cycles.
The trouble is the other processes that must also be polled,
and which can take varying times.

No matter how quick I make the code there will always be
from 1-20 instructions before I can detect the 0-1 change
and log the TIMER0 count. Big jitters.

If I use int on change I get 3-4 cycles int latency, +5
cycles to save off w/status, but this is OK as jitter
is down to 1 or maybe 2 cycles so the delay doesn't
matter. But then I need to do regular PORTB reads, so
that stuffs it, and i can't do int on change for two
pins.

I have looked at a lateral approach of using very tight
looping for pin polling (like your code) then since
the freq of the two inputs changes slowly I can calc
guaranteed safe times for the other processes when
I know the pins are not going to change state based
on the current periods and last known change times.
Again this has its own problems!! :o)

Now i'm trying to justify a larger PIC (with 2 capture)
by telling myself that the extra processing time
available can be used to give data compression on the
storage, justifying the extra cost... ;o)
-Roman

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


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