• AVR Freaks

Hot!PIC16F17/18/19 UART Question

Author
gcode
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2018/09/01 21:55:55
  • Location: 0
  • Status: offline
2019/08/18 21:41:36 (permalink)
0

PIC16F17/18/19 UART Question

I spent more time than I care to admit, so I come to you asking for some assistance.
I've (also) spent many hours in the forum searching for answers, googling, and (trying) to make sense of the data sheet.

It seems as if you have solved the UART using the PIC16F1719 (which is similar to the 17 and 18). The code I am using, normally works for the 16F876, but unfortunately I had to leave this old chip behind (finally). Needed more programming memory and other peripherals.

My current initialization function looks like this. If you could let me know where I went wrong, or send me your working program, I can compare what I got and figure out what I am not doing correctly. While I wait on a reply, I will continue reading the data sheet, hoping I get a break through.
 
MPLAB X IDE v5.20
Compiler XC8 (v2.05)

Code...
The baud isn't coming out like the data sheet specifies on page 368.
@ _XTAL_FREQ 32000000 the data sheet says that SPBRG should be 51 for 9600 (baud). I'm getting 207. So that is the first thing I'm correcting. I suspect my algebra formula SP1BRGL = (_XTAL_FREQ/16 * (UART_BAUDRATE + 1)) is incorrect.
 
 
post edited by gcode - 2019/08/21 18:12:51
#1

14 Replies Related Threads

    NKurzman
    A Guy on the Net
    • Total Posts : 17719
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: PIC16F17/18/19 UART Question 2019/08/18 21:51:38 (permalink)
    +1 (1)
    Get MCC and make a test project with just the UART. It should be useful as an example
    #2
    ric
    Super Member
    • Total Posts : 23582
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F17/18/19 UART Question 2019/08/18 22:33:38 (permalink)
    +1 (1)
    The "51" value is for when BRGH=0.
    The formula you specify is for when BRGH=1
    Just look at the third table on the same page.
     
    Your real code does this:
        TXSTA = 0x24; 

    it would make everyone's life much simpler if you put comments on your magic numbers!
        TXSTA = 0x24;    // Set TXEN and BRGH 

     
    post edited by ric - 2019/08/18 22:34:55

    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!
    #3
    gcode
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2018/09/01 21:55:55
    • Location: 0
    • Status: offline
    Re: PIC16F17/18/19 UART Question 2019/08/19 00:07:05 (permalink)
    0
    ric
    The "51" value is for when BRGH=0.
    The formula you specify is for when BRGH=1
    Just look at the third table on the same page.

     
    Thanks Ric, don't know how I missed reading the top header boxes. This means my math is right, no more work necessary on that area. I'll push forward.
     
    NKurzman
    Get MCC and make a test project with just the UART. It should be useful as an example

     
    Tried that and didn't find anything. Interesting enough, I'm not really fond of MCC yet. I wish I knew of a way to change the way it spit out code. I guess as I learn more about it, I'll learn to appreciate it, but as of today - I would prefer coding it by hand.
    #4
    gcode
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2018/09/01 21:55:55
    • Location: 0
    • Status: offline
    Re: PIC16F17/18/19 UART Question 2019/08/19 00:37:38 (permalink)
    0
    I still can't find what I am missing.
    #5
    ric
    Super Member
    • Total Posts : 23582
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F17/18/19 UART Question 2019/08/19 00:47:43 (permalink)
    +1 (1)
    gcode
    I still can't find what I am missing.

    Can't help with this totally fact free problem description.
     
     

    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
    ric
    Super Member
    • Total Posts : 23582
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F17/18/19 UART Question 2019/08/19 00:49:36 (permalink)
    +1 (1)
    I would suggest you post the complete test program (including CONFIG bits),
    and throw away all the PPSLOCK stuff until AFTER you have a working program.
     
     

    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!
    #7
    NorthGuy
    Super Member
    • Total Posts : 5580
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: PIC16F17/18/19 UART Question 2019/08/19 06:30:44 (permalink)
    +1 (1)
    SP1BRGL = (_XTAL_FREQ/16 * (UART_BAUDRATE + 1));

     
    This cannot be right. At the very least, you should divide by baudrate, not multiply.
     
    SP1BRGH = 1;

     
    Why is that?
    #8
    gcode
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2018/09/01 21:55:55
    • Location: 0
    • Status: offline
    Re: PIC16F17/18/19 UART Question 2019/08/19 09:35:11 (permalink)
    0
    I don't know how that got in there, it is supposed to be
    SP1BRGL = ((_XTAL_FREQ / 16) / UART_BAUDRATE) - 1;
     
    SP1BRGH = SPBRGH
    #9
    ric
    Super Member
    • Total Posts : 23582
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F17/18/19 UART Question 2019/08/19 13:15:34 (permalink)
    0
    I don't know what you were trying to achieve with "SP1BRGH = SPBRGH" but it's wrong.
    Replace
    SP1BRGL = ((_XTAL_FREQ / 16) / UART_BAUDRATE) - 1;
    with
    SP1BRG = ((_XTAL_FREQ / 16) / UART_BAUDRATE) - 1;
     
    That will write to the whole 16 bit double register. Your version only writes the low 8 bits.
    Once again, please post a COMPLETE test program, and tell us what happens when you try to run it.
    It's far easier to test transmit first, looking at the TX pin output on a scope. Once that works, you can test receive.
     

    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
    gcode
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2018/09/01 21:55:55
    • Location: 0
    • Status: offline
    Re: PIC16F17/18/19 UART Question 2019/08/21 16:56:19 (permalink)
    0
    Pardon my typo.
    SPBRGH           EQU  H'019C'
    SPBRGH1          EQU  H'019C'
     
    Several late nights with coffee.
    #11
    ric
    Super Member
    • Total Posts : 23582
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC16F17/18/19 UART Question 2019/08/21 17:03:50 (permalink)
    +1 (1)
    gcode
    Pardon my typo.
    SPBRGH           EQU  H'019C'
    SPBRGH1          EQU  H'019C'

    Huh?
    I have no idea what you are trying to say now.
    Do you still have a problem?
    If yes, post the whole program, and explain what is happening.
    You're just wasting time posting incomplete fragments.
     

    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!
    #12
    gcode
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2018/09/01 21:55:55
    • Location: 0
    • Status: offline
    Re: PIC16F17/18/19 UART Question 2019/08/21 18:11:20 (permalink)
    0
    Don't trouble yourself rick, thanks, but I got it from here. The nicest and most effective comment was made by NKurzman. Against my better judgement, I used MCC. Produced a code. Then I compared the MCC code to my code. Viola! I found the error quickly. For anyone reading this in the future, don't waste your time. Use MCC, it isn't cynical, judgemental, or rude. And most importantly you receive results. Which is probably why Microchip decided to implement it.
    #13
    NKurzman
    A Guy on the Net
    • Total Posts : 17719
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: PIC16F17/18/19 UART Question 2019/08/21 19:06:49 (permalink)
    0
    Even if you do not use it for your code, you can use it for sample code.
    #14
    Howard Long
    Super Member
    • Total Posts : 681
    • Reward points : 0
    • Joined: 2005/04/04 08:50:32
    • Status: offline
    Re: PIC16F17/18/19 UART Question 2019/08/23 07:48:13 (permalink)
    0
    NKurzman
    Even if you do not use it for your code, you can use it for sample code.

    That's one of the very few positive aspect of MCC IMHO when you can't get something to behave, there's a chance it might generate working code. But I have to be desperate!
    #15
    Jump to:
    © 2019 APG vNext Commercial Version 4.5