STM32 CAN模块运用详解.要点介绍以STM32F103E系列芯片为根底介绍CAN 总线的运用方法。CAN 总线在操控范畴运用的十分广泛,现在大多数CPU芯片外围都扩展CAN接口。
1. 硬件根底
CAN总线工作需求两根数据线,RX和TX,即为输入总线和输出总线。一般CPU与外界通讯需求接一个驱动芯片(这点很像UART接口),常用的CAN芯片首要有:SN65VHD230、PCA82C250T等,本体系运用SN65VHD230作为CAN接口芯片。而CPU供给的CAN接口为CAN_L和CAN_H。
2. 软件设计
在进行软件设计时,咱们首要来看这样的一个结构体:
typedef struct
{
uint32_t StdId;
uint32_t ExtId;
uint8_t IDE;
uint8_t RTR;
uint8_t DLC;
uint8_t Data[8];
} CanTxMsg;
这是界说一个can数据包的结构体,即一个CAN数据包包括以上几个部分。
现在咱们考虑一个问题:因为can可衔接多个节点,假如一个体系为星形网络衔接方法,那么主机应该怎样差异这些从机发送的信息呢?
答案在上面那个结构体中,咱们能够给每个分机界说一个ID,那么主机在接纳到分机发送的数据后,经过ID号判别接纳到的信息是那个从机发送的。
留意: DLC界说发送数据的长度,其规模为:0~8。
下面咱们来编写CAN驱动:
对CAN模块初始化
void CAN_CfgInit(void)
{
CAN_InitTypeDef CAN_InitStructure;
CAN_FilterInitTypeDefCAN_FilterInitStructure;
CAN_PortInit();
CAN_DeInit(CAN1);
CAN_StructInit(&CAN_InitStructure);
CAN_InitStructure.CAN_TTCM=DISABLE;
CAN_InitStructure.CAN_ABOM=DISABLE;
CAN_InitStructure.CAN_AWUM=DISABLE;
CAN_InitStructure.CAN_NART=DISABLE;
CAN_InitStructure.CAN_RFLM=DISABLE;
CAN_InitStructure.CAN_TXFP=DISABLE;
CAN_InitStructure.CAN_Mode=CAN_Mode_LoopBack;
CAN_InitStructure.CAN_SJW=CAN_SJW_1tq;
CAN_InitStructure.CAN_BS1=CAN_BS1_8tq;
CAN_InitStructure.CAN_BS2=CAN_BS2_7tq;
CAN_InitStructure.CAN_Prescaler=5;
CAN_Init(CAN1, &CAN_InitStructure);
CAN_FilterInitStructure.CAN_FilterNumber=0;
CAN_FilterInitStructure.CAN_FilterMode=CAN_FilterMode_IdMask;
CAN_FilterInitStructure.CAN_FilterScale=CAN_FilterScale_32bit;
CAN_FilterInitStructure.CAN_FilterIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterIdLow=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdHigh=0x0000;
CAN_FilterInitStructure.CAN_FilterMaskIdLow=0x0000;
//CAN_FilterInitStructure.CAN_FilterFIFOAssignment=0;
CAN_FilterInitStructure.CAN_FilterFIFOAssignment=CAN_FIFO0;
CAN_FilterInitStructure.CAN_FilterActivation=ENABLE;
CAN_FilterInit(&CAN_FilterInitStructure);
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE);
}
这儿为can 界说一个专门的结构体,便于数据处理:
typedef struct CAN_Socket CAN;
struct CAN_Socket {
INT32U u32_StdId;
INT32U u32_ExtId;
INT8Uu8_IDE;
INT8Uu8_RTR;
INT8Uu8_DLC;
INT8Uu8_FMI;
INT8Uu8_data[8];
};
数据打包,而且发送
void CAN_TxSocket( CAN *can_Socket )
{
INT8U i = 0;
CanTxMsg TxMessage;
CAN_ITConfig(CAN1, CAN_IT_FMP0, ENABLE); // CAN FIFO0 message pending interrupt enable
// Transmit a message
TxMessage.StdId=can_Socket->u32_StdId;
TxMessage.ExtId=can_Socket->u32_ExtId;
TxMessage.IDE=can_Socket->u8_IDE;
TxMessage.RTR= can_Socket->u8_RTR;
TxMessage.DLC=can_Socket->u8_DLC;
for( i =0; i
TxMessage.Data = can_Socket->u8_data;
}
CAN_Transmit(CAN1, &TxMessage);
CAN_ITConfig(CAN1, CAN_IT_FMP0, DISABLE); // Disable interrupt handling
}
接纳函数放在中止程序中处理:
void USB_LP_CAN1_RX0_IRQHandler(void)
{
CanRxMsg RxMessage;
INT8U i = 0;
INT8U u8_RxLen = 0;
RxMessage.StdId = 0x00;
RxMessage.ExtId = 0x00;
RxMessage.IDE = 0;
RxMessage.RTR = 0;
RxMessage.DLC = 0;
RxMessage.FMI = 0;
for( i=0;i<8;i++){
RxMessage.Data=0x00;
}
CAN_Receive(CAN1, CAN_FIFO0, &RxMessage);
u8_RxLen = RxMessage.DLC;
if((RxMessage.ExtId==0x12) && (RxMessage.IDE==CAN_ID_EXT)){
for( i=0;i
CAN_ReceiveBuff = RxMessage.Data;
}
}
}
这儿只介绍CAN运用方法和首要驱动程序,测验程序可自行设计。
自己的测验程序:
~INT8U SendBuff1[]=”Hello”;
INT8U SendBuff2[]=”World”;
INT8U SendBuff3[]=”ADC= 255″;
void main(void)
{
System_HardwareInit();
while (1)
{
CAN_Tx(SendBuff1);
Disp_CanReceveData();
CAN_Tx(SendBuff2);
Disp_CanReceveData();
CAN_Tx(SendBuff3);
Disp_CanReceveData();
}
}
接纳数据,并在液晶上打印出来!
void Disp_CanReceveData( void )
{
INT8U u8_CanRecBuff[64];
if(CAN_ReceiveBuff[0] == H){
sprintf(( char *)u8_CanRecBuff,”CAN receve buff1 is : %s”,CAN_ReceiveBuff);
GCLDASC_PutChar16x16Str(10,30,u8_CanRecBuff, Red, Green, 1);
}
if(CAN_ReceiveBuff[0] == W){
sprintf(( char *)u8_CanRecBuff,”CAN receve buff2 is : %s”,CAN_ReceiveBuff);
GCLDASC_PutChar16x16Str(10,50,u8_CanRecBuff, Red, Green, 1);
}
if(CAN_ReceiveBuff[0] == A){
sprintf(( char *)u8_CanRecBuff,”CAN receve buff3 is : %s”,CAN_ReceiveBuff);
GCLDASC_PutChar16x16Str(10,70,u8_CanRecBuff, Red, Green, 1);
}
}
声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/news/guandian/260500.html