CAN通訊的速率,可以用示波器測(cè)試TX或RX,看最窄的波形,如果為16us,則為62500,如果為8us則是125000,我也搞不懂為什么STM8選內(nèi)部時(shí)鐘CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); 即16M
而CAN_Prescaler = 8;//8@125KHZ 16@62500HZ ,預(yù)分頻選8時(shí),是125KHZ,這是我量出的實(shí)際結(jié)果。
void CHECK_MAIL(void)//這個(gè)只是我調(diào)試中用于參考的,可以不檢查直接發(fā)送
{
uchar i;
i=CAN_TransmitStatus(CAN_TxStatus_MailBox0Ok);
PRINTF("maiL0=%d ",&i);
i=CAN_TransmitStatus(CAN_TxStatus_MailBox1Ok);
PRINTF("maiL1=%d ",&i);
i=CAN_TransmitStatus(CAN_TxStatus_MailBox2Ok);
PRINTF("maiL2=%d rn",&i);
}
extern __IO uint8_t _Data[8];
extern char b_can_rxon;
void main(void)
{
uint16_t tx_count=0;
uint16_t re_tx=0;
CAN_TxStatus_TypeDef status = CAN_TxStatus_Failed;
CAN_Id_TypeDef Tx_IDE ;//CAN_Id_Standard; //= CAN_Id_Extended
CAN_RTR_TypeDef Tx_RTR = CAN_RTR_Data;
uint8_t Tx_DLC = 0;
uint8_t Tx_Data[8] = {0};
uint32_t Tx_Id = 0;
CLK_Config();
UART_CONFIG();
// GPIO_Config();
// LCD_Config();
CAN_Config();
enableInterrupts();
PRINTF("hello world!~rn",NULL);
GPIO_Init(GPIOC, GPIO_PIN_1,GPIO_MODE_OUT_PP_HIGH_FAST);
while(1)
{
#if 1
//PRINTF("hello world!~rn",NULL);
//Delay(1000);
tx_count++;
if(b_can_rxon)
{
GPIO_WriteReverse(GPIOC,GPIO_PIN_1);
PRINTF("RX:%drn",&_Data[0]);
b_can_rxon=0;
}
#else
Delay(500);
GPIO_WriteReverse(GPIOC,GPIO_PIN_1);
Tx_Id = 0x12345678;//29bit
Tx_IDE = CAN_Id_Extended;//CAN_Id_Standard;
Tx_RTR = CAN_RTR_Data;
Tx_DLC = 2;
Tx_Data[0] = tx_count++;
Tx_Data[1] = ~Tx_Data[0];
CHECK_MAIL();
status = CAN_Transmit(Tx_Id,Tx_IDE,Tx_RTR,Tx_DLC,Tx_Data);
if(status>0x10){PRINTF(" error:%drn",&status);continue;}//CAN_TxStatus_Ok
while(CAN_TransmitStatus(status)!=CAN_TxStatus_Ok)
{if(++re_tx==0)break;
}
if(re_tx==0)
{
if(status==CAN_TxStatus_MailBox0Ok) CAN_ClearFlag(CAN_FLAG_RQCP0);
if(status==CAN_TxStatus_MailBox1Ok) CAN_ClearFlag(CAN_FLAG_RQCP1);
if(status==CAN_TxStatus_MailBox2Ok) CAN_ClearFlag(CAN_FLAG_RQCP2);
PRINTF(" 強(qiáng)制CLEAR mail=%drn",&status);//CAN_TxStatus_Ok
}
else
{
PRINTF("tx:success mail=%drn",&status);//CAN_TxStatus_Ok
}
#endif
}
}
void CLK_Config(void)
{
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
}
void CAN_Config(void)
{
CAN_InitStatus_TypeDef status = CAN_InitStatus_Failed;
CAN_FilterNumber_TypeDef CAN_FilterNumber;
FunctionalState CAN_FilterActivation;
CAN_FilterMode_TypeDef CAN_FilterMode;
CAN_FilterScale_TypeDef CAN_FilterScale;
uint8_t CAN_FilterID1;
uint8_t CAN_FilterID2;
uint8_t CAN_FilterID3;
uint8_t CAN_FilterID4;
uint8_t CAN_FilterIDMask1;
uint8_t CAN_FilterIDMask2;
uint8_t CAN_FilterIDMask3;
uint8_t CAN_FilterIDMask4;
CAN_MasterCtrl_TypeDef CAN_MasterCtrl;
CAN_Mode_TypeDef CAN_Mode;
CAN_SynJumpWidth_TypeDef CAN_SynJumpWidth;
CAN_BitSeg1_TypeDef CAN_BitSeg1;
CAN_BitSeg2_TypeDef CAN_BitSeg2;
uint8_t CAN_Prescaler;
CAN_DeInit();
CAN_MasterCtrl=CAN_MasterCtrl_AllDisabled;
CAN_Mode = CAN_Mode_Normal;
CAN_SynJumpWidth = CAN_SynJumpWidth_1TimeQuantum;
CAN_BitSeg1 = CAN_BitSeg1_8TimeQuantum;
CAN_BitSeg2 = CAN_BitSeg2_7TimeQuantum;
CAN_Prescaler = 8;//8@125KHZ 16@62500HZ
status = CAN_Init(CAN_MasterCtrl, CAN_Mode, CAN_SynJumpWidth, CAN_BitSeg1,
CAN_BitSeg2, CAN_Prescaler);
CAN_FilterNumber = CAN_FilterNumber_0;
CAN_FilterActivation = ENABLE;
CAN_FilterMode = CAN_FilterMode_IdMask;
CAN_FilterScale = CAN_FilterScale_32Bit;
CAN_FilterID1=0x91;
CAN_FilterID2=0x00;
CAN_FilterID3=0x00;
CAN_FilterID4=0x00;
CAN_FilterIDMask1=0xff;
CAN_FilterIDMask2=0;
CAN_FilterIDMask3=0;
CAN_FilterIDMask4=0;
CAN_FilterInit(CAN_FilterNumber, CAN_FilterActivation, CAN_FilterMode,
CAN_FilterScale,CAN_FilterID1, CAN_FilterID2, CAN_FilterID3,
CAN_FilterID4,CAN_FilterIDMask1, CAN_FilterIDMask2,
CAN_FilterIDMask3, CAN_FilterIDMask4);
CAN_ITConfig(CAN_IT_FMP, ENABLE);
}