A challenge for JAL and it's basic libraries ?
- hi All,
I one of the discussions last week, someone wrote that he had a PIC
program that sometimes worked well and sometimes didn't work. I
confirmed this experience and told to come back on this issue.
My program was performing the following behaviour:
- sometimes it worked well all evening
- sometimes it worked for a while
- sometimes it worked a few times
- sometimes it worked just once
- sometimes it worked never
After I noticed a particular behaviour, I could repeat that behaviour
the whole evening by resetting the PIC and/or disconnecting the power
supply. The next evening the behaviour was often different without
having changed the program.
Oh, I agree with Vasile (I don't believe in ghosts and vampires) so it
had to do something with the environement in combination with some bad
programming from my side.
Investigating the problem and finding the cause, took me a whole
evening. After that, the solution took only 10 minutes.
I think that I and other beginners will run into this problem in the
The program consisted of
- a matrix keyboard scanner
- the software serial output routine from SERIAL.JAL
Both routines separate worked well, but the combination of the 2 showed
the strange behaviour mentioned above.
The problem arises as both routines makes use of the same IO-port and at
least one of them is using some IO-pins both as input and output. After
I had located the error, I found that the problem also was well
described in the datasheets of the 16F62x PICs (page 44, DS40300B ):
"using read/modify write instructions (like BCF / BSF / etc) can
change the output latch of portpins that are in input mode".
So the solution:
before switching a portpin from input to output, always first definine
the state of the output latch.
Wouldn't it be a nice idea, if this problem and it's solution would be
encapsulated in JAL and/or its basic libraries.
I cann't overlook all the implications of such a solution, but I gues
that by keeping shadow registers of TRIS (already) and PORT it could be
done solely in the basic libraries.
- Hi Stef,
If you find a solution to that embarrased "bock diagram I/O pin" I'll be very pleasant with you.
[Non-text portions of this message have been removed]
- Stef, could be more preciese about what caused your problem? When you use
the Jal IO (pin_a0, port_a etc.) you never do bcf/bsf directly on the port
so the problem does not arise (this is exactly the reason pin_a0 etc exist
and use a shadow register).
> So the solution is: always set the output latch before changing from inputto
> output.I would say: don't mess with the RA etc file registers, nor with the TRIS
regsiters and ditto instructions: use pin/port_* and *_direction!
- hi Wouter,
maybe my conclusion was too premature.
It was based on: "if I disable the serial ouput, the circuit works perfect".
> Stef, could be more preciese about what caused your problem? When you useNow you say this, I looked again through my own code, and there it was/is :
> the Jal IO (pin_a0, port_a etc.) you never do bcf/bsf directly on the port
> so the problem does not arise (this is exactly the reason pin_a0 etc exist
> and use a shadow register).
- RB0 is used in an interrupt routine, that directly changes the value of RB0
(so that could be well the problem)
- RB2 is used for the serial output
- RB0,RB4,RB5,RB6,RB7 are used for a 20 key matrix
For the keybord scan I set the output latches of (RB0,) RB4..RB7 at 0, only
Then I scanned the keybord just by changing the TRISB values, so a logic "0"
is placed on 1 of the scan lines.
But randomly 1 or more output latches became 1 and stayed there.
So the solution is: always set the output latch before changing from input to
Now the circuit behaves well and stable.
Thanks for the tip on the pragma fuses !
>[Non-text portions of this message have been removed]
> > So the solution is: always set the output latch before changing from inputYou are quiet right, that's a better solution, mea culpa.
> > output.
> I would say: don't mess with the RA etc file registers, nor with the TRIS
> regsiters and ditto instructions: use pin/port_* and *_direction!