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

📄 main.c

📁 DS18B20的驱动程序。通过KEIL C编译
💻 C
字号:
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int 
#define SEND P1

sbit E=P3^5;
sbit RW=P3^6;
sbit RS=P3^7;

sbit D=P2^7;

static uchar disp1[]="Test temperature";   
static uchar disp2[]="Temperature 22.2";   

void delay(uchar j);   /* 7,16us  29,60us 119,240us  149,300us*/

void read_1602_state();
void write_1602(bit k,uchar j);
void init_1602();
void disp_1602();

void init_18b20();
void write_18b20(uchar j);
uint read_18b20();

void dell_data(uint j);

void main()
{
	uchar temp1;
	uint temp;

	init_1602();  	
	while(1)
	{
		init_18b20();
		write_18b20(0xcc);
		write_18b20(0x44);
		init_18b20();
		write_18b20(0xcc);
		write_18b20(0xbe);
    	temp1=read_18b20();
		temp=read_18b20();
		temp<<=8;
		temp=temp|temp1;
    	dell_data(temp);	
		disp_1602();
	}	
}

void read_1602_state()
{
	uchar j;

	RW=1;
aa:
	SEND=0xff;
	RS=0;
	E=0;
	delay(30);
	E=1;
	delay(30);
	j=SEND;
	E=0;
	if((j&0x80)!=0)
		goto aa;   
}
void write_1602(bit k,uchar j)
{
	read_1602_state();   
	E=0;
	RW=0;
	RS=k;
	SEND=j;
	E=0;
		delay(20);
	E=1;
		delay(20);
	E=0;
}
void delay(uchar j)
{
	while(--j);
}
void init_1602()
{
	write_1602(0,0x01);			/* clear disp*/
		delay(20);
	write_1602(0,0x38);			/* 16*2 */
		delay(20);
	write_1602(0,0x06);			/* clear disp*/
		delay(20);
	write_1602(0,0x0e);			/* open disp*/
		delay(20);
	write_1602(0,0x0f);
		delay(20);
}
void disp_1602()
{
	uchar i,j;

	write_1602(0,0x80);		
	for(i=0;i<16;++i)
	{	
		j=disp1[i];		
    	write_1602(1,j);
		delay(20);
	}

	write_1602(0,0xc0);	
	for(i=0;i<16;++i)
	{	
		j=disp2[i];	
		write_1602(1,j);
		delay(10);	
	}       
}

void init_18b20()
{
INIT:
    D=1;
	delay(5);
	D=0;
	delay(149);  /* 300us */
	delay(149);  /* 300us */
	D=1;
	delay(29);/* 60us */
	delay(29);
	if(D==1)
		goto INIT;
	delay(119);  /* 240us */
	if(D==0)
	    {delay(119);
		goto INIT;}
	delay(119);
	delay(119);
}
void write_18b20(uchar j)
{
	uchar k;

	for(k=0;k<8;k++)
	{
		D=1;
		delay(5);
		D=0;
		D=0;
		D=j&0x01;
	    delay(29); /*60us*/			
		j=j>>1;	
	}
	delay(4);
}
uint read_18b20()
{
	uchar j,k=0;
	for(j=0;j<8;j++)
	{
		D=0;
		k=k>>1;
		D=1;     /* male D is input*/
		delay(6);/* 15us*/
		if(D)
		k=k|0x80;
		delay(22); /*45us*/
	 }
	return k;
} 
void dell_data(uint j)
{	float k;
    uint y;
	k=0.625*j;	
	y=(int)k;
	disp2[12]=(y/100)+0x30;	y=y%100;	/*because disp must ASCII ,so then data +0x30 */
	disp2[13]=(y/10)+0x30;
	disp2[15]=(y%10)+0x30;
}





⌨️ 快捷键说明

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