STM32之CAN总线例程
生活随笔
收集整理的這篇文章主要介紹了
STM32之CAN总线例程
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include "stm32f10x.h"/* RCC時鐘配置 */
void RCC_config(void)
{ ErrorStatus HSEStartUpStatus;/* RCC寄存器設置為默認配置 */RCC_DeInit();/* 打開外部高速時鐘 */RCC_HSEConfig(RCC_HSE_ON);/* 等待外部高速時鐘穩定 */HSEStartUpStatus = RCC_WaitForHSEStartUp();if(HSEStartUpStatus == SUCCESS) { /* 設置HCLK = SYSCLK */RCC_HCLKConfig(RCC_SYSCLK_Div1);/* 設置PCLK2 = HCLK */RCC_PCLK2Config(RCC_HCLK_Div1);/* 設置PCLK1 = HCLK / 2 */RCC_PCLK1Config(RCC_HCLK_Div2);/* 設置FLASH代碼延時 */FLASH_SetLatency(FLASH_Latency_2);/* 使能預取址緩存 */FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);/* 設置PLL時鐘源為HSE倍頻9 72MHz */RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);/* 使能PLL */RCC_PLLCmd(ENABLE);/* 等待PLL穩定 */while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);/* 設置PLL為系統時鐘源 */RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);/* 等待系統時鐘源切換到PLL */while(RCC_GetSYSCLKSource() != 0x08);}
}/* GPIO配置 */
void GPIO_config(void)
{GPIO_InitTypeDef GPIO_InitStructure;/* 時鐘配置 */RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);/* RX */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;GPIO_Init(GPIOA, &GPIO_InitStructure);/* TX */GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_Init(GPIOA, &GPIO_InitStructure);
}/* 中斷嵌套控制器配置 */
void NVIC_config(void)
{NVIC_InitTypeDef NVIC_InitStructure;/* 選擇中斷分組2 */NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);/* 設置CAN接收中斷 */NVIC_InitStructure.NVIC_IRQChannel = USB_LP_CAN1_RX0_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);
}/* CAN總線配置 */
void CAN_config(void)
{CAN_InitTypeDef CAN_InitStructure;CAN_FilterInitTypeDef CAN_FilterInitStructure;RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE);/* CAN1默認參數 */CAN_DeInit(CAN1);/* 將結構體填入默認參數 */CAN_StructInit(&CAN_InitStructure);/* 關閉時間觸發模式 */CAN_InitStructure.CAN_TTCM = DISABLE;/* 關閉自動離線管理 */CAN_InitStructure.CAN_ABOM = ENABLE;/* 關閉自動喚醒 */CAN_InitStructure.CAN_AWUM = ENABLE;/* 自動重傳 */CAN_InitStructure.CAN_NART = DISABLE;/* 禁止FIFO溢出時覆蓋原報文 */CAN_InitStructure.CAN_RFLM = DISABLE;/* 關閉優先級取決于ID */CAN_InitStructure.CAN_TXFP = DISABLE;/* 正常模式 */CAN_InitStructure.CAN_Mode = CAN_Mode_Normal;/* 設置波特率 */CAN_InitStructure.CAN_SJW = CAN_SJW_2tq;CAN_InitStructure.CAN_BS1 = CAN_BS1_6tq;CAN_InitStructure.CAN_BS2 = CAN_BS2_3tq;CAN_InitStructure.CAN_Prescaler = 4;/* 初始化CAN總線 */CAN_Init(CAN1, &CAN_InitStructure);/* 過濾器配置 */CAN_FilterInitStructure.CAN_FilterNumber = 0;CAN_FilterInitStructure.CAN_FilterMode = CAN_FilterMode_IdMask;CAN_FilterInitStructure.CAN_FilterScale = CAN_FilterScale_32bit;CAN_FilterInitStructure.CAN_FilterIdHigh = 0x0020;CAN_FilterInitStructure.CAN_FilterIdLow = 0x0007;CAN_FilterInitStructure.CAN_FilterMaskIdHigh = 0x0020;CAN_FilterInitStructure.CAN_FilterMaskIdLow = 0x0000;CAN_FilterInitStructure.CAN_FilterFIFOAssignment = CAN_Filter_FIFO0;CAN_FilterInitStructure.CAN_FilterActivation = ENABLE;CAN_FilterInit(&CAN_FilterInitStructure);/* 接收掛起中斷 */CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}/* 毫秒延時函數 */
void delay_ms(uint16_t time)
{ uint16_t i = 0; while(time--){i = 12000;while(i--); }
}CanTxMsg TxMessage;/* 主函數 */
int main(void)
{uint8_t TransmitMailbox = 0;/* RCC時鐘配置 */RCC_config();/* GPIO配置 */GPIO_config(); /* CAN總線配置 */CAN_config();/* 中斷嵌套控制器配置 */NVIC_config();while(1){TxMessage.StdId = 1;TxMessage.ExtId = 0;TxMessage.IDE = CAN_ID_STD;TxMessage.RTR = CAN_RTR_DATA;TxMessage.DLC = 8;TxMessage.Data[0] = 0x11;TxMessage.Data[1] = 0x22;TxMessage.Data[2] = 0x33;TxMessage.Data[3] = 0x44;TxMessage.Data[4] = 0x55;TxMessage.Data[5] = 0x66;TxMessage.Data[6] = 0x77;TxMessage.Data[7] = 0x88;TransmitMailbox = CAN_Transmit(CAN1, &TxMessage);while((CAN_TransmitStatus(CAN1, TransmitMailbox) != CANTXOK));delay_ms(1000);}
}void USB_LP_CAN1_RX0_IRQHandler(void)
{CanRxMsg RxMessage;volatile CanRxMsg Message;CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);Message.StdId = RxMessage.StdId;Message.ExtId = RxMessage.ExtId;Message.IDE = RxMessage.IDE;Message.RTR = RxMessage.RTR;Message.DLC = RxMessage.DLC;Message.Data[0] = RxMessage.Data[0];Message.Data[1] = RxMessage.Data[1];Message.Data[2] = RxMessage.Data[2];Message.Data[3] = RxMessage.Data[3];Message.Data[4] = RxMessage.Data[4];Message.Data[5] = RxMessage.Data[5];Message.Data[6] = RxMessage.Data[6];Message.Data[7] = RxMessage.Data[7];
}
?
總結
以上是生活随笔為你收集整理的STM32之CAN总线例程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: STM32之FSMC-SRAM/NOR原
- 下一篇: DALI调光的计算方式