PDA

View Full Version : Need some pointers on 16F628 inputs



KD6OJI
- 9th December 2005, 13:22
I am trying to get my 628 to read PORTA as input for a BCD switch. I am having problems though.

I tried CMCON = 7 and TRISA = %00001111

I set the switch on PORTA.0 - PORTA.3

Couldnt get anything to go..
So I switched to the following code just to test with, still no joy...
I need some pointers on this imput business...

Code follows :


define osc 4

TRISB = %11110000

PB0 VAR PORTB.0
PB1 VAR PORTB.1
PB2 VAR PORTB.2
PB3 VAR PORTB.3

SW0 VAR PORTB.4
SW1 VAR PORTB.5
SW2 VAR PORTB.6
SW3 VAR PORTB.7

SCAN:

If SW0 = 1 then
HIGH PB0
ELSE
LOW PB0
ENDIF

If SW1 = 1 then
HIGH PB1
ELSE
LOW PB1
ENDIF

If SW0 = 2 then
HIGH PB2
ELSE
LOW PB2
ENDIF

If SW0 = 3 then
HIGH PB3
ELSE
LOW PB3
ENDIF

goto scan

BobK
- 9th December 2005, 14:51
Hello KD6OJI,

Since I have been working with PICs for awhile I think I might be able to get you started in the right direction. I am just starting to work with the 628 myself after doing some big work with 16F74's 877's and 18F452's. But they are all basically the same.

First on your PortA problem. The CMCON and TRIS settings looked okay but you have to convert analog inputs to digital. This is done with ADCON1=7.
That should take care of that part.
ADCON1 = 7
CMCON = 7
TRISA = %00001111
TRISB = %11110000

Next is the PortB snippet you posted. SW0 thru 3 are fine but your program will only have an output on the selected port pin as long as you are holding the switch at ground. I am, of course, assuming you have pull up resistors on the input pins or have enabled the weak pullups. Now, the SW's can only equal 0 or 1. This means either the switch is open or closed. With a pull up resistor in place and the switch in the open state the pin will = 1. With the switch closed the pin will = 0. "SW0 = 2" and "SW0 = 3" will never work in your case.

After setting up the pull up resistors try this code:
SCAN:
If SW0 = 1 then HIGH PB0:PAUSE 2000
ELSE
LOW PB0
ENDIF

If SW1 = 1 then HIGH PB1:PAUSE 2000
ELSE
LOW PB1
ENDIF

What this will do is if the switch is closed it will then turn PB0 on for 2 seconds then off if the switch is released. The same thing should happen with SW1.

This is just basic stuff but this is how I learned!

Let me know if I can help more. There are some great patient people on this site that will probably jump in also.

BobK

Acetronics2
- 9th December 2005, 15:22
HI, KD60...

You say : I connect my switches to PortA ... and you define PortB as the inputs ... while you read Portb.

Where are your switches really connected ???

would a little look there help you ????

Alain

Melanie
- 9th December 2005, 18:15
People... let's download and read the DATASHEET!

ADCON1 = 7

Last I looked the 16F628 didn't have A/D Converters and as such doesn't have ADCON Registers - but you might want to correct me on that...

CMCON=7 however is good because this PIC does have Comparators.

Connect your switches between the PICs pins and Vss. Connect pull-up Resistors between the PICs pins and Vdd. 10K is a good value. When the switch is OPEN, the PIC will see a 1 on that pin. When the Switch is Closed the PIC will see a zero.

If you use PortB, you won't need pull-up Resistors because you can enable the PICs weak pull-up's on that Port. Go look in the OPTION Register for Bit 7 to see how to do this.

KD6OJI
- 9th December 2005, 18:28
>>You say : I connect my switches to PortA ... and you define PortB as the inputs ... while you read Portb.

I did have PORTA originally. I switched to PORTB for testing.

same results, no hi-low.

Also, forgive the syntax, was writing BC (before coffee)

Shawn

KD6OJI
- 10th December 2005, 02:35
Here is the latest code bit:

define osc 4

CMCON = 7

TRISA = %11111111
TRISB = %00000000

PB0 VAR PORTB.0
PB1 VAR PORTB.1
PB2 VAR PORTB.2
PB3 VAR PORTB.3

SW0 VAR PORTA.0
SW1 VAR PORTA.1
SW2 VAR PORTA.2
SW3 VAR PORTA.3

SCAN:
If SW0 = 1 then
HIGH PB0:PAUSE 2000
ELSE
LOW PB0
ENDIF

If SW1 = 1 then
HIGH PB1:PAUSE 2000
ELSE
LOW PB1
ENDIF

If SW2 = 1 then
HIGH PB2:PAUSE 2000
ELSE
LOW PB2
ENDIF

If SW3 = 1 then
HIGH PB3:PAUSE 2000
ELSE
LOW PB3
ENDIF

HIGH PORTB.5
PAUSE 20
LOW PORTB.5
PAUSE 20

GOTO SCAN

I added the portb.5 so I could flash an LED to indicate the loop is running, which it does.
Still have no ouput on portb.0 to portb.3. The porta inputs are tied high with 10K resistors to +5v

KD6OJI
- 11th December 2005, 03:29
Well, turns out the code was not the issue here.. Seems my WinPic tool I use to write to the chip could not find the device files from MPLAB. I reset the path, it found its device files, and now I have PORTA 0-3 working correctly as inputs.

Thanks to all for the help!