自动初始化CAN波特率void CAN_AutoCfg_BaudParam(CAN_Peli_InitTypeDef* pInitStruct, u32 SrcClk, u32 baud)
{
u32 i, value = baud, record = 1;
u32 remain = 0, sumPrescaler = 0;
while ((baud == 0) || (SrcClk == 0))
;
sumPrescaler = SrcClk / baud;
sumPrescaler = sumPrescaler / 2;
for (i = 25; i > 3; i--) {
remain = sumPrescaler - ((sumPrescaler / i) * i);
if (remain == 0) {
record = i;
break;
}
else {
if (remain < value) {
value = remain;
record = i;
}
}
}
pInitStruct->SJW = 0;
pInitStruct->BRP = (sumPrescaler / record) - 1;
pInitStruct->TESG2 = (record - 3) / 3;
pInitStruct->TESG1 = (record - 3) - pInitStruct->TESG2;
}
|