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

📄 main.c

📁 proteus与keil仿真89c55控制18b20 ds1302 字符液晶
💻 C
字号:
#include "data.h"

/*---全局数据定义区---*/
unsigned char str[25];		//存储需要在显示屏显示的字符
unsigned char PageFlage;	//显示的当前页号
unsigned char up;			//上键按下次数
unsigned char down;			//下键按下次数
unsigned char left;			//左键按下次数
unsigned char right;		//右键按下次数

typedef struct time			//时间结构体
{
	uchar sec;				//秒
	uchar min;				//分
	uchar hour;				//小时
	uchar day;				//日期
	uchar month;			//月
	uchar year;				//年
	uchar week;				//星期
};

uchar xdata sec1 	_at_ 0x4000;
uchar xdata min1 	_at_ 0x4001;
uchar xdata hour1	_at_ 0x4002;
uchar xdata day1	_at_ 0x4003;
uchar xdata month1	_at_ 0x4004;
uchar xdata year1	_at_ 0x4005;
uchar xdata week1	_at_ 0x4006;

uchar xdata sec2 	_at_ 0x4007;
uchar xdata min2 	_at_ 0x4008;
uchar xdata hour2	_at_ 0x4009;
uchar xdata day2	_at_ 0x400a;
uchar xdata month2	_at_ 0x400b;
uchar xdata year2	_at_ 0x400c;
uchar xdata week2	_at_ 0x400d;

uchar xdata cal[8][7]	_at_ 0x4020;	//日历表存储

/*---全局数据定义区---*/

/*---外部函数引用区---*/
//---显示器函数区
extern char InitLcd();					//液晶显示器初始化函数
extern void cls();						//液晶显示器清屏函数
//液晶显示器显示函数
extern uchar dprintf(uchar x,uchar y,char *string,uchar mod);
//液晶显示器坐标设置函数
extern void SetPos(uchar row, uchar col);
extern void WriteCom(uchar cmd);		//液晶显示器命令写函数
extern void WriteData(uchar Data);		//液晶显示器写数据函数

//---键盘处理函数区
extern uchar KeyScan();					//按键扫描函数
extern uchar KeyProcess(uchar KeyNum);	//按键处理函数

//---DS1302处理函数区
//1302写数据函数
extern uchar Write1302(uchar Addr,uchar Data);
extern uchar Read1302(uchar Addr);		//1302读数据函数

//---DS18B20处理函数区
extern void Init_DS18B20();				//DS18B20复位函数
extern unsigned char ReadOneChar();		//DS18B20字节读函数
extern void WriteOneChar(uchar dat);	//DS18B20字节写函数
extern void delay_18B20(unsigned int i);//DS18B20延时函数
//extern uchar BcdToHex(uchar time,uchar Data);
/*---外部函数引用区---*/

/*---内部函数区---*/
void BcdToChar(struct time times);		//BCD码转字符函数		
void MainPage();						//主页显示函数
int GetTemperature();					//读取温度函数
/*---内部函数区---*/


void BcdToChar(struct time times)
{
	uchar str1[5],i;

	for(i=0;i<25;i++)
		str[i]=0;
	for(i=0;i<5;i++)
		str1[i]=0;

	strcat(str,"20");
	str1[0] = times.year >> 4 | 0x30;
	strcat(str,str1);
	str1[0] = times.year & 0x0f | 0x30;
	strcat(str,str1);

	strcat(str,".");

	str1[0] = times.month >> 4 | 0x30;
	strcat(str,str1);
	str1[0] = times.month & 0x0f | 0x30;
	strcat(str,str1);

	strcat(str,".");

	str1[0] = times.day >> 4 | 0x30;
	strcat(str,str1);
	str1[0] = times.day & 0x0f | 0x30;
	strcat(str,str1);

	strcat(str," w:");

	str1[0] = times.week & 0x0f | 0x30;
	strcat(str,str1);

	strcat(str," ");

	str1[0] = times.hour >> 4 | 0x30;
	strcat(str,str1);
	str1[0] = times.hour & 0x0f | 0x30;
	strcat(str,str1);

	strcat(str,":");

	str1[0] = times.min >> 4 | 0x30;
	strcat(str,str1);
	str1[0] = times.min & 0x0f | 0x30;
	strcat(str,str1);	
}

int GetTemperature()		//读取18B20温度函数
{
	uchar a=0;
	uchar b=0;
	uchar c=0;
	uchar temp,temp1,temp2;
	short cou[3],i;

	uchar str1[3];	

	Init_DS18B20();
	WriteOneChar(0xCC);    	// 跳过读序号列号的操作
	WriteOneChar(0x44); 	// 启动温度转换

	delay_18B20(200);       // this message is wery important

	Init_DS18B20();
	WriteOneChar(0xCC); 	//跳过读序号列号的操作
	WriteOneChar(0xBE); 	//读取温度寄存器等(共可读9个寄存器) 前两个就是温度

	delay_18B20(200);

	a=ReadOneChar();    	//读取温度值低位
	b=ReadOneChar();   		//读取温度值高位
	temp1=b<<4;
	temp1+=(a&0xf0)>>4;
	temp2=a&0x0f;

    temp=((b*256+a)>>4);    //当前采集温度值除16得实际温度值
	if((temp & 0x80) == 0x80)
	{
		temp1 = 1;
		temp = abs((char)temp);
	}
	else
		temp1 = 0;

	cou[0] = 0;
	cou[1] = 0;
	cou[2] = 0;

	while(temp >= 100)
	{
		temp = temp - 100;
		cou[0]++;
	}

	while(temp >= 10)
	{
		temp = temp - 10;
		cou[1]++;
	}

	cou[2] = temp;

	for(i=0;i<25;i++)
		str[i]=0;

	if(temp1 == 1)
		strcat(str,"温度: -");
	else
		strcat(str,"温度: +");

	cou[0] = cou[0] | 0x30;
	str1[0] = cou[0];
	cou[1] = cou[1] | 0x30;
	str1[1] = cou[1];
	cou[2] = cou[2] | 0x30;
	str1[2] = cou[2];
	strcat(str,str1);

	return temp;
}

void MainPage()
{
	struct time times;
	int value;

	PageFlage = 1;
	left = 3;
	right = 1;
	times.min	=	Read1302(MIN|0x01);
	times.hour	=	Read1302(HOUR|0x01);
	times.day	=	Read1302(DAY|0x01);
	times.month	=	Read1302(MONTH|0x01);
	times.year	=	Read1302(YEAR|0x01);
	times.week	=	Read1302(WEEK|0x01);
	times.week	=	times.week - 1;

	BcdToChar(times);  	//温度数值转换为字符
	
	SetPos(0,0);
	dprintf(0,0,str,1);		//显示时间

	value = GetTemperature();	   //读取温度

	if(value == 0x7fff)
		dprintf(0,16,"温度: ",1);
	else
		dprintf(0,16,str,1);

	dprintf(0,48,"遥控",0);
	dprintf(32,48," ",1);
	dprintf(40,48,"日历",1);
	dprintf(72,48," ",1);
	dprintf(80,48,"计算器",1);
	dprintf(0,80,"受控车状态:  待命",1);
	dprintf(0,112,"确定",1);
	dprintf(127,112,"返回",1);
}

main()
{
	int key;
	int value;
	struct time times;

	InitLcd();

	dat = 0;
	clk = 0;
	PageFlage = 1;

	MainPage();	
	
	while(1)
	{
		if(PageFlage == 1)
		{
			times.min	=	Read1302(MIN|0x01);
			times.hour	=	Read1302(HOUR|0x01);
			times.day	=	Read1302(DAY|0x01);
			times.month	=	Read1302(MONTH|0x01);
			times.year	=	Read1302(YEAR|0x01);
			times.week	=	Read1302(WEEK|0x01);
			times.week	=	times.week - 1;

			day1 = day2 = times.day;
			month1 = month2 = times.month;
			year1 = year2 = times.year;
			week1 = week2 = times.week;
	
			BcdToChar(times);  	//温度数值转换为字符
		
			SetPos(0,0);
			dprintf(0,0,str,1);		//显示时间
			
			value = GetTemperature();	   //读取温度
	
			if(value == 0x7fff)
				dprintf(0,16,"温度: ",1);
			else
				dprintf(0,16,str,1);
		}

		key = KeyScan();
		KeyProcess(key);
	}
}

⌨️ 快捷键说明

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