CAN总线通信程序
//CAN <==> UART的协议转换器
//
//程序名称:CAN <---> UART协议转换程序<透明方式>
//
//作者:王猛
//创建:2007-10-09
//
//说明:
//1,单片机使用P89C61X2BA
//--晶振11.0592MHZ
//--CAN总线中断使用单片机的中断0,外部有上拉电阻,波特率可以设定
//2,CAN总线发送采用查询方式,接收采用中断方式
//3,看门狗复位时间1.2S
//4,SJA1000晶振8MHZ,Peil模式
//5,串口中断接收,查询发送,波特率可设置
//6,×××当串口收到数据后,每8个数一组打包,通过CAN总线发送出去
//
//-----10.16日,重新修改程序完成以下功能-----
//----此功能已经改为,每收到一帧数据,启动一次CAN传输,传输字节数等于串口收到的数据
//----串行帧的帧间界定通过当前波特率下传输5个字节为时间间隔,具体为当顺序接收到的任
//意两个数据,它们之间的时间间隔大于5个字节传送时间,认为这两个数据分属于两个不
//同的帧
//
//7,当CAN总线每接收一帧信息后,通过串口发送出去
//-----10.15日,改为可以识别CAN的报文字节长度,即串口只发送CAN报文长度个字节
//8,看门狗芯片MAX1232CPA,硬件溢出时间1.2S
//
//-------------------------------------------------------
#include
#include
#include
#include "CANCOM.h"
//unsigned char UART_TX_Data[8] = {0,1,2,3,4,5,6,7};
//unsigned char CAN_TX_Data[8] = {0,1,2,3,4,5,6,7};
unsigned char xdata UART_RX_Data[255];//串口接收到的串行帧
unsigned char xdata CAN_TX_Data[255];//待发送的数据缓冲区
unsigned char code ACR_ID[4] = {0,0,0,0};//CAN初始设置验收滤波值
unsigned char code AMR_ID[4] = {0xff,0xff,0xff,0xff};
unsigned char CAN_TX_ID[4] = {0,0,0,0};//待发送的目标的ID
unsigned char CAN_RX_ID[4] = {0,0,0,0};//接收到的信息来自何ID
unsigned char CAN_RX_Data[8] = {7,6,5,4,3,2,1,0};//接受到的数据缓冲
unsigned char code CAN_BTR0[10] = {0xdf,0xcf,0xc7,0xc3,0x43,0xc1,0xc1,0xc0,0xc0,0x80};
unsigned char code CAN_BTR1[10] = {0x7f,0x7f,0x7f,0x7f,0x2f,0x7f,0x4d,0x3e,0x3a,0x23};
//5K 10K 20K 40K 50K 80K 100K 200K 250K 500K
unsigned char code UART_BTR[4] = {0xe8,0xf4,0xfa};
// 1.2K,2.4K,4.8K
unsigned char CAN_flag;//CAN发送标志位
unsigned char UART_flag;//
unsigned char CAN_ERROR_flag = NOT;//
unsigned char CAN_DataLength = 8;//CAN信息的报文长度
unsigned char UART_DataLength = 0;//串口接收时的当前指示
unsigned char UART_Length = 0;//串口接收区的长度指示
//sbit AAA = P1^4;
void main(void)
{
EA = 0;
System_init();//系统初始化
Timer_init();//定时器初始化
Interrupt_init();//中断
UART_ini();
CAN_init();
Delay(1);
W_WDT();
EA = 1;
//Delay(1);
//UART_Length = 8;
//CAN_Transmit(0);
//UART_Transmit();
while (1)
{
W_WDT();
if (CAN_flag == YES)
{
CAN_flag = NOT;
CAN_Transmit(0);
LED1 = !LED1;
}
else
{
CAN_flag = NOT;
}
/*
if (UART_flag == YES)
{
UART_flag = NOT;
//Delay(50);
UART_Transmit();
//Clear_Buffer(CAN_RX_Data,8);
//LED3 = !LED3;
}
else
{
UART_flag = NOT;
}
*/
if ((CAN_ERROR_flag == YES))
{
CAN_ERROR_flag = NOT;
CAN_init();
}
else
{
CAN_ERROR_flag = NOT;
}
}
}
//---------------------------
//功能:系统设置
//--外部数据存储区访问使能
//--LED指示灯关(1=on,0=off)
//--流程控制标志置为无效NOT
//--清空串口,CAN的相关数据缓冲区
//---------------------------
void System_init(void)
{
CKCON = 0x00;//Fosc devide 12
AUXR = 0x00;//0x02;//EXM enable
LED1 = 0;//LED0-3 off指示灯,共阴接法,1时亮
LED2 = 0;
LED3 = 0;
LED4 = 0;
WDT = 1;//WDT ini
CAN_DataLength = 8;
UART_DataLength = 0;
UART_Length = 0;
CAN_flag = NOT;
CAN_ERROR_flag = NOT;
//UART_flag = NOT;
Clear_Buffer(UART_RX_Data,255);
Clear_Buffer(CAN_TX_Data,255);
Clear_Buffer(CAN_TX_ID,4);
Clear_Buffer(CAN_RX_ID,4);
Clear_Buffer(CAN_RX_Data,8);
/*
CAN_flag = YES;
UART_flag = YES;
*/
}
//-----------------------------
//
//软件延时(非精确)
//----内置清看门狗定时器子函数
//防止多次调用延时过长导致
//看门狗复位
//
//-----------------------------
void Delay(unsigned char time)
{
unsigned char i;
unsigned int j;
for (i = 0;i < time;i++)
{
W_WDT();
for (j=0;j<30000;j++)
{
}
}
}
CAN总线通信程 相关文章:
- 基于STM32的CAN总线通信程序(11-24)
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)