reading I/O state of pic12f675...


Closed Thread
Results 1 to 22 of 22

Hybrid View

  1. #1
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326

    Default reading I/O state of pic12f675...

    Hi to all in this forum,
    I am using pic12f675 and the PICKIT_3 programmer.
    I would like to use all the six available pins as I/O and so I wrote this small program:

    @ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF
    OSCCAL=64
    include "modedefs.bas"

    ' Set Debug pin port ________________________________________
    DEFINE DEBUG_REG PORTA
    ' Set Debug pin bit _________________________________________
    DEFINE DEBUG_BIT 0
    ' Set Debug baud rate _________________________________________
    DEFINE DEBUG_BAUD 9600
    ' Set Debug mode: 0 = true, 1 = inverted ______________________
    DEFINE DEBUG_MODE 1

    PAUSE 500 : DEBUG 13,10
    PU VAR GPIO.2

    MAIN: ' **************************************************

    IF PU=0 THEN
    DEBUG "low" ,13,10
    ENDIF

    IF PU=1 THEN
    DEBUG "high",13,10
    ENDIF

    GOTO MAIN ' **************************************************

    Debug works well but there is no way to read the GPIO.2 status since the terminal reports just LOW state.
    Any assistance please ?
    Thanks in advance .
    Regards,
    Ambrogio

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: reading I/O state of pic12f675...

    Yes, read section 3.1 of the datasheet. They tell you exactly what you're seeing and what you need to do.

    /Henrik.

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: reading I/O state of pic12f675...

    The most surprising thing is that it compiles and produces an output



    ' Set Debug pin port ________________________________________
    DEFINE DEBUG_REG PORTA
    since that chip has no PORTA

    debug must be a bit different
    Warning I'm not a teacher

  4. #4
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326


    Did you find this post helpful? Yes | No

    Default Re: reading I/O state of pic12f675...

    Thanks Richard,
    Could you please indicate tio me the correct syntax for the DEBUG ?
    ( I do not understand how to write the debug in case GPIO is involved ).
    Thanks for helping .
    regards,
    Ambrogio

  5. #5
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326


    Did you find this post helpful? Yes | No

    Default Re: reading I/O state of pic12f675...

    Thanks Henrik,
    I read the data sheet... but , to be sure : have you some sample to help in setting the register please ?
    I really need three input and three output pins.
    Thanks again
    regards,
    Ambrogio

  6. #6
    Join Date
    May 2013
    Location
    australia
    Posts
    2,389


    Did you find this post helpful? Yes | No

    Default Re: reading I/O state of pic12f675...

    Could you please indicate tio me the correct syntax for the DEBUG ?
    ( I do not understand how to write the debug in case GPIO is involved ).

    more properly would be

    DEFINE DEBUG_REG GPIO

    but it seems not to matter in this instance ,would also be wise to set the pin as digital too I think
    Warning I'm not a teacher

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: reading I/O state of pic12f675...

    I read the data sheet... but , to be sure : have you some sample to help in setting the register please ?
    The datasheet says: The ANSEL (9Fh) and CMCON (19h) registers (9Fh) must be initialized to configure an analog channel as a digital input.
    So lets start with ANSEL (page 44 in the datasheet)where the lower four bits determines if AN0-AN3 should be analog (which they are by default) or digital. These bits needs to zero for the pins to be digital and since you're not using the ADC the state of the other bits doesn't matter, therefor ANSEL = 0 will do just fine.

    Next. CMCON, figure 6-2 (page 37) in the datasheet shows the various configurations, you don't want to use the comparator so it should be off, therefor CMCON = 7

    Finally you say that you need three inputs and three outputs but not which pins should be what so I can't tell you more than that you need to set TRISIO up with a '1' for each input and a '0' for each output. Remember that the MCLR-pin (GP3) can not be configured as an output.

    Code:
    ANSEL = 0
    CMCON = 7
    TRISIO = %00111000 ' GP0-GP2 outputs, GP3-GP5 inputs.
    /Henrik.
    Last edited by HenrikOlsson; - 9th May 2017 at 10:10.

  8. #8
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326


    Did you find this post helpful? Yes | No

    Default Re: reading I/O state of pic12f675...

    Thanks,
    I corrected the define debug as indicated and it OK too.

    I do not understand the data sheet as far as the GPIO setting is concerned:
    bit 5-0: GPIO<5:0>: General Purpose I/O pin.
    1 = Port pin is >VIH
    0 = Port pin is <VIL
    I have to read both states of the pin.
    Could you please clarify ?
    Thanks
    regards,
    Ambrogio

  9. #9
    Join Date
    Jun 2008
    Location
    Varese , Italy
    Posts
    326


    Did you find this post helpful? Yes | No

    Default Re: reading I/O state of pic12f675...

    thanks Henrik,
    I received your msg 7 just now. Now it is very clear to me ,.

    In my previous msg I refer to register 05h.
    rgds,
    Ambro

  10. #10
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: reading I/O state of pic12f675...

    I wouldn't call the GPIO registers a "setting" (though it's really no different than any other register). It is the port register which is connected to the actual I/O pins.

    Write to GPIO and the bits will appear on the port pins configured as outputs. Read from GPIO and you'll get the state of the register. The state of each individual bit will properly reflect the state logic state of the actual pin provided that the pin IS set to digital, it IS set to input and the voltage is either less than VIL (for a logic zero) or more than VIH (for a logic one).

    In other words, if the voltage on GPIO.0 is less than VIL myVAR = GPIO.0 will be 0 and if the voltage is higher than VIH myVAR = GPIO.0 will be 1. If the voltage is somewhere in between VIL and VIH the result is unspecified.

    Your original problem was that the pin was not set to digital and as the datasheet says: I/O pins configured as analog inputs always read ‘0’.

    /Henrik.

Similar Threads

  1. Knowledge of State Variable Filters
    By Lucy248 in forum General
    Replies: 1
    Last Post: - 9th September 2016, 08:56
  2. Changing output state after 2 minutes
    By carljbrooks in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 21st February 2014, 05:28
  3. DC - AC Solid State Relay
    By shahidali55 in forum General
    Replies: 6
    Last Post: - 10th November 2007, 16:01
  4. State machine programming
    By tjstevens in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 17th April 2007, 22:45
  5. Replies: 2
    Last Post: - 27th March 2007, 12:48

Members who have read this thread : 1

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