Hi,
It is possible. Did you connect your PIC according to the schematic I provided. <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=1507&d=1175158348" align="absmiddle">Try increasing the pause. Please post your entire code and schematic. It is possible in one way or the other. I have chosen the Digital way. Detecting multiple switches with one pin is also possible using the ADC. By the way the circuit doesn't work when both the switches are pressed simultaneously. The 1K resistors are provided for that purpose so that you don't blow up your PS. When both are pressed most likely your PIC would read a logic high. As you get 1/2VDD and @ 5 volts it is good enough to be a logic 1. In any TTL/CMOS gate your cannot have anything other than 0 or 1 in your output whether your inout is at 0,1 or Z. So software tricks does it. By switching your port pin between input and output and detecting the holding capacitors state. I repeat please post your exact code and schematic and it should work.
Let me clarify my understanding about your requirement.
- You need to use only one pin of your PIC.
- You need to connect 2 switches to that.
- You need to determine whether your input is hanging idle (Z)
- Switch connected to VDD was pressed (H)
- Switch connected to VSS was pressed (L)
I am waiting.
Last edited by sougata; - 30th March 2007 at 11:00.
Regards
Sougata
? I don't understand. I said I don't care about the Z state. I would like to read "0" and "1" states (= read two buttons on 1 pin).Curious...why are you worried about the 'Z' state?
Sougata,
The code I use is almost the same as yours. I'll post it this evening (I'm in the office now).
The shematic is absolutely the same as yours but I've some different values:
PIC16F88-4MHz int osc
R1 = 10k
R2/R3=1k2
Nevertheless, you have pointed out what I was expecting.
I'm afraid that reading states that must be defined by software will slow down the maniability of your program.So software tricks does it.
In my case, I'm (re)making a SIMON game and when you get use to play with this, you can be quite fast on the buttons.
I already tried with A/D; this was far to slow. RCTime was to slow too.
I'll have another try and come back for a feedback.
Roger
Ok, now I smell what you're cooking.
I know you don't care about the 'Z' state, but you probably have to 'worry' about it in this case.
How about this... when you write to a port, you write the data register, when you read a port, you read the actual pin...so...
The button's are set up just like in the schematic above.
You set the pin to an output, and high...read back the pin...
If it's still high, either the one button pulling it high is pressed or, neither are pressed, if it's low, the other button is pressed...
Then you set the pin low...read back the pin...
If it's still low, the one button pulling it low is pressed or, neither are pressed, if it's high, the other button is pressed...
Problem with this approach is finding a low value to pull the pin far enough (either way) to change states, and yet not smoke the guts of the pin itself.
pinput var portd.0 : temp var bit : realinput var byte '0=lo, 1=hi, 2=no push
output pinput : pinput = 1 : temp = pinput
if temp = 0 then
realinput = 0 : goto finish
else
pinput = 0 : temp = pinput
if temp = 1 then
realinput = 1
else
realinput = 2 : goto finish
endif
endif
finish: 'done here
I haven't tried this, might work, might not. If you do go this route, start with high resistor values (10k or so) and work your way down. It'll either start working or smoke the PIC.
Personally, I like sougata's solution better. If you drop the cap and resistor values, you'll be able to drop the pause values as well, and make everything a lot faster.
in general you don't want to add the capacitor. In this case you just need a current limiting resistor, anything bellow 10 and above Vcc/25mA. Just do what Skimask said.
Set pin as output
set it high
Set the pin as input
read it
Set pin as Output
Set it low
Set pin as input
read it
Now compare both reading and have fun with.
It may work in PBP, but i'll suggest some ASM lines.
Steve
It's not a bug, it's a random feature.
There's no problem, only learning opportunities.
Okay; finally, it works.
Here is my code I had to correct according to Sougata's example.
ANSEL or not, it works.Code:' Fuses @ DEVICE PIC16F88,INTRC_OSC_NOCLKOUT @ DEVICE PIC16F88,PROTECT_OFF @ DEVICE PIC16F88,WDT_OFF @ DEVICE PIC16F88,PWRT_ON @ DEVICE PIC16F88,MCLR_ON @ DEVICE PIC16F88,BOD_OFF @ DEVICE PIC16F88,LVP_OFF @ DEVICE PIC16F88,CPD_OFF @ DEVICE PIC16F88,DEBUG_OFF @ DEVICE PIC16F88,CCPMX_OFF '------------------------------------------------------------------------------- ' Registers 76543210 OSCCON = %01100000 '4MHz OPTION_REG = %10000000 'D I S A B L E PORTB's Pull-Ups for buttons 'ANSEL = %00000000 'Disable Analogue Inputs TRISA = %00000000 'Inputs/Outputs TRISB = %00100000 'Inputs/Outputs '------------------------------------------------------------------------------- ' Variables Led0 var PORTA.0 Led1 var PORTA.1 HiLow var PORTB.5 'this port is TTL only InOut var TRISB.5 Time var byte time = 2 '------------------------------------------------------------------------------- ' Program MAIN: inout = 0 hilow = 0 pause time inout = 1 pause time if hilow = 1 then led1 = 1 : goto main led1 = 0 inout = 0 hilow = 1 pause time inout = 1 if hilow = 0 then led0 = 1 : goto main led0 = 0 Goto MAIN end
I changed the 0,1µF cap for a 0,01µF and reduced the pause time to 2 so it goes drastically faster.
Thanks a lot for your patience.
In fact, I was stuck in my mind thinking this problem would have to be resolved another way than by a software routine...
Roger
Thanks mister_e, I just read your post.
I removed the cap and PAUSEs.
It works too very well.
Cool![]()
Roger
use a ADC input....
EDIT: just read yours... WOOOHOOO! good luck!
Steve
It's not a bug, it's a random feature.
There's no problem, only learning opportunities.
mister_e,
May I ask you what this BASIC routine would have become in assembly?
Please, don't waste your time if it is long to do.
I'm just curious to compare what "I know" (=BASIC) to what I don't (=ASM).
I don't really understand the difference between BASIC and ASSEMBLY in terms of program execution speed (looks to be faster in ASM but I don't understand why - have to find other threads for more info) because I never looked into it up to now - looks very complicated.
Roger
Bookmarks