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

Dependencies:   mbed TextLCD

Committer:
jimeixuehua
Date:
Mon Mar 20 02:17:40 2017 +0000
Revision:
0:3cd0b6cacf45
Child:
1:cac0160da4f7
55

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 0:3cd0b6cacf45 4
jimeixuehua 0:3cd0b6cacf45 5 CAN_HandleTypeDef hcan1;
jimeixuehua 0:3cd0b6cacf45 6 static CanTxMsgTypeDef TxMessage;
jimeixuehua 0:3cd0b6cacf45 7 static CanRxMsgTypeDef RxMessage;
jimeixuehua 0:3cd0b6cacf45 8 /* CAN1 init function */
jimeixuehua 0:3cd0b6cacf45 9 static void MX_CAN1_Init(void)
jimeixuehua 0:3cd0b6cacf45 10 {
jimeixuehua 0:3cd0b6cacf45 11 CAN_FilterConfTypeDef CAN1_FilerConf;
jimeixuehua 0:3cd0b6cacf45 12 __HAL_RCC_GPIOA_CLK_ENABLE();
jimeixuehua 0:3cd0b6cacf45 13 hcan1.Instance = CAN1;
jimeixuehua 0:3cd0b6cacf45 14 hcan1.pTxMsg=&TxMessage; //发送消息
jimeixuehua 0:3cd0b6cacf45 15 hcan1.Init.Prescaler = 16;
jimeixuehua 0:3cd0b6cacf45 16 hcan1.Init.Mode = CAN_MODE_NORMAL;
jimeixuehua 0:3cd0b6cacf45 17 hcan1.Init.SJW = CAN_SJW_1TQ;
jimeixuehua 0:3cd0b6cacf45 18 hcan1.Init.BS1 = CAN_BS1_1TQ;
jimeixuehua 0:3cd0b6cacf45 19 hcan1.Init.BS2 = CAN_BS2_1TQ;
jimeixuehua 0:3cd0b6cacf45 20 hcan1.Init.TTCM = DISABLE;
jimeixuehua 0:3cd0b6cacf45 21 hcan1.Init.ABOM = DISABLE;
jimeixuehua 0:3cd0b6cacf45 22 hcan1.Init.AWUM = DISABLE;
jimeixuehua 0:3cd0b6cacf45 23 hcan1.Init.NART = DISABLE;
jimeixuehua 0:3cd0b6cacf45 24 hcan1.Init.RFLM = DISABLE;
jimeixuehua 0:3cd0b6cacf45 25 hcan1.Init.TXFP = DISABLE;
jimeixuehua 0:3cd0b6cacf45 26 if (HAL_CAN_Init(&hcan1) != HAL_OK)
jimeixuehua 0:3cd0b6cacf45 27 {
jimeixuehua 0:3cd0b6cacf45 28
jimeixuehua 0:3cd0b6cacf45 29 }
jimeixuehua 0:3cd0b6cacf45 30 CAN1_FilerConf.FilterIdHigh=0X0000; //32位ID
jimeixuehua 0:3cd0b6cacf45 31 CAN1_FilerConf.FilterIdLow=0X0000;
jimeixuehua 0:3cd0b6cacf45 32 CAN1_FilerConf.FilterMaskIdHigh=0X0000; //32位MASK
jimeixuehua 0:3cd0b6cacf45 33 CAN1_FilerConf.FilterMaskIdLow=0X0000;
jimeixuehua 0:3cd0b6cacf45 34 CAN1_FilerConf.FilterFIFOAssignment=CAN_FILTER_FIFO0;//过滤器0关联到FIFO0
jimeixuehua 0:3cd0b6cacf45 35 CAN1_FilerConf.FilterNumber=0; //过滤器0
jimeixuehua 0:3cd0b6cacf45 36 CAN1_FilerConf.FilterMode=CAN_FILTERMODE_IDMASK;
jimeixuehua 0:3cd0b6cacf45 37 CAN1_FilerConf.FilterScale=CAN_FILTERSCALE_32BIT;
jimeixuehua 0:3cd0b6cacf45 38 CAN1_FilerConf.FilterActivation=ENABLE; //激活滤波器0
jimeixuehua 0:3cd0b6cacf45 39 CAN1_FilerConf.BankNumber=14;
jimeixuehua 0:3cd0b6cacf45 40 HAL_CAN_ConfigFilter(&hcan1,&CAN1_FilerConf);
jimeixuehua 0:3cd0b6cacf45 41 }
jimeixuehua 0:3cd0b6cacf45 42 // 其他,失败;
jimeixuehua 0:3cd0b6cacf45 43 uint8_t CAN1_Send_Msg(uint8_t* msg,uint8_t len)
jimeixuehua 0:3cd0b6cacf45 44 {
jimeixuehua 0:3cd0b6cacf45 45 uint16_t i=0;
jimeixuehua 0:3cd0b6cacf45 46 hcan1.pTxMsg->StdId=0X12; //标准标识符
jimeixuehua 0:3cd0b6cacf45 47 hcan1.pTxMsg->ExtId=0x12; //扩展标识符(29位)
jimeixuehua 0:3cd0b6cacf45 48 hcan1.pTxMsg->IDE=CAN_ID_STD; //使用标准帧
jimeixuehua 0:3cd0b6cacf45 49 hcan1.pTxMsg->RTR=CAN_RTR_DATA; //数据帧
jimeixuehua 0:3cd0b6cacf45 50 hcan1.pTxMsg->DLC=len;
jimeixuehua 0:3cd0b6cacf45 51 for(i=0;i<len;i++)
jimeixuehua 0:3cd0b6cacf45 52 hcan1.pTxMsg->Data[i]=msg[i];
jimeixuehua 0:3cd0b6cacf45 53 if(HAL_CAN_Transmit(&hcan1,10)!=HAL_OK) return 1; //发送
jimeixuehua 0:3cd0b6cacf45 54 return 0;
jimeixuehua 0:3cd0b6cacf45 55 }
jimeixuehua 0:3cd0b6cacf45 56 int main() {
jimeixuehua 0:3cd0b6cacf45 57 uint8_t canbuf[8]={1,2,3,4,5,6,7,8};
jimeixuehua 0:3cd0b6cacf45 58 MX_CAN1_Init();
jimeixuehua 0:3cd0b6cacf45 59 while(1) {
jimeixuehua 0:3cd0b6cacf45 60 CAN1_Send_Msg(canbuf,8);//发送8个字节
jimeixuehua 0:3cd0b6cacf45 61 myled = 1; // LED is ON
jimeixuehua 0:3cd0b6cacf45 62 wait(0.2); // 200 ms
jimeixuehua 0:3cd0b6cacf45 63 myled = 0; // LED is OFF
jimeixuehua 0:3cd0b6cacf45 64 wait(1.0); // 1 sec
jimeixuehua 0:3cd0b6cacf45 65 }
jimeixuehua 0:3cd0b6cacf45 66 }