之前在網上找了一個STM32F103C8T6關於can通信的例子,直接燒錄,can不能使用,
使用示波器在VP230前后端查看沒有波形,
先介紹板子硬件資源:
HSE時鍾:8MHz;
MCU : STM32F103C8T6
CAN:一路;
在軟件的配置中找不到任何原因,使用USB-CAN分析儀不能識別波特率,懷疑是不是因為波特率設置不對,
是不是硬件設計出現錯誤,
查找手冊 《stm32f103c8t6》
can的發送接收引腳是在PA11 PA12,(注意:can和其他的復用)
我找的那個程序,我把原理找到看了一下,can的發送接收是在 PB8 PB9,懷疑是我的板子硬件出錯了,
我的板子硬件原理圖上是PA11 PA12,是按照手冊來設計的,
既然硬件沒有問題,應該是軟件問題,看看GPIO的配置:
/*CAN GPIO */ void CAN_GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; /* GPIOB */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE); /* CAN1 */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); /* Configure CAN pin: RX */ // PB8 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // GPIO_Init(GPIOB, &GPIO_InitStructure); /* Configure CAN pin: TX */ // PB9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); //#define GPIO_Remap_CAN GPIO_Remap1_CAN1 GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE); }
明白了,原來是把can的發送接收端口映射到了PB8、9上面了,
修改后的代碼:
void CAN_GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; /* GPIOA */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA, ENABLE); /* CAN1 */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_CAN1, ENABLE); /* Configure CAN pin: RX */ // PA11 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure CAN pin: TX */ // PA12 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); //#define GPIO_Remap_CAN GPIO_Remap1_CAN1 //GPIO_PinRemapConfig(GPIO_Remap1_CAN1, ENABLE); //取消端口映射 craigtao 2014-4-4 }
這回編譯,燒錄,示波器檢測,,哇,波形出來了,
耶穌愛你, craigtao 2014-4-4

