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

📄 com.c

📁 这是一称的项目原文件,包括T9汉字输入,打印机,24C512,已经引用到产品上
💻 C
字号:
/*******************************************************************************
*  标题:  蓝海微芯LJD-SY-5100单片机开发系统演示程序                            *
*  文件:  com.c                                                              *
*  日期:  2006-7-22                                                           *
*  版本:  1.0                                                                  *
*  作者:  Pillar                                                             *
********************************************************************************
*  描述:                                                                       *
*             通信程序                                                        *     
*                                                                              *
********************************************************************************
* 【版权】 Copyright(C) belongs to 天宇蓝翔科技有限公司     All Rights Reserved *          
* 【声明】 此程序仅用于学习与参考,引用请注明版权和作者信息!                  *
*******************************************************************************/
/********************************EEPROM存储分配********************************/
//第一个扇区

//要存储的内容:
//1.发射机工作频率:3位。1-8频率
//2.零点:2个字节     int
//3.分度值:1个字节  char
//4.量程:两个字节;一个字节数字,1个字节10幂次 int*10
//5.单位:1位,k,0=kg,1=t;
//6.斜率:两个字节   int
//7.传感器量程:两个字节;一个字节数字,1个字节10幂次 int*10

//第二个扇区

//1.出厂编号:两个字节
//2.出厂时间:两个字节。年,月,日。
//3.接收机编号:两个字节

//第三扇区

//1.用户名:20个字节 
/**************保存参数寄存器***********************************************/
#include<com.h>


//常数区
	//内部数据Flash第一扇区地址
	#define FLASH_ADDRH	0x20
	#define FLASH_ADDRL	0x00    //52

	//内部数据Flash第2扇区地址
	#define FLASH_ADDRH1	0x22
	#define FLASH_ADDRL1	0x00    //52

	//内部数据Flash第3扇区地址
	#define FLASH_ADDRH2	0x24
	#define FLASH_ADDRL2	0x00    //52
	
	

	

void LoadInt(int cs);
unsigned char Data_XRL(char *buf,int begin,int end);
void Hex_To_Ascii(buf);  //把校验数据转换成ASCII
void Ascii_To_Hex(buf);  //设备地址
void Make_Error(unsigned char ch); // 发射** 接收数据不正确要求重发
void Make_Two_Byte(int sj,char *jg);

/**********发射数据********/
void r_chengshang_data(unsigned char ch)

//	IsBeginCom=1;          //要求发射数据的标志
{
	unsigned char buf[4];
	ComHand=4;
	
	ComBuf[0]=(unsigned char)(Para1.chuchang_bianhao&0x00ff);	  //出厂编号低位,设备地址	
	ComBuf[1]=(unsigned char)(Para1.chuchang_bianhao>>8);//出厂编号高位,设备地址	

	ComBuf[2]=0x30;	  //标志读	
	ComBuf[3]=0x30;
 
	if(ch==1)//读重量数据 4
	{
		LoadInt(ch);
		ComBuf[8]=0x30;	  //数据直接数							
		ComBuf[9]=0x38;	  
	}
	
	else if(ch==2)//发射机工作频率	1
	{
		LoadInt(ch); //数据地址  4
		ComBuf[8]=0x30;	  //数据直接数							
		ComBuf[9]=0x38;	  
	}
	
	else if(ch==3)//零点 	2
	{
		LoadInt(ch); //数据地址
		ComBuf[8]=0x30;	  //数据直接数							
		ComBuf[9]=0x34;	  
	}
		
	else if(ch==4)//分度值	1
	{
		LoadInt(ch); //数据地址
		ComBuf[8]=0x30;	  //数据直接数							
		ComBuf[9]=0x32;	  
	}
	
	else if(ch==5)//量程	2
	{
		LoadInt(ch); //数据地址
		ComBuf[8]=0x30;	  //数据直接数							
		ComBuf[9]=0x34;	  
	}
	
	else if(ch==6)//单位	1		
	{
		LoadInt(ch); //数据地址
		ComBuf[8]=0x30;	  //数据直接数							
		ComBuf[9]=0x32;	  
	}
	
	else if(ch==7)//斜率	2
	{
		LoadInt(ch); //数据地址
		ComBuf[8]=0x30;	  //数据直接数							
		ComBuf[9]=0x34;	  
	}
	
	else if(ch==8)//传感器量程	2
	{
		LoadInt(ch); //数据地址
		ComBuf[8]=0x30;	  //数据直接数							
		ComBuf[9]=0x34;	  
	}

	else if(ch==9)//出厂编号	2
	{
		LoadInt(ch); //数据地址
		ComBuf[8]=0x30;	  //数据直接数							
		ComBuf[9]=0x34;	  
	}

	else if(ch==10)//出厂时间	2
	{
		LoadInt(ch); //数据地址
		ComBuf[8]=0x30;	  //数据直接数							
		ComBuf[9]=0x34;	  
	}

	else if(ch==11)//接收机编号	2
	{
		LoadInt(ch); //数据地址
		ComBuf[8]=0x30;	  //数据直接数							
		ComBuf[9]=0x34;	  
	}

	else if(ch==12)//用户名	20
	{
		LoadInt(ch); //数据地址
		ComBuf[8]=0x30;	  //数据直接数							
		ComBuf[9]=0x44;	  
	}

	else if(ch==13)//电池数据	4
	{
		LoadInt(ch); //数据地址
		ComBuf[8]=0x30;	  //数据直接数							
		ComBuf[9]=0x38;	  
	}

	buf[0]=Data_XRL(ComBuf,0,ComHand);
	Hex_To_Ascii(buf);                //把校验数据转换成ASCII
	ComBuf[ComHand]=buf[0];
	ComHand++;
	ComBuf[ComHand]=buf[1];
	ComHand++;
	ComBuf[ComHand]=0x0D;//发射数据尾

	SBUF=0x40;          //启动发射数据
}

/***********写数据到称上*****/

void w_data_to_chengshang(unsigned char ch)//ch为数据地址
{   
	unsigned char j;
	unsigned char buf[4];

	IsBeginCom=1;          //要求发射数据的标志
	
    ComHand=4;

	//ComBuf[1]=ComBuf[0]; //设备地址
	//ComBuf[2]=ComBuf[1];
	ComBuf[0]=Para1.chuchang_bianhao&0x000f;	  //出厂编号低位,设备地址	
	ComBuf[1]=(Para1.chuchang_bianhao>>8);//出厂编号高位,设备地址

	ComBuf[2]=0x30;	  //标志写	
	ComBuf[3]=0x31;
 

	 if(buf[0]==2) //发射机工作频率
		{						
	 		ComBuf[ComHand]=0x32; //字节数为2
			ComHand++; 
 		    LoadInt(Para.fasheji_pinlv);		
							
		}
	else if(buf[0]==3) //零点
		{
			ComBuf[ComHand]=0x34; //字节数4
			ComHand++;
			LoadInt(Para.lingdian);
							
		}
	else if(buf[0]==4) //分度值
		{
			ComBuf[ComHand]=0x32; //字节数2
			ComHand++; 
			LoadInt(Para.fenduzhi);
		}
	else if(buf[0]==5) //量程
		{
    		ComBuf[ComHand]=0x34; //字节数4
			ComHand++; 
			LoadInt(Para.liangcheng);
		}

	else if(buf[0]==6) //单位
    	{
	    	ComBuf[ComHand]=0x32; //字节数2
			ComHand++; 
			LoadInt(Para.dianwei);	
		}
		

	else if(buf[0]==7) //斜率
		{
			ComBuf[ComHand]=0x34; //字节数4
			ComHand++; 
			LoadInt(Para.xielv_data);
		}

	else if(buf[0]==8) //传感器量程
		{
			ComBuf[ComHand]=0x34; //字节数4
			ComHand++; 						
			LoadInt(Para.chuanganqi_liangcheng);
		}

	else if(buf[0]==9) //出厂编号
		{
			ComBuf[ComHand]=0x34; //字节数4
			ComHand++; 							
			LoadInt(Para1.chuchang_bianhao);
		}
	else if(buf[0]==10) //出厂时间
		{
			ComBuf[ComHand]=0x34; //字节数4
			ComHand++; 
			LoadInt(Para1.chuchang_time);
    	}
	else if(buf[0]==11) //接收机编号
		{
			ComBuf[ComHand]=0x34; //字节数4
			ComHand++;
   			LoadInt(Para1.jieshouji_bianhao);
		}
	
	else if(buf[0]==12) //用户名
		{
		ComBuf[ComHand]=0x4f; //字节数20
		for(j=0;j<10,j++;)
 			{
				LoadInt(Para2.yonghuming[j]);
			}
		}   


//CRC校验数据
		buf[0]=Data_XRL(ComBuf,1,ComHand);
		Hex_To_Ascii(buf);                //把校验数据转换成ASCII
		ComBuf[ComHand]=buf[0];
		ComHand++;
		ComBuf[ComHand]=buf[1];
		ComHand++;
		ComBuf[ComHand]=0x0D;
		ComHand++;
		IsBeginCom=1;          //要求发射数据的标志
    	//ComTime=5;

        SBUF=0x40;          //启动发射数据
}


/*************************串行中断程序**********************/

void ComInt() interrupt 4
{
	unsigned char ch,j;
	unsigned char buf[4];
	unsigned char str[2];
	long int i ;

	if(TI==1) //发中断
	{
		TI=0;
		ComJs++;
		if(ComJs<ComHand) //未发完
		{
			SBUF=ComBuf[ComJs];
		}
		else //已发完
		{
			IsBeginCom=0;          //要求发射数据的标志
			//QH=0;
		}
	}
	else //收中断
	{
		RI=0;
		ch=SBUF;
		if(!Is40H) //未收到数据头
		{
			if(ch==0x40) //为数据头
			{
				Is40H=1;
				ComHand=0;
			}
			return;
		}
		else //已收到数据头
		{
			if(ch!=0x0D) //不为数据尾
			{
				ComBuf[ComHand]=ch;
				ComHand++;
				if(ComHand>=23) //接收超个数
				{
					Is40H=0;
				}
				return;
			}
			else //收到数据尾
			{
				Is40H=0;

				buf[0]=ComBuf[0]; //ComBuf的0,1字节为设备地址
				buf[1]=ComBuf[1];

				Ascii_To_Hex(buf); //设备地址
				
				if(Para1.chuchang_bianhao!=buf[0]) //不为本机
				{
					return;
				}
				//为本机
                //QH=1;
				buf[0]=ComBuf[ComHand-2];
				buf[1]=ComBuf[ComHand-1];
				Ascii_To_Hex(buf); //取异或

				ch=Data_XRL(ComBuf,0,ComHand-2); //计算异或

				if(ch!=buf[0]) //异或错
				{
					Make_Error(0x2A); // 发射** 接收数据不正确要求重发
				}
				else //异或和正确  接收数据正确
				{
					
				   	buf[0]=ComBuf[6];
					buf[1]=ComBuf[7];
					Ascii_To_Hex(buf); //buf[0]中为数据地址

					ch=ComBuf[3]&0x01;

				if(ch==1) //写
					{
//						IsSave=1;
						str[0]=ComBuf[10];
						str[1]=ComBuf[11];
						Ascii_To_Hex(str); //高字节
						ch=str[0];

						str[0]=ComBuf[12];
						str[1]=ComBuf[13];
						Ascii_To_Hex(str); //低字节
						i=(long int)ch*256+(long int)str[0];
						
						if(buf[0]==1)   //称重数据
						{
						str[0]=ComBuf[14];
						str[1]=ComBuf[15];
						Ascii_To_Hex(str); //高字节
						ch=str[0];

						str[0]=ComBuf[16];
						str[1]=ComBuf[17];
						Ascii_To_Hex(str); //高字节
						
						zhongliang_data=(i<<16)+(long int)ch*256+(long int)str[0];		

						}

    					else if(buf[0]==2)         //发射机频率
						{
							Para.fasheji_pinlv=i;	
						}
						else if(buf[0]==3) //零点
						{
							Para.lingdian=i;
						}
						else if(buf[0]==4) //分度值
						{
							Para.fenduzhi=i;
						}							

						else if(buf[0]==5) //量程
						{
							Para.liangcheng=i;
						}

					/*	else if(buf[0]==6) //单位
						{
							Para.danwei=ch;
						}
						*/
						else if(buf[0]==7) //斜率
						{
							Para.xielv_data=i;
						}

						else if(buf[0]==8) //传感器量程
						{
							Para.chuanganqi_liangcheng=i;
						}

						else if(buf[0]==9) //出厂编号
						{
							Para1.chuchang_bianhao=i;
						}

						else if(buf[0]==10) //出厂时间
						{
							Para1.chuchang_time=i;
						}
						else if(buf[0]==11) //接收机编号
						{
							Para1.jieshouji_bianhao=i;
						}

						else if(buf[0]==12) //用户名
						{
							for (j=0;j<20;j++)
							{
							 str[0]=ComBuf[j+10];
							 str[1]=ComBuf[j+11];
							 Ascii_To_Hex(str); 
							 Para2.yonghuming[j]=str[0];
						
							 }
						}
					}
				else return;
					
				}
			}
		}
	}
}



//向通信缓冲装入int数据
void LoadInt(int cs)
{
	unsigned char buf[4];
	Make_Two_Byte(cs,buf);  //把一个数据转化成两个ASCII并装入
	ComBuf[ComHand]=buf[0];
	ComHand++;
	ComBuf[ComHand]=buf[1];
	ComHand++;
	ComBuf[ComHand]=buf[2];
	ComHand++;
	ComBuf[ComHand]=buf[3];
	ComHand++;
}
	
//初始化串口
void Init_Com()
{
/*  TR2=0;
	T2CON=0x30; //定时器2作为波特率发生器

	TH2=Com24M[Para.Com*2];
	RCAP2H=TH2;
	TL2=Com24M[Para.Com*2+1];
	RCAP2L=TL2; //波特率

	SCON=0x50; //串口工作方式1+
	PCON=PCON&0x7F; //波特率不加倍
*/

/*定义T1为串口发生器*/
	TMOD=0x20;                                    //确定定时器1工作方式为方式2

⌨️ 快捷键说明

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