PDA

View Full Version : TRISIO on PIC12F683, I have a bit stuck midway



zipwize
- 31st October 2010, 19:44
I'm having a hard time trying to figure out why I have pin 7 at 1.247 volts when it has a 10K pull up resistor to Vdd (3.86V). It is connected to a momentary switch. I have disconnected pin 7 from the switch and the switch goes to 3.8 volts with the pull up.

It looks like I have pin 7 GPIO.0 set as an output.

Overview: I have two LED's and two switches. Switch S2 works OK electrically. But S1 is stuck in mid voltage and never goes high. It goes to zero when I press it. Also, It is never read as low even when I press it or the other switch S2

I can't figure out why this is happening.

Here is the code:


----------------------------------------------------------------------------------------------------------
' PIC12F683 SOIC-8 Pin Assignments:

' Pin 1 Vdd +3.8V Power, Battery Voltage +2.5 - 4.5V
' Pin 2 GP5 Out GreenLED Driven by 2N2222, High = LED ON
' Pin 3, GP4 Out RedLED Drivven by 2N2222, High = LED ON
' Pin 4 GP3 In Switch (S1), Low = Pressed
' Pin 5, GP2 N/C
' Pin 6, GP1 N/C
' Pin 7, GP0 In Switch (S2), Low = Pressed
' Pin 8, Vss Supply Ground
'
' Using INTRC Oscillator

INCLUDE "modedefs.bas" ' Mode definitions for Serout
DEFINE OSC 4
@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
CMCON=7

INTCON = %10001000 'internal oscillator
OSCCON = %01110000 ' 8mHz
CMCON0 = 7 'Comparators off
GPIO = %00000000 'outputs low
TRISIO = %00001111 'GP0-GP3 as input




'======== SET UP VARIABLES & INITIAL VALUES ====================================

GreenLED CON 5 ' Alias Green LED Drive, High = LED ON, Pin 2 Green
RedLED CON 4 ' Alias Red LED Drive, High= LED ON, Pin 3 Red
SwitchS1 VAR GPIO.0 ' Alias Lip Switch, Low = Pressed, Pin 4
SwitchS2 VAR GPIO.2 ' Alias Switch S1, Low = Pressed, Pin 5
'Pin 4 is set for external MCLR and has a 5.9K pull up

N VAR BYTE ' Counting Variable, used in Auto-OFF Routine


Low GreenLED
Low RedLED

N=0
StartLoop:

N=N+1
High GreenLED
Low RedLED

PAUSE 1000

If S1 = 0 Then N=0
If S2 = 0 Then N=0


Low GreenLED
High RedLED

PAUSE 1000

If N=10 Then
Low GreenLED
Low RedLED
NAP 7
EndIF

GoTo StartLoop ' Repeat

END

ScaleRobotics
- 31st October 2010, 20:03
It looks like I have pin 7 GPIO.0 set as an output.



TRISIO = %00001111 'GP0-GP3 as input
It looks like it is set as an input to me...

Plus, you define it as 4 mhz, but tell it to be 8 mhz.



DEFINE OSC 4
@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_OFF & _CP_OFF
CMCON=7

INTCON = %10001000 'internal oscillator
OSCCON = %01110000 ' 8mHz

zipwize
- 2nd November 2010, 00:36
I still can't read momentary push buttons on GPIO.0 and GPIO.3. I simplified the code. I think (and could be wrong) that PBP sets the TRISIO bits depending on if the High or Low is used or if the pin is used in an If statement.

Here is the simplified code that doesn't work yet.

' PIC12F683 SOIC-8 Pin Assignments:

' Pin 1 Vdd +3.8V Power, Battery Voltage +2.5 - 4.5V
' Pin 2 GP5 Out GreenLED Driven by 2N2222, High = LED ON
' Pin 3, GP4 Out RedLED Drivven by 2N2222, High = LED ON
' Pin 4 GP3 In Switch (S1), Low = Pressed
' Pin 5, GP2 N/C
' Pin 6, GP1 N/C
' Pin 7, GP0 In Switch (S2), Low = Pressed
' Pin 8, Vss Supply Ground
'
' Using INTRC Oscillator

INCLUDE "modedefs.bas" ' Mode definitions for Serout
DEFINE OSC 4
@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_ON & _CP_OFF

INTCON = %10001000 'internal oscillator
OSCCON = %01100000 ' 4mHz
CMCON0 = 7 'Comparators off
GPIO = %00000000 'outputs low
'TRISIO = %00001111 'GP0-GP3 as input
TRISL = %1111
TRISH = %1111

'======== SET UP VARIABLES & INITIAL VALUES ====================================

GreenLED VAR GPIO.5 ' Alias Green LED Drive, High = LED ON, Pin 2 Green
RedLED VAR GPIO.4 ' Alias Red LED Drive, High= LED ON, Pin 3 Red
SwitchS1 VAR GPIO.0 ' Alias Switch S2, Low = Pressed, Pin 7
SwitchS2 VAR GPIO.2 ' Alias Switch S1, Low = Pressed, Pin 5
'Pin 4 is set for external MCLR and has a 5.9K pull up

N VAR BYTE ' Counting Variable, used in Auto-OFF Routine


Low GreenLED
Low RedLED


StartLoop:
Pause 10


If SwitchS1 = 0 Then
High GreenLED
ELSE
Low GreenLED
EndIf

PAUSE 10

If SwitchS2 = 0 Then
High RedLED
ELSE
Low RedLED
EndIf

PAUSE 1000

GoTo StartLoop ' Repeat

END

mackrackit
- 2nd November 2010, 01:50
You might try turning the analog off
ANSEL = %000000

Darrel Taylor
- 2nd November 2010, 01:52
Change _MCLRE_ON to _MCLRE_OFF, if you want to use GPIO.3 as input.

Go back to TRISIO, and remove TRISL/TRISH.

INTCON controls Interrupts, not the internal oscillator.
So remove that line.

And add ...

ANSEL = 0 ; enable digital inputs

hth,

zipwize
- 2nd November 2010, 23:57
Well, I'm a little embarrassed because your suggestion for ANSEL=0 worked! I have two working inputs now. I thought I had tried this but measured the pin and decided it did not work. But it works as an input. One last worry remains. The input on Pin 7 (GPIO.0) still has 1.245 Volts. Vdd is 3.866 Volts. MCLR is pulled up externally with a 5.9K to Vdd. Pin 7 has an external 10K and a switch to GND. Pin 5 (GPIO.2) has the same circuit. Pin 5 measures 3.865V when the switch is not pressed. Both inputs go to zero volts when the switches are pressed. Something is still on at Pin 7 (GPIO.0).

Here is the code that seems to work.

' PIC12F683 SOIC-8 Pin Assignments:

' Pin 1 Vdd +3.8V Power, Battery Voltage +2.5 - 4.5V
' Pin 2 GP5 Out GreenLED Driven by 2N2222, High = LED ON
' Pin 3, GP4 Out RedLED Drivven by 2N2222, High = LED ON
' Pin 4 GP3 N/C
' Pin 5, GP2 In Switch (S1), Low = Pressed
' Pin 6, GP1 N/C
' Pin 7, GP0 In Switch (S2), Low = Pressed
' Pin 8, Vss Supply Ground
'
' Using INTRC Oscillator

INCLUDE "modedefs.bas" ' Mode definitions for Serout
DEFINE OSC 4
@ __config _INTRC_OSC_NOCLKOUT & _WDT_OFF & _MCLRE_ON & _CP_OFF

INTCON = %000001000 'Enable GPIO Change Int but enable interrupts later
OSCCON = %01100000 ' 4mHz
CMCON0 = 7 'Comparators off
ANSEL = %00000000 ; enable digital inputs
TRISIO = %00001111'GP0-GP3 as input
OPTION_REG = %10000000 'Disable Pull ups

'======== SET UP VARIABLES & INITIAL VALUES ====================================

GreenLED VAR GPIO.5 ' Alias Green LED Drive, High = LED ON, Pin 2 Green
RedLED VAR GPIO.4 ' Alias Red LED Drive, High= LED ON, Pin 3 Red
SwitchS1 VAR GPIO.0 ' Alias Switch S2, Low = Pressed, Pin 7
SwitchS2 VAR GPIO.2 ' Alias Switch S1, Low = Pressed, Pin 5
'Pin 4 is set for external MCLR and has a 5.9K pull up

N VAR BYTE ' Counting Variable, used in Auto-OFF Routine


Low GreenLED
Low RedLED


StartLoop:
Pause 10

N=N+1

If SwitchS1 = 0 Then
High GreenLED
ELSE
Low GreenLED
EndIf

PAUSE 10

If SwitchS2 = 0 Then
High RedLED
ELSE
Low RedLED
EndIf

PAUSE 10

GoTo StartLoop ' Repeat

END

zipwize
- 5th November 2010, 22:29
I just wanted to add that the extra current 65uA that I was experiencing turned out to be from the BOR being enabled.

Thanks for the forum's help.