📄 ex6-4.c
字号:
/*
标题:范例6-4
版本:1.0
Target:89S51
程序描述:智能温度感测器,这个范例中使用到
(1)8051 的Port 1连接到LCD显示器,PORT 3的P3.3,P3.4和P3.5分别连接到LCD显示器的控制线,程序执行时可以让LCD显示器显示出时间和日期。
(2)温度感测组件AD590经由ADC0804将感测到的温度,转换成数字数据,然后经由PORT 0输入8051。
(3)8051的第10和第11只脚RXD和TXD分别连线到MAX232,然后连接到PC的COM埠。
(4)8051实验板每 小时就读入ADC0804转换的数字温度数据,然后记录在串行式EEPROM 93C66中。
(5)8051实验板可以经由RS232将记录在串行EEPROM 93C66中的数字温度数据传送PC端。*/
/* ***************************************************** */
#include <REGX51.H>
#include <lcd.h>
#define XTAL 11059200
#define baudrate 9600
#define TIMES 25
#define TIMER0_COUNT 0xD8F0 /* 10000h-((12,000,000/(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_2
#define operation_button P2_0
#define up_button 1
#define down_button 1
#define heat P2_3
#define cool P2_1
//
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[10]="0123456789";
unsigned char txOK,c,set_time;
int ii;
int AT93C66_add,send_count;
int heatfast=1;
int t,t1,tt,tt1;
int min,max;
// 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-130;
SBUF=number; // 传送数据
//***************************
if(y>=min&&y<=max)
{
heat=1;
cool=1;
}
else if(y<min)
{
if(heatfast==1){ heat=0;cool=1; P2=0X9F; } //慢加热
else if(heatfast==2) {heat=1;cool=0; P2=0XDF; } //快加热
}
else if(y>max) { cool=0;heat=0; } //制冷
//**********************************************
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) //润年的2月有29天
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;
/****************************************
if (timer0_tick==times) {
i++;
if(i==4) i=0;
timer0_tick=0;
if (status==FORWARD) heat=forward[i];
}
***************************/
timer0_tick=100;
adc_wr=0;
now.second++; //秒加1
if(timer0_tick%2==0)
{
adc_wr=1;
temp_wr=1; }
if (now.second==60) { //如果秒等于60
now.second=0;
//秒恢复为0
now.minute++;
//分加1
if (now.minute==60) { //如果分等于60
now.minute=0; //分恢复为0
now.hour++; //时加1
if (now.hour==24) { //如果时等于24
now.hour=0; //时恢复为0
today.day++; //日加1
if (today.day>monthday(today.year,today
.month)) {
today.day=1; //如果日超过当月最大日数,就变成1
today.month++; //月加1
if(today.month==13) { //如果月等于13
today.month=1; //月恢复为1
today.year++; //年加1
}
}
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=adc_in;
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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -