xtrn 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) //主函数,int类型,无参数
{
u32 i=0;
Stm32_Clock_Init(9);// 调用系统时钟初始化子程序
delay_init(72);// 调用延时初始化子程序
IOSET_init();// 调用io口初始化子程序
PWM_Init(9000,0);// 定时器4初始化,设定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;//数据帧
//i=0;
//下面是数据报文
CAN_TxMsg.data[0] = 1;
CAN_TxMsg.data[1] = 2;
CAN_TxMsg.data[2] =3;
CAN_TxMsg.data[3] =4;
//发送CAN报文
CAN_TxRdy = 0;
CAN_wrMsg (&CAN_TxMsg);
}
else if(i==1200)
{
i=0;
//下面是数据报文
CAN_TxMsg.data[0] = 0;
CAN_TxMsg.data[1] = 1;
CAN_TxMsg.data[2] = 1;
CAN_TxMsg.data[3] = 1;
//发送CAN报文
CAN_TxRdy = 0;
CAN_wrMsg (&CAN_TxMsg);
}
}
if (CAN_RxRdy)
{
CAN_RxRdy = 0;
if(CAN_RxMsg.data[0]==1)////PC8状态
LED1=0;
else
LED1=1;
if(CAN_RxMsg.data[1]==2)////PC9状态
LED2=0;
else
LED2=1;
if(CAN_RxMsg.data[2]==3)////PC10状态
LED3=0;
else
LED3=1;
if(CAN_RxMsg.data[3]==4)////PC11状态
LED4=0;
else
LED4=1;
}
}
}