• AVR Freaks

Hot!USART PIC16F877A

Page: 12 > Showing page 1 of 2
Author
delfindelfin
Super Member
  • Total Posts : 387
  • Reward points : 0
  • Joined: 2017/01/19 12:32:58
  • Location: Mexico
  • Status: offline
2019/07/19 15:56:05 (permalink)
0

USART PIC16F877A

I was thinking about USART and I noticed that it is impossible to have clean standard baudrates .. I made a libre office calc document .. So .. Is this a problem? When trying to communicate with systems other than PICs, this might be bad, isn't? .. I was also thinking, the purpose of BRGH bit, is just to allow to have a big range of values for the SPBRG Register, right? If not, what is the purpose of having a High Speed Mode and a Low Speed Mode
 
  

Attached Image(s)


MPLAB X IDE v5.05
XC8 2.00
#1

25 Replies Related Threads

    1and0
    Access is Denied
    • Total Posts : 9762
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: USART PIC16F877A 2019/07/19 16:49:55 (permalink)
    0
    delfindelfin
    I was thinking about USART and I noticed that it is impossible to have clean standard baudrates .. I made a libre office calc document .. So .. Is this a problem? When trying to communicate with systems other than PICs, this might be bad, isn't? .. I was also thinking, the purpose of BRGH bit, is just to allow to have a big range of values for the SPBRG Register, right? If not, what is the purpose of having a High Speed Mode and a Low Speed Mode

    In some cases, the use of high speed baud rate can reduce the baud rate error. Calculate the baud rates for both low speed and high speed, and use the one with the lower baud rate error. An error of less than 2.5% is acceptable; less than 2% is recommended.
     
    post edited by 1and0 - 2019/07/19 17:12:56
    #2
    delfindelfin
    Super Member
    • Total Posts : 387
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: USART PIC16F877A 2019/07/19 17:20:05 (permalink)
    0
    So, if I use any terminal software .. And let's say I select: 9600 baud rate .. The baudrate of the PIC can be between 9360 and 9840 and still it will communicate with it 

    MPLAB X IDE v5.05
    XC8 2.00
    #3
    1and0
    Access is Denied
    • Total Posts : 9762
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: USART PIC16F877A 2019/07/19 17:45:51 (permalink)
    0
    delfindelfin
    So, if I use any terminal software .. And let's say I select: 9600 baud rate .. The baudrate of the PIC can be between 9360 and 9840 and still it will communicate with it 

    That is 2.5% error and it should work; however, YMMV as it depends on both the transmitter and the receiver. ;)  As said, it is recommended less than 2% error for safer margin.
     
    post edited by 1and0 - 2019/07/19 17:49:24
    #4
    delfindelfin
    Super Member
    • Total Posts : 387
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: USART PIC16F877A 2019/07/19 18:25:22 (permalink)
    0
    But, the speed is the same, whether I choose "High Speed" or "Low Speed", right? 
    post edited by delfindelfin - 2019/07/19 18:26:34

    MPLAB X IDE v5.05
    XC8 2.00
    #5
    1and0
    Access is Denied
    • Total Posts : 9762
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: USART PIC16F877A 2019/07/19 18:37:53 (permalink)
    0
    delfindelfin
    But, the speed is the same, whether I choose "High Speed" or "Low Speed", right? 

    Why don't you just calculate them and see it for yourself? Most often they are not the same, or just take a closer look to the two tables you attached in Post #1!
     
    #6
    NKurzman
    A Guy on the Net
    • Total Posts : 17846
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: USART PIC16F877A 2019/07/19 19:14:25 (permalink)
    +1 (1)
    If you want exact for standard Baud rates, choose a Binary crystal.  Like 7.3728Mhz or 11.0592Mhz
    #7
    delfindelfin
    Super Member
    • Total Posts : 387
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: USART PIC16F877A 2019/07/19 21:05:26 (permalink)
    0
    Well, I just want to communicate with the PC, altough, that's a good idea .. It's not sending exactly what I want .. It must say: "DIP Switch Value:" .. How do I make a Binary Crystal? .. I googled the phrase but I didn't find anything .. 
    post edited by delfindelfin - 2019/07/19 21:13:24

    Attached Image(s)


    MPLAB X IDE v5.05
    XC8 2.00
    #8
    ric
    Super Member
    • Total Posts : 23859
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: USART PIC16F877A 2019/07/19 22:03:51 (permalink)
    0
    delfindelfin
    Well, I just want to communicate with the PC,

    As already explained, anything within 2.5% should work fine, unless you have a different problem.
     

    It's not sending exactly what I want .. It must say: "DIP Switch Value:" ..

    Which tells us nothing if you don't mention what you are actually getting.
     

    How do I make a Binary Crystal? .. I googled the phrase but I didn't find anything ..

    You don't (unless you are a crystal manufacturer). You buy one with a frequency that is an exact multiple of the baud rate you want to achieve.
    NKurzman mentioned a couple of common frequencies that you can buy easily.
     

    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!
    #9
    delfindelfin
    Super Member
    • Total Posts : 387
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: USART PIC16F877A 2019/07/19 22:55:41 (permalink)
    0
    ric
    Which tells us nothing if you don't mention what you are actually getting.
     

    I am getting the attached image that I posted 
     
    ric
     
    You buy one with a frequency that is an exact multiple of the baud rate you want to achieve.
    NKurzman mentioned a couple of common frequencies that you can buy easily.

    Like this one?: https://www.amazon.com/De...p;s=gateway&sr=8-6
    I hope they are easy to find locally in my town 
    post edited by delfindelfin - 2019/07/19 23:04:43

    MPLAB X IDE v5.05
    XC8 2.00
    #10
    ric
    Super Member
    • Total Posts : 23859
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: USART PIC16F877A 2019/07/20 00:40:36 (permalink)
    0
    delfindelfin
    ric
    You buy one with a frequency that is an exact multiple of the baud rate you want to achieve.
    NKurzman mentioned a couple of common frequencies that you can buy easily.

    Like this one?: https://www.amazon.com/De...p;s=gateway&sr=8-6
    I hope they are easy to find locally in my town

    Yes, but as we've been trying to tell you, that is NOT your problem.
    So long as you have the bad rate within 2.5%, it will work. If it doesn't, it is because of something else.
    Can we see the entire source for your PIC test program?
    What are you running on the PC to display the results?
     

    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!
    #11
    PStechPaul
    Super Member
    • Total Posts : 2398
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: online
    Re: USART PIC16F877A 2019/07/20 01:24:04 (permalink)
    +1 (1)
    I have used 14.7456 MHz, which is 9 * 25 * 65536, which can provide exact common baud rates of 75, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600 (19200*3), and 115,200. These numbers also correspond to integer multiples of both 50 and 60 Hz (and 400 Hz), for optimal ADC sampling rate for true RMS computation as well as power line noise cancellation.

     
    #12
    pcbbc
    Super Member
    • Total Posts : 1325
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: USART PIC16F877A 2019/07/20 02:32:02 (permalink)
    0
    And what is your wiring for the UART like?

    Some strings of characters are being received successfully, while others are complete gibberish. Makes me think it is not a baud rate issue but electrical noise, or maybe as ric says a coding error.
    #13
    1and0
    Access is Denied
    • Total Posts : 9762
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: USART PIC16F877A 2019/07/20 06:29:56 (permalink)
    +1 (1)
    delfindelfin
    Well, I just want to communicate with the PC ...

    Agreed with others on it's most likely not a baud rate issue. Anyway, instead of stating the baud rates at 2.5% error, why not tell us the actual baud rate being used? You seem to have a habit of making simple things complicated.
     
    #14
    delfindelfin
    Super Member
    • Total Posts : 387
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: USART PIC16F877A 2019/07/20 07:20:03 (permalink)
    0
    Well, this is what I am using...
    Main Function:

     
    // Including C Standard Libraries
    #include <stdint.h>
    // Including XC8 Compiler Library
    #include <xc.h>
    // Including User's Libraries
    #include "usart.h"
     
    // Defining Oscillator Frequency
    #define _XTAL_FREQ 8000000
     
    // Setting Configuration Bits
    #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
    #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
    #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
    #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
    #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
    #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
    #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
    #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
     
    void main()
    {
    /// UART Initiation /////////////////////////////////////////////////////////
    TRISB = 0xFF; // PORTB as Input

    OPTION_REGbits.nRBPU = 0; // Enables PORTB Internal Pull Up Resistors
    UART_Init(bps_9600); // Initiate USART in UART Mode with 9600 Baudrate
     
    /// Main Function ////////////////////////////////////////////////////////////
    while(1)
    {
    UART_Write_Text("DIP Switch Value:");
    //UART_Write(PORTB);
    UART_Write_Text("\r\n");

    __delay_ms(100);

    }

    }
     

    usart.c

     
    // Including C Standard Libraries
    #include <stdint.h>
    // Including XC8 Compiler Library
    #include <xc.h>
    // Including User's Libraries
    #include "usart.h"
     
    // Defining Oscillator Frequency
    #define _XTAL_FREQ 8000000 
     
    /// UART Initiation /////////////////////////////////////////////////////////////////
    void UART_Init(const uint32_t baudrate) // Function to set USART Frequency
    {

    TXSTAbits.SYNC = 0; // USART Mode Select Bit: Asynchronous Mode

    uint8_t BG_Genarator_High_Speed;
    uint8_t BG_Genarator_Low_Speed;
    uint32_t Inverse_BaudRate_HS;
    uint32_t Inverse_BaudRate_LS;

    BG_Genarator_High_Speed = ( _XTAL_FREQ / ( 16 * baudrate ) ) - 1;
    BG_Genarator_Low_Speed = ( _XTAL_FREQ / ( 64 * baudrate ) ) - 1;

    Inverse_BaudRate_HS = _XTAL_FREQ / ( 16 * (BG_Genarator_High_Speed + 1) );
    Inverse_BaudRate_LS = _XTAL_FREQ / ( 64 * (BG_Genarator_High_Speed + 1) );

    uint8_t error_hs = Inverse_BaudRate_HS - baudrate;
    uint8_t error_ls = Inverse_BaudRate_LS - baudrate;

    if(error_hs < error_ls) // High Speed Configuration
    {
    TXSTAbits.BRGH = 1; // High Baud Rate Enable Bit: High Speed

    SPBRG = BG_Genarator_High_Speed;
    }
    else // Low Speed Configuration
    {
    TXSTAbits.BRGH = 0; // High Baud Rate Enable Bit: Low Speed

    SPBRG = BG_Genarator_Low_Speed; // Saving Value in USART Baud Rate Generator
    }

    RCSTAbits.SPEN = 1; // Serial Port Enabled

    TRISCbits.TRISC7 = 1; // RX Pin as Input
    TRISCbits.TRISC6 = 0; // TX Pin as Output

    RCSTAbits.CREN = 1; // Enabling Continuous Receive until enable bit CREN is cleared
    TXSTAbits.TXEN = 1; // Transmit Enabled
    }
     
    /// Checking Transmit Register /////////////////////////////////////////////////////
    uint8_t UART_TX_Empty()
    {
    return TXSTAbits.TRMT; // Transmit Shift Register Status Bit: 1=Empty. 0=Full
    }
     
    /// Data Received or Not //////////////////////////////////////////////////////////
    uint8_t UART_Data_Ready()
    {
    return PIR1bits.RCIF; // If RCIF 1 = Data Received, 0=Data not Received
    }
     
    char UART_Read()
    {
    while(!PIR1bits.RCIF);
    return RCREG; // Returning Value of USART Receive Register
    }
     
    void UART_Read_Text(char *Output, unsigned int length)
    {
    unsigned int i;
    for(int i=0;i<length;i++)
    Output[i] = UART_Read();
    }
     
    //// USART Operations for Writing ///////////////////////////////////////////////////
    void UART_Write(char data)
    {
    while(!TXSTAbits.TRMT); // Wait for the Transmit Shift Register Status Bit: 1=Empty. 0=Full

    TXREG = data; // Setting Value in USART Transmit Register

     
     void UART_Write_Text(char *text)
    {
    uint8_t i;
    for(i=0;text[i]!='\0';i++)
    {
    UART_Write(text[i]);
    }
    }
     

    and in Linux to receive the data I use the next command:
    cu -l /dev/ttyUSB0 -s 9600
    Altough I have a simmilar result with:
    screen /dev/ttyUSB0 9600
    And something simmilar using PUTTY
    .. In the Proteus Simulation, the data sent is received correctly ..
     I am using a baudrate of 9615.3846...
     
    Edit
    Can I use the same linux commands if I use USRT instead of UART? 
    post edited by delfindelfin - 2019/07/20 07:37:17

    MPLAB X IDE v5.05
    XC8 2.00
    #15
    1and0
    Access is Denied
    • Total Posts : 9762
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: USART PIC16F877A 2019/07/20 08:19:58 (permalink)
    0
    Overall, your code looks okay (I would do a few things differently) and it should work. Check your connection between the PIC and the PC.
    #16
    delfindelfin
    Super Member
    • Total Posts : 387
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: USART PIC16F877A 2019/07/20 08:33:16 (permalink)
    0
    I am using one of these modules:

    Could recommend me those things you would do differently .. And also .. Some suggestions for USRT?

    MPLAB X IDE v5.05
    XC8 2.00
    #17
    pcbbc
    Super Member
    • Total Posts : 1325
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: USART PIC16F877A 2019/07/20 11:26:33 (permalink)
    0
    How do you have the pins on that board connected to the pins on the PIC?
    Hopefully not by long runs of unshielded single core wire?
    Keep them as short as possible or you will pick up noise.

    And I wouldn’t bother calculating the baud rate value in the PIC. You know your clock speed in advance, so calculate in advance and just set the necessary SPBRG value.
    Probably BRGH = 1 and SPBRG = 51 according to your spreadsheet and the datasheet.
    #18
    PStechPaul
    Super Member
    • Total Posts : 2398
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: online
    Re: USART PIC16F877A 2019/07/20 12:20:38 (permalink)
    0
    I find it much more convenient to use a Bluetooth module (HC-05) and not have to worry about external cables. I use Teraterm in Windows as a terminal application.
     
    https://www.instructables.com/id/Modify-The-HC-05-Bluetooth-Module-Defaults-Using-A/
     


     
    #19
    delfindelfin
    Super Member
    • Total Posts : 387
    • Reward points : 0
    • Joined: 2017/01/19 12:32:58
    • Location: Mexico
    • Status: offline
    Re: USART PIC16F877A 2019/07/20 12:31:37 (permalink)
    0
    Well, I am using 10cm jumper wires .. The logic analyzer, gets the right value, though ...
     
     
    post edited by delfindelfin - 2019/07/20 13:07:04

    Attached Image(s)


    MPLAB X IDE v5.05
    XC8 2.00
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2019 APG vNext Commercial Version 4.5