Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

internal constants from a code

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
Alexander-Dan

Bascom Member



Joined: 13 Mar 2014
Posts: 70
Location: Bryansk - Russia

russia.gif
PostPosted: Sat Apr 24, 2021 9:55 am    Post subject: internal constants from a code Reply with quote

Hi!
is it possible to know the current uart communication speed by reading the internal constants from the code while the program is running? I would like to read the uart speed (BAUD) without using the registers of the MCU.

thanks

(BASCOM-AVR version : 2.0.8.3 )
Back to top
View user's profile Visit poster's website
SZTRAD

Bascom Member



Joined: 30 Dec 2019
Posts: 126

blank.gif
PostPosted: Sat Apr 24, 2021 10:29 am    Post subject: Reply with quote

Hi
since it will be placed in the translation what is stored in the appropriate registry so in my opinion not.
The easiest way is to either save it as a constant in the program or load the registry and recalculate it. You don't want to do that.
The compiler calculates the constant according to the frequency set value so loading and recalculating it is the easiest thing to do.
RS
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1944

blank.gif
PostPosted: Sat Apr 24, 2021 12:05 pm    Post subject: Re: internal constants from a code Reply with quote

Alexander-Dan wrote:
is it possible to know the current uart communication speed by reading the internal constants from the code while the program is running?

For the UART-speeds no internal constants are available, like for example _XTAL for CPU clock speed.

There are two ways.
Simple way: Create the constant yourself.
Code:
Const bdrate = 19200
$baud = bdrate

Hacker's approach: locate the position in controller's flash where UBRR is set, disassemble the commands and extract the constants written to UBRR.

Quote:
I would like to read the uart speed (BAUD) without using the registers of the MCU.

Why would one want that? It is in any case more important what baudrate the controller uses at runtime, in case one expects something going wrong. For this purpose one needs to know the actual register values in UBRR/UCSRA.
For a static check after compile-time a disassembly of the hex-code will do.
As far one needs to change UART speed, it is always a write-operation based on program logic.

Or don't you want to read UBRR because you do not know how to calculate the baudrate from it?
This is easy: BAUD = (XTAL / (UBRR + 1)) / ((2 - UCSRA.U2X) * 8)
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5272
Location: Holland

blank.gif
PostPosted: Sat Apr 24, 2021 12:15 pm    Post subject: Reply with quote

i would recommend to use CONFIG COMx to set the baud rate.
and you can simply use it again to reset the baud.
or use baud statement.

and no you can not know the current speed based on a constant.
the compiler use the setting from $crystal and the baud you specify to calculate the best value for ubrr or what ever it is named among the chips : there are a lot of different ways to do it. some store the high bits in a different register.

since the constants are given in the datasheet and there is a constant named _XTAL you can simpy calculate the baud by reading the proper registers.

_________________
Mark
Back to top
View user's profile Visit poster's website
Alexander-Dan

Bascom Member



Joined: 13 Mar 2014
Posts: 70
Location: Bryansk - Russia

russia.gif
PostPosted: Sat Apr 24, 2021 12:43 pm    Post subject: Reply with quote

thanks!
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum