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

📄 text1.c

📁 单片机通信程序
💻 C
字号:
#include <reg52.h> 
#include <absacc.h>
#include <stdio.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#include <lcd_1602_guang.h>
uint wendu,volt;
uint a[10];
uint wen_du;
sbit p12=P1^2;  //7135 run
sbit p15=P1^5;//加热控制管脚
unsigned char key;//键盘返回值
uchar dayin;
uchar c=0;
char lstemp;//放置临时设置的温度
//unsigned char *zf;//温度转字符
uchar TempBuffer[10];//暂时存放要显示的直
uchar bc[10];//保持要显示的值
uint baochi;//保持要显示的值
unsigned char table[7];//放置设定温度值的数组
unsigned char count,up,down,done;
char hide_t1,hide_t2;
int fout;//fout是决定是否加热,加热时间
sbit fs=P2^0;
/*****************定时器1**********************************/

void timer1()interrupt 3 using 2{
if(fout>0)fout--;//fout减到0为止
if(fout<0)fout++;
TH1=(65536-55000)/256;
TL1=(65536-55000)%256;

}

/******************************************************/
/*********************PID结构体***********************************/
typedef struct PID
{
uint setpoint;             //设定目标值
uint proportion;          //比例系数
uint integral;            //积分系数
uint derivative;          //微分系数
int lasterror;            // 上次偏差
int sumerror;             //历史误差计数
}PID;
 
/*********************PID结构体***********************************/
/*********************************PD算法********************************************/
uint PIDCalc(PID*pp,uint NextPoint)
{
int derror,error;
error=pp->setpoint*10-NextPoint;
derror=error-pp->lasterror; 
pp->lasterror=error;
return(pp->proportion*error+pp->derivative*derror);
}

PID stPID;   //定义一个STpid变量


/******************************************************/
void dl_1ms(void)
{unsigned char z;

for(z=124;z>0;z--); 

}
/******************************************************/
delay(void)
   {
  int d;
  for(d=20000;d>0;d--){}   //延时10ms
     }

/*****************  整数型转字符型方便打印*************************************/
	void IntToStr( uint t, unsigned char *sc, unsigned char n) 
{
	unsigned char a[5]; char i, j;                                 	
	a[0]=(t/10000)%10;         //取得整数值到数组         	
	a[1]=(t/1000)%10;                                     	
	a[2]=(t/100)%10;                                      	
	a[3]=(t/10)%10;                                       	
	a[4]=(t/1)%10;                                        	
                                                      
	for(i=0; i<5; i++)         //转成ASCII码              	
		a[i]=a[i]+'0';                                    	
	for(i=0; a[i]=='0' && i<=3; i++);                     	
	for(j=5-n; j<i; j++)       //填充空格                 	
		{ *sc=' ';  sc++; }                             	
	for(; i<5; i++)                                       	
		{ *sc=a[i]; sc++; }  //加入有效的数字           	
	*sc='\0'; 
} 


/*****************发射函数***************************/

void transmit(uint aa)
{
	uchar i;
	uint bite;
				fs=0;	    //起始位		26
				_nop_();
				
				_nop_();
				_nop_();
				_nop_();
				
				_nop_();
				_nop_();
				_nop_();
				
				_nop_();
				_nop_();
				_nop_();
				
				_nop_();
				_nop_();
				_nop_();
				
				_nop_();
				_nop_();
				_nop_();
				
				_nop_();
				_nop_();
				_nop_();
				
				_nop_();
				_nop_();
				_nop_();
								
				_nop_();
				_nop_();
				_nop_();

				fs=1;

				_nop_();
				_nop_();
				_nop_();
				
				_nop_();
				_nop_();
				_nop_();
				
	for(i=0;i<16;i++)
	{ 
		bite=aa&0x8000;						  //由高到低
		aa=aa<<1;
		if(bite==0)
		 	 {
				fs=0;		//0				 8-13
				_nop_();
				_nop_();
				_nop_();				
				_nop_();		 
				fs=1;
			}
          else 	
		  	{
				fs=0;		//1				 18-23
				_nop_();
				_nop_();
				_nop_();
				
				_nop_();
				_nop_();
				_nop_();
				
				_nop_();
				_nop_();
				_nop_();
				
				_nop_();
				_nop_();
				_nop_();
				
				_nop_();
				_nop_();			   
				fs=1;
			}
	}
	for(i=0;i<50;i++);	
}


/******************************************************/




void int0() interrupt 0 using 1
{  

	volt=TH0*256+TL0;
	a[c]=volt/2-10000+115;//单片机周期1us ,ad7135周期2us,后面加的115是模数转换时的偏差
  
   c++;
   TL0=0;
	TH0=0;
	if(c==6)
	{p12=0;
	}
}
/******************显示当前温度********************************************/
void diswendu()
{
    IntToStr(baochi,&TempBuffer[0],5);
	display_string(0,0,&TempBuffer[0]);
    IntToStr(wendu,&bc[0],5);
	display_char(0,1,bc[0]);  
	display_char(1,1,bc[1]); 
	display_char(2,1,bc[2]); 
	display_char(3,1,bc[3]); 
	display_char(4,1,'.'); 
	display_char(5,1,bc[4]); 
	display_char(6,1,0xdf);
	display_char(7,1,'c');
}
/****************************************************************/
/***********************读取6次去除最大最小值,取其中4个值***********************/
void pxbj()
{


uchar i,j; 
 unsigned long sum=0;
 uint temp;
for(j=0;j<=5;j++) 
{ for (i=0;i<6-j;i++) 
  if (a[i]>a[i+1]) 
       { temp=a[i]; 
       a[i]=a[i+1]; 
       a[i+1]=temp;} 
  } 

       for(i=1;i<=4;i++)
       {
         sum+=a[i];
        }
   baochi=(uint)(sum/4);
   wendu=(baochi-1394)*10/87;
}


  /*******************显示设置温度用**********************************/

void dtwd()
{

if(hide_t1<2)
    {display_char(10,1,(table[0]+'0')); 
	

	}
    else
	{
	
     display_char(10,1,' '); 
	
	}
	if(hide_t2<2)
	{display_char(11,1,(table[1]+'0')); 
	

	}
    else
	{
	
     display_char(11,1,' '); 
	
	}
}

/*****************************************************/

  /********************设置**************************/
void set()
{
switch(count)
{case 1:do{lstemp=table[0];
                   if(up==1){up=0;lstemp++;}
                   if(down==1){down=0;lstemp--;}  
                   if(lstemp>0x09)lstemp=0;
                   if(lstemp==-1)lstemp=0x09;
				    table[0]=lstemp;
				   hide_t1++;
				   hide_t2=0;
				   if(hide_t1>3)
				   hide_t1=0;
                   dtwd();
            } 
              while(count==2);break;
   case 2:
         do{
            lstemp=table[1];
                  if(up==1){up=0;lstemp++;}
                   if(down==1){down=0;lstemp--;}  
                   if(lstemp>0x09)lstemp=0;
                   if(lstemp==-1)lstemp=0x09;
				   table[1]=lstemp;
				   hide_t2++;
				   hide_t1=0;
				   if(hide_t2>3)
				   hide_t2=0;
                  dtwd();
		 }
		  while(count==3);break; 
         case 3:  do {(stPID.setpoint)=wen_du=(uint)((table[0])*10+table[1]); ET1=0;count=0;done=0;dl_1ms();display_char(10,1,(table[0]+'0'));dl_1ms();display_char(11,1,(table[1]+'0'));  ET1=1;dl_1ms();EX0=1;}while (count==4);break;
		            
          default:  (stPID.setpoint)=wen_du=(uint)((table[0])*10+table[1]); ET1=0;count=0;done=0;dl_1ms();display_char(10,1,(table[0]+'0'));dl_1ms();display_char(11,1,(table[1]+'0')); ET1=1;dl_1ms(); EX0=1; break;
}
}

/*****************************************************/

void key_clockontrol(void)                    //键盘映射
{ switch(key)
  {case 0x10:done=1;up=0;down=0;count++;break;
   case 0x20:up=1;break;
   case 0x40:down=1;break;
   case 0x80:done=1;count=3;break;
   default:break;}
}
/******************************************************************/

void keyboard() interrupt 2 using 3  //键盘扫描
{    
	unsigned char i,j,w;
	
    
	EX1=0;
	EX0=0;
	key=0;
   delay();
	if(~XBYTE[0xf800]&0x0f)
	{
		w=XBYTE[0xf8f0]&0x0f;
			if(~w&0x0f)
			{
				i=4;
				j=w;
				w=0xff;
			}
    	w=XBYTE[0xf8e8]&0x0f;
			if(~w&0x0f)
			{
				i=3;
				j=w;
				w=0xff;
			}
 		w=XBYTE[0xf8d8]&0x0f;
			if(~w&0x0f)
			{
				i=2;
				j=w;
				w=0xff;
			}
		w=XBYTE[0xf8b8]&0x0f;
			if(~w&0x0f)
			{
				i=1;
				j=w;
				w=0xff;
			}
		w=XBYTE[0xf878]&0x0f;
			if(~w&0x0f)
			{
				i=0;
				j=w;
				w=0xff;
			}
	w=~j;
	key=(w<<4)|i;
	}
	while(~XBYTE[0xf800]&0xf);
	key_clockontrol();
	EX1=1;
}






 /****************************************************************/

void main()
{
stPID.proportion=4;
stPID.integral=0;
stPID.derivative=2;
    inttilcs();    //液晶初始化
    EX0=1;
	
	TH1=(65536-1000)/256;
    TL1=(65536-1000)%256;
	EX1=1;
	IT0=1;
	TCON=0x55;
	TMOD=0X19;
	TH0=TL0=0;
	TR0=1;
	p12=1;

	EA=1;

	while(1)
{if(c>6||c==6)
  {
	pxbj();
    c=0;
	diswendu();
/*******************发射所得到的温度数据***************************************/
 TR0=0;
 TR1=0;
 EX0=0;
 EX1=0;
 transmit(wendu);
 EX0=1;
 EX1=1;
 TR0=1;
 TR1=1;



/*****************************************************************************/
	 p12=1;
     dtwd(); 
     fout=PIDCalc(&stPID,wendu);
     ET1=1;
 	 if(fout>0)
		  {  p15=1;
		    display_char(10,0,'k');
		  }
		  if(fout<0){
		              p15=0;
		              display_char(10,0,'g');}
                     }
    if(fout==0)
	{ET1=0;
     p15=0;
     display_char(10,0,' ');
	 }
        if(done==1)
		{
		   
          set();}    //进入调整模式
		  
		   delay();
	
	  }


}

⌨️ 快捷键说明

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