• AVR Freaks

Hot!PIC16F1789: PORTB DIGITAL INPUT PROBLEM

Author
irmak
Starting Member
  • Total Posts : 38
  • Reward points : 0
  • Joined: 2019/07/05 23:50:12
  • Location: 0
  • Status: offline
2019/10/19 05:15:22 (permalink)
0

PIC16F1789: PORTB DIGITAL INPUT PROBLEM

Dear Forum Members
 
I could not find solution for a simple problem with PIC16f1789.
PUSH Button on other ports work fine but I could not suceed to solve PUSH Button problem on PORTB. It does not work.
Code and schematics are below
 
What is missing ? Is there additional Registers to make settings ?
_____________
void main(void) {
TRISA=0; // LED
TRISB=255; // INPUT
TRISC=255; // INPUT
PORTA=0; // LED
while(1)
{
if (RB0==1) RA0=1;
if (RB0==0) RA0=0;
//
if (RC0==1) RA0=1;
if (RC0==0) RA0=0;

}
}
_____________
post edited by irmak - 2019/10/19 05:17:07

Attached Image(s)

#1

13 Replies Related Threads

    ric
    Super Member
    • Total Posts : 24244
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F1789: PORTB DIGITAL INPUT PROBLEM 2019/10/19 05:18:41 (permalink)
    +1 (1)
    You forgot to switch the PORTB pins from analog to digital mode.
    Have another read of "13.5 PORTB Registers" in the datasheet, and you will see that it does tell you about this.
     
     
    post edited by ric - 2019/10/19 05:19:42

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    irmak
    Starting Member
    • Total Posts : 38
    • Reward points : 0
    • Joined: 2019/07/05 23:50:12
    • Location: 0
    • Status: offline
    Re: PIC16F1789: PORTB DIGITAL INPUT PROBLEM 2019/10/19 05:22:08 (permalink)
    0
    Dear Ric
     
    ANSELB = 0b00000000;
     
    works but can you explain why does PIC16f1789 needs additional settings ?
    #3
    ric
    Super Member
    • Total Posts : 24244
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F1789: PORTB DIGITAL INPUT PROBLEM 2019/10/19 05:24:57 (permalink)
    +1 (1)
    What additional settings?
    Setting any analog pins to digital mode has to be done on ALL PICs that have analog inputs.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    irmak
    Starting Member
    • Total Posts : 38
    • Reward points : 0
    • Joined: 2019/07/05 23:50:12
    • Location: 0
    • Status: offline
    Re: PIC16F1789: PORTB DIGITAL INPUT PROBLEM 2019/10/19 05:27:01 (permalink)
    0
    Dear RIC
     
    I do not make an ANSEL settings to other ports of this device as they have analog functions. But I have to make additional settings for PORT B
    #5
    ric
    Super Member
    • Total Posts : 24244
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F1789: PORTB DIGITAL INPUT PROBLEM 2019/10/19 05:28:32 (permalink)
    +2 (2)
    No you're not.
    The only pins you are reading from in the code you have shown are PORTB and PORTC.
    PORTC does not have analog input capability.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    irmak
    Starting Member
    • Total Posts : 38
    • Reward points : 0
    • Joined: 2019/07/05 23:50:12
    • Location: 0
    • Status: offline
    Re: PIC16F1789: PORTB DIGITAL INPUT PROBLEM 2019/10/19 05:31:59 (permalink)
    0
    You are right !!!
     
    I have understand the problem. 
     
    If I use any pin with ADC function I have to make ANSEL settings
     
    Thank you
    #7
    1and0
    Access is Denied
    • Total Posts : 9882
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC16F1789: PORTB DIGITAL INPUT PROBLEM 2019/10/19 05:59:54 (permalink)
    +1 (1)
    Do use LATx for outputs.
    #8
    irmak
    Starting Member
    • Total Posts : 38
    • Reward points : 0
    • Joined: 2019/07/05 23:50:12
    • Location: 0
    • Status: offline
    Re: PIC16F1789: PORTB DIGITAL INPUT PROBLEM 2019/10/19 06:03:46 (permalink)
    0
    Dear 1and0
     
    I have tried but not work without ANSEL
     
    MICROCHIP Sample on web site is based on LATx.
    Do you have a sample code ?
    Can you explain the difference between PORT and LAT ?
    #9
    ric
    Super Member
    • Total Posts : 24244
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F1789: PORTB DIGITAL INPUT PROBLEM 2019/10/19 06:13:40 (permalink)
    +3 (5)
    He means change
    if (RB0==1) RA0=1;

    to
    if (RB0==1) LATA0=1;

    Or you could use the real names of those bits...
    if (PORTBbits.RB0 == 1) LATAbits.LATA0 = 1;

     
    I get tired of explaining this over and over again.
    Read my latest explanation at:
    http://picforum.ric323.com/viewtopic.php?f=31&t=744#p3870
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #10
    1and0
    Access is Denied
    • Total Posts : 9882
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC16F1789: PORTB DIGITAL INPUT PROBLEM 2019/10/19 06:16:29 (permalink)
    +2 (2)
    Without going into the details, use PORTx for inputs and LATx for outputs.  Read Ric's explanation for details.
    #11
    irmak
    Starting Member
    • Total Posts : 38
    • Reward points : 0
    • Joined: 2019/07/05 23:50:12
    • Location: 0
    • Status: offline
    Re: PIC16F1789: PORTB DIGITAL INPUT PROBLEM 2019/10/19 06:23:12 (permalink)
    0
    Dear 1and0 
     
    I have read PORT and LAT many times but I am not sure the difference in practice ?
     
    e.g
    From: https://stackoverflow.com/questions/2623428/difference-between-port-and-latch-on-pic-18f
     
    "The latch is the output latch onto which values are written.
    The port is the voltage at the actual pin.
     
    There are a few situations where they can be different.
     
    The one that I've encountered most frequently is if you have a pin (accidentally) shorted to ground. If you set the latch high, the latch will read high, but the port will read low because the voltage on the pin is still approximately ground.
     
     
    Another situation leading to what you've described is when the port pin hasn't been configured correctly. I (and everyone I work with) have spent many hours trying to figure out why our PIC isn't working to expectations, to eventually find out that we glossed over turning off the analog modules, for instance. Make sure you go over the section I/O Ports -> PORT?, TRIS?, and LAT? registers in the datasheet. ...."
     
     
     
    _______________________________
    Knowledge glorifies man, arrogance makes man arrogant
    post edited by irmak - 2019/10/19 06:27:48
    #12
    1and0
    Access is Denied
    • Total Posts : 9882
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC16F1789: PORTB DIGITAL INPUT PROBLEM 2019/10/19 06:26:47 (permalink)
    +4 (4)
    Who gives Ric's Post #10 a downvote???  I upvote it.
    #13
    1and0
    Access is Denied
    • Total Posts : 9882
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: PIC16F1789: PORTB DIGITAL INPUT PROBLEM 2019/10/19 06:32:44 (permalink)
    0
    irmak
    I have read PORT and LAT many times but I am not sure the difference in practice ?

    I don't have the time now to explain in details. Gotta take the family out for a fun weekend!
     
    For now, just notice output to PORTx can result in Read-Modify-Write (RMW) issues. Give this a read: https://www.microchip.com/forums/m478014.aspx
    #14
    Jump to:
    © 2019 APG vNext Commercial Version 4.5