Need some pointers on 16F628 inputs


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2005
    Posts
    8

    Question Need some pointers on 16F628 inputs

    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

  2. #2
    Join Date
    Sep 2004
    Location
    Mentor, Ohio
    Posts
    352


    Did you find this post helpful? Yes | No

    Smile

    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

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Question Porta or PortB ????

    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
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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.

  5. #5
    Join Date
    Dec 2005
    Posts
    8


    Did you find this post helpful? Yes | No

    Cool

    >>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

  6. #6
    Join Date
    Dec 2005
    Posts
    8


    Did you find this post helpful? Yes | No

    Default This is getting frustrating...

    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
    Last edited by KD6OJI; - 10th December 2005 at 02:38.

  7. #7
    Join Date
    Dec 2005
    Posts
    8


    Did you find this post helpful? Yes | No

    Talking I Found the problem

    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!

Similar Threads

  1. Erratic analog inputs .
    By timseven in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 10th October 2009, 22:34
  2. inputs on 12F675 (or for that matter any PIC)
    By muddy0409 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 11th April 2007, 19:43
  3. Using portb as inputs PIC goes crazy
    By peterdeco1 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 14th November 2005, 14:15
  4. SERIN2 not working with 16F628
    By d1camero in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 6th May 2004, 20:37
  5. 16F628 on-chip eeprom problem
    By atomski in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 4th March 2004, 07:43

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts