RS232 Baud Rate


Closed Thread
Results 1 to 6 of 6

Thread: RS232 Baud Rate

  1. #1
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240

    Default RS232 Baud Rate

    Hi Everyone;

    I made this program and works fine at 9600 or 2400bps, however if i put at for exemple 57600 or 19200, i don't receive the the chars correctly, can somebody help me;

    PIC16F877A
    4Mhz

    Thanks

    Code:
    Code:
    include "modedefs.bas"
    DEFINE OSC 4
    
    '                               VARIÁVEIS
    ' ====================================================================
    DADOS var byte(4)
    ID    var byte(2)
    
    '                              REGISTOS PINOUT 1 = IN; 0 = OUT
    ' ====================================================================
             '76543210
    TRISA  = %00000001 
    TRISB  = %00001111 
    TRISC  = %10000000 
    TRISD  = %00000000 
    TRISE  = %00000000
    ADCON1 = 7
    
    '                              NOMES PINOUT
    ' ====================================================================
    LED    VAR PORTA.1
    TX     Var PORTC.6
    RX     VAR PORTC.7
    
    '                              DEFINIÇÕES
    ' ====================================================================
       DEFINE LCD_DREG PORTB       ' LCD Data bits on PORTB
       DEFINE LCD_DBIT 4           ' PORTB starting address
       DEFINE LCD_RSREG PORTB      ' LCD RS bit on PORTB
       DEFINE LCD_RSBIT 3          ' LCD RS bit address
       DEFINE LCD_EREG PORTB       ' LCD E bit on PORTB
       DEFINE LCD_EBIT 2           ' LCD E bit address
       DEFINE LCD_BITS 4           ' LCD in 4-bit mode
       DEFINE LCD_LINES 2          ' LCD has 2 rows
       DEFINE LCD_COMMANDUS 2000   ' Set command delay time in us
       DEFINE LCD_DATAUS 50        ' Set data delay time in us
       
       DEFINE HSER_RCSTA 90h
       DEFINE HSER_TXSTA 24h 
       DEFINE HSER_BAUD 9600
       DEFINE HSER_SPBRG 25
          
    '                             INICIO PROGRAMA
    ' ====================================================================
    Start:
     
    LCDOUT $fe, 1
    lcdout "      Teste USB"
    lcdout $FE,$C0," Por Hugo  Oliveira"
    pause 2000
    
    Main:
     toggle led
     hserout ["Hugo "] 
     lcdout "      Teste USB"
     lcdout $FE,$C0,"        Hugo        "
     Pause 2000
     toggle led
     hserout ["Oliveira",cr] 
     LCDOUT $fe, 1 
     lcdout "      Teste USB"
     lcdout $FE,$C0,"      Oliveira      "
     Pause 2000
     low led
    
    
    '       Rotina que espera por trama, ID, e Valor e compara
    ' ====================================================================
    Recebe:
    Hserin [wait("OK"),STR ID\2,str DAdos\4]
    if id(0)="3" and id(1)="2" then
        if DADOS(0)="H" and DADOS(1)="u" and DADOS(2)="g" and DADOS(3)="o" then
            high led
            LCDOUT $fe, 1 
            lcdout "      Teste USB"
            lcdout $FE,$C0,"Recebido H          "
        endif 
             
        if DADOS(0)="L" and DADOS(1)="u" and DADOS(2)="L" and DADOS(3)="u" then
            low led
            LCDOUT $fe, 1 
            lcdout "      Teste USB"
            lcdout $FE,$C0,"Recebido L          " 
        endif
     
        if DADOS(0)<>"L" and DADOS(0)<>"H"  then
            LCDOUT $fe, 1 
            lcdout "      Teste USB"
            lcdout $FE,$C0,"Recebido    :",DADOS(0),DADOS(1),DADOS(2),DADOS(3)    
        endif
    endif
    goto Recebe
    
    
    END

  2. #2
    sinoteq's Avatar
    sinoteq Guest


    Did you find this post helpful? Yes | No

    Default More speed :)

    Quote Originally Posted by gadelhas View Post
    PIC16F877A
    4Mhz
    Hi
    If you need higher baudrate than 9600 you really must change oscillator speed to something higher. In this case more is better

  3. #3
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default

    Hi Again,

    I change this 2 lines;
    Code:
    include "modedefs.bas"
    DEFINE OSC 4
    for this;
    Code:
    DEFINE OSC 4
    include "modedefs.bas"
    And now i have tested with success this baud rates;
    HSER_BAUD - HSER_TXSTA
    300 - 20h
    600 - 20h
    1200 - 20h
    2400 - 24h
    4800 - 24h
    9600 - 24h
    14400 - 24h
    19200 - 24h
    28800 - 24h

    38400 and 57600, tested, but without success.
    PIC16F877A-I/P
    4Mhz

    Regards

  4. #4
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by sinoteq View Post
    Hi
    If you need higher baudrate than 9600 you really must change oscillator speed to something higher. In this case more is better
    Hi sinoteq,
    Thanks for your answer, however int he datasheet, it says that it will work up to 57600 with 4mhz oscilator.
    I allready teste with success baud rates higher than 9600, as you can see in my previous post.

  5. #5
    malc-c's Avatar
    malc-c Guest


    Did you find this post helpful? Yes | No

    Default Pic Multicalc

    Hi,

    Do a search for Pic Multicalc, it's very useful for setting up the defines etc. Entering an osc of 4Mhz and 3400 or 57600 it gives these values

    Code:
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
    DEFINE HSER_SPBRG 6   ' 38400 Baud @ 4MHz, -6.99%
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    
    
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
    DEFINE HSER_SPBRG 0   ' 57600 Baud @ 4MHz, 8.51%
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    You could try these values, however for running at a fast baud rate I would recommend using a 20Mhz crystal on the 16F877A with the following defines

    Code:
    @   __config _HS_OSC 
    
    DEFINE  OSC 20
    
    DEFINE HSER_BAUD    115200        ' Hser baud rate 
    DEFINE HSER_RCSTA   90h           ' Hser receive status init 
    DEFINE HSER_TXSTA   24h           ' Hser transmit status init 
    DEFINE HSER_SPBRG 10                ' 115200 Baud @ 20MHz, -1.36%
    DEFINE HSER_CLROERR 1             ' Hser clear overflow automatically

  6. #6
    Join Date
    Aug 2008
    Location
    Portugal
    Posts
    240


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by malc-c View Post
    Hi,

    Do a search for Pic Multicalc, it's very useful for setting up the defines etc. Entering an osc of 4Mhz and 3400 or 57600 it gives these values

    Code:
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h ' Enable transmit, BRGH = 1
    DEFINE HSER_SPBRG 6   ' 38400 Baud @ 4MHz, -6.99%
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    
    
    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
    DEFINE HSER_SPBRG 0   ' 57600 Baud @ 4MHz, 8.51%
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    You could try these values, however for running at a fast baud rate I would recommend using a 20Mhz crystal on the 16F877A with the following defines

    Code:
    @   __config _HS_OSC 
    
    DEFINE  OSC 20
    
    DEFINE HSER_BAUD    115200        ' Hser baud rate 
    DEFINE HSER_RCSTA   90h           ' Hser receive status init 
    DEFINE HSER_TXSTA   24h           ' Hser transmit status init 
    DEFINE HSER_SPBRG 10                ' 115200 Baud @ 20MHz, -1.36%
    DEFINE HSER_CLROERR 1             ' Hser clear overflow automatically
    Hi Again, ad thanks again for your reply;

    Hi allready used the Pic Multicalc exactly like you post here, however at 38400 and 57600, don´t work Maybe my serial port don't accept that % of error.

    I don' thave a 20Mhz oscilator, i'm going to buy one and then i will make more test.

    Thanks to all.

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