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

📄 main.c

📁 在S3C2410上实现CAN总线的协议及驱动
💻 C
字号:

#include "2410addr.h"
#include "2410lib.h"
#include "spi.h"
#include "can.h"

#include <stdarg.h>
#include <stdio.h>
#include <string.h>

void write(char * data,int count);


void xmain(void)
{
	char option;
	char buf[60];

	ChangeClockDivider(1,1);            // 1:2:4
	ChangeMPllValue(0xa1,0x3,0x1);      // FCLK=202.8MHz
	Port_Init();
	Uart_Select(0);
	Uart_Init(0, 115200);
		        
	CAN_Init();
	pISR_EINT8_23=(unsigned)CAN_ISR;
	
	Uart_Printf("\n/***********************************************/\n");
	Uart_Printf("/*              ShenZhen WaterTek              */\n");
	Uart_Printf("/*                  CAN     test               */\n");
	Uart_Printf("/***********************************************/\n");
	Uart_Printf("工作模式:1、主站;2、从站\n");
	Uart_Printf("请选择本实验箱工作模式(通信的两台设备不要选择相同的工作模式):");
	
	option=Uart_Getch();
	Uart_SendByte(option);
	while(!((option=='1')||(option=='2'))){
		Uart_Printf("\n工作模式选择不正确,请重新输入!\n");
		Uart_Printf("请选择本实验箱工作模式(通信的两台设备不要选择相同的工作模式):");
		option=Uart_Getch();
		Uart_SendByte(option);
	}
	
	if(option=='2')
	{
	  	Uart_Printf("\n当前设备处于从站状态,");
		Uart_Printf("请等待,以下是主站发送过来的消息:\n");
		while(1);
	}
	if(option=='1')
	{
	  Uart_Printf("\n当前设备处于主站状态");
	  while(1){
		Uart_Printf("请输入你想发送的数据:");
		Uart_GetString(buf);
		write(buf,strlen(buf));
	  }
	}	
	
}

void write(char * data,int count)
{
    char str[8];
    int n=count/7;
    int m=count%7;
    int i,j;
    for(i=0;i<n;i++)
    {
    	strncpy(str,data,7);
    	str[7]='\0';
    	CAN_Write(str,sizeof(str));
    	for(j=0;j<7;j++)
    		data++;
    }
    if(m!=0){
	    strncpy(str,data,m);
	    str[m]='\0';
	    CAN_Write(str,sizeof(str));
    }
}

⌨️ 快捷键说明

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