串口驱动模块

/**********************************************************
*                   									  *
*				     串口驱动程序						  *
*														  *
**********************************************************/

#include <reg52.h>
#include <intrins.h>
#include <stdio.h>
#include "chuankou.h"
#include "main.h"
#include "delay.h"

bit Flag=1;
unsigned char i=0;
unsigned char ReData;
unsigned char rece_all[16],receive_ctl[2];

/**********************************************************
*函数:sendall()						        		  *
*功能:用串口发送收集到的所有数据		     			  *
*														  *
**********************************************************/
void sendall(void)
 {              // int k;
                 //湿度
                // send_byte('s');
	             send_byte((char)(0X30+RH_data/10));     //湿度 高8位  十位
                 send_byte((char)(0X30+RH_data%10));     //湿度 高8位  个位
                 send_byte((char)(0X30+RL_data/10));     //湿度 低8位  十分位
                // send_byte((char)(0X30+RL_data%10));     //湿度 低8位  百分位 
                 
				 //温度
				 //send_byte('w');
                 send_byte((char)(0X30+TH_data/10));     //温度 高8位  十位
                 send_byte((char)(0X30+TH_data%10));     //温度 高8位  个位
                 send_byte((char)(0X30+TL_data/10));     //温度 低8位  十分位
                // send_byte((char)(0X30+TL_data%10));     //温度 低8位  百分位
                 
				 //光照强度
				 //send_byte('g');
				 send_byte(guang[0]);     // 千
                 send_byte(guang[1]);     // 百
                 send_byte(guang[2]);     // 十
                 send_byte(guang[3]);     // 个
                 
				 //二氧化碳浓度
				 //send_byte('e');
                 send_byte(co2[0]);       // 千
                 send_byte(co2[1]);       // 百
                 send_byte(co2[2]);       // 十
                 send_byte(co2[3]);       // 个
                 

				 send_byte((char)(action[0]+0x30));
				 send_byte((char)(action[1]+0x30));
				 send_byte((char)(action[2]+0x30));
				 send_byte((char)(action[3]+0x30));
			    
               
					 Flag=1;
		 }
     


/**********************************************************
*函数:ini()						            		  *
*功能:定时器初始化		     		                	  *
*														  *
**********************************************************/
void ini()
{

        SCON = 0x50;      //REN=1允许串行接受状态,串口工作模式2     	   
	    TMOD|= 0x20;      //定时器工作方式2                       
		PCON|= 0x80;      //波特率提高一倍                                                       
	    TH1 = 0xF3;		  // 波特率4800、数据位8、停止位1。效验位无 (12M)
    	TL1 = 0xF3; 
		TR1  = 1;        //开启定时器1                                                      
		ES   = 1;        //开串口中断                  
		EA   = 1;        // 开总中断 
}




/**********************************************************
*函数:ser_int()						        		  *
*功能:串口中断程序		     		                	  *
*														  *
**********************************************************/
void ser_int (void) interrupt 4
{

	if(RI==1)
	{
		RI=0;
		ReData = SBUF;
	
			if(ReData!='\0')
			{
			rece_all[i]=ReData;
				i++;
				if(i>15)
				{
					i=0;
					Flag=1;
				}	

			}
			else
			{
			  i=0;
			 }
			  
	}
		
}


/**********************************************************
*函数:send_byte()						        		  *
*功能:发送一个字节		                    			  *
*														  *
**********************************************************/
void send_byte(unsigned char c)
{
 SBUF = c;
 while(!TI);
 TI = 0;
}


/**********************************************************
*函数:assign()		    				        		  *
*功能:根据通信协议处理从上位机接收的数据      			  *
*														  *
**********************************************************/


void assign(void)
{
         receive_ctl[0]=rece_all[0];
         receive_ctl[1]=rece_all[1];

	
		  	  //湿度整数部分
	         shi_s[0] = rece_all[2]; 
	         shi_s[1] = rece_all[3];
	         shi_s[2] =  0x2e; //小数点
	          //湿度小数部分
	         shi_s[3] = rece_all[4]; 
	       
	         //温度整数部分
	         wen_s[0] = rece_all[5]; 
	         wen_s[1] = rece_all[6];
	         wen_s[2] =  0x2e; //小数点
	         //温度小数部分
	         wen_s[3] = rece_all[7]; 
	       
			  //光
			 guang_s[0]=rece_all[8];   //千位
		     guang_s[1]=rece_all[9];  //百位
			 guang_s[2]=rece_all[10];  //十位
		     guang_s[3]=rece_all[11];  //个位
		
		 
		     //二氧化碳
			 co2_s[0]=rece_all[12];   //千位
		     co2_s[1]=rece_all[13];   //百位
			 co2_s[2]=rece_all[14];   //十位
		     co2_s[3]=rece_all[15];	  //个位
		
	
	
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值