Pin control problems


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Nov 2006
    Posts
    70

    Default Pin control problems

    I've been tinkering with this all day, and I think I need some help.

    I want to use a PIC16F916 to control LEDs. Pretty simple stuff here, but I can't get PORTA to behave normally. I've tried dozens of options, and I keep getting wacko results. I commonly get desired LED blinking on two pins but the rest just sit low or seem to float (ignoring commands).

    Here is an example of some code that should flash 6 LEDs, but only PORTA.4 and PORTA.5 work properly:

    B1 VAR BYTE
    TRISA = 0
    loop:
    B1 = 0
    GOSUB blueon
    pause 500
    B1 = 1
    gosub blueon
    pause 500
    goto loop

    blueon:
    PORTA.0 = B1
    PORTA.1 = B1
    PORTA.2 = B1
    PORTA.3 = B1
    PORTA.4 = B1
    PORTA.5 = B1
    RETURN

    And this might be a hint: If I light up the entire PORTA (PORTA = 255) then things seem to work, but I need to reserve Pins A6 and A7 for serial communication.

    I'm using PBP via Microcode Studio, and I'm activating the internal timer by selecing INTOSCIO before programming with MPLAB.

    I've searched around and found a few examples of problems with analog/digital settings, but none of these seem to apply here. Any ideas?

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Default

    If Dave's suggestion does not pan out, it could be a read-modify-write problem (which makes sense for RA5 to always work since it is the last pin you updated. Try this instead and let us know

    Code:
    B1 VAR BYTE
    TRISA = 0
    loop:
    B1 = 0
    GOSUB blueon
    pause 500
    B1 = %00111111
    gosub blueon
    pause 500
    goto loop
    
    blueon:
    PORTA = B1
    RETURN
    If this works, you will need to be careful as this also updates RA6 and RA7. If it does work, however, there are workarounds to leave these two pins as they were found. Let us know.
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

  4. #4
    Join Date
    Nov 2006
    Posts
    70


    Did you find this post helpful? Yes | No

    Default

    Thank you Paul and Dave. I think the issue may be a combination of both problems.

    I had not seen those posts by Melanie that you referenced, Dave. But tried to set PORTA to digital using various permutations of CMCON and ADCON1 and ANSEL--all to no avail. CMCON actually will not compile--have to use CMCON0. Suffice it to say that CMCON0 = 7 and ADCON1 = 7 seem to do nothing.

    I'm too stupid to actually understand what is in the 16F916 spec sheet. It gives an assembly language example of how to set A to digital, but I cannot translate this into PicBasic code. So I'm still uncertain if I have tried this with PORTA in digital. Here's the asm code from the spec sheet.

    BCF STATUS,RP0 ;Bank 0
    BCF STATUS,RP1 ;
    CLRF PORTA ;Init PORTA
    BSF STATUS,RP0 ;Bank 1
    BCF STATUS,RP1 ;
    MOVLW 07h ;Set RA<2:0> to
    MOVWF CMCON0 ;digital I/O
    CLF ANSEL ;Make all PORTA I/O
    MOVLW F0h ;Set RA<7:4> as inputs
    MOVWF TRISA ;and set RA<3:0>
    ; as outputs
    BCF STATUS,RP0 ;Bank 0
    BCF STATUS,RP1 ;

    Can I just insert this as ASM in the PBP code?

    Your code works, Paul, as I suspected it would given that PORTA = 255 would light everything up. So read-modify-write may be the issue. However, when I put a "Pause 10" after each of the pin commands, I still get good performance only on A.4 and A.5.

    I would really like to be able to manipulate the pins independently, but if that is impossible, I think I can do a work-around by writing to the entire port.

  5. #5
    Join Date
    Nov 2006
    Posts
    70


    Did you find this post helpful? Yes | No

    Default

    Just got it to work with

    CMCON0 = 7
    ANSEL = 0

    At the top. I must have messed something else up when I tried that before.

    Thanks for getting me back in the saddle guys.

  6. #6
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Default

    BCF STATUS,RP0 ;Bank 0
    BCF STATUS,RP1 ;
    CLRF PORTA ;Init PORTA
    BSF STATUS,RP0 ;Bank 1
    BCF STATUS,RP1 ;
    MOVLW 07h ;Set RA<2:0> to
    MOVWF CMCON0 ;digital I/O
    CLF ANSEL ;Make all PORTA I/O
    MOVLW F0h ;Set RA<7:4> as inputs
    MOVWF TRISA ;and set RA<3:0>
    ; as outputs
    BCF STATUS,RP0 ;Bank 0
    BCF STATUS,RP1 ;
    Translated:

    PORTA = 0
    CMCON0 = 7
    ANSEL = 0 ; THE CLF on the ANSEL command you provide must be a typo - it should say CLRF
    TRISA=240 ; or =$F0 or =%11110000

    Adjust the TRISA the way you want it

    If you still get only RA4 and 5 to work with the pause in between, then it is most likely NOT a R-M-W problem.

    EDIT: Looks like you got it - nice work!
    Last edited by paul borgmeier; - 19th October 2007 at 20:04. Reason: Update noted in body of reply
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

Similar Threads

  1. My code for TV remote and MIBAM for RGB control
    By idtat in forum Code Examples
    Replies: 4
    Last Post: - 12th January 2013, 20:52
  2. Is this a K Type sensor?
    By jessey in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 21st November 2009, 13:55
  3. Microcontroller with 2 way paging application problem
    By oneohthree in forum mel PIC BASIC Pro
    Replies: 30
    Last Post: - 20th April 2007, 17:27
  4. Another RTC, DS1287
    By DavidK in forum Code Examples
    Replies: 0
    Last Post: - 12th December 2006, 17:07
  5. Port pin problems
    By sgreelish in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 8th March 2004, 18:56

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