PDA

View Full Version : Swaping I/O back and forth during runtime 12F683



retepsnikrep
- 28th December 2010, 07:14
I have a 12F683 with an analog input device on GP0. No problem with that.
I want to piggy back an lcd on the same pin and drive it between adc aquisition.

So in escence during runtime i want to swap the pin back and forth between an adc input and a serial output.

My analog device is quite high impedance so the high low pic output will not affect it. Also the lcd input is high impedance and should not affect the voltage data, and should ignore is as invalid serial data.

I want to

1) initialise pin as analog input and take a reading
2) initialise pin as serial output and send data to lcd
3) Repeat

Any issues? I'm only taking adc readings 5 times a second or so. Rest of the time pin can be sending serial data to lcd.

mackrackit
- 29th December 2010, 19:30
I gave it a try, could not get it to work correctly

Two 10k resistors as a voltage divider giving a reading of 127 with the serial output on a different pin.

When the serial output is placed on the ADC pin the ADC value drops to 16. The ADC value will change if the divider is changed and the serial still works, just getting incorrect values.

Test code:


' ADC TEST FOR CHANNEL 2 WITH SERIAL2 OUT
'<FL_PIC12F675>'
'<FL_PBPW>'
DEFINE OSC 4
@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF
CMCON=7
' ANSEL=%00000100
' TRISIO = %000100
RESULT VAR BYTE 'A/D CONVERSION RESULT STORAGE BYTE

START: 'A HEART BEAT
GPIO = %010000:PAUSE 500:GPIO = %000000:PAUSE 500
SEROUT2 GPIO.2, 16780, ["RESULT ",DEC RESULT,$a,$d]
PAUSE 250
GOSUB ADC_2 'STARTS THE READING OF ADC CHANNEL 2
GOTO START

ADC_2: 'READ AN2 GPIO2
PAUSE 100
ANSEL=%00000100
TRISIO = %000100
GPIO = %000100
ADCON0 = %00001001 'TURNS ADC ON
GOSUB READ_AD
RESULT = ADRESH 'PLACES THE ADC VALUE INTO VAR
ANSEL=%00000000
TRISIO = %000000
RETURN

READ_AD: 'DOES THE ADC CONVERSION
PAUSE 50
ADCON0.1=1
WHILE ADCON0.1=1:WEND
RETURN

retepsnikrep
- 31st December 2010, 16:02
I'll give it a try in due course. The impedance of my adc source is 10k so is unaffected by the input/output change. Thanks

retepsnikrep
- 25th January 2011, 11:42
I've been trying this today and it almost works.

The adc in works as does the serial out and i can see the data on my scope, but the serial lcd does not recognise it. I'll get my logic probe on it and see what values it thinks are arriving.

I believe the problem is that the lcd is expecting N9600 (idle high)

however when you switch from the adc to serial output the pin goes low and it won't come back up even if I send a high pin command? As soon as the serial data has been sent it then idles high. So the false low at the start is corrupting the data i believe.

I'll keep trying. Any thoughts. I tried serout and debug out same result.

mister_e
- 25th January 2011, 14:47
you want to modify Start /goto Start section so it doesn't touch GPIO.2 bit.


START: 'A HEART BEAT
GPIO = %010100:PAUSE 500:GPIO = %000100:PAUSE 500
SEROUT2 .....
.....
GOTO START

retepsnikrep
- 31st January 2011, 15:48
I have this working now. You are limited a bit by the adc input, but so long as any switch you implement is held above logic high say >4v and has enough impedance not to effect the serial data when it appears on the pin, then you can have an input on the adc so long as you stay within that top 1 volt. A few switches and carefully choosen pd resistors works.

Nice for 8 pin pics as I was short of an output pin for my lcd. Now I have serial output and adc input on the same pin :)