PDA

View Full Version : Problems concurrently using digital in and digital out on PORTA.



ShortBus
- 11th October 2009, 03:08
I have a switch hooked to PORTA.0 and a LED on PORTA.1 on a PIC16F688. All I am trying to do is read the value of the switch and then set the LED high or low, accordingly. Here is my code:


' PIC16F688
INCLUDE "MODEDEFS.BAS" ' Include Shiftin/out modes

DEFINE OSC 8 ' 8mHz clock
OSCCON = %01110001 ' 8mHz clock

' ======= Set up serial communications
DEFINE HSER_RCSTA 90h 'SPEN=0, CREN=0
DEFINE HSER_TXSTA 24h 'BRGH=0, SYNC=0, TXEN=1
DEFINE HSER_BAUD 19200
DEFINE HSER_CLROERR 1
' =====================================

' ======= Disable unused features & setup IO pins
ANSEL = %00000000 ' everything is digital
ADCON0 = 0 ' disable ADC
CMCON0 = 7 ' no comparators
TRISA = %00000001 ' all but RA0 is output
TRISC = %00100000 ' all outputs on port C except MCLR
' =====================================

' Variables
ALARM VAR PORTC.0
SWITCH VAR PORTA.0
LED VAR PORTA.1

' Intialize
pause 100
hserout ["============", 13, 10]

main:
gosub print_status
pause 1000
goto main

print_status:
hserout ["Val: ", DEC SWITCH, 13, 10]
If switch = 1 then
led = 0
else
LED = 1
endif
return

END

What it's doing instead is toggling the LED on and off:

.============
.Val: 1
.Val: 0
.Val: 1
.Val: 0
.Val: 1
.Val: 0
.Val: 1
.Val: 0


Any idea what's going on?

Archangel
- 11th October 2009, 05:18
Dammit! Gotta get my eyes checked!
Ok, you have a pull up or pull down on PortA.0?
Does the switch seek power or ground when activated ?
Seems like stupid questions, but the problem is nearly always a simple overlooked detail, and hair is too precious to waste on not asking the question.
Ok Ok, I see the val Var is tracking the port status of PortA.0 I would try setting the port status prior to the tris register and triple check for analog stuff still in operation. Does it work if you use High and Low instead of 1 / 0 ? If so that is an indicator of analog stuff still active.

aratti
- 11th October 2009, 09:06
This is your code:

ANSEL = %00000000 ' everything is digital
ADCON0 = 0 ' disable ADC
CMCON0 = 7 ' no comparators



This is what it should be:



Ansel = 0 ' disable analog
CMCON = 7 ' no comparators


Al.

joseluistajada
- 15th October 2009, 18:43
IŽm a similar problem.
The outputs in a 16f690 controlled by HIGH command dŽont work fine.

This simpy code present the follw voltages in the pins of 16F690 pic.



inicio:


High PORTC.5
High PORTC.4
High PORTC.3

High PORTC.6
High PORTC.7
High PORTB.7

GOTO inicio

VCC Values:

PORTC.5 = 5V
PORTC.4 = 5V
PORTC.3 = 5V

PORTC.6 = 0.6V
PORTC.7 = 1.8V
PORTB.7 = 5V

BobK
- 15th October 2009, 23:23
Hi joseluistajada,

Now add a "pause 500" then make all of your port pins "LO"

All you have done in your simple little program is turn the pins ON.

Put a short delay after your last HIGH command then make all of the pins LO
and you should see your outputs flash on and off at a 1/2 second on and 1/2 second off (assuming a 4mhz crystal).

HTH,

BobK

falingtrea
- 16th October 2009, 18:29
joseluistajada, you also need to show your initialization code. The pins may not be set to digital by default. You need to set TRIS values and ANSEL and other registers before a PIC will work as expected.