View previous topic :: View next topic |
Author |
Message |
gertc
Joined: 06 May 2006 Posts: 88
|
Posted: Tue Dec 20, 2016 12:20 am Post subject: Same pin as Input and Output |
|
|
Hello,
I have 3 wires going out, connected to two pushbuttons and one Led. As I only have 2 I/O pins left, I am using one of the I/O
pins as Output and Input. It does work, but when I am pressing S2 and reading PC3 it as an Input, I am shorting the output to gnd.
Is that safe in the long run, or are there a better way to that?
Here is reduced code showing what I am doing:
Code: | $regfile = "m1284pdef.dat"
$crystal = 8000000
$hwstack = 110
$swstack = 110
$framesize = 80
Config Portc.2 = Input
Config Portc.3 = Output
Portc.2 = 1
Portc.3 = 1
Do
'Do something
'--
'--
If Pinc.2 = 0 Then Gosub External_box
Toggle Portc.3 'Togle LED
Waitms 500
Loop
External_box:
Portc.3 = 1 'Turn LED ON
Do
'Do something
'--
'--
If Pinc.3 = 0 Then 'PB S2 Pressed
Exit Loop
End If
Loop
Return
End |
Thanks,
GerrySC
(BASCOM-AVR version : 2.0.7.9 , Latest : 2.0.7.8 ) |
|
Back to top |
|
|
Paulvk
Joined: 28 Jul 2006 Posts: 1257 Location: SYDNEY
|
Posted: Tue Dec 20, 2016 4:15 am Post subject: |
|
|
When I run out of pins for I/O I use 74HC165 & 74HC595 to get more
with 4 pins of the chip I get almost unlimited digital inputs and outputs.
Or with an I2C port extender you could use two of the 3 pins to get more even use the 3rd as an interrupt
to trigger code when a button is pressed as some I2C extenders have provision for this the PCF8574 for example.
Shorting the output to ground while having +5v on a pin does not appear to be a good idea
but a reading of the spec sheet may tell you.
Regards Paul |
|
Back to top |
|
|
six1
Joined: 27 Feb 2009 Posts: 553
|
Posted: Tue Dec 20, 2016 7:10 am Post subject: |
|
|
If Port is active high and you shorten switch, port gets broken...
Better you connect switch behind resistor! (parallel to led) _________________ For technical reasons, the signature is on the back of this message. |
|
Back to top |
|
|
Evert :-)
Joined: 18 Feb 2005 Posts: 2156
|
Posted: Tue Dec 20, 2016 9:22 am Post subject: |
|
|
It's hard to get an avr port broken, but better save then sorry.
Add a series resistor to S2, or connect S2 to the other side of the 475ohm resistor. (S2 parallel to the led1) Then you have your current limiter. _________________ www.evertdekker.com Bascom code vault |
|
Back to top |
|
|
gertc
Joined: 06 May 2006 Posts: 88
|
Posted: Wed Dec 21, 2016 1:54 am Post subject: |
|
|
Thank you for the suggestions.
The PCB is made already, so adding an additional chip is out of question. I have an external device with 2 Push-Buttons connected to the existing PCB board, and just realized that I need an additional LED.
I made a test connecting a resistor from PC3 to Gnd. (VCC=3.3V, LED and 475R resistor not connected)
50R: Measuring 1.96V on PC3, current 33.0mA - Reading Input PC3 as HI
25R: Measuring 0.91V on PC3, current 36.6mA - Reading Input PC3 as LO
0R: Measuring 0.0V on PC3, current 37.6mA - Reading Input PC3 as LO
Using a 25 Ohm resistor keeps the current below the maximum 40mA current per I/O pin
However, I am little puzzled, as the specifications indicate that the Max guarantied Vlo is 0.3V. I am reading LO at 0.91V
GerrySC |
|
Back to top |
|
|
Paulvk
Joined: 28 Jul 2006 Posts: 1257 Location: SYDNEY
|
Posted: Wed Dec 21, 2016 3:38 am Post subject: |
|
|
gertc wrote: | Thank you for the suggestions.
The PCB is made already, so adding an additional chip is out of question.
GerrySC |
No it would be external to your PCB its a great way to have just a few
wires running to a set of buttons and LEDs you can buy a small pre made
board on E-Bay for a few dollars which you can mount with the buttons & LEDs
Regards Paul |
|
Back to top |
|
|
Deanus
Joined: 26 May 2006 Posts: 188 Location: Adelaide
|
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Wed Dec 21, 2016 3:43 pm Post subject: |
|
|
gertc wrote: | 50R: Measuring 1.96V on PC3, current 33.0mA - Reading Input PC3 as HI
25R: Measuring 0.91V on PC3, current 36.6mA - Reading Input PC3 as LO
0R: Measuring 0.0V on PC3, current 37.6mA - Reading Input PC3 as LO
Using a 25 Ohm resistor keeps the current below the maximum 40mA current per I/O pin
However, I am little puzzled, as the specifications indicate that the Max guarantied Vlo is 0.3V. I am reading LO at 0.91V |
It's a pretty weird approach to force a pin low against its output driver, as a more clever and save way exists, simply by connecting the switch parallel to the led, leaving everything else as-is.
Every time you need to know status of the attached switch, you change data direction of the pin to input and activate the internal pullup, maybe add a small delay.
After the pin's value is read, set pullup off and to output again.
This way only moderate currents flow and the pins aren't tortured with 25 Ohm resistors..
It works, as the forward voltage of the led is enough to read the pin high.
In doubt check the datasheet under Electrical Characteristics, there DC Characteristics and the voltage, that is considered as min for reading high. |
|
Back to top |
|
|
gertc
Joined: 06 May 2006 Posts: 88
|
Posted: Wed Dec 21, 2016 6:29 pm Post subject: |
|
|
I did think about the approach of changing the the direction of the pin to Input for reading, and then changing it to Output. But the problem is that the program doesn't now when the pushbutton is being pressed.
The PCF8574 using IC2 looks as a solution. However, I am already using the hardware pins PC1/Sda and PC0/Scl for clock module DS1338Z
Code: | $lib "ds1307clock.lib" ' Using DS1338Z
Config Sda = Portc.1
Config Scl = Portc.0
Const Ds1307w = &HD0
Const Ds1307r = &HD1
Config Clock = User
|
I did look in the help file, but not quite sure if I can have two IC2's. Portc.0/Portc.1 for the clock, and Portc.2/Portc.3 for the external PCF8574.
GerrySC |
|
Back to top |
|
|
i.dobson
Joined: 05 Jan 2006 Posts: 1570 Location: Basel, Switzerland
|
Posted: Wed Dec 21, 2016 6:52 pm Post subject: |
|
|
Hi,
Do you know that the i2c bus can Support upto 127 devices (with just 2 wires).
Every chip has an address and as Long as two Chips don't have the same address they can "sit" on the i2c bus and the Software selects the chip required.
Regards
Ian Dobson _________________ Walking on water and writing software to specification is easy if they're frozen. |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Wed Dec 21, 2016 7:40 pm Post subject: |
|
|
gertc wrote: | But the problem is that the program doesn't now when the pushbutton is being pressed. |
Like you showed at your starting post
Code: | If Pinc.2 = 0 Then ... |
you need a loop somewhere, where you read status of the pin. Of course the loop and thus query of the pin needs to be often enough to detect user interaction.
Beside you use external interrupts, it is a basic requirement to read the pin sometime, and where you read it, you can also use the described approach. |
|
Back to top |
|
|
Deanus
Joined: 26 May 2006 Posts: 188 Location: Adelaide
|
Posted: Wed Dec 21, 2016 10:25 pm Post subject: |
|
|
Hi gertc,
As mentioned before by PaulVk, Myself and I.Dobson the I2c lines can support many devices, each has it's own address.
Therefore, since you now mention that you have a DS clock chip attached to the micro which uses I2c, you can use those same two ports
(Portc.0 & .1) to connect to the PCF8574 and now you have the other two ports (Portc.2 & .3) free to do whatever you want.
Put your LED on the PCF8574 and you switches on Portc.2 & 3
Dean |
|
Back to top |
|
|
Paulvk
Joined: 28 Jul 2006 Posts: 1257 Location: SYDNEY
|
Posted: Wed Dec 21, 2016 11:18 pm Post subject: |
|
|
Deanus wrote: |
Put your LED on the PCF8574 and you switches on Portc.2 & 3
Dean |
Or use the PCF8574 for leds and switches and still have a spare pin (using one as interrupt)
Regards Paul |
|
Back to top |
|
|
EDC
Joined: 26 Mar 2014 Posts: 971
|
Posted: Thu Dec 22, 2016 12:45 am Post subject: |
|
|
I was thinking about this brain teaser
So this is my solution
You can change pins configuration, wait for ~two NOP`s and then read Inputs...then switch LED on again if needed |
|
Back to top |
|
|
JC
Joined: 15 Dec 2007 Posts: 585 Location: Cleveland, OH
|
Posted: Thu Dec 22, 2016 9:28 pm Post subject: |
|
|
OK, so how can I embed a photo in a post?
The Img tag seems to want to link to a url, and I just want to insert / paste an image.
When I use a single pin for both a push button switch and an LED I isolate them with an Nfet, perhaps overkill, but fully in spec.
Image to follow, maybe...
JC |
|
Back to top |
|
|
|