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

📄 intelligent_thermometer.c

📁 /*智能型温度感测器: (1)8051的PORT1连接LCD显示器
💻 C
字号:
/*智能型温度感测器:
(1)8051的PORT1连接LCD显示器,PORT3的P3.3,P3.4,P3.5分别连接到LCD控制线,程序执行时可以在LCD显示器显示时间和日期
(2)温度感应组件AD590通过ADC0804将感应到的温度转换数字数据,然后通过PORT0输入8051
(3)8051的第10只和第11只引脚RXD和TXD分别连接到MAX232,然后连接到PC的COM端口
(4)8051实验板每小时读入ADC0804转换的数字温度数据,然后记录在串行EEPROM93C66中
(5)8051实验板可以通过RS232将记录在串行EEPROM93C66中数字温度数据传送到PC端
*/
#include<regx51.h>
#include<lcd.h>
#define		XTAL				11059200
#define		baudrate			9600
#define		TIMES				25
#define		TIMER0_COUNT		0xD8F0//10000-((12000000/(12*100))
//数字时钟的工作模式
#define		SET					11
#define		TRUE				1
#define		FALSE				0
#define		putchar				write_LCD_data
#define		adc_in				P0
#define		adc_rd				P3_7
#define		adc_wr				P3_6
//
#define		mode_button			P2_7
#define		operation_button	P2_6
#define		up_button			P2_5
#define		down_button			P2_4
//
typedef struct
{
	char hour;
	char minute;
	char second;
}time;
typedef struct
{
	char year;
	char month;
	char day;
}date;
time now={23,59,0},display;
date today={04,12,15},tmpday;
static unsigned timer0_tick=100,mode=0,operation;
char code dayofmonth[]={31,28,31,30,31,30,31,31,30,31,30,31};
char code weekday[7][4]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
char code int2char[]="0123456789";
unsigned char txOK,c,set_time;
int AT93C66_add,send_count;
//0表示不记录在AT93C66中,1表示要记录在AT93C66中
unsigned char temp_wr;

/*****AT93C66的读写程序*************/
unsigned char read_byte(int);
void write_byte(int,unsigned char);

void delay(void)
{
	unsigned char i,j;
	for(i=0;i<125;i++)
		for(j=0;j<255;j++)
		;
}

void display_tempreture(unsigned char number)
{
	unsigned char x,y;
	y=(number<<1)-232;
	x=y/10;
	gotoxy(1,9);
	write_LCD_data(int2char[x]);
	x=y%10;
	write_LCD_data(int2char[x]);
	if(temp_wr==1)
	{
		write_byte(AT93C66_add,y);
		if(AT93C66_add>255)
		{
			write_byte(0,1);
			x=AT93C66_add-256;
			write_byte(1,x);
		}
		else
		write_byte(1,(unsigned char)AT93C66_add);
		temp_wr=0;
		AT93C66_add++;
	}
}

void display_time(void)
{
	gotoxy(1,0);
	display_LCD_number(display.hour);
	display_LCD_string(":");
	display_LCD_number(display.minute);
	display_LCD_string(":");
	display_LCD_number(display.second);
}

void display_date()
{
	char i,days=4;
	gotoxy(2,2);
	display_LCD_number(tmpday.year);
	display_LCD_string("/");
	display_LCD_number(tmpday.month);
	display_LCD_string("/");
	display_LCD_number(tmpday.day);
	display_LCD_string(" ");
	if(tmpday.month>1)
		for(i=0;i<=tmpday.month-2;i++)
			days+=(dayofmonth[i]%7);
	if(tmpday.year!=0)days+=((tmpday.year-1)/4)+tmpday.year+1;
	if(tmpday.year%4==0&&tmpday.month>2)days++;
	days=(days+tmpday.day)%7;
	display_LCD_string(&weekday[days][0]);
}

char monthday(char year,char month)
{
	if(month==2&&year%4==0)
	return(29);
	else
	return(dayofmonth[month-1]);
}

static void timer0_isr(void) interrupt TF0_VECTOR using 1
{
	TR0=0;
	TL0=(TIMER0_COUNT&0x00FF);
	TH0=(TIMER0_COUNT>>8);
	TR0=1;
	if(mode==1)return;
	if(--timer0_tick)return;
	timer0_tick=100;
	now.second++;
	if(now.second==60)
	{
		now.second=0;
		now.minute++;
		if(now.minute==60)
		{
			now.minute=0;
			adc_wr=0;
			now.hour++;
			adc_wr=1;
			temp_wr=1;
			if(now.hour==24)
			{
				now.hour=0;
				today.day++;
				if(today.day>monthday(today.year,today.month))
				{
					today.day=1;
					today.month++;
					if(today.month==13)
					{
						today.month=1;
						today.year++;
					}
				}
				tmpday=today;
				display_date();
			}
		}
	}
	display=now;
	display_time();
}

static void timer0_initialize(void)
{
	EA=0;
	TR0=0;
	TMOD&=0xF0;
	TMOD|=0x01;
	TL0=(TIMER0_COUNT&0x00FF);
	TH0=(TIMER0_COUNT>>8);
	PT0=0;
	ET0=1;
	TR0=1;
	EA=1;
}

static void int0_isr(void) interrupt 0 using 0
{
	unsigned char voltage;
	adc_in=0xFF;
	adc_rd=0;
	voltage=voltage<<1;
	adc_rd=1;
	display_tempreture(voltage);
}

static void com_isr(void) interrupt SIO_VECTOR using 1
{
	if(TI)
	{
		TI=0;
		if(send_count<AT93C66_add)
		{
			SBUF=read_byte(send_count)+32;//传输数据
			send_count++;
		}
	}
}

void com_initialize(void)
{
	PCON|=0x80;
	TMOD=0x20;
	TH1=(unsigned char)(256-(XTAL/(16L*12L*baudrate)));
	TR1=(unsigned char)(256-(XTAL/(16L*12L*baudrate)));
	SCON=0x50;
	ES=1;
	TR1=1;
}

char gotkey()
{
	if(mode_button==0)
	{
		delay();
		if(mode_button==0)return(0);
	}
	if(operation_button==0)
	{
		delay();
		if(operation_button==0)
		return(1);
	}
	if(up_button==0)
	{
		delay();
		if(up_button==0)return(2);
	}
	if(down_button==0)
	{
		delay();
		if(down_button==0)
		return(3);
	}
	return(15);
}

void main(void)
{
	char keys;
	txOK=1;
	mode=0;
	operation=0;
	init_LCD();
	clear_LCD();
	gotoxy(2,0);
	display_LCD_string("20");
	display=now;
	display_time();
	tmpday=today;
	display_date();
	EA=1;
	com_initialize();
	IT0=1;
	EX0=1;
	adc_wr=0;
	adc_wr=1;
	do
	{
		keys=gotkey();
		switch(keys)
		{
			case 0:
					mode++;
					if(mode==4)mode=0;
					gotoxy(1,12);
					write_LCD_data(int2char[mode]);
					if(mode==1)
					{
						display=now;
						tmpday=today;
					}
					if(mode==2)
					{
						now=display;
						today=tmpday;
					}
					break;
			case 1:
					if(mode==0)break;
					if(mode==1)
					{
						operation++;
							if(operation==5)operation=0;
					}
					if(mode==2)
					{
						send_count=1;
						SBUF=read_byte(0)+32;//传输数据
					}
					if(mode==3)
					{
						write_byte(0,0);
						write_byte(0,0);
						write_byte(1,6);
						write_byte(2,today.year);
						write_byte(3,today.month);
						write_byte(4,today.day);
						write_byte(5,now.hour);
						AT93C66_add=6;
						adc_wr=0;
						temp_wr=1;
						adc_wr=1;
					}
					gotoxy(1,14);
					write_LCD_data(int2char[operation]);
					break;
			case 2:
					if(mode!=1)break;
					switch(operation)
					{
						case 0:
								display.hour++;
								if(display.hour>=24)
									display.hour=0;
								gotoxy(1,0);
								display_LCD_number(display.hour);
								break;
						case 1: 
								display.minute++;
								if(display.minute>=60)
									display.minute=0;
								gotoxy(1,3);
								display_LCD_number(display.minute);
								break;
						case 2:
								tmpday.year++;
								if(tmpday.year>=100)
									tmpday.year=0;
								display_date();
								break;
						case 3:
								tmpday.month++;
								if(tmpday.month>12)
								tmpday.month=1;
								display_date();
								break;
						case 4:
								tmpday.day++;
								if(tmpday.day>monthday(tmpday.year,tmpday.month))
								tmpday.day=1;
								display_date();
								break;
					}
					break;
			case 3:
					if(mode!=1)break;
					switch(operation)
					{
						case 0:
								display.hour--;
								if(display.hour<0)display.hour=23;
									gotoxy(1,0);
									display_LCD_number(display.hour);
								break;
						case 1:
								display.minute--;
								if(display.minute<0)
									display.minute=59;
									gotoxy(1,3);
									display_LCD_number(display.minute);
								break;
						case 2:
								tmpday.year--;
								if(tmpday.year<0)tmpday.year=99;
								display_date();
								break;
						case 3:
								tmpday.month--;
								if(tmpday.month<1)tmpday.month=12;
								display_date();
								break;
						case 4:	
								tmpday.day--;
								if(tmpday.day<1)
								tmpday.day=monthday(tmpday.year,tmpday.month);
								display_date();
								break;
									
					}
		}

	}while(1);
}

⌨️ 快捷键说明

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