Cotex-M3 LM9S92 CAN总线发送数据

芯片为LM3S9B92,使用PB4和PB5作为CAN0RX和CAN0TX。

下面是一个一直发送的例子:

//This is demo for lm3s9b92
//Created by Robin
//2012-02-05
//CAN发送数据例子
//使用PB4和PB5作为CAN0RX和CAN0TX

#include "hw_memmap.h"
#include "hw_types.h"
#include "hw_can.h"
#include "hw_ints.h"
#include "can.h"
#include "interrupt.h"
#include "sysctl.h"
#include "gpio.h"


tCANBitClkParms CANBitClk;
tCANMsgObject sMsgObjectRx;
tCANMsgObject sMsgObjectTx;
unsigned char ucBufferIn[8];
unsigned char ucBufferOut[8]={0,2,3,4,5,6,7,8};


//延时函数
void delay()
{
	unsigned long ulLoop;
	for(ulLoop = 0; ulLoop < 4000000; ulLoop++)
	{
	}
}

//发送
void send()
{
	sMsgObjectTx.ulMsgID = 0x7ff;
	sMsgObjectTx.ulFlags = 0;
	sMsgObjectTx.ulMsgLen = 1;
	sMsgObjectTx.pucMsgData = ucBufferOut;
	//发送
	CANMessageSet(CAN0_BASE, 1, &sMsgObjectTx, MSG_OBJ_TYPE_TX);
}

int main()
{
	//系统初始化
	SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN | SYSCTL_XTAL_16MHZ);

	//使能端口B
	SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

	//PB4和PB5用作CAN
	GPIOPinConfigure(GPIO_PB4_CAN0RX);
	GPIOPinConfigure(GPIO_PB5_CAN0TX);

	//PB4 PB5作为CAN引脚
	GPIOPinTypeCAN(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_5);

	//使能外设CAN0
	SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);

	//初始化CAN0
	CANInit(CAN0_BASE);
	//设置通信速率125kbps
	CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 125000);
	//使能CAN0
	CANEnable(CAN0_BASE);

	for(;;){
        send();
        delay();
		++ucBufferOut[0];
	}
}



引脚配置的这两个很重要,但是很多地方的源代码都没提到。。。。
GPIOPinConfigure(GPIO_PB4_CAN0RX);
GPIOPinConfigure(GPIO_PB5_CAN0TX);



你可能感兴趣的:(Cotex-M3 LM9S92 CAN总线发送数据)