PDA

View Full Version : Driving me NUTS!



muddy0409
- 13th October 2005, 06:18
Hi once again all.
I have a medium length program, with everything working....except for a couple of lines .....

What I have is a BINARY coded switch feeding 4 bits into portD lower 4 bits. The inputs have pull-down resistors and the switch switches 5v into the inputs.

The code is like this...


BLAH
BLAH
BLAH chip setup and first part working fine.
included in the above:
TRISD = %00001111 'low 4 bits inputs, high 4 bits unused

LOOP:
PORTB = PORTD
goto loop

after I read the inputs, go on to more code, which works OK
BLAH
BLAH
BLAH



Now, there are LEDs on PORTB to repeat the settings on the input port.
To my way of thinking, the inputs will be repeated on the LEDs and going to the loop will continually update the LEDs to match the inputs. IE, changing the switch position should be immediately repeated on the LEDs. (I have checked the PORTD input lines and they do change with the switch changes)
BUT.....the only way I can get the LEDs to update is to reset the PIC and then the current switch setting comes up on the LEDs correctly. I am using a 16F877 and have in fact swapped chips 3 times with the same results each time.

I have also added TOGGLE PORTC.0 , as a debug indicator, to just before the GOTO LOOP line and this output does in fact toggle, showing that the loop is looping correctly.

The 3 line of code were originally to read the input switch settings, calculate a delay and a few other things. The 3 lines shown above are a result of simplifying the final code back to absolute basics because the original code wasn't working either and was taken simpler to try and see what the trouble was, but that didn't show anything. It seems to me that the 3 lines above can't get much simpler and should work OK.

PaulJC
- 13th October 2005, 10:34
Im no expert (far from it lol) but i don't think that "PORTB = PORTD" would work.

I would try something like:

Loop:
IF PortD.0 = 1 Then PortB.0 = 1
IF PortD.1 = 1 Then PortB.1 = 1
IF PortD.2 = 1 Then PortB.2 = 1
IF PortD.3 = 1 Then PortB.3 = 1
Goto Loop


Cheers Paul

muddy0409
- 13th October 2005, 10:48
Thanks for the suggestion Paul.
Just tried that but still no go.
It appears that the loop is looping, just that the portD input data is not being read into wherever it is supposed to go, unless I reset the chip.

Bloody frustrating, ain't it???

PaulJC
- 13th October 2005, 11:01
Bloody frustrating, ain't it???

Yep lol.

Do you have the datasheet for your switch?

peterdeco1
- 13th October 2005, 11:05
Hi Muddy. I am not at all familiar with the PIC you are using except I read a post by someone who had PORTD problems and fixed them with setting the ADCON1 and ADCON2 registers.

HenrikOlsson
- 13th October 2005, 11:21
Hi Muddy,
First make a short loop blinking the LED's on PORTB to make sure they work and that the port is set up correct. (Which they seem to be since it reflects changes on reset).



For i = 1 to 5
PortB = %00001111
Pause 200
PortB = %00000000
Next


Then try to read PortD to a variable first and then write that variable to PortD.



Switch VAR Byte
Loop:
Switch = PortD 'Read PortD, store in Switch
PortB = Switch 'Write switch to PortB
Pause 10 'Wait 10mS
Goto Loop 'Do it again.


How about that, does that work?

/Henrik Olsson. (At work, no board to test with here.)

muddy0409
- 13th October 2005, 14:34
Gooday Henrik.
Tried that along the way back from final code back to the simplified code described above. No, it doesn't work.

Peter

muddy0409
- 13th October 2005, 15:06
Thanks to everyone who made suggestions. But I RTFM and came up with the answer, which just may help someone else.....
PORTD is also the PARALLEL SLAVE PORT. To make it a general purpose I/O port, Bit4 of TRISE must be set to 0 IE TRISE.4 = 0

Did that and now seems to be OK. Will spend the next few hours reverting to the final code and see what happens.


Peter Moritz.