dong chunfeng
/
f103_can_demo_hal
这是一个调用hal ,来控制CAN的程序
main.cpp@3:bb8ad71d82cb, 2017-03-21 (annotated)
- Committer:
- jimeixuehua
- Date:
- Tue Mar 21 15:27:59 2017 +0800
- Revision:
- 3:bb8ad71d82cb
- Parent:
- 2:4472d83236bc
bac
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jimeixuehua | 0:3cd0b6cacf45 | 1 | #include "mbed.h" |
jimeixuehua | 0:3cd0b6cacf45 | 2 | #include "stm32f1xx_hal.h" |
jimeixuehua | 2:4472d83236bc | 3 | #include "TextLCD.h" |
jimeixuehua | 0:3cd0b6cacf45 | 4 | DigitalOut myled(LED1); |
jimeixuehua | 1:cac0160da4f7 | 5 | SPI device(PB_15, PB_14, PB_13,NC); // mosi, miso, sclk, ssel |
jimeixuehua | 1:cac0160da4f7 | 6 | CAN_HandleTypeDef hcan; |
jimeixuehua | 0:3cd0b6cacf45 | 7 | static CanTxMsgTypeDef TxMessage; |
jimeixuehua | 1:cac0160da4f7 | 8 | I2C i2c(PB_9, PB_8); |
jimeixuehua | 0:3cd0b6cacf45 | 9 | /* CAN1 init function */ |
jimeixuehua | 0:3cd0b6cacf45 | 10 | static void MX_CAN1_Init(void) |
jimeixuehua | 0:3cd0b6cacf45 | 11 | { |
jimeixuehua | 1:cac0160da4f7 | 12 | hcan.Instance = CAN1; |
jimeixuehua | 2:4472d83236bc | 13 | hcan.pTxMsg=&TxMessage; //åéæ¶æ? |
jimeixuehua | 1:cac0160da4f7 | 14 | hcan.Init.Prescaler = 6; |
jimeixuehua | 1:cac0160da4f7 | 15 | hcan.Init.Mode = CAN_MODE_LOOPBACK; |
jimeixuehua | 1:cac0160da4f7 | 16 | hcan.Init.SJW = CAN_SJW_1TQ; |
jimeixuehua | 1:cac0160da4f7 | 17 | hcan.Init.BS1 = CAN_BS1_8TQ; |
jimeixuehua | 1:cac0160da4f7 | 18 | hcan.Init.BS2 = CAN_BS2_3TQ; |
jimeixuehua | 1:cac0160da4f7 | 19 | hcan.Init.TTCM = DISABLE; |
jimeixuehua | 1:cac0160da4f7 | 20 | hcan.Init.ABOM = DISABLE; |
jimeixuehua | 1:cac0160da4f7 | 21 | hcan.Init.AWUM = DISABLE; |
jimeixuehua | 1:cac0160da4f7 | 22 | hcan.Init.NART = DISABLE; |
jimeixuehua | 1:cac0160da4f7 | 23 | hcan.Init.RFLM = DISABLE; |
jimeixuehua | 1:cac0160da4f7 | 24 | hcan.Init.TXFP = DISABLE; |
jimeixuehua | 1:cac0160da4f7 | 25 | if (HAL_CAN_Init(&hcan) != HAL_OK) |
jimeixuehua | 0:3cd0b6cacf45 | 26 | { |
jimeixuehua | 1:cac0160da4f7 | 27 | //Error_Handler(); |
jimeixuehua | 0:3cd0b6cacf45 | 28 | } |
jimeixuehua | 2:4472d83236bc | 29 | |
jimeixuehua | 0:3cd0b6cacf45 | 30 | } |
jimeixuehua | 0:3cd0b6cacf45 | 31 | // å ¶ä»,失败; |
jimeixuehua | 0:3cd0b6cacf45 | 32 | uint8_t CAN1_Send_Msg(uint8_t* msg,uint8_t len) |
jimeixuehua | 0:3cd0b6cacf45 | 33 | { |
jimeixuehua | 0:3cd0b6cacf45 | 34 | uint16_t i=0; |
jimeixuehua | 2:4472d83236bc | 35 | hcan.pTxMsg->StdId=0X12; //æ åæ è¯ç¬? |
jimeixuehua | 2:4472d83236bc | 36 | hcan.pTxMsg->ExtId=0X12; //æ©å±æ è¯ç¬?(29ä½?) |
jimeixuehua | 2:4472d83236bc | 37 | hcan.pTxMsg->IDE=CAN_ID_STD; //使ç¨æ åå¸? |
jimeixuehua | 2:4472d83236bc | 38 | hcan.pTxMsg->RTR=CAN_RTR_DATA; //æ°æ®å¸? |
jimeixuehua | 1:cac0160da4f7 | 39 | hcan.pTxMsg->DLC=len; |
jimeixuehua | 0:3cd0b6cacf45 | 40 | for(i=0;i<len;i++) |
jimeixuehua | 1:cac0160da4f7 | 41 | hcan.pTxMsg->Data[i]=msg[i]; |
jimeixuehua | 2:4472d83236bc | 42 | if(HAL_CAN_Transmit(&hcan,10)!=HAL_OK) return 1; //åé? |
jimeixuehua | 0:3cd0b6cacf45 | 43 | return 0; |
jimeixuehua | 0:3cd0b6cacf45 | 44 | } |
jimeixuehua | 1:cac0160da4f7 | 45 | |
jimeixuehua | 1:cac0160da4f7 | 46 | |
jimeixuehua | 1:cac0160da4f7 | 47 | |
jimeixuehua | 1:cac0160da4f7 | 48 | void HAL_CAN_MspInit(CAN_HandleTypeDef* hcan) |
jimeixuehua | 1:cac0160da4f7 | 49 | { |
jimeixuehua | 1:cac0160da4f7 | 50 | |
jimeixuehua | 1:cac0160da4f7 | 51 | GPIO_InitTypeDef GPIO_InitStruct; |
jimeixuehua | 1:cac0160da4f7 | 52 | if(hcan->Instance==CAN1) |
jimeixuehua | 1:cac0160da4f7 | 53 | { |
jimeixuehua | 1:cac0160da4f7 | 54 | /* USER CODE BEGIN CAN1_MspInit 0 */ |
jimeixuehua | 1:cac0160da4f7 | 55 | |
jimeixuehua | 1:cac0160da4f7 | 56 | /* USER CODE END CAN1_MspInit 0 */ |
jimeixuehua | 1:cac0160da4f7 | 57 | /* Peripheral clock enable */ |
jimeixuehua | 1:cac0160da4f7 | 58 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
jimeixuehua | 1:cac0160da4f7 | 59 | __HAL_RCC_CAN1_CLK_ENABLE(); |
jimeixuehua | 1:cac0160da4f7 | 60 | |
jimeixuehua | 1:cac0160da4f7 | 61 | /**CAN GPIO Configuration |
jimeixuehua | 1:cac0160da4f7 | 62 | PA11 ------> CAN_RX |
jimeixuehua | 1:cac0160da4f7 | 63 | PA12 ------> CAN_TX |
jimeixuehua | 1:cac0160da4f7 | 64 | */ |
jimeixuehua | 1:cac0160da4f7 | 65 | GPIO_InitStruct.Pin = GPIO_PIN_11; |
jimeixuehua | 1:cac0160da4f7 | 66 | GPIO_InitStruct.Mode = GPIO_MODE_INPUT; |
jimeixuehua | 1:cac0160da4f7 | 67 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
jimeixuehua | 1:cac0160da4f7 | 68 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
jimeixuehua | 1:cac0160da4f7 | 69 | |
jimeixuehua | 1:cac0160da4f7 | 70 | GPIO_InitStruct.Pin = GPIO_PIN_12; |
jimeixuehua | 1:cac0160da4f7 | 71 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
jimeixuehua | 1:cac0160da4f7 | 72 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
jimeixuehua | 1:cac0160da4f7 | 73 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
jimeixuehua | 1:cac0160da4f7 | 74 | |
jimeixuehua | 1:cac0160da4f7 | 75 | /* USER CODE BEGIN CAN1_MspInit 1 */ |
jimeixuehua | 1:cac0160da4f7 | 76 | |
jimeixuehua | 1:cac0160da4f7 | 77 | /* USER CODE END CAN1_MspInit 1 */ |
jimeixuehua | 1:cac0160da4f7 | 78 | } |
jimeixuehua | 1:cac0160da4f7 | 79 | |
jimeixuehua | 1:cac0160da4f7 | 80 | |
jimeixuehua | 1:cac0160da4f7 | 81 | } |
jimeixuehua | 1:cac0160da4f7 | 82 | |
jimeixuehua | 0:3cd0b6cacf45 | 83 | int main() { |
jimeixuehua | 1:cac0160da4f7 | 84 | int address = 0x62; |
jimeixuehua | 1:cac0160da4f7 | 85 | char data[2]; |
jimeixuehua | 1:cac0160da4f7 | 86 | |
jimeixuehua | 0:3cd0b6cacf45 | 87 | uint8_t canbuf[8]={1,2,3,4,5,6,7,8}; |
jimeixuehua | 0:3cd0b6cacf45 | 88 | MX_CAN1_Init(); |
jimeixuehua | 2:4472d83236bc | 89 | |
jimeixuehua | 0:3cd0b6cacf45 | 90 | while(1) { |
jimeixuehua | 2:4472d83236bc | 91 | CAN1_Send_Msg(canbuf,8);//åé?8个åè? |
jimeixuehua | 1:cac0160da4f7 | 92 | i2c.read(address, data, 2); |
jimeixuehua | 1:cac0160da4f7 | 93 | device.write(0x55); |
jimeixuehua | 0:3cd0b6cacf45 | 94 | myled = 1; // LED is ON |
jimeixuehua | 0:3cd0b6cacf45 | 95 | wait(0.2); // 200 ms |
jimeixuehua | 0:3cd0b6cacf45 | 96 | myled = 0; // LED is OFF |
jimeixuehua | 0:3cd0b6cacf45 | 97 | wait(1.0); // 1 sec |
jimeixuehua | 0:3cd0b6cacf45 | 98 | } |
jimeixuehua | 0:3cd0b6cacf45 | 99 | } |