51单片机,DS18B20,液晶,超声波

#include <REG52.H>
#include <DS18B20.H>

sbit DQ=P1^2;				   //温度传感器的数据口

unsigned char readdata[2]={0,0};


void delay_18B20(unsigned int i) 
{
 while(i--); 
}
 /**********ds18b20初始化函数**********************/ 
void Init_DS18B20(void)
 { 
	unsigned char x=0; 
	DQ = 1;          //DQ复位
 	delay_18B20(2); //稍做延时 
	DQ = 0;          //单片机将DQ拉低 
	delay_18B20(60); //精确延时 大于 480us 
	DQ = 1;          //拉高总线 
	delay_18B20(2); 
	x=DQ;            //稍做延时后 如果x=0则初始化成功 x=1则初始化失败 
	delay_18B20(15); 
} 
/***********ds18b20读一个字节**************/ 
unsigned char ReadOneChar(void)
{ 
	unsigned char i=0; 
	unsigned char dat = 0; 
	for (i=8;i>0;i--)
 	{    
		DQ = 0; // 给脉冲信号
     	dat>>=1;     
		DQ = 1; // 给脉冲信号     
		if(DQ)     
		dat |=0x80;    
 		delay_18B20(4);
 	}    
	return(dat);
} 
/*************ds18b20写一个字节****************/ 
void WriteOneChar(unsigned char dat) 
{    
	unsigned char i=0;   
 	for (i=8; i>0; i--)    
	{    
		DQ = 0;    
 		DQ = dat&0x01;     
		delay_18B20(2);    
 		DQ = 1;     
		dat>>=1; 
	}
} 
int ReadTemperature(void) 
{ 
	int a,b,temp=0;
	delay_18B20(40);       // this message is very important 
	Init_DS18B20(); 
	WriteOneChar(0xCC);    // 跳过读序号列号的操作 
	WriteOneChar(0x44); // 启动温度转换 
	delay_18B20(40);       // this message is wery important
	Init_DS18B20(); 
	WriteOneChar(0xCC); //跳过读序号列号的操作 
	WriteOneChar(0xBE); //读取温度寄存器等共可读9个寄存器 前两个就是温度 
	delay_18B20(40);      
	readdata[0]=ReadOneChar();    //读取温度值低位 
	readdata[1]=ReadOneChar();      //读取温度值高位
	a=readdata[0];
	b=readdata[1];
	b<<=8;
	temp=a+b;


	return temp;
	    
 } 

#ifndef __DS18B20_H__
#define __DS18B20_H__


int ReadTemperature(void);


#endif


/*-----------------------------------------------
 功能:通过显示字符、数字、汉字和图片测试液晶基本功能
------------------------------------------------*/
#include <reg52.h>
#include <intrins.h>
#include "LCD.h"


sbit RS  = P0^5;
sbit RW  = P0^6;
sbit E   = P0^7;
sbit RES = P0^4;
sbit PSB  = P0^2;
sbit PAUSE = P0^3;


#define DataPort P2        //MCU P2<------> LCM




/*------------------------------------------------
 uS延时函数,含有输入参数 unsigned char t,无返回值
 unsigned char 是定义无符号字符变量,其值的范围是
 0~255 这里使用晶振12M,精确延时请使用汇编,大致延时
 长度如下 T=tx2+5 uS 
------------------------------------------------*/
 void DelayUs2x(unsigned char t)
{   
 while(--t);
}
/*------------------------------------------------
 mS延时函数,含有输入参数 unsigned char t,无返回值
 unsigned char 是定义无符号字符变量,其值的范围是
 0~255 这里使用晶振12M,精确延时请使用汇编
------------------------------------------------*/
void DelayMs(unsigned char t)
{
     
 while(t--)
 {
     //大致延时1mS
     DelayUs2x(245);
	 DelayUs2x(245);
 }
}










/*------------------------------------------------
                    检测忙位
------------------------------------------------*/
void Check_Busy()
{  
    RS=0;
    RW=1;
    E=1;
    DataPort=0xff;
    while((DataPort&0x80)==0x80);//忙则等待
    E=0;
}
/*------------------------------------------------
                   写命令
------------------------------------------------*/
void Write_Cmd(unsigned char Cmd)
{
	Check_Busy();
	RS=0;
	RW=0;
	E=1;
	DataPort=Cmd;
	DelayUs2x(5);
	E=0;
	DelayUs2x(5);
}
/*------------------------------------------------
                    写数据
------------------------------------------------*/
void Write_Data(unsigned char Data)
{
	Check_Busy();
	RS=1;
	RW=0;
	E=1;
	DataPort=Data;
	DelayUs2x(5);
	E=0;
	DelayUs2x(5);
}
/*------------------------------------------------
                   液晶屏初始化
------------------------------------------------*/
void Init_ST7920()
{  
   DelayMs(40);           //大于40MS的延时程序
   PSB=1;                 //设置为8BIT并口工作模式
   DelayMs(1);            //延时
   RES=0;                 //复位
   DelayMs(1);            //延时
   RES=1;                 //复位置高
   DelayMs(10);
   Write_Cmd(0x30);       //选择基本指令集
   DelayUs2x(50);         //延时大于100us
   Write_Cmd(0x30);       //选择8bit数据流
   DelayUs2x(20);         //延时大于37us
   Write_Cmd(0x0c);       //开显示(无游标、不反白)
   DelayUs2x(50);         //延时大于100us
   Write_Cmd(0x01);       //清除显示,并且设定地址指针为00H
   DelayMs(15);           //延时大于10ms
   Write_Cmd(0x06);       //指定在资料的读取及写入时,设定游标的移动方向及指定显示的移位,光标从右向左加1位移动
   DelayUs2x(50);         //延时大于100us
}
/*------------------------------------------------
                   用户自定义字符


void CGRAM()
{ 
     int i;
     Write_Cmd(0x30); 
     Write_Cmd(0x40);
     for(i=0;i<16;i++)
       {
	    Write_Data(user16x16[i*2]);
        Write_Data(user16x16[i*2+1]);
      }
} 
------------------------------------------------*/  
/*------------------------------------------------
                   显示用户自定义字符
------------------------------------------------*/
void DisplayCGRAM(unsigned char x,unsigned char y)
{ 
 switch(y)
     {
	  case 1: Write_Cmd(0x80+x);break;
	  case 2: Write_Cmd(0x90+x);break;
	  case 3: Write_Cmd(0x88+x);break;
	  case 4: Write_Cmd(0x98+x);break;
      default:break;
	 }
    Write_Data(00);
    Write_Data(00);


}         
/*------------------------------------------------
                   显示字符串
x:横坐标值,范围0~8
y:纵坐标值,范围1~4
------------------------------------------------*/
void LCD_PutString(unsigned char x,unsigned char y,unsigned char code *s)
{ 
 switch(y)
     {
	  case 1: Write_Cmd(0x80+x);break;
	  case 2: Write_Cmd(0x90+x);break;
	  case 3: Write_Cmd(0x88+x);break;
	  case 4: Write_Cmd(0x98+x);break;
      default:break;
	 }
 while(*s>0)
   { 
      Write_Data(*s);
      s++;
      DelayUs2x(50);
   }
}
/*------------------------------------------------
                      清屏
------------------------------------------------*/
void ClrScreen()
{ 
   Write_Cmd(0x01);
   DelayMs(15);
}
   	
/*------------------------------------------------
                   显示图片
------------------------------------------------*/
void LCD_PutGraphic(unsigned char code *img)
{ 
 int i,j;
//显示上半屏内容设置
   for(i=0;i<32;i++)            
    { 
      Write_Cmd(0x80 + i); //SET  垂直地址 VERTICAL ADD
      Write_Cmd(0x80);     //SET  水平地址 HORIZONTAL ADD
      for(j=0;j<16;j++)
       {
         Write_Data(*img);
         img++;
       }
    }
//显示下半屏内容设置
   for(i=0;i<32;i++)            
    {
      Write_Cmd(0x80 + i); //SET 垂直地址 VERTICAL ADD
      Write_Cmd(0x88);     //SET 水平地址 HORIZONTAL ADD
      for(j=0;j<16;j++)
       {
         Write_Data(*img);
         img++;
       }
    }  
           
}
/*------------------------------------------------
                 设置到绘图模式
------------------------------------------------*/
void SetGraphicMode()
{ 
   Write_Cmd(0x36);       //选择8bit数据流 图形模式
   DelayUs2x(20);


}







#ifndef __LCD_H__
#define __LCD_H__


/*------------------------------------------------
 uS延时函数,含有输入参数 unsigned char t,无返回值
 unsigned char 是定义无符号字符变量,其值的范围是
 0~255 这里使用晶振12M,精确延时请使用汇编,大致延时
 长度如下 T=tx2+5 uS 
------------------------------------------------*/
void DelayUs2x(unsigned char t);




void DelayMs(unsigned char t);




void Check_Busy();


/*------------------------------------------------
                   写命令
------------------------------------------------*/
void Write_Cmd(unsigned char Cmd);


/*------------------------------------------------
                    写数据
------------------------------------------------*/
void Write_Data(unsigned char Data);


/*------------------------------------------------
                   液晶屏初始化
------------------------------------------------*/
void Init_ST7920();




/*------------------------------------------------
                   显示用户自定义字符
------------------------------------------------*/
void DisplayCGRAM(unsigned char x,unsigned char y);
        
/*------------------------------------------------
                   显示字符串
x:横坐标值,范围0~8
y:纵坐标值,范围1~4
------------------------------------------------*/
void LCD_PutString(unsigned char x,unsigned char y,unsigned char code *s);


/*------------------------------------------------
                      清屏
------------------------------------------------*/
void ClrScreen(); 	
/*------------------------------------------------
                   显示图片
------------------------------------------------*/
void LCD_PutGraphic(unsigned char code *img);
/*------------------------------------------------
                 设置到绘图模式
------------------------------------------------*/
void SetGraphicMode();


#endif







#include <reg52.h>
#include "Ultrasonic.h"




sbit P00=P1^0;		  //超声波ting端
sbit P01=P1^1;		 //超声波Echo端


void Init_Timer0(void)
{
 TMOD=0x01;	  //使用模式1,16位定时器		     
 TH0=0x00;	     //给定初值,这里使用定时器最大值从0开始计数一直到65535溢出
 TL0=0x00;
 //EA=1;          //总中断打开
 //ET0=1;        //定时器中断打开
 //TR0=1;       //定时器开关打开
}


/*------------------------------------------------
 uS延时函数,含有输入参数 unsigned char t,无返回值
 unsigned char 是定义无符号字符变量,其值的范围是
 0~255 这里使用晶振12M,精确延时请使用汇编,大致延时
 长度如下 T=tx2+5 uS 
------------------------------------------------*/
void Delay_Us2x(unsigned char t)
{   
 while(--t);
}
/*------------------------------------------------
 mS延时函数,含有输入参数 unsigned char t,无返回值
 unsigned char 是定义无符号字符变量,其值的范围是
 0~255 这里使用晶振12M,精确延时请使用汇编
------------------------------------------------*/
void Delay_Ms(unsigned char t)
{
     
 while(t--)
 {
     //大致延时1mS
     Delay_Us2x(245);
	 Delay_Us2x(245);
 }
}
/*------------------------------------------------
 获取障碍物距离   程序需优化,未考虑模块工作失常的情况!
------------------------------------------------*/
int get_length()
{
  float ret_time=0;   //高电平持续时间
  P00=P01=0;
  Init_Timer0();	
  P00=1;
  Delay_Ms(10);
  P00=0;
  while(!P01);
  TH0=0xFF;	     //给定初值,这里使用定时器最大值从0开始计数一直到65535溢出
  TL0=0xFF;
  TR0=1;            //定时器开关打开
  while(P01);
  TR0=0;
  ret_time=TH0*256+TL0;
  TH0=0xFF;	     //给定初值,这里使用定时器最大值从0开始计数一直到65535溢出
  TL0=0xFF;
  ret_time=(ret_time*1.7)/100;     //算出来是CM			
	
  return (int)ret_time;		




}








/*------------------------------------------------
                 定时器中断子程序
------------------------------------------------*/
void Timer0_isr(void) interrupt 1 using 1
{
   
}






#ifndef __ULTRASONIC_H__
#define __ULTRASONIC_H__



int get_length();



#endif

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值