Problems concurrently using digital in and digital out on PORTA.


+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Jun 2007
    Posts
    20

    Default Problems concurrently using digital in and digital out on PORTA.

    I have a switch hooked to PORTA.0 and a LED on PORTA.1 on a PIC16F688. All I am trying to do is read the value of the switch and then set the LED high or low, accordingly. Here is my code:

    Code:
     ' PIC16F688
    INCLUDE "MODEDEFS.BAS"       ' Include Shiftin/out modes
    
    DEFINE OSC 8                ' 8mHz clock
    OSCCON = %01110001          ' 8mHz clock
    
    ' ======= Set up serial communications
    DEFINE HSER_RCSTA 90h 'SPEN=0, CREN=0
    DEFINE HSER_TXSTA 24h 'BRGH=0, SYNC=0, TXEN=1
    DEFINE HSER_BAUD 19200
    DEFINE HSER_CLROERR 1
    ' =====================================
    
    ' ======= Disable unused features & setup IO pins
    ANSEL = %00000000   ' everything is digital    
    ADCON0 = 0          ' disable ADC
    CMCON0 = 7          ' no comparators
    TRISA = %00000001   ' all but RA0 is output
    TRISC = %00100000   ' all outputs on port C except MCLR 
    ' =====================================
    
    ' Variables
    ALARM       VAR PORTC.0
    SWITCH      VAR PORTA.0
    LED         VAR PORTA.1
    
    ' Intialize
    pause 100
    hserout ["============", 13, 10]
    
    main:
        gosub print_status
        pause 1000
        goto main
    
    print_status:
        hserout ["Val: ", DEC SWITCH, 13, 10]
        If switch = 1 then
            led = 0
        else
            LED = 1
        endif
        return
    
    END
    What it's doing instead is toggling the LED on and off:
    Code:
    .============
    .Val: 1
    .Val: 0
    .Val: 1
    .Val: 0
    .Val: 1
    .Val: 0
    .Val: 1
    .Val: 0
    Any idea what's going on?

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,817

    Default

    Dammit! Gotta get my eyes checked!
    Ok, you have a pull up or pull down on PortA.0?
    Does the switch seek power or ground when activated ?
    Seems like stupid questions, but the problem is nearly always a simple overlooked detail, and hair is too precious to waste on not asking the question.
    Ok Ok, I see the val Var is tracking the port status of PortA.0 I would try setting the port status prior to the tris register and triple check for analog stuff still in operation. Does it work if you use High and Low instead of 1 / 0 ? If so that is an indicator of analog stuff still active.
    Last edited by Archangel; - 11th October 2009 at 05:43. Reason: missed seeing what he had
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    Join Date
    May 2008
    Location
    Italy
    Posts
    825

    Default

    This is your code:
    ANSEL = %00000000 ' everything is digital
    ADCON0 = 0 ' disable ADC
    CMCON0 = 7 ' no comparators

    This is what it should be:

    Ansel = 0 ' disable analog
    CMCON = 7 ' no comparators
    Al.
    All progress began with an idea

  4. #4

    Cool Digital Outputs 16FXXX dŽont work fine in PBP?...

    IŽm a similar problem.
    The outputs in a 16f690 controlled by HIGH command dŽont work fine.

    This simpy code present the follw voltages in the pins of 16F690 pic.


    Code:
    inicio:
    
     
    High PORTC.5    
    High PORTC.4     
    High PORTC.3 
    
    High PORTC.6     
    High PORTC.7  
    High PORTB.7 
    
    GOTO inicio


    VCC Values:

    PORTC.5 = 5V
    PORTC.4 = 5V
    PORTC.3 = 5V

    PORTC.6 = 0.6V
    PORTC.7 = 1.8V
    PORTB.7 = 5V

  5. #5
    Join Date
    Sep 2004
    Location
    Mentor, Ohio
    Posts
    347

    Smile

    Hi joseluistajada,

    Now add a "pause 500" then make all of your port pins "LO"

    All you have done in your simple little program is turn the pins ON.

    Put a short delay after your last HIGH command then make all of the pins LO
    and you should see your outputs flash on and off at a 1/2 second on and 1/2 second off (assuming a 4mhz crystal).

    HTH,

    BobK

  6. #6

    Default

    joseluistajada, you also need to show your initialization code. The pins may not be set to digital by default. You need to set TRIS values and ANSEL and other registers before a PIC will work as expected.
    Tim Barr

Posting Permissions

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