PDA

View Full Version : Watching for pin High



GatorGuy
- 15th October 2011, 20:35
I am having trouble getting a program to run If statements when a pin goes high.

I have a 16f628a for testing using three pins for outputs and four for inputs. Pins PortA.0 - PortA.3 are set low at the first of the program using:

Low PortA.0 ect.

The PCB has terminals when shorted send +5v to the inputs pulling them high.

The main loop is such:

:main
If Porta.0 = HIGH then goto example1
goto main

When I pull the pin high, it doesn't do anything different. The rest of the pic and program is running fine sending out data when appropriate.

cncmachineguy
- 15th October 2011, 22:46
Are you actually comparing porta.0 to "HIGH"? If so, try changing high to 1. High is a reserved word. You could also just say:
If porta.0 then example1

This will be true when the pin is high, and false when it is low.

HankMcSpank
- 15th October 2011, 23:35
not familiar with that pic, but RA0 is shared with an analogue comparator, and the pin defaults to analogue so probably worth slapping in an...

CMCON = 7

to turn the comparator module off. (http://www.svetelektro.com/upload/16F628A.pdf page 64 upper right box)

HenrikOlsson
- 16th October 2011, 05:47
What's been said and if you first do LOW PortA.0 the TRIS bit will be cleared so that the pin (RA0 in this case) is an output. When you then do If PortA.0 = 1 you aren't reading the state of the pin since it's not set to be an input.

Heckler
- 16th October 2011, 17:26
Just to expand on what others have posted...
Below is a schematic (sorry about the ugly resistors) that shows two different ways of wiring to detect a change on a port pin.

First... with PortA.0 pulled high and a press on the button will cause a LOW or 0.

Second... with PortA.1 pulled LOW and a press on the button will cause a HIGH or 1.

The resistors can be 1K to 10K...


6064

It makes no difference and is not necessary to pre-set the state of the pin you are trying to test by "make port.pin LOW or HIGH" as that is an OUTPUT function not an INPUT function.

The correct way is to choose one of the wiring options as illistrated, set the corresponding TRIS BIT for that port.pin to an INPUT or "1" and then test for the opposite state, which will indicate a change or button press.


TrisA= 000011 'set PortA.0 and PortA.1 as input

IF PortA.0=0 THEN 'do something here when PortA.0 changes

IF PortA.1=1 THEN 'do something here when PortA.1 changes



as shown below... it is also correct to test for a "0" by testing for "NOT" and it is also correct to test for a "1" as shown...



IF NOT PortA.0 then '(this code tests for a "0" or "LOW")

IF PortA.1 THEN ' (this code tests for a "1" or "HIGH"

I hope this helps...

PS... if you can not see the schematic image above, click on it.

GatorGuy
- 16th October 2011, 20:30
Thank you for the help. So far it is still not working(I have not tried everything suggested though). I will try to add the pull down resistors as well. I already have the working PCB but I should be able to drill and add them with some creative soldering.

I didn't really intend this board to be an input board but figured I would use it for testing while I was ironing everything (output) else out.

Byte_Butcher
- 16th October 2011, 20:41
Have you set the pins to be inputs using TRISA?

(Heckler's post #5)

Unless those pins are set as inputs, all other effort will be wasted...


Steve

Heckler
- 16th October 2011, 22:20
actually the "TRIS" statement should have read...

TrisA = %00000011 the "%" indicates the following number is binary

you also need to turn off the comparators by including this statement...

CMCON = %00000111
or
CMCON = 7 if you prefer to work with decimal numbers

I like to show the configuration registers using binary as each bit usually does something different and it is easier for me to interpret my code.

I am not sure of your level of knowledge setting up PIC micro's, but you should read the data sheet (at least parts of it) regarding the particular pins and functions you are trying to set up... for example TABLE 5-2 in the data sheet shows ALL the registers that affect PortA. There you see that CMCON must be configured to turn off the comparators.

It is good to look around the forum and in the wiki section and view and study code examples to see how others set up the registers in order to do certain functions.

That was by far the most difficult thing for me to understand when beginning to work with PIC's.

As far as changing your circut board to include the pull-down resistor... you can also just hook it up externally without modifying your board. Just install a resistor (say 10K ohm) from the pin to ground... then when you touch that pin with +5 volts your code should be able to detect a "1".

If you need more help... do post back here.

good luck