dong chunfeng
/
f103_can_demo_hal
这是一个调用hal ,来控制CAN的程序
Diff: main.cpp
- Revision:
- 1:cac0160da4f7
- Parent:
- 0:3cd0b6cacf45
- Child:
- 2:4472d83236bc
diff -r 3cd0b6cacf45 -r cac0160da4f7 main.cpp --- a/main.cpp Mon Mar 20 02:17:40 2017 +0000 +++ b/main.cpp Tue Mar 21 05:13:15 2017 +0000 @@ -1,63 +1,93 @@ #include "mbed.h" #include "stm32f1xx_hal.h" DigitalOut myled(LED1); - -CAN_HandleTypeDef hcan1; +SPI device(PB_15, PB_14, PB_13,NC); // mosi, miso, sclk, ssel +CAN_HandleTypeDef hcan; static CanTxMsgTypeDef TxMessage; -static CanRxMsgTypeDef RxMessage; +I2C i2c(PB_9, PB_8); /* CAN1 init function */ static void MX_CAN1_Init(void) { - CAN_FilterConfTypeDef CAN1_FilerConf; - __HAL_RCC_GPIOA_CLK_ENABLE(); - hcan1.Instance = CAN1; - hcan1.pTxMsg=&TxMessage; //发送消息 - hcan1.Init.Prescaler = 16; - hcan1.Init.Mode = CAN_MODE_NORMAL; - hcan1.Init.SJW = CAN_SJW_1TQ; - hcan1.Init.BS1 = CAN_BS1_1TQ; - hcan1.Init.BS2 = CAN_BS2_1TQ; - hcan1.Init.TTCM = DISABLE; - hcan1.Init.ABOM = DISABLE; - hcan1.Init.AWUM = DISABLE; - hcan1.Init.NART = DISABLE; - hcan1.Init.RFLM = DISABLE; - hcan1.Init.TXFP = DISABLE; - if (HAL_CAN_Init(&hcan1) != HAL_OK) + hcan.Instance = CAN1; + hcan.pTxMsg=&TxMessage; //发送消息 + hcan.Init.Prescaler = 6; + hcan.Init.Mode = CAN_MODE_LOOPBACK; + hcan.Init.SJW = CAN_SJW_1TQ; + hcan.Init.BS1 = CAN_BS1_8TQ; + hcan.Init.BS2 = CAN_BS2_3TQ; + hcan.Init.TTCM = DISABLE; + hcan.Init.ABOM = DISABLE; + hcan.Init.AWUM = DISABLE; + hcan.Init.NART = DISABLE; + hcan.Init.RFLM = DISABLE; + hcan.Init.TXFP = DISABLE; + if (HAL_CAN_Init(&hcan) != HAL_OK) { - + //Error_Handler(); } - CAN1_FilerConf.FilterIdHigh=0X0000; //32位ID - CAN1_FilerConf.FilterIdLow=0X0000; - CAN1_FilerConf.FilterMaskIdHigh=0X0000; //32位MASK - CAN1_FilerConf.FilterMaskIdLow=0X0000; - CAN1_FilerConf.FilterFIFOAssignment=CAN_FILTER_FIFO0;//过滤器0关联到FIFO0 - CAN1_FilerConf.FilterNumber=0; //过滤器0 - CAN1_FilerConf.FilterMode=CAN_FILTERMODE_IDMASK; - CAN1_FilerConf.FilterScale=CAN_FILTERSCALE_32BIT; - CAN1_FilerConf.FilterActivation=ENABLE; //激活滤波器0 - CAN1_FilerConf.BankNumber=14; - HAL_CAN_ConfigFilter(&hcan1,&CAN1_FilerConf); } // 其他,失败; uint8_t CAN1_Send_Msg(uint8_t* msg,uint8_t len) { uint16_t i=0; - hcan1.pTxMsg->StdId=0X12; //标准标识符 - hcan1.pTxMsg->ExtId=0x12; //扩展标识符(29位) - hcan1.pTxMsg->IDE=CAN_ID_STD; //使用标准帧 - hcan1.pTxMsg->RTR=CAN_RTR_DATA; //数据帧 - hcan1.pTxMsg->DLC=len; + hcan.pTxMsg->StdId=0X12; //标准标识符 + hcan.pTxMsg->ExtId=0X12; //扩展标识符(29位) + hcan.pTxMsg->IDE=CAN_ID_STD; //使用标准帧 + hcan.pTxMsg->RTR=CAN_RTR_DATA; //数据帧 + hcan.pTxMsg->DLC=len; for(i=0;i<len;i++) - hcan1.pTxMsg->Data[i]=msg[i]; - if(HAL_CAN_Transmit(&hcan1,10)!=HAL_OK) return 1; //发送 + hcan.pTxMsg->Data[i]=msg[i]; + if(HAL_CAN_Transmit(&hcan,10)!=HAL_OK) return 1; //发送 return 0; } + + + +void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan) +{ + + GPIO_InitTypeDef GPIO_InitStruct; + if(hcan->Instance==CAN1) + { + /* USER CODE BEGIN CAN1_MspInit 0 */ + + /* USER CODE END CAN1_MspInit 0 */ + /* Peripheral clock enable */ + __HAL_RCC_GPIOA_CLK_ENABLE(); + __HAL_RCC_CAN1_CLK_ENABLE(); + + /**CAN GPIO Configuration + PA11 ------> CAN_RX + PA12 ------> CAN_TX + */ + GPIO_InitStruct.Pin = GPIO_PIN_11; + GPIO_InitStruct.Mode = GPIO_MODE_INPUT; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = GPIO_PIN_12; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* USER CODE BEGIN CAN1_MspInit 1 */ + + /* USER CODE END CAN1_MspInit 1 */ + } + + +} + int main() { + int address = 0x62; + char data[2]; + uint8_t canbuf[8]={1,2,3,4,5,6,7,8}; MX_CAN1_Init(); while(1) { - CAN1_Send_Msg(canbuf,8);//发送8个字节 + CAN1_Send_Msg(canbuf,8);//发送8个字节 + i2c.read(address, data, 2); + device.write(0x55); myled = 1; // LED is ON wait(0.2); // 200 ms myled = 0; // LED is OFF