I am using the BSF instruction to set bits
on PORTD on a 16c74A. The whole port is
configured as output (CLRF TRISD). The
problem is, when I set a bit on the port, any
other bits that were set, clear and then the
bit the instruction specifies, sets.
Why don't the other bits remain set?
I also had a problem getting the serial port
on the 16c74A to work. I tried everything!
It turned out the 2.4576MHz crystal was
not quite accurate enough. One would think
that crystals were supposed to be quite
accurate.
Gavin Jackson wrote:
>
> Hi there
>
> I am using the BSF instruction to set bits
> on PORTD on a 16c74A. The whole port is
> configured as output (CLRF TRISD). The
> problem is, when I set a bit on the port, any
> other bits that were set, clear and then the
> bit the instruction specifies, sets.
> Why don't the other bits remain set?
>
> I also had a problem getting the serial port
> on the 16c74A to work. I tried everything!
> It turned out the 2.4576MHz crystal was
> not quite accurate enough. One would think
> that crystals were supposed to be quite
> accurate.
Can't answer your PortD problem, I use mine for muxed address/data so I
only output bytes to it. I have had no problems with the UART and I use
a 2.4576MHz crystal, too. Even with BHRG low (note the silicon bug),
you should be able to get bang-on baud rates at 38,400 (SPBRG==0),
19,200 (SPBRG==1) and lower. I use 9600 (SPBRG==3) and 1200 (SPBRG==31)
with no problem. (I can't directly vouch for 19.2 and 38.4, but they
should be fine.)
Bottom line: make sure that you've got BHRG low. Otherwise, look to
your code because baud rates should be fine even at +/- 10% -- that
would be a *really* flaky crystal.
>I am using the BSF instruction to set bits
>on PORTD on a 16c74A. The whole port is
>configured as output (CLRF TRISD). The
>problem is, when I set a bit on the port, any
>other bits that were set, clear and then the
>bit the instruction specifies, sets.
>Why don't the other bits remain set?
Gavin,
Sounds like youre being caught by the read-modify-write problem.
When you use a bit level instruction on a port, the PIC reads the port,
changes the bit and writes the previously read levels back
to the output latches. But when it reads the pins, it reads
in the actual voltage level at the pin (even if not configured
as an input.)
So suppose you set an output pin high, and then drain enough current
from it to pull the voltage at the pin below the logic-low threshold.
(Example: driving an LED without a series resistor) Then when you BSF
or BCF another pin of the same port, that first pin is read in,
and re-written as LOW.
Result: your specified bit sets correctly, but other bits mysteriously
clear themselves.
One solution is to use a software buffer for the port. Keep a file
register that mirrors your desired output for the port. Then change
the value of a bit in the file register, and write the whole file
register to the port each time you change a bit.
On Sat, 11 Oct 1997 09:31:07 +1300 Gavin Jackson <vulcanKILLspamIHUG.CO.NZ>
writes:
>Hi there
>
>I am using the BSF instruction to set bits
>on PORTD on a 16c74A. The whole port is
>configured as output (CLRF TRISD). The
>problem is, when I set a bit on the port, any
>other bits that were set, clear and then the
>bit the instruction specifies, sets.
>Why don't the other bits remain set?
What we're always warned about, and has caught me once, is to
make sure you are not doing a read-modify-write instruction (such as bit
set) immediately after a write to the port. The read-modify-write
actually reads the port lines, and if they have not yet settled from the
previous write, you may get strange results.
>I also had a problem getting the serial port
>on the 16c74A to work. I tried everything!
>It turned out the 2.4576MHz crystal was
>not quite accurate enough. One would think
>that crystals were supposed to be quite
>accurate.
I'm also surprised! In general, I believe an asynchronous serial
link should work if each end is within 5% of the correct speed. That's
awfully far for a crystal to be off unless the crystal was not really
controlling the oscillator (some times they kinda take off on their own).
I'm using 16 MHz ceramic resonators on the 16c74a and sending/receiving
250 Kbps quite well.
>>I am using the BSF instruction to set bits
>>on PORTD on a 16c74A. The whole port is
>>configured as output (CLRF TRISD). The
>>problem is, when I set a bit on the port, any
>>other bits that were set, clear and then the
>>bit the instruction specifies, sets.
>>Why don't the other bits remain set?
>So suppose you set an output pin high, and then drain enough current
>from it to pull the voltage at the pin below the logic-low threshold.
>(Example: driving an LED without a series resistor) Then when you BSF
>or BCF another pin of the same port, that first pin is read in,
>and re-written as LOW.
>regards
>Alec
You hit the nail right on the head, Alec. I was driving LED's without a
series resistor and the voltage was dropping to about 2.3V. The
Schmitt trigger inputs must have seen it as a 'low' and cleared the
bit.
>I also had a problem getting the serial port
>on the 16c74A to work. I tried everything!
>It turned out the 2.4576MHz crystal was
>not quite accurate enough. One would think
>that crystals were supposed to be quite
>accurate.
Once I forgot to put bypass capacitor across the power to the PIC and I
experienced a slightly faster execution of the program. The chip picked up
some noise and translated it into false clock cycles. This completly
disappeared when 0.1 uF cap was added between Vdd and GND. (My crystal was
32kHz.)
Probably not source of your troubles (I expect another crystal worked fine).