新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計應(yīng)用 > 基于STM32的CAN總線通信程序

基于STM32的CAN總線通信程序

作者: 時間:2016-11-24 來源:網(wǎng)絡(luò) 收藏

void USB_HP_CAN_TX_IRQHandler (void)
{
// request completed mbx 0
//郵箱0請求完成 當發(fā)送完成后硬件對該位置1407
if (CAN->TSR & (1<<0)) //檢測是否置1來判斷發(fā)送已完成
{
// reset request complete mbx 0
//復(fù)位郵箱0請求,對該位寫1可以清0該位 407
CAN->TSR |= 1<<0;
// disable TME interrupt

CAN->IER &= ~(1<<0); //禁止發(fā)送郵箱空中斷

CAN_TxRdy = 1;
}
}

本文引用地址:http://butianyuan.cn/article/201611/320933.htm


void USB_LP_CAN_RX0_IRQHandler (void) {
// message pending ?
//接收到CAN報文,通過判斷報文數(shù)目判斷是否有報文
if (CAN->RF0R & (1<<0))
{
// read the message
//接收報文
CAN_rdMsg (&CAN_RxMsg);

CAN_RxRdy = 1; // set receive flag
}
}


////////////////////////CAN初始化////////////////////////////////////////////////////////////////////////
void can_Init (void)
{

CAN_setup (); // setup CAN interface
CAN_wrFilter (33, STANDARD_FORMAT); // Enable reception of messages,filter set


//CAN_testmode(CAN_BTR_SILM | CAN_BTR_LBKM); // Loopback, Silent Mode (self-test)

//軟件仿真使用環(huán)回模式
//CAN_testmode(CAN_BTR_SILM | CAN_BTR_LBKM);
//硬件仿真使用正常模式
CAN_testmode(); //正常模式
// leave init mode
//進入正常模式
CAN_start ();
// wait til mbx is empty
//等待CAN就緒
CAN_waitReady ();
}

以下是.H文件
#ifndef _CAN_H_
#define _CAN_H_

#define STANDARD_FORMAT 0
#define EXTENDED_FORMAT 1

#define DATA_FRAME 0
#define REMOTE_FRAME 1

#define CAN_ID_STD ((u32)0x00000000)
#define CAN_ID_EXT ((u32)0x00000004)


typedef struct {
unsigned int id; // 29 bit identifier
unsigned char data[8]; // Data field
unsigned char len; // Length of data field in bytes
unsigned char format; // 0 - STANDARD, 1- EXTENDED IDENTIFIER
unsigned char type; // 0 - DATA FRAME, 1 - REMOTE FRAME
} CAN_msg;


void CAN_setup (void);
//void CAN_init (void);
void CAN_start (void);
void CAN_waitReady (void);
void CAN_wrMsg (CAN_msg *msg);
void CAN_rdMsg (CAN_msg *msg);
void CAN_wrFilter (unsigned int id, unsigned char filter_type);
void can_Init (void);
void CAN_testmode (void);

extern CAN_msg CAN_TxMsg; // CAN messge for sending
extern CAN_msg CAN_RxMsg; // CAN message for receiving
extern u8 CAN_TxRdy; // CAN HW ready to transmit a message
extern u8 CAN_RxRdy; // CAN HW received a message

#endif // _CAN_H_

以下是主程序部分:
int main(void) //主函數(shù),int類型,無參數(shù)
{
u32 i=0;
Stm32_Clock_Init(9);// 調(diào)用系統(tǒng)時鐘初始化子程序
delay_init(72);// 調(diào)用延時初始化子程序
IOSET_init();// 調(diào)用io口初始化子程序
PWM_Init(9000,0);// 定時器4初始化,設(shè)定PWM頻率=72MHz/9000=8KH

can_Init (); // initialise CAN interface

// initialise message to send
for (i = 0; i < 8; i++) CAN_TxMsg.data[i] = 0;







while(1)
{
delay_us(160);


if (CAN_TxRdy)
{
if(++i==600)
{

CAN_TxMsg.id = 33;
CAN_TxMsg.len = 4;
CAN_TxMsg.format = STANDARD_FORMAT;//使用標準幀
CAN_TxMsg.type = DATA_FRAME;//數(shù)據(jù)幀


//i=0;
//下面是數(shù)據(jù)報文
CAN_TxMsg.data[0] = 1;
CAN_TxMsg.data[1] = 2;
CAN_TxMsg.data[2] =3;
CAN_TxMsg.data[3] =4;

//發(fā)送CAN報文
CAN_TxRdy = 0;
CAN_wrMsg (&CAN_TxMsg);
}
else if(i==1200)
{
i=0;
//下面是數(shù)據(jù)報文
CAN_TxMsg.data[0] = 0;
CAN_TxMsg.data[1] = 1;
CAN_TxMsg.data[2] = 1;
CAN_TxMsg.data[3] = 1;

//發(fā)送CAN報文
CAN_TxRdy = 0;
CAN_wrMsg (&CAN_TxMsg);
}

}

if (CAN_RxRdy)
{
CAN_RxRdy = 0;

if(CAN_RxMsg.data[0]==1)////PC8狀態(tài)
LED1=0;
else
LED1=1;

if(CAN_RxMsg.data[1]==2)////PC9狀態(tài)
LED2=0;
else
LED2=1;

if(CAN_RxMsg.data[2]==3)////PC10狀態(tài)
LED3=0;
else
LED3=1;

if(CAN_RxMsg.data[3]==4)////PC11狀態(tài)
LED4=0;
else
LED4=1;

}

}
}


上一頁 1 2 下一頁

關(guān)鍵詞: STM32CAN總線通信程

評論


技術(shù)專區(qū)

關(guān)閉