|
楼主 |
发表于 2015-9-25 11:15:00
|
显示全部楼层
波特率设置,实现方法巧妙:
/*--------------------------- CAN_set_baudrate ------------------------------
* Setup the requested baudrate
*
* Parameter: ctrl: Index of the hardware CAN controller (1 .. x)
* baudrate: Baudrate
*
* Return: CAN_ERROR: Error code
*---------------------------------------------------------------------------*/
static CAN_ERROR CAN_hw_set_baudrate (U32 ctrl, U32 baudrate) {
U32 brp;
/* Note: this calculations fit for PCLK1 = 36MHz */
/* Determine which nominal time to use for requested baudrate and set
appropriate bit timing */
if (baudrate <= 500000) {
brp = (CAN_CLK / 18) / baudrate;----------->这里较巧妙,18正好对应18个TQ,所以只需设置参数brp即可
/* Load the baudrate registers BTR */
/* so that sample point is at about 72% bit time from bit start */
/* TSEG1 = 12, TSEG2 = 5, SJW = 4 => 1 CAN bit = 18 TQ, sample at 72% */
CAN_set_timing(ctrl, 12, 5, 4, brp);
} else if (baudrate <= 1000000) {
brp = (CAN_CLK / 9) / baudrate; ----------->这里较巧妙,9正好对应9个TQ,所以只需设置参数brp即可
/* Load the baudrate registers BTR */
/* so that sample point is at about 72% bit time from bit start */
/* TSEG1 = 5, TSEG2 = 3, SJW = 3 => 1 CAN bit = 9 TQ, sample at 66% */
CAN_set_timing(ctrl, 5, 3, 3, brp);
} else {
return CAN_BAUDRATE_ERROR;
}
return CAN_OK;
} |
|