#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char//简写
#define uint unsigned int//简写
unsigned char code table[]="MONTUEWEDTHUFRISATSUN";//星期
unsigned char table2[]="0123456789";//子模
unsigned char displaytemp[16];//数组
unsigned int hour=0;//时
unsigned int min=0;//分
unsigned int sec=0;//秒
unsigned int count;//计数
unsigned int date=5;//日
unsigned int monse=11;//月
unsigned int year=2019;//年
unsigned int i;//计数
signed int temp;//温度函数使用
float temperature;//温度函数使用
sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;
sbit DQ=P2^6;
sbit speak01 = P3^0;//声音
sbit k1=P1^0;
sbit k2=P1^1;
sbit k3=P1^2;
sbit plusOne=P1^3;//加
sbit decrement=P1^4;//减
void delay(unsigned char com){//延时函数
unsigned int i;
for(i=com;i>0;i--);
}
void DelayUs2x(unsigned char t)//延时函数
{
while(--t);
}
void DelayMs(unsigned char t)//延时函数
{
while(t--)
{
DelayUs2x(245);
DelayUs2x(245);
}
}
bit Init_DS18B20(void)
{
bit dat=0;
DQ = 1; //DQ复位
DelayUs2x(5); //延时
DQ = 0; //拉低总线
DelayUs2x(250); //不小于 480us
DQ = 1; //释放总线
DelayUs2x(50); //15~60us
dat=DQ; //读18b20响应
DelayUs2x(200); //延时使间隙不小于480us
return dat;
}
unsigned char ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; //拉低
dat>>=1; //
DQ = 1; // 释放总线
DelayUs2x(1);
if(DQ)
dat|=0x80;
DelayUs2x(25); //读间隙不小于60us
}
return(dat);
}
void WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0; // 拉低
DQ = dat&0x01; //写最低位
DelayUs2x(25); //延时
DQ = 1; //释放
dat>>=1;
}
}
unsigned int ReadTemperature(void)
{
unsigned char a=0;
unsigned int b=0;
unsigned int t=0;
Init_DS18B20();
WriteOneChar(0xCC); //ROM忽略
WriteOneChar(0x44); // 温度转换
Init_DS18B20();
WriteOneChar(0xCC); // ROM忽略
WriteOneChar(0xBE); //读暂存器
a=ReadOneChar(); //第一个字节
b=ReadOneChar(); //第二个字节
b<<=8;
t=a+b;
return(t);
}
void write_com(unsigned char com){//写指令函数
P0= com;
RS=0;
RW=0;
EN=1;
delay(200);
EN=0;
}
void write_dat(unsigned char dat){//写数据函数
P0=dat;
RS=1;
RW=0;
EN=1;
delay(200);
EN=0;
}
void init(){//初始化
write_com(0x01);//清屏
write_com(0x38);//设置16*2显示,5*7点阵
write_com(0x0c);//开显示
write_com(0x06);//地址加1,写入数据时光标右移一位
}
void LCD_clear(void) //清屏函数
{
write_com(0x01);
DelayMs(5);
}
void speak(void){//扬声器函数
unsigned int i;
for(i=0;i<100;i++)
speak01=!speak01;
delay(10000);
speak01=0;
}
void shop(){//按键函数
if(k1==0&&k2==0&&k3==0){
if(plusOne==0){
i=i+3;
if(i==21)//若满8后将置一
i=0;
}
}
if(k1==0&&k3==0){//条件2个或以上的放前面,没有条件放最后
if(plusOne==0){
min++;
while(plusOne==0);
}
if(decrement==0){
min--;
while(decrement==0);
}
}
if(k2==0&&k3==0){
if(plusOne==0){
sec=0;
while(plusOne==0);
}
if(decrement==0){
sec=0;
while(decrement==0);
}
}
if(k1==0){
if(plusOne==0){
monse++;
while(plusOne==0);
}
if(decrement==0){
monse--;
while(decrement==0);
}
}
if(k2==0){
if(plusOne==0){
date++;
while(plusOne==0);
}
if(decrement==0){
date--;
while(decrement==0);
}
}
if(k3==0){
if(plusOne==0){
hour++;
while(plusOne==0);
}
if(decrement==0){
hour--;
while(decrement==0);
}
}
if(plusOne==0)year++;
while(plusOne==0);
if(decrement==0)year--;
while(decrement==0);
}
void inter() interrupt 1{//定时T0的中断函数,装载初值50MS溢出计数20次为1S
TH0=0x3c;
TL0=0xb0;
count++;
if(count>=20){
count=0;
sec++;
}
if(sec>=60){
sec=0;
min++;
}
if(min>=60){
min=0;
hour++;
}
if(hour>=24){
hour=0;
date++;
}
if(date>31){
date=0;
monse++;
}
if(monse>12){
monse=1;
year++;
}
if(min==0&&sec==0){
speak();
delay(100);
}
}
void display_LCD(){//LCD的具体输入函数
write_com(0x80);
write_dat(' ');
write_dat(table2[(year/1000)]);
write_dat(table2[(year%1000/100)]);
write_dat(table2[(year%100/10)]);
write_dat(table2[(year%10)]);
write_dat('-');
write_dat(table2[(monse/10)]);
write_dat(table2[(monse%10)]);
write_dat('-');
write_dat(table2[(date/10)]);
write_dat(table2[(date%10)]);
write_dat(' ');
write_dat(table[i]);
write_dat(table[i+1]);
write_dat(table[i+2]);
write_com(0xc0);
write_dat(' ');
write_dat(' ');
write_dat(' ');
write_dat(table2[(hour/10)]);
write_dat(table2[(hour%10)]);
write_dat(':');
write_dat(table2[(min/10)]);
write_dat(table2[(min%10)]);
write_dat(':');
write_dat(table2[(sec/10)]);
write_dat(table2[(sec%10)]);
shop();
temp=ReadTemperature();
temperature=(float)temp*0.0625;
write_dat(' ');
write_dat(table2[(int)temperature/10]);
write_dat(table2[(int)temperature%10]);
write_dat('C');
write_com(0x02);
}
void main(){
init();//初始化
EA=1;//开总中断
ET0=1;
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
TR0=1;//开T0时钟
while(1){
display_LCD();
}
}
单片机课程设计 2019-12-14完成,2020-1-2上传