作者:左莹薇_834 | 来源:互联网 | 2023-05-18 03:41
IamtryingtoorganisemyUARTlibraryandprettifyitalittlebitbyaddingsome#definessoIc
I am trying to organise my UART library and prettify it a little bit by adding some #define s so I can customize it later without having to dig deeply into the code, but I can't seem to get the following bit of code working:
我正在试图组织我的UART库,并通过添加一些#define s来美化它,这样我就可以在以后定制它,而不必深入挖掘代码,但是我似乎无法得到下面的代码:
#define FOSC 8000000
#define BAUDRATE 9600
#define BRGVAL (FOSC/2)/(16*BAUDRATE)-1
void uart_init(){
U1BRG = BRGVAL;
}
After the calculation BRGVAL becomes 25.0416667, and because it is not an integer I get the following warning for it when I assign that into U1BRG:
在计算BRGVAL为25.0416667之后,由于它不是整数,当我将它赋给U1BRG时,我得到如下警告:
UART.c: In function 'uart_init':
UART。c:在函数“uart_init”:
UART.c:24: warning: integer overflow in expression
UART。c:24:警告:表达式中的整数溢出。
...and the code simply does not work on target hardware. (If I manually put in U1BRG = 25 it works like a charm though)
…而代码根本就不能在目标硬件上工作。(如果我手动输入U1BRG = 25,它就像一个咒语)
Is there any way to typecast that constant into an integer to make the compiler happy?
有没有什么方法可以将这个常量输入到一个整数中,使编译器高兴呢?
Many Thanks, Hamza.
许多谢谢,哈姆扎。
5 个解决方案