18 基于51单片机的心率体温监测报警系统(包括程序、仿真、原理图、流程图)-三、程序编程

时间:2024-10-09 17:25:53
#include <REGX52.H>#include<intrins.h>#include<stdio.h>#include "Delay.h"#include "LCD1602.h"#define uchar unsigned char#define uint unsigned  intsbit beep = P2^5;			  //蜂鸣器引脚sbit DS=P2^4;                 //DS18B20温度传感器sbit key1=P3^2;
sbit key2=P3^3;
sbit key3=P3^4;
sbit key4=P3^5;
sbit led =  P1^0;unsigned char count;typedef unsigned char u8;typedef unsigned int  u16;static uint temp;static float ftemp = 0.0f;//温度转变uint temp;static unsigned char num;static int wdyz=38,xlyzmax=100,xlyzmin=60;static int xl=65;void tmpchange();uint tmp();void beep\_warning();void Time0\_Init()          //定时器初始化{
TMOD = 0x01;           //定时器0工作在方式1    IE   = 0x82;
TH0  = 0xfe;
TL0  = 0x33;     //11.0592MZ晶振,0.5msTR0=1;                 //定时器开始EA=1;
}void Time0_Int() interrupt 1 //中断程序{
   TH0  = 0xfe;             //重新赋值
   TL0  = 0x33;
    num++;	if(num==200)
	{	    tmpchange();        //让18b20开始转换温度
	    temp = tmp();       //读取温度
	    ftemp = temp/10.0f; //转换温度
		num=0;
	}
}void dsreset(void)            //发出命令{
  uint i;
  DS=0;		              
  i=103;				   //将总线拉低480us~960us

  while(i>0)i--;
  DS=1;					   //然后拉高总线,若DS18B20做出反应会将在15us~60us后将总线拉低
  i=4;					   //15us~60us等待
  while(i>0)i--;  //while(DS);}bit tmpreadbit(void)          //读取数据{
   uint i;
   bit dat;
   DS=0;i++;          //i++ for delay
   DS=1;i++;i++;
   dat=DS;
   i=8;while(i>0)i--;   return (dat);
}uchar tmpread(void)           //读取数据{
  uchar i,j,dat;
  dat=0;  for(i=1;i<=8;i++)
  {
    j=tmpreadbit();
    dat=(j<<7)|(dat>>1);   //读出的数据最低位在最前面,这样刚好一个字节在DAT里
  }  return(dat);
}void tmpwritebyte(uchar dat)  //传输数据给DS18B20{
  uint i;
  uchar j;
  bit testb;  for(j=1;j<=8;j++)
  {
    testb=dat&0x01;
    dat=dat>>1;    if(testb)     //write 1
    {
      DS=0;
      i++;i++;
      DS=1;
      i=8;while(i>0)i--;
    }    else
    {
      DS=0;       //write 0
      i=8;while(i>0)i--;
      DS=1;
      i++;i++;
    }
  }
}void tmpchange(void)          //DS18B20开始工作{  dsreset();  Delay(1);  tmpwritebyte(0xcc);  
  tmpwritebyte(0x44);  
}					  
uint tmp()                    //获得温度{  float tt;
  uchar a,b;  dsreset();  Delay(1);  tmpwritebyte(0xcc);  tmpwritebyte(0xbe);
  a=tmpread();//低八位
  b=tmpread();//高八位
  temp=b;
  temp<<=8;             //two byte  compose a int variable
  temp=temp|a;
  tt=temp*0.0625; //算出来的是测到的温度,数值可到小数点后两位
  temp=tt*10+0.5; //为了显示温度后的小数点后一位并作出四舍五入,因为取值运算不能取小数点后的数
  return temp;
}void beep_warning() //温度传感器蜂鸣器警报并且电机转动{	if(ftemp>=wdyz)
	{
		beep=1;			 //蜂鸣器报警
		led = 1;
	}	
	if(xl>xlyzmax)
	{
		beep=1;			 //蜂鸣器报警
		led = 1;
	}	
	if(xl<xlyzmin)
	{
		beep=1;			 //蜂鸣器报警
		led = 1;
	}	
	if(ftemp<wdyz && xl<xlyzmax && xl>xlyzmin)
	{
	 beep=0;					//蜂鸣器报警
	 led = 0;
	}
}void main()					  //主函数{	
	beep=0;			    //蜂鸣器关掉
	led = 0;	LCD_Init();         //显示屏初始化
	Time0\_Init();	LCD\_ShowString(1,1,"wendu:");	LCD_ShowString(2,1,"xinlv:");	while(1)
	{		
		
	 if(key1==0)                  //第一个按键按下
	 {	  Delay(150);	  if(key1==0)
	  {
	   xl++;
	  }
	 }	 if(key2==0)
	  {	  Delay(150);	  if(key2==0)
	  {
	   xl--;
	  }
	 }	 	 
	 	if(key3==0)                  //第一个按键按下
	 {	  Delay(150);	  if(key3==0)
	  {
	   wdyz++;
	  }
	 }	 if(key4==0)
	  {	  Delay(150);	  if(key4==0)
	  {
	   wdyz--;
	  }
	 }	  LCD_ShowNum(1,7,ftemp,2); //第一行显示温度
		LCD_ShowString(1,9,"C "); 
	  LCD\_ShowNum(2,7,xl,2);	 LCD\_ShowNum(1,13,wdyz,2);	 beep_warning(); //温度/心率超出报警
	}
}