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

Dependencies:   mbed TextLCD

Revision:
0:3cd0b6cacf45
Child:
1:cac0160da4f7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Mar 20 02:17:40 2017 +0000
@@ -0,0 +1,66 @@
+#include "mbed.h"
+#include "stm32f1xx_hal.h"
+DigitalOut myled(LED1);
+
+CAN_HandleTypeDef hcan1;
+static CanTxMsgTypeDef        TxMessage;
+static CanRxMsgTypeDef        RxMessage;
+/* 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)
+  {
+    
+  }
+    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;                
+    for(i=0;i<len;i++)
+    hcan1.pTxMsg->Data[i]=msg[i];
+    if(HAL_CAN_Transmit(&hcan1,10)!=HAL_OK) return 1;     //发送
+    return 0;       
+}
+int main() {
+     uint8_t canbuf[8]={1,2,3,4,5,6,7,8};
+    MX_CAN1_Init();
+    while(1) {
+        CAN1_Send_Msg(canbuf,8);//发送8个字节 
+        myled = 1; // LED is ON
+        wait(0.2); // 200 ms
+        myled = 0; // LED is OFF
+        wait(1.0); // 1 sec
+    }
+}