这是一个调用hal ,来控制CAN的程序

Dependencies:   mbed TextLCD

Committer:
jimeixuehua
Date:
Tue Mar 21 05:13:15 2017 +0000
Revision:
1:cac0160da4f7
Parent:
0:3cd0b6cacf45
Child:
2:4472d83236bc
hal can

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jimeixuehua 0:3cd0b6cacf45 1 #include "mbed.h"
jimeixuehua 0:3cd0b6cacf45 2 #include "stm32f1xx_hal.h"
jimeixuehua 0:3cd0b6cacf45 3 DigitalOut myled(LED1);
jimeixuehua 1:cac0160da4f7 4 SPI device(PB_15, PB_14, PB_13,NC); // mosi, miso, sclk, ssel
jimeixuehua 1:cac0160da4f7 5 CAN_HandleTypeDef hcan;
jimeixuehua 0:3cd0b6cacf45 6 static CanTxMsgTypeDef TxMessage;
jimeixuehua 1:cac0160da4f7 7 I2C i2c(PB_9, PB_8);
jimeixuehua 0:3cd0b6cacf45 8 /* CAN1 init function */
jimeixuehua 0:3cd0b6cacf45 9 static void MX_CAN1_Init(void)
jimeixuehua 0:3cd0b6cacf45 10 {
jimeixuehua 1:cac0160da4f7 11 hcan.Instance = CAN1;
jimeixuehua 1:cac0160da4f7 12 hcan.pTxMsg=&TxMessage; //发送消息
jimeixuehua 1:cac0160da4f7 13 hcan.Init.Prescaler = 6;
jimeixuehua 1:cac0160da4f7 14 hcan.Init.Mode = CAN_MODE_LOOPBACK;
jimeixuehua 1:cac0160da4f7 15 hcan.Init.SJW = CAN_SJW_1TQ;
jimeixuehua 1:cac0160da4f7 16 hcan.Init.BS1 = CAN_BS1_8TQ;
jimeixuehua 1:cac0160da4f7 17 hcan.Init.BS2 = CAN_BS2_3TQ;
jimeixuehua 1:cac0160da4f7 18 hcan.Init.TTCM = DISABLE;
jimeixuehua 1:cac0160da4f7 19 hcan.Init.ABOM = DISABLE;
jimeixuehua 1:cac0160da4f7 20 hcan.Init.AWUM = DISABLE;
jimeixuehua 1:cac0160da4f7 21 hcan.Init.NART = DISABLE;
jimeixuehua 1:cac0160da4f7 22 hcan.Init.RFLM = DISABLE;
jimeixuehua 1:cac0160da4f7 23 hcan.Init.TXFP = DISABLE;
jimeixuehua 1:cac0160da4f7 24 if (HAL_CAN_Init(&hcan) != HAL_OK)
jimeixuehua 0:3cd0b6cacf45 25 {
jimeixuehua 1:cac0160da4f7 26 //Error_Handler();
jimeixuehua 0:3cd0b6cacf45 27 }
jimeixuehua 0:3cd0b6cacf45 28 }
jimeixuehua 0:3cd0b6cacf45 29 // 其他,失败;
jimeixuehua 0:3cd0b6cacf45 30 uint8_t CAN1_Send_Msg(uint8_t* msg,uint8_t len)
jimeixuehua 0:3cd0b6cacf45 31 {
jimeixuehua 0:3cd0b6cacf45 32 uint16_t i=0;
jimeixuehua 1:cac0160da4f7 33 hcan.pTxMsg->StdId=0X12; //标准标识符
jimeixuehua 1:cac0160da4f7 34 hcan.pTxMsg->ExtId=0X12; //扩展标识符(29位)
jimeixuehua 1:cac0160da4f7 35 hcan.pTxMsg->IDE=CAN_ID_STD; //使用标准帧
jimeixuehua 1:cac0160da4f7 36 hcan.pTxMsg->RTR=CAN_RTR_DATA; //数据帧
jimeixuehua 1:cac0160da4f7 37 hcan.pTxMsg->DLC=len;
jimeixuehua 0:3cd0b6cacf45 38 for(i=0;i<len;i++)
jimeixuehua 1:cac0160da4f7 39 hcan.pTxMsg->Data[i]=msg[i];
jimeixuehua 1:cac0160da4f7 40 if(HAL_CAN_Transmit(&hcan,10)!=HAL_OK) return 1; //发送
jimeixuehua 0:3cd0b6cacf45 41 return 0;
jimeixuehua 0:3cd0b6cacf45 42 }
jimeixuehua 1:cac0160da4f7 43
jimeixuehua 1:cac0160da4f7 44
jimeixuehua 1:cac0160da4f7 45
jimeixuehua 1:cac0160da4f7 46 void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan)
jimeixuehua 1:cac0160da4f7 47 {
jimeixuehua 1:cac0160da4f7 48
jimeixuehua 1:cac0160da4f7 49 GPIO_InitTypeDef GPIO_InitStruct;
jimeixuehua 1:cac0160da4f7 50 if(hcan->Instance==CAN1)
jimeixuehua 1:cac0160da4f7 51 {
jimeixuehua 1:cac0160da4f7 52 /* USER CODE BEGIN CAN1_MspInit 0 */
jimeixuehua 1:cac0160da4f7 53
jimeixuehua 1:cac0160da4f7 54 /* USER CODE END CAN1_MspInit 0 */
jimeixuehua 1:cac0160da4f7 55 /* Peripheral clock enable */
jimeixuehua 1:cac0160da4f7 56 __HAL_RCC_GPIOA_CLK_ENABLE();
jimeixuehua 1:cac0160da4f7 57 __HAL_RCC_CAN1_CLK_ENABLE();
jimeixuehua 1:cac0160da4f7 58
jimeixuehua 1:cac0160da4f7 59 /**CAN GPIO Configuration
jimeixuehua 1:cac0160da4f7 60 PA11 ------> CAN_RX
jimeixuehua 1:cac0160da4f7 61 PA12 ------> CAN_TX
jimeixuehua 1:cac0160da4f7 62 */
jimeixuehua 1:cac0160da4f7 63 GPIO_InitStruct.Pin = GPIO_PIN_11;
jimeixuehua 1:cac0160da4f7 64 GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
jimeixuehua 1:cac0160da4f7 65 GPIO_InitStruct.Pull = GPIO_NOPULL;
jimeixuehua 1:cac0160da4f7 66 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
jimeixuehua 1:cac0160da4f7 67
jimeixuehua 1:cac0160da4f7 68 GPIO_InitStruct.Pin = GPIO_PIN_12;
jimeixuehua 1:cac0160da4f7 69 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
jimeixuehua 1:cac0160da4f7 70 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
jimeixuehua 1:cac0160da4f7 71 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
jimeixuehua 1:cac0160da4f7 72
jimeixuehua 1:cac0160da4f7 73 /* USER CODE BEGIN CAN1_MspInit 1 */
jimeixuehua 1:cac0160da4f7 74
jimeixuehua 1:cac0160da4f7 75 /* USER CODE END CAN1_MspInit 1 */
jimeixuehua 1:cac0160da4f7 76 }
jimeixuehua 1:cac0160da4f7 77
jimeixuehua 1:cac0160da4f7 78
jimeixuehua 1:cac0160da4f7 79 }
jimeixuehua 1:cac0160da4f7 80
jimeixuehua 0:3cd0b6cacf45 81 int main() {
jimeixuehua 1:cac0160da4f7 82 int address = 0x62;
jimeixuehua 1:cac0160da4f7 83 char data[2];
jimeixuehua 1:cac0160da4f7 84
jimeixuehua 0:3cd0b6cacf45 85 uint8_t canbuf[8]={1,2,3,4,5,6,7,8};
jimeixuehua 0:3cd0b6cacf45 86 MX_CAN1_Init();
jimeixuehua 0:3cd0b6cacf45 87 while(1) {
jimeixuehua 1:cac0160da4f7 88 CAN1_Send_Msg(canbuf,8);//发送8个字节
jimeixuehua 1:cac0160da4f7 89 i2c.read(address, data, 2);
jimeixuehua 1:cac0160da4f7 90 device.write(0x55);
jimeixuehua 0:3cd0b6cacf45 91 myled = 1; // LED is ON
jimeixuehua 0:3cd0b6cacf45 92 wait(0.2); // 200 ms
jimeixuehua 0:3cd0b6cacf45 93 myled = 0; // LED is OFF
jimeixuehua 0:3cd0b6cacf45 94 wait(1.0); // 1 sec
jimeixuehua 0:3cd0b6cacf45 95 }
jimeixuehua 0:3cd0b6cacf45 96 }