⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 232tocan.c

📁 RS232/CAN转换器 硬件电路需自己设计(电路很简单)
💻 C
📖 第 1 页 / 共 4 页
字号:
	else
	{
		W_byte(WRITE);
	}

	W_byte((uchar)(address & 0xff));
	W_byte(Data);
		
	SPI_CS = 1;
		
	Delay(10);
	
	WP = 0;
}
//------------定时器0中断程序----------
void Server_T0(void) interrupt 1 
{
	//25ms中断一次
	TH0 = TIMER25ms_TH0;
	TL0 = TIMER25ms_TL0;
	
	//50ms定时
	if (++Timer50ms < 2)
	{
	    return;
	}
    
    Timer50ms = 0;
    //关闭串口通讯指示灯(通过定时时间可以改变闪烁频率)
	REC_LED = 1;
	SEND_LED = 1;
    
	if (Com_rec_timer > 0)	//接收一帧数据的最大延时时间
	{
		if (--Com_rec_timer == 0)
		{
		    Can_send_ready = 1;
		}
	}
	
	if (Wait_timer > 0)
	{
	    Wait_timer--;
	}
}
//CAN通讯中断
void Server_INT1(void) interrupt 2
{
    if (Com_send_ready)
    {
        //释放接收缓冲区
        PBYTE[1] = RRB_CMD;
        return;
    }
    CANInterrupt = PBYTE[3];
    
    //错误警告中断
    if (_testbit_(EI_Bit))
    {
        //重新配置SJA1000
        Can_err = 1;
    }
    //数据超载中断
    if (_testbit_(DOI_Bit))
    {
        //清除数据
        PBYTE[1] = CDO_CMD;
        //数据清除不成功
        if ((PBYTE[2] & 0x02) != 0)
        {
           //重新配置SJA1000
           Can_err = 1;
        }
    }
    //接收中断
    if (_testbit_(RI_Bit))
    {
        REC_LED = 0;
        CanRec_prg();
    }
}
//-------------通讯中断程序------------
void Server_UART(void) interrupt 4 
{
	uchar t_c;
	
	if (_testbit_(RI))
	{
		t_c = SBUF;
		
		//接收缓冲区是否溢出
		if (Com_rxcount < MAX_LENGHT && !Can_send_ready)
		{
		    Com_rxbuf[Com_rxcount++] = t_c;
		    Com_rec_timer = Rec_delay;
		    //REC_LED = 0;
		}
	}	
	
	if (_testbit_(TI))	//发送数据包
	{
        //SEND_LED = 0;
        
		if (Com_send_now)
		{
			Com_txcount--;
			
			if (Com_txcount == 0)
			{
				Com_send_now = 0;
			}
			else
			{
				Telcom++;
				SBUF = *Telcom;
			}
		}
	}
}
//往EEROM中写入一个short int型数据
void Write_int(uint address, uint Data)
{
	uchar m;
	
	union 
	{
		uint crc;
		uchar t[2];
	}var;
	
	var.crc = Data;
	
	for (m=0; m<2; m++)
	{
		Write_byte((address+m), var.t[m]);
	}
}
//从EEROM中读出一个short int型数据
uint Read_int(uint address)
{
	union 
	{
		uint crc;
		uchar t[2];
	}var;
	
	var.t[0] = Read_byte(address);
	var.t[1] = Read_byte(address + 1);
	
	return var.crc;
}
//EEROM中单元中参数初始化
void Cs_init(void)
{
	ComBaud = 3;             //串口波特率
    Rec_delay = 1;           //串口接收延时
    CanBaud = 0;             //CAN波特率
    Can_mode = 1;            //CAN工作模式
    Acr_data = 0xffff;       //验收码
    Amr_data = 0xffff;       //屏蔽码
    
    Write_byte(0, ComBaud);
    Write_byte(1, Rec_delay);
    Write_byte(2, CanBaud);
    Write_byte(3, Can_mode);
    Write_int(4, Acr_data);
    Write_int(6, Amr_data);
}
//从EEROM单元中读数据到内存中
void Read_cs(void)
{
    ComBaud = Read_byte(0);         //串口波特率
    Rec_delay = Read_byte(1);       //串口接收延时
    CanBaud = Read_byte(2);         //CAN波特率
    Can_mode = Read_byte(3);        //CAN工作模式
    Acr_data = Read_int(4);         //验收码
    Amr_data = Read_int(6);         //屏蔽码
}
//读软件版本号
void Read_version(void)
{
	Ver_a=Read_byte(505);
	Ver_b=Read_byte(506);
	Ver_c=Read_byte(507);
}
//写软件版本号
void Write_version(void)
{
	Ver_a=1;          //第一次上电初始化版本号
	Ver_b=0;
	Ver_c=0;
		
	Write_byte(505,Ver_a);
	Write_byte(506,Ver_b);
	Write_byte(507,Ver_c);
}
//是否初始化存储器单元
void Clear_blank(void)
{
	uint t_a;
	uchar Ver_update;
	
	Read_version();    //读软件版本号
	
	if (Ver_a == 0 || Ver_a > 9 || Ver_b > 9 || Ver_c > 9) //兼容V1.00-V9.99版本
	{
		Ver_update = 1;
	}
	else
	{
		Ver_update = 0;
		
		if (Ver_a != 1 || Ver_b != 0 || Ver_c != 0)
		{
			Write_version();
		}
	}
	
	if (Ver_update > 0)
	{
		Ver_update = 0;
		
		for (t_a=0; t_a <= 0x1ff; t_a++)
		{
	        Write_byte(t_a,0);
		}
		
		Dog();
		//第一次上电初始化系统参数
        Cs_init();
        	
        Write_version();
	}
}
//5045开机初始化
void X5045_init(void)
{
	SCLK   = 1;
	SPI_SI = 0;
	SPI_SO = 1;
	
	SPI_CS = 1;
	SPI_CS = 0;
	SPI_CS = 1;
}
//串口发送一串字节
void Send_stream(uchar buf[])
{
    uchar t_a;
    uchar len;
    
    len = strlen(buf);
    
    for (t_a = 0; t_a < len; t_a++)
    {
	    SEND_LED = 0;
        SBUF = buf[t_a];
    
        while (!TI)
        {
            Dog();
        }
    
        TI = 0;
    }
}
//发送回车换行符
void Send_end(void)
{
    SBUF = ASCII_ENTER;

	SEND_LED = 0;
    while (!TI)
    {
        Dog();
    }
    
    TI = 0;
    
    SBUF = ASCII_LF;
    
    while (!TI)
    {
        Dog();
    }
    
    TI = 0;
}
//命令执行成功
void Send_ok(void)
{
    Send_stream("Succeed!");
    Send_end();
}
//转换十六进制数为ASCII值
void Hex_to_ascii(uchar *p)
{
    uchar t_c;
    
    t_c = *p;
    
    //0-9
    if (t_c < 10)
    {
        t_c += 48;
    }
    //A-F
    else
    {
        t_c += 55;
    }
    
    *p = t_c;
}
//命令解析
uchar Perform_cmd(void)
{   
    uchar t_a;
    uchar t_b;
    uint  crc;
    
    //去掉回车及换行符
    Recjs -= 2;
    
    if (Recjs != 0)
    {
        for (t_a = 0; t_a < Recjs; t_a++)
        {
            Command[t_a] = tolower(Command[t_a]);
        }
        //字符串结束标专
        Command[Recjs] = '\0';
        
        //串口波特率
        if (strncmp(Command,"com+",4) == 0)
        {
            if (strcmp(Command,"com+1200") == 0)
            {
                ComBaud = 0;
                Write_byte(0, ComBaud);
                Send_ok();
            }
            else if (strcmp(Command,"com+2400") == 0)
            {
                ComBaud = 1;
                Write_byte(0, ComBaud);
                Send_ok();
            }
            else if (strcmp(Command,"com+4800") == 0)
            {
                ComBaud = 2;
                Write_byte(0, ComBaud);
                Send_ok();
            }
            else if (strcmp(Command,"com+9600") == 0)
            {
                ComBaud = 3;
                Write_byte(0, ComBaud);
                Send_ok();
            }
            else if (strcmp(Command,"com+19200") == 0)
            {
                ComBaud = 4;
                Write_byte(0, ComBaud);
                Send_ok();
            }
            else if (strcmp(Command,"com+38400") == 0)
            {
                ComBaud = 5;
                Write_byte(0, ComBaud);
                Send_ok();
            }
            else if (strcmp(Command,"com+57600") == 0)
            {
                ComBaud = 6;
                Write_byte(0, ComBaud);
                Send_ok();
            }
            else if (strcmp(Command,"com+115200") == 0)
            {
                ComBaud = 7;
                Write_byte(0, ComBaud);
                Send_ok();
            }
            //命令出错
            else
            {
                Send_stream("Command error!");
                Send_end();
            }
        }
        //串口接收延时
        else if (strncmp(Command,"delay",5) == 0)
        {
            if (strcmp(Command,"delay50ms") == 0)
            {
                Rec_delay = 1;
                Write_byte(1, Rec_delay);
                Send_ok();
            }
            else if (strcmp(Command,"delay100ms") == 0)
            {
                Rec_delay = 2;
                Write_byte(1, Rec_delay);
                Send_ok();
            }
            else if (strcmp(Command,"delay200ms") == 0)
            {
                Rec_delay = 4;
                Write_byte(1, Rec_delay);
                Send_ok();
            }
            else if (strcmp(Command,"delay500ms") == 0)
            {
                Rec_delay = 10;
                Write_byte(1, Rec_delay);
                Send_ok();
            }
            else if (strcmp(Command,"delay1s") == 0)
            {
                Rec_delay = 20;
                Write_byte(1, Rec_delay);
                Send_ok();
            }
            else if (strcmp(Command,"delay2s") == 0)
            {
                Rec_delay = 40;
                Write_byte(1, Rec_delay);
                Send_ok();
            }
            else if (strcmp(Command,"delay5s") == 0)
            {
                Rec_delay = 100;
                Write_byte(1, Rec_delay);
                Send_ok();
            }
            else if (strcmp(Command,"delay10s") == 0)
            {
                Rec_delay = 200;
                Write_byte(1, Rec_delay);
                Send_ok();
            }
            //命令出错
            else
            {
                Send_stream("Command error!");
                Send_end();
            }
        }
        //can通讯速率
        else if (strncmp(Command,"can+",4) == 0)
        {
            if (strcmp(Command,"can+125k") == 0)
            {
                CanBaud = 0;
                Write_byte(2, CanBaud);
                Send_ok();
            }
            else if (strcmp(Command,"can+250k") == 0)
            {
                CanBaud = 1;
                Write_byte(2, CanBaud);
                Send_ok();

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -