News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

115200 at 32mhz

Started by TimB, Feb 01, 2021, 11:11 PM

Previous topic - Next topic

TimB


Hi all

I'm writing some code for a 16(L)F1823 that needs to talk to another perif at 115200 baud.

I think I can push the intrnl Osc to 32mhz on the device

My question is will the USART run at 115200 in 32mhz

Sorry if dumb question...


Tim

DaveS

Yes you may need to search the ppi file, just have looks good

USART + EUSART PLUGIN OUTPUT

;Calculated Baudrate = 115942 @ Xtal 32MHz, Error = 0.64%
RCSTA     = 144 ; Enable continuous receive
TXSTA     = 36  ; Enable transmit, BRGH = 1
SPBRG     = 68  ; Baud Rate Generator Low Byte Value
SPBRGH    = 0   ; Baud Rate Generator High Byte Value
BAUDCON.3 = 1   ; Enable the 16 bit Baud Rate Generator

Or

;Calculated Baudrate = 115942 @ Xtal 32MHz, Error = 0.64%
Hserial_RCSTA  = 144 ; Enable continuous receive
Hserial_TXSTA  = 36  ; Enable transmit, BRGH = 1
Hserial_Clear  = On  ; Clear overflow automatically
Hserial_SPBRG  = 68  ; Baud Rate Generator Low Byte Value
SPBRGH         = 0   ; Baud Rate Generator High Byte Value
BAUDCON.3      = 1   ; Enable the 16 bit Baud Rate Generator

OG


TimB

#3
Many thanks,

I turns out I have that plugin already on my IDE.

Tim

top204

#4
The compiler sets up the USART's Baud based upon the Declare HSerial_Baud value. So for a program such as:

    Device = 16F1823                            ' Choose the microcontroller
    Declare Xtal = 32                           ' Inform the compiler we are using a device operating at 32MHz
'
' Setup USART1
'   
    Declare Hserial1_Baud = 115200
    Declare HRSOut1_Pin = PORTC.0
    Declare HRSIn1_Pin = PORTC.1

    Dim MyByte As Byte
       
'---------------------------------------------------------------------------
' A simple loop to transmit the character received and to tell the compiler to setup USART1
'
Main:
    Do
        MyByte = HRSIn1
        HRSOut1 MyByte
    Loop

The compiler will produce the Asm code:
_compiler_main_start_
; UART1_ACTUALBAUD = 117647
; UART1_BAUDERROR = 2.12413
    movlb 0X03
    movlw 16
    movwf SPBRG
    clrf SPBRGH
    movlw 36
    movwf TXSTA
    movlw 144
    movwf RCSTA
    movlb 0

Internally, the compiler runs through the Baud calculations based upon an 8-bit and 16-bit Baud clock and sees which one has the less error percentage, and uses this value to setup the SFRs. This Asm code is run before the user's code so it sets up the peripherals for the user.

DaveS

Hi Les

There is a problem either with my software or the compiler.
Software checks for BAUDCON, 16-bit Baud is available for the device and does the calculation.

Regards
Dave

Screen Shot Usart Calc Plugin.jpg

top204

#6
It will be the compiler Dave.

I did the internal conversions quite a few years ago for the PIC24 Proton24 compiler and back then not all of the 8-bit devices had a 16-bit Baud clock, and some of them that did have the 16-bit Baud clock had known silicon problems with using it, but the PIC24 and dsPIC33 types did have the extra bits for the Baud clock from their start, so I used the functions from the Proton24 compiler and adapted them somewhat.

I'll go through the Proton8 compiler's USART setup functions and tighten the calculations for the newer devices. I had a sneaky feeling it was not as accurate on 8-bit devices as it should be, but other things crept up and I did not have a chance to get into them.

Thanks for pointing this out, and I will get into them this weekend and make the 16-bit Baud counter the default, then work down using the calculations in the datasheets.

DaveS

Thanks Les

I think my program looks for BAUDCON in the PPI file if 16-bit is available, probably need to check for other names used.
Good idea making the 16-bit Baud counter the default, keeps it simple.

Dave

krausediego

Why doesn't my ide accept the Declare HRSOut1_Pin command?

top204

What PIC microcontroller device are you using? and what compiler version?

Without some details within a question no answer will be valid, or even possible.

krausediego

pic 16f1823
compiler version 3.5.9.6

John Lawton


top204

What error message are you getting?

Compiler version 3.5.9.6 is from 2017!

Just to be on the safe side, I tried the code listing below and it worked as expected:

    Device = 16F1823                            ' Choose the microcontroller
    Declare Xtal = 32                           ' Inform the compiler we are using a device operating at 32MHz

    Declare Hserial1_Baud = 9600                ' Tell the compiler what Baud rate for the USART
    Declare HRSOut1_Pin = PORTC.4               ' Tell the compiler what pin to use for the USART TX
    Declare HRSIn1_Pin = PORTC.5                ' Tell the compiler what pin to use for the USART RX

With such an old compiler version, maybe the newer (about 3 years ago) declare texts are not valid. Try:

    Device = 16F1823                            ' Choose the microcontroller
    Declare Xtal = 32                           ' Inform the compiler we are using a device operating at 32MHz

    Declare Hserial_Baud = 9600                 ' Tell the compiler what Baud rate for the USART
    Declare HRSOut_Pin = PORTC.4                ' Tell the compiler what pin to use for the USART TX
    Declare HRSIn_Pin = PORTC.5                 ' Tell the compiler what pin to use for the USART RX

Using the latest compiler manual for commands and directives will not always work with old compiler versions because things get changed and added, so the examples in the manual may not be valid for old compilers.

John Lawton

Quote from: top204 on Oct 14, 2021, 04:21 PMUsing the latest compiler manual for commands and directives will not always work with old compiler versions because things get changed and added, so the examples in the manual may not be valid for old compilers.

If krausediego is using that old version then their manual will probably be pretty out of date too :)

krausediego

I found it strange because it did not correct the letters of the command's writing, nor did it change color, but it is compiling.

another question is, someone could create a simple code, communication with a bluetooth module HC-05 for example, I'm a beginner in the subject, so if someone can show me a brief path, it's better than starting from absolute zero.

I'm using a PIC 16F1823, thanks in advance.