实现波特率设置函数:
要实现这个函数,需用注意几点:
1. 先是从1.1到1.9中取出一个FRest值来计算出DLest,然后在计算出FRest,看其是否在1.1都1.9之间,如果在表示计算出的FRest是有效的。
2. 取FRest是从1.5开始,这样做可以减少FRest的取值,如果取大了,我们减少;如果取小了,我们增加。从而减少了取值的个数,加快了计算速度。
3. 计算出来的结果并不一定是误差最小的,因为我们是从1.5开始,而不是去1.1~1.9中所有值进行计算,找出误差最小的值。
4. 设置值可能有很多种,我们这里只计算出了一种可以接受的值。
- typedef struct _UART_DCM_TBL
- {
- fp32 FR; // 小数值
- u8 Div; // 分频
- u8 Mul; // 乘数
- } UART_DCM_TBL;
- /********************************************************************************************************
- * FunctionName : UART_BaudSet()
- * Description : 波特率设置
- * EntryParameter : bps - 波特率
- * ReturnValue : 成功返回TRUE,否则FALSE
- ********************************************************************************************************/
- u8 UART_BaudSet(u8 num, u32 bps)
- {
- u32 fDiv; //
- u32 uartClock = SystemCoreClock / 4; // 外设时钟与内核时钟的比例
- fp32 fFRest = 1.5, tFRest = 1.5, tAbs, min;
- u8 i, k;
- u32 uDLest;
- u8 tmpStr[64];
- UART_DCM_TBL const tbl[] =
- {
- {1.000, 0, 1}, {1.067, 1, 15}, {1.071, 1, 14}, {1.077, 1, 13},
- {1.083, 1, 12}, {1.091, 1, 11}, {1.100, 1, 10}, {1.111, 1, 9},
- {1.125, 1, 8}, {1.133, 2, 15}, {1.143, 1, 7}, {1.154, 2, 13},
- {1.167, 1, 6}, {1.182, 2, 11}, {1.200, 1, 5}, {1.214, 3, 14},
- {1.222, 2, 9}, {1.231, 3, 13}, {1.250, 1, 4}, {1.267, 4, 15},
- {1.273, 3, 11}, {1.286, 2, 7}, {1.300, 3, 10}, {1.308, 4, 13},
- {1.333, 1, 3}, {1.357, 5, 14}, {1.364, 4, 11}, {1.375, 3, 8},
- {1.385, 5, 13}, {1.400, 2, 5}, {1.417, 5, 12}, {1.429, 3, 7},
- {1.444, 4, 9}, {1.455, 5, 11}, {1.462, 6, 13}, {1.467, 7, 15},
- {1.500, 1, 2}, {1.533, 8, 15}, {1.538, 7, 13}, {1.545, 6, 11},
- {1.556, 5, 9}, {1.571, 4, 7}, {1.583, 7, 12}, {1.600, 3, 5},
- {1.615, 8, 13}, {1.625, 5, 8}, {1.636, 7, 11}, {1.643, 9, 14},
- {1.667, 2, 3}, {1.692, 9, 13}, {1.700, 7, 10}, {1.714, 5, 7},
- {1.727, 8, 11}, {1.733, 11, 15}, {1.750, 3, 4}, {1.769, 10, 13},
- {1.778, 7, 9}, {1.786, 11, 14}, {1.800, 4, 5}, {1.818, 9, 11},
- {1.833, 5, 6}, {1.846, 11, 13}, {1.857, 6, 7}, {1.867, 13, 15},
- {1.875, 7, 8}, {1.889, 8, 9}, {1.900, 9, 10}, {1.909, 10, 11},
- {1.917, 11, 12}, {1.923, 12, 13}, {1.929, 13, 14}, {1.933, 14, 15},
- };
- if (uartClock % (16*bps) == 0) // PCLK / (16*bps)为整数
- {
- fDiv = uartClock / (16*bps); // baud rate
- LPC_UART[num]->DLM = fDiv / 256;
- LPC_UART[num]->DLL = fDiv % 256;
- LPC_UART[num]->FDR = 0x10;
- } // 关闭小数分频
- else
- {
- do
- {
- uDLest = (u32)(uartClock / (16*bps*fFRest));
- fFRest = (fp32)uartClock / (fp32)(16*bps*uDLest);
- if ((1.100
- {
- k = 0;
- min = tbl[0].FR;
- for (i=0; i
- {
- tAbs = fabs(tbl[i].FR-fFRest);
- if (min > tAbs)
- {
- k = i;
- min = tAbs;
- }
- }
- UART_Dlab(num, ENABLE);
- LPC_UART[num]->FDR = (tbl[k].Mul << 4) + tbl[k].Div; // 得出小数分频值
- LPC_UART[num]->DLM = uDLest / 256;
- LPC_UART[num]->DLL = uDLest % 256;
- UART_Dlab(num, DISABLE);
- sprintf((char *)(tmpStr), "Hello! Baud is %4d bps\n", bps);
- UARTSendStr(num, tmpStr);
- return TRUE;
- }
- else // 从1.1到1.9中选择其他tFRest的值
- {
- if (fFRest <= 1.1)
- {
- tFRest += 0.1;
- fFRest = tFRest;
- }
- else
- {
- tFRest -= 0.1;
- fFRest = tFRest;
- }
- }
- } while ((1.1<=fFRest) && (fFRest<=1.9));
- }
- return FALSE;
- }
上面的函数并不难,如果看不懂,请对照上面的算法看,应该很容易明白。。。。。。。。。。。
[
本帖最后由 zhaojun_xf 于 2012-8-6 07:13 编辑 ]