Posted: Sat Apr 24, 2021 9:55 am Post subject: internal constants from a code
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.
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
Posted: Sat Apr 24, 2021 12:05 pm Post subject: Re: internal constants from a code
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)
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
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