用预编译的方法实现430单片机计算波特率
时间:12-13 10:26 阅读:1731次
*温馨提示:点击图片可以放大观看高清大图
简介:本文介绍的是用预编译的方法实现430单片机计算波特率的程序设计。
//这是一段求430波特率的计算程序
//U_UxBR1,U_UxBR0,U_UxMCTL可以直接传送给相关寄存器
//你只要设置以下两项UART_CLK,UART_BAUD
//U_UxMCTL求取的思想:
//某位为0引起的(整数N分频)的累积误差,小于,为1引起的(整数N + 1分频)的累积误差,则取0,否则取1
//由于预编译系统对小数比较大小,处理不便,因此把小数部分*256,换为整数处理
//程序中的256到小数系统中就是1
//为了与程序中其它的其它变量不重名,本预编译中所以常量都以"U_"开头并大写.
//刘玉宏编写
#define UART_CLK 1048576
#define UART_BAUD 76800
#define U_UxBR1 (UART_CLK/UART_BAUD)/256 //波特率分频整数部分的高8位
#define U_UxBR0 (UART_CLK/UART_BAUD)%256 //波特率分频整数部分的低8位
//波特率分频小数部分*256
#define U_DECIMAL_FRACTION (((256*UART_CLK)/UART_BAUD) - 256*(UART_CLK/UART_BAUD))
#define U_ERROR_SUM0 0 //累计误差
//-------------------------------
#if (U_ERROR_SUM0 - U_DECIMAL_FRACTION) > 0 //求某位为0时的累积误差绝对值
#define U_ER_BIT_IS_0 (U_ERROR_SUM0 - U_DECIMAL_FRACTION)
#else
#define U_ER_BIT_IS_0 -(U_ERROR_SUM0 - U_DECIMAL_FRACTION)
#endif
#if (U_ERROR_SUM0 + 256 - U_DECIMAL_FRACTION) > 0 //求某位为1时的累积误差绝对值
#define U_ER_BIT_IS_1 (U_ERROR_SUM0 + 256 - U_DECIMAL_FRACTION)
#else
#define U_ER_BIT_IS_1 -(U_ERROR_SUM0 + 256 - U_DECIMAL_FRACTION)
#endif
#if U_ER_BIT_IS_0 <= U_ER_BIT_IS_1 //据累积误差小者,取0或1
#define U_C0 0
#define U_ERROR_SUM1 U_ERROR_SUM0 - U_DECIMAL_FRACTION
#else
#define U_C0 1
#define U_ERROR_SUM1 U_ERROR_SUM0 + 256 - U_DECIMAL_FRACTION
#endif
#undef U_ER_BIT_IS_0
#undef U_ER_BIT_IS_1
//-------------------------------
//-------------------------------
#if (U_ERROR_SUM1 - U_DECIMAL_FRACTION) > 0
#define U_ER_BIT_IS_0 (U_ERROR_SUM1 - U_DECIMAL_FRACTION)
#else
#define U_ER_BIT_IS_0 -(U_ERROR_SUM1 - U_DECIMAL_FRACTION)
#endif
#if (U_ERROR_SUM1 + 256 - U_DECIMAL_FRACTION) > 0
#define U_ER_BIT_IS_1 (U_ERROR_SUM1 + 256 - U_DECIMAL_FRACTION)
#else
#define U_ER_BIT_IS_1 -(U_ERROR_SUM1 + 256 - U_DECIMAL_FRACTION)
#endif
#if U_ER_BIT_IS_0 <= U_ER_BIT_IS_1
#define U_C1 0
#define U_ERROR_SUM2 U_ERROR_SUM1 - U_DECIMAL_FRACTION
#else
#define U_C1 2
#define U_ERROR_SUM2 U_ERROR_SUM1 + 256 - U_DECIMAL_FRACTION
#endif
#undef U_ER_BIT_IS_0
#undef U_ER_BIT_IS_1
//-------------------------------
//-------------------------------
#if (U_ERROR_SUM2 - U_DECIMAL_FRACTION) > 0
#define U_ER_BIT_IS_0 (U_ERROR_SUM2 - U_DECIMAL_FRACTION)
#else
#define U_ER_BIT_IS_0 -(U_ERROR_SUM2 - U_DECIMAL_FRACTION)
#endif
#if (U_ERROR_SUM2 + 256 - U_DECIMAL_FRACTION) > 0
#define U_ER_BIT_IS_1 (U_ERROR_SUM2 + 256 - U_DECIMAL_FRACTION)
#else
#define U_ER_BIT_IS_1 -(U_ERROR_SUM2 + 256 - U_DECIMAL_FRACTION)
#endif
#if U_ER_BIT_IS_0 <= U_ER_BIT_IS_1
#define U_C2 0
#define U_ERROR_SUM3 U_ERROR_SUM2 - U_DECIMAL_FRACTION
#else
#define U_C2 4
#define U_ERROR_SUM3 U_ERROR_SUM2 + 256 - U_DECIMAL_FRACTION
#endif
#undef U_ER_BIT_IS_0
#undef U_ER_BIT_IS_1
//-------------------------------
//-------------------------------
#if (U_ERROR_SUM3 - U_DECIMAL_FRACTION) > 0
#define U_ER_BIT_IS_0 (U_ERROR_SUM3 - U_DECIMAL_FRACTION)
#else
#define U_ER_BIT_IS_0 -(U_ERROR_SUM3 - U_DECIMAL_FRACTION)
#endif
#if (U_ERROR_SUM3 + 256 - U_DECIMAL_FRACTION) > 0
#define U_ER_BIT_IS_1 (U_ERROR_SUM3 + 256 - U_DECIMAL_FRACTION)
#else
#define U_ER_BIT_IS_1 -(U_ERROR_SUM3 + 256 - U_DECIMAL_FRACTION)
#endif
#if U_ER_BIT_IS_0 <= U_ER_BIT_IS_1
#define U_C3 0
#define U_ERROR_SUM4 U_ERROR_SUM3 - U_DECIMAL_FRACTION
#else
#define U_C3 8
#define U_ERROR_SUM4 U_ERROR_SUM3 + 256 - U_DECIMAL_FRACTION
#endif
#undef U_ER_BIT_IS_0
#undef U_ER_BIT_IS_1
//-------------------------------
//-------------------------------
#if (U_ERROR_SUM4 - U_DECIMAL_FRACTION) > 0
#define U_ER_BIT_IS_0 (U_ERROR_SUM4 - U_DECIMAL_FRACTION)
#else
#define U_ER_BIT_IS_0 -(U_ERROR_SUM4 - U_DECIMAL_FRACTION)
#endif
#if (U_ERROR_SUM4 + 256 - U_DECIMAL_FRACTION) > 0
#define U_ER_BIT_IS_1 (U_ERROR_SUM4 + 256 - U_DECIMAL_FRACTION)
#else
#define U_ER_BIT_IS_1 -(U_ERROR_SUM4 + 256 - U_DECIMAL_FRACTION)
#endif
#if U_ER_BIT_IS_0 <= U_ER_BIT_IS_1
#define U_C4 0
#define U_ERROR_SUM5 U_ERROR_SUM4 - U_DECIMAL_FRACTION
#else
#define U_C4 16
#define U_ERROR_SUM5 U_ERROR_SUM4 + 256 - U_DECIMAL_FRACTION
#endif
#undef U_ER_BIT_IS_0
#undef U_ER_BIT_IS_1
//-------------------------------
//-------------------------------
#if (U_ERROR_SUM5 - U_DECIMAL_FRACTION) > 0
#define U_ER_BIT_IS_0 (U_ERROR_SUM5 - U_DECIMAL_FRACTION)
#else
#define U_ER_BIT_IS_0 -(U_ERROR_SUM5 - U_DECIMAL_FRACTION)
#endif
#if (U_ERROR_SUM5 + 256 - U_DECIMAL_FRACTION) > 0
#define U_ER_BIT_IS_1 (U_ERROR_SUM5 + 256 - U_DECIMAL_FRACTION)
#else
#define U_ER_BIT_IS_1 -(U_ERROR_SUM5 + 256 - U_DECIMAL_FRACTION)
#endif
#if U_ER_BIT_IS_0 <= U_ER_BIT_IS_1
#define U_C5 0
#define U_ERROR_SUM6 U_ERROR_SUM5 - U_DECIMAL_FRACTION
#else
#define U_C5 32
#define U_ERROR_SUM6 U_ERROR_SUM5 + 256 - U_DECIMAL_FRACTION
#endif
#undef U_ER_BIT_IS_0
#undef U_ER_BIT_IS_1
//-------------------------------
//-------------------------------
#if (U_ERROR_SUM6 - U_DECIMAL_FRACTION) > 0
#define U_ER_BIT_IS_0 (U_ERROR_SUM6 - U_DECIMAL_FRACTION)
#else
#define U_ER_BIT_IS_0 -(U_ERROR_SUM6 - U_DECIMAL_FRACTION)
#endif
#if (U_ERROR_SUM6 + 256 - U_DECIMAL_FRACTION) > 0
#define U_ER_BIT_IS_1 (U_ERROR_SUM6 + 256 - U_DECIMAL_FRACTION)
#else
#define U_ER_BIT_IS_1 -(U_ERROR_SUM6 + 256 - U_DECIMAL_FRACTION)
#endif
#if U_ER_BIT_IS_0 <= U_ER_BIT_IS_1
#define U_C6 0
#define U_ERROR_SUM7 U_ERROR_SUM6 - U_DECIMAL_FRACTION
#else
#define U_C6 64
#define U_ERROR_SUM7 U_ERROR_SUM6 + 256 - U_DECIMAL_FRACTION
#endif
#undef U_ER_BIT_IS_0
#undef U_ER_BIT_IS_1
//-------------------------------
//-------------------------------
#if (U_ERROR_SUM7 - U_DECIMAL_FRACTION) > 0
#define U_ER_BIT_IS_0 (U_ERROR_SUM7 - U_DECIMAL_FRACTION)
#else
#define U_ER_BIT_IS_0 -(U_ERROR_SUM7 - U_DECIMAL_FRACTION)
#endif
#if (U_ERROR_SUM7 + 256 - U_DECIMAL_FRACTION) > 0
#define U_ER_BIT_IS_1 (U_ERROR_SUM7 + 256 - U_DECIMAL_FRACTION)
#else
#define U_ER_BIT_IS_1 -(U_ERROR_SUM7 + 256 - U_DECIMAL_FRACTION)
#endif
#if U_ER_BIT_IS_0 <= U_ER_BIT_IS_1
#define U_C7 0
#define U_ERROR_SUM8 U_ERROR_SUM7 - U_DECIMAL_FRACTION
#else
#define U_C7 128
#define U_ERROR_SUM8 U_ERROR_SUM7 + 256 - U_DECIMAL_FRACTION
#endif
#undef U_ER_BIT_IS_0
#undef U_ER_BIT_IS_1
//-------------------------------
#define U_UxMCTL U_C7 + U_C6 + U_C5 + U_C4 + U_C3 + U_C2 + U_C1 + U_C0
//-------------------------------
//本次预编译到此结束,你可以使用U_UxBR1;U_UxBR0;U_UxMCTL;给相关寄存器赋值.
//-------------------------------
unsigned int b1=U_UxBR1;
unsigned int b0=U_UxBR0;
unsigned int m= U_UxMCTL;
main()
{
while(1);
}