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

📄 rt1602c+ds18b20_整数.c

📁 RT1602C+DS18B20_整数.rar
💻 C
字号:
//单片机RT1602_C语言程序__DS18B20读写一体化C51源程序
//只能显示两位整数,书中例程
//2006年3月8951_LCD_1602_Q_D_MCU适用
//AT89C54_Ver1.00
//[注]:AT89C51的晶振频率为12MHz

#include <REGX51.H>
#include <stdio.h> 
#include <absacc.h>
#include <intrins.h> 
#include <SS89BIN.h> 
   
sbit E_E=P3^3; 									//片选,下降沿触发
sbit R_S=P3^4;									//H数据寄存器,L指令寄存器 
sbit R_W=P3^5;									//H读数据,L写数据  
sbit TMDAT=P3^0;								//根据实际情况定义端口

void dmsec(unsigned int count)					//延时1ms
	{unsigned int i;							
	while(count--)
		{for(i=0;i<125;i++);
		}
    }
void tmreset(void)								//复位
	{unsigned int i;
	TMDAT=0; 									
	i=103;while(i>0)i--; 						//延时900us									
	TMDAT=1; 									
	i=4;while(i>0)i--; 		
	}     										
void tmpre(void)								//等待接收数据
	{unsigned int i;
	while(TMDAT);
	while(~TMDAT);
	i=4;while(i>0)i--;
	}
bit tmrbit(void)								//读一个bit
	{unsigned int i;
	bit dat;
	TMDAT=0;i++;
	TMDAT=1;i++;i++;
	dat=TMDAT;
	i=8;while(i>0)i--;
	return(dat);
	}
unsigned char tmrbyte(void)						//读一个byte
	{unsigned int i,j,dat;
	dat=0;
	for(i=0;i<=8;i++)
		{j=tmrbit();
		dat=(j<<7)|(dat>>1);
		}
	return(dat);
	}
void tmwbyte(unsigned char dat)					//写一个byte
	{unsigned int i;
	unsigned char j;
	bit testb;
	for(j=1;j<=8;j++)
		{testb=dat&0x01;
		dat=dat>>1;
		if(testb)
			{TMDAT=0;							//写1
			i++;i++;
			TMDAT=1;
			i=8;while(i>0)i--;
			}
		else
			{TMDAT=0;							//写0
			i=8;while(i>0)i--;
			TMDAT=1;
			i++;i++;
			}
		}
	}
void tmstart(void)								//DS18B20开始工作
	{tmreset();
	tmpre();
	dmsec(1);
	tmwbyte(0xCC);								//skip rom
	tmwbyte(0x44);								//convert
	}
unsigned char tmrtemp(void)
	{unsigned char a,b,y1,y2,y3;
	tmreset();
	tmpre();
	dmsec(1);
	tmwbyte(0xCC);								//skip rom
	tmwbyte(0xBE);								//convert
	a=tmrbyte();								//LSB
	b=tmrbyte();								//MSB
	y1=a>>4;
	y2=b<<4;
	y3=y1|y2;
	return(y3);
	}


void TIME(int nt)								//延时函数
	{long int it=0;
	for(it=0;it<nt*1000;it++);
	return;
	}
void DELAY(void)								//判断液晶显示器是否忙的子程序
	{P1=0xFF;
	R_S=0;R_W=1;E_E=0;
	_nop_();
	E_E=1;
	while(P1_7);								//如果P1.7为高电平表示忙就循环等待
	return;
	}
void WR1602C(char mun,char rs)					//写入控制命令的子程序
	{P1=mun;									//将代码送到P1口
	R_S=rs;										//1数据寄存器,0指令寄存器 
	R_W=0;E_E=0;								//执行显示命令
	DELAY();									//判断液晶模块是否忙
	E_E=1;										//显示完成,程序停车
	return;
	}

void main(void)
{	unsigned int c02;	
	P3_1=0;P3_2=1;TIME(3);						//指示灯闪烁
	P3_1=1;P3_2=0;TIME(3);	
	P3_1=0;P3_2=1;TIME(3);

	WR1602C(B0000_0001,0);						//清屏并光标复位									
	WR1602C(B0011_1000,0);						//设置显示模式:8位2行5x7点阵
	WR1602C(B0000_1111,0);						//显示器开,光标开,光标允许闪烁
	WR1602C(B0000_0110,0);						//文字不动,光标自动右移
	WR1602C(B1110_1000,0);						//写入显示起始地址(第一行第一个位置)
	WR1602C(B0100_0001,1);						//字母A的代码
	WR1602C(B0100_0010,1);
	WR1602C(B0100_0011,1);
	TIME(5);

START:
	tmstart();	
	c02=tmrtemp();    							//读温度
	
	WR1602C(B1100_0000,0);						//写入显示起始地址(第二行第一个位置)
	WR1602C(0x4E,1);WR1602C(0x6F,1);			//第二排从头开始显示NowTemp:
	WR1602C(0x77,1);WR1602C(0x54,1);
	WR1602C(0x65,1);WR1602C(0x6D,1);				
	WR1602C(0x70,1);WR1602C(0x3A,1);

	WR1602C(c02/10+48,1);						//温度整数位_十位
	WR1602C(c02%10+48,1);						//温度整数位_个位
	WR1602C(0x2E,1);							//显示.

	TIME(5);	
	goto START;	


}


⌨️ 快捷键说明

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