dong chunfeng
/
f103_can_demo_hal
这是一个调用hal ,来控制CAN的程序
Diff: main.cpp
- Revision:
- 2:4472d83236bc
- Parent:
- 1:cac0160da4f7
--- a/main.cpp Tue Mar 21 05:13:15 2017 +0000 +++ b/main.cpp Tue Mar 21 15:26:16 2017 +0800 @@ -1,5 +1,6 @@ #include "mbed.h" #include "stm32f1xx_hal.h" +#include "TextLCD.h" DigitalOut myled(LED1); SPI device(PB_15, PB_14, PB_13,NC); // mosi, miso, sclk, ssel CAN_HandleTypeDef hcan; @@ -9,7 +10,7 @@ static void MX_CAN1_Init(void) { hcan.Instance = CAN1; - hcan.pTxMsg=&TxMessage; //åéæ¶æ¯ + hcan.pTxMsg=&TxMessage; //åéæ¶æ? hcan.Init.Prescaler = 6; hcan.Init.Mode = CAN_MODE_LOOPBACK; hcan.Init.SJW = CAN_SJW_1TQ; @@ -25,19 +26,20 @@ { //Error_Handler(); } + } // å ¶ä»,失败; uint8_t CAN1_Send_Msg(uint8_t* msg,uint8_t len) { uint16_t i=0; - hcan.pTxMsg->StdId=0X12; //æ åæ è¯ç¬¦ - hcan.pTxMsg->ExtId=0X12; //æ©å±æ è¯ç¬¦(29ä½) - hcan.pTxMsg->IDE=CAN_ID_STD; //使ç¨æ å帧 - hcan.pTxMsg->RTR=CAN_RTR_DATA; //æ°æ®å¸§ + 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++) hcan.pTxMsg->Data[i]=msg[i]; - if(HAL_CAN_Transmit(&hcan,10)!=HAL_OK) return 1; //åé + if(HAL_CAN_Transmit(&hcan,10)!=HAL_OK) return 1; //åé? return 0; } @@ -84,8 +86,9 @@ 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