New user: port configuration problem/unexpected interaction/16F887


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2014
    Location
    England
    Posts
    16

    Default New user: port configuration problem/unexpected interaction/16F887

    Hi,

    I am a recent convert from the easy-to-use Picaxe to the much more powerful PBP3 .

    My progress is inconsistent: with one step back for every two forward!

    The latest project scans a 3 column by 4 row keypad, ouputs to a 2 X 16 OLED display and crunches some maths. The code has been cobbled together from the Melabs examples and performs flawlessly.

    Until I come to the simple task of reading a push-button and lighting an led! Every time I press a key on the keypad, it shows correctly on the OLED but blinks the led as well. Whereas the push-button on D.0 will not blink the led. I have stripped the code down in an effort to debug it, but the problem persists. At one stage I had the push-button blinking the led - but it nadgered the keypad scanning as a bonus ...

    I suspect that the configuration is at the heart of this. All of the keypad scanning is done on Port B; the OLED display is split between Port C and D;
    the push-button is on D.0; the led on D.4;

    Any help would be appreciated - thank you!

    ; define OLED connections
    Define LCD_DREG PORTC
    Define LCD_DBIT 0
    Define LCD_RSREG PORTD
    Define LCD_RSBIT 3
    Define LCD_EREG PORTD
    Define LCD_EBIT 2
    Define LCD_BITS 8
    Define LCD_LINES 2
    Define LCD_COMMANDUS 1500
    Define LCD_DATAUS 44


    ; define program variables
    col var byte ' Keypad column
    row var byte ' Keypad row
    key var word ' Key value


    gosub init ; initialise
    gosub welcome ; OLED splash

    mainloop:
    gosub getkey
    lcdout 254, 192, "key=",#key, " " ; line 2
    if portD.0 = 0 then gosub flash
    goto mainloop


    init:
    ANSEL = 000000 ; AN0-AN7 digital
    ANSELH= 000000 ; AN8-AN13 digital
    OPTION_REG.7 = 0 ; Port B pullups
    TRISB = 0 ; Port B all outputs
    TRISD = 000011 ; Port D all output except D0 & D1 PBT inputs
    PORTD = 000000 ; setport PortD ouputs low
    pause 1000 ; await OLED power-up
    return


    welcome:
    lcdout 254, 1 ; CLS
    pause 200
    lcdout 254, 128 , "Hello "
    pause 2500
    lcdout 254, 1 ; CLS
    pause 200
    return


    flash:
    portD.4=1
    pause 500
    portD.4=0
    return

    getkey:
    pause 50 ;debounce


    getkeyu:
    ' Wait for all keys up
    PORTB = 0 ;all output pins low
    TRISB = 110000 ;bottom 4 pins out, top 4 pins in
    If ((PORTB >> 4) != 001111) Then getkeyu ' If any keys down, loop
    Pause 50 ;debounce


    getkeyp:
    ' Wait for keypress
    For col = 0 To 3 ' 4 columns in keypad
    PORTB = 0 ' All output pins low
    TRISB = (dcd col) ^ $ff ' Set one column pin to output
    row = PORTB >> 4 ' Read row
    If row != 001111 Then gotkey ' If any keydown, exit
    Next col
    Goto getkeyp ' No keys down, go look again


    ' Change row and column to key number 1-12
    gotkey:
    key = (col * 3) + (ncd (row ^ 001111)) ;4RX3C matrix
    return

  2. #2
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333


    Did you find this post helpful? Yes | No

    Default Re: New user: port configuration problem/unexpected interaction/16F887

    Quote Originally Posted by Experimenter View Post
    mainloop:
    gosub getkey
    lcdout 254, 192, "key=",#key, " " ; line 2
    if portD.0 = 0 then gosub flash
    goto mainloop
    flash:
    portD.4=1
    pause 500
    portD.4=0
    return

    getkey:
    pause 50 ;debounce


    getkeyu:
    ' Wait for all keys up
    PORTB = 0 ;all output pins low
    TRISB = 110000 ;bottom 4 pins out, top 4 pins in
    If ((PORTB >> 4) != 001111) Then getkeyu ' If any keys down, loop
    Pause 50 ;debounce


    getkeyp:
    ' Wait for keypress
    For col = 0 To 3 ' 4 columns in keypad
    PORTB = 0 ' All output pins low
    TRISB = (dcd col) ^ $ff ' Set one column pin to output
    row = PORTB >> 4 ' Read row
    If row != 001111 Then gotkey ' If any keydown, exit
    Next col
    Goto getkeyp ' No keys down, go look again


    ' Change row and column to key number 1-12
    gotkey:
    key = (col * 3) + (ncd (row ^ 001111)) ;4RX3C matrix
    return
    Your program starts at Mainloop then gosubs to getkey and loops round getkeyp until a key is pressed when a key is pressed then it returns to mainloop. So it never will execute the 'if portD.0 = 0 then gosub flash' statement until the keypad is pressed. This why the push button does not work. Also portD.0 must be 0 when the button is not pressed as it flashes when the keypad is pressed.

    Hope this helps.

    I like your programing style as it is easy for me to follow.

  3. #3
    Join Date
    Mar 2014
    Location
    England
    Posts
    16


    Did you find this post helpful? Yes | No

    Default Re: New user: port configuration problem/unexpected interaction/16F887

    Thank you very much for this, Steve.

    I 'll correct the program flow as you describe and will then investigate the voltages on D.0;

    I think I will need to simplify the wiring first, get it working and then reconnect the complicated bits ...

    Tony

  4. #4
    Join Date
    Oct 2011
    Posts
    54


    Did you find this post helpful? Yes | No

    Default Re: New user: port configuration problem/unexpected interaction/16F887

    Had a quick look at your code
    TRISD = 000011 should be TRISD = %000011
    TRISB = 110000 should be TRISB = %11110000
    You have missed off the "%" denoting a binary number in several places
    Phil

  5. #5
    Join Date
    Mar 2014
    Location
    England
    Posts
    16


    Did you find this post helpful? Yes | No

    Default Re: New user: port configuration problem/unexpected interaction/16F887

    Thanks for replying, Phil.

    In fact the 'missing digits' and % sign slipped out during the operation to copy/paste the code.

    With the forum's help I found the software bugs; and I tracked down the hardware bug that Steve alluded to.
    Everything is now working.

    Moreover it has restored my confidence that I should stick with Picbasic, rather than going back to the Picaxe!

    Regards,

    Tony

Similar Threads

  1. pic 16f887 port holding problems
    By lockjawz in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 12th April 2011, 14:01
  2. 18F2520 - problem configuring fuse for port B as digital port.
    By hwhisperer in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 11th October 2010, 11:41
  3. Problem driving lcd from PIckit2 and 16F887
    By Davidmarks in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 31st January 2010, 18:28
  4. 16f877 port configuration & interrupts
    By Rob Martin in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 15th April 2005, 17:30
  5. User Configuration Interface in PBP
    By Radiance in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 7th February 2004, 08:00

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