• AVR Freaks

Hot!How to write only to a portion of PORTB ?

Author
indula
New Member
  • Total Posts : 10
  • Reward points : 0
  • Joined: 2019/10/19 06:35:00
  • Location: 0
  • Status: offline
2019/11/15 07:45:38 (permalink)
0

How to write only to a portion of PORTB ?

I'm using dsPIC30F4013. I need to write data that will be sent to LCD only to a portion of the PORTB. The first of the port B which is RB0 needs to be an ADC input. The rest needs to be digital outputs. The LCD D0-D7 are connected to RB1 - RB8. How do I program this on registers ? 
PinOut of the dsPIC30F4013 is as follows. 

#1

5 Replies Related Threads

    KTrenholm
    Super Member
    • Total Posts : 742
    • Reward points : 0
    • Joined: 2012/08/08 14:04:23
    • Location: Connecticut, USA
    • Status: offline
    Re: How to write only to a portion of PORTB ? 2019/11/15 07:55:34 (permalink)
    0
    Compiler Name/Version?
    If I assume you're using XC16...
     
    If I'm not mistaken, if RB0 is analog it will ignore any writes to the digital latch for that pin, and will not care what value is in the LATB<0> bit, as it is set for analog operation.
     
    You can just take your byte that needs to written to D<7:0>, shift it left once (to align it to <8:1>) and write it to LATB.
     
    uint8_t LCD_DataByte = 0xAA;
    LATB = (LCD_DataByte << 1);
    post edited by KTrenholm - 2019/11/15 07:57:38
    #2
    NorthGuy
    Super Member
    • Total Posts : 5805
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: online
    Re: How to write only to a portion of PORTB ? 2019/11/15 08:00:22 (permalink)
    0
    You configure each bit separately using TRISB and ADPCFG registers, then you write to LATB register to manipulate your pins. To make sure you don't disturb the bits you don't want to write, you set bits individually, such as
     
    LATBbits.LATB0 = 0;

     
    You can also set a group of bits, if it's more convenient:
     
     LATB ^= (LATB ^ val) & mask;

     
    where "mask" contains '1' for the bits you want to change and 'val' contains the bits you want to set. For example, if mask is 0x0f and val is 0x0a, it'll set bits 1 and 3, clear bits 0 and 2, and will leave everything else unchanged.
     
    post edited by NorthGuy - 2019/11/15 08:42:01
    #3
    indula
    New Member
    • Total Posts : 10
    • Reward points : 0
    • Joined: 2019/10/19 06:35:00
    • Location: 0
    • Status: offline
    Re: How to write only to a portion of PORTB ? 2019/11/15 08:19:10 (permalink)
    0
    Do I have to configure RB0 as a analog pin and the rest as digital pins ? If yes, how do I do that ? Please give me an example with register names that needs to be set?
    #4
    rodims
    Super Member
    • Total Posts : 1532
    • Reward points : 0
    • Joined: 2009/02/10 11:08:59
    • Location: 51.9627, 7.6262
    • Status: offline
    Re: How to write only to a portion of PORTB ? 2019/11/15 08:40:13 (permalink)
    5 (1)
    NorthGuy already answered it for you.
     
    For details it is helpful to look up your manual
    http://ww1.microchip.com/downloads/en/devicedoc/70138c.pdf
    especially chapter
    7.0  and 7.1  and 7.2  (that's not much to read)
    and may be:
    19.0  12-BIT ANALOG-TO-DIGITAL CONVERTER (A/D) MODULE
    19.12 Configuring Analog Port Pins
    #5
    ric
    Super Member
    • Total Posts : 24581
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: How to write only to a portion of PORTB ? 2019/11/15 16:41:43 (permalink)
    0
    Note,
    indula
    Do I have to configure RB0 as a analog pin and the rest as digital pins ? If yes, how do I do that ? Please give me an example with register names that needs to be set?

    It's not the analog mode that matters, it's if the pin is set as an input or an output.
    That is controlled by TRISB.
    It doesn't matter what you write to pins that are in input mode.
     

    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
    Jump to:
    © 2019 APG vNext Commercial Version 4.5