?? 5.c
字號(hào):
#include <reg51.h>
#include <intrins.h>
#define HIGH 1
#define LOW 0
#define uchar unsigned char
#define uint unsigned int
//***************LCD1602********************************
#define DATA_MODE 0x28 //LCD模式設(shè)置,0x28位四位數(shù)據(jù)線模式
#define OPEN_SCREEN 0x0C //打開LCD顯示
#define DISPLAY_ADDRESS 0x80 //寫地址指令
#define CLEARSCREEN LCD_en_com(0x01) //清屏
#define LCDIO P0 //LCD1602的高四位接在P0口的高四位
sbit LCD1602_RS=P0^1; //RS為寄存器選擇,高電平時(shí)選擇數(shù)據(jù)寄存器、低電平時(shí)選擇指令寄存器。
sbit LCD1602_EN=P0^0; //E端為使能端,當(dāng)E端由高電平跳變成低電平時(shí),液晶模塊執(zhí)行命令。
void LCD_en_com(uchar command); //寫命令指令
void LCD_en_dat(uchar temp); //寫數(shù)據(jù)指令
void LCD_set_xy(uchar x, unsigned char y); //設(shè)置顯示地址:X為橫軸(0-15),Y為縱軸(0-1).
void LCD_write_char(uchar x,uchar y,uchar dat); //寫一個(gè)字符
void LCD_write_string(uchar x,uchar y,uchar *s);//寫一個(gè)字符串
void LCD_write_int(uchar x,uchar y,uint intdat); //寫無字符型整形數(shù)
void LCD_init(void); //lcd初始化
void cover(void);
//************************DS1302*******************************************//
sbit RST =P3^4; //ds1302的接線
sbit IO =P3^3;
sbit SCLK=P3^2;
sbit ACC0=ACC^0; //累加器
sbit ACC7=ACC^7;
uchar times[7]={50,59,23,30,11,7,8}; //存放時(shí)間
uchar led[13];
void change_time(unsigned char i);
//*************************DS18B20*******************************************//
sbit DQ=P1^0;
uint h; //定義循環(huán)變量
uchar data temp_data[2]={0x00,0x00}; // 讀出溫度暫放
uchar data display[5]={0x00,0x00,0x00,0x00,0x00};//顯示單元數(shù)據(jù),共4個(gè)數(shù)據(jù),一個(gè)運(yùn)算暫存用
uchar code ditab[16]={0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};
//************************其他******************************************//
sbit alarm_speaker=P3^7; //蜂鳴器的接線
void delay(uchar i); //延時(shí)程序
unsigned char key=16; //存放健值
//***************************************************************************************
void LCD_en_write(void) //EN端產(chǎn)生一個(gè)高電平脈沖,寫LCD
{
LCD1602_EN=HIGH;
_nop_();
LCD1602_EN=LOW;
}
//****************************************************************************************
void LCD_en_com(uchar command)
{
delay(9);
LCD1602_RS=LOW; //RS=0 寫命令
LCDIO&=0x0f; //清高四位
LCDIO|=command&0xf0; //先發(fā)送高四位數(shù)據(jù)
LCD_en_write();
command=command<<4;
LCDIO&=0x0f;
LCDIO|=command&0xf0; //發(fā)送低四位數(shù)據(jù)
LCD_en_write();
}
//*****************************************************************************************
void LCD_en_dat(uchar dat)
{
delay(9);
LCD1602_RS=HIGH; //RS=1 寫數(shù)據(jù)
LCDIO&=0x0f; //清高四位
LCDIO|=dat&0xf0; //寫高四位
LCD_en_write();
dat=dat<<4; //低四位移到高四位
LCDIO&=0x0f; // 清高四位
LCDIO|=dat&0xf0; //寫低四位
LCD_en_write();
}
//**************設(shè)置顯示地址:X為橫軸(0-15),Y為縱軸(0-1)**************************************
void LCD_set_xy(uchar x,uchar y)
{
uchar address;
x&=0x0f;
if(!y)
address=0x80+x;
else
address=0xc0+x;
LCD_en_com(address);
}
//***************寫一個(gè)字符:X為橫軸(0-15),Y為縱軸(0-1)**********
void LCD_write_char(uchar x,uchar y,uchar dat)
{
LCD_set_xy(x,y);
LCD_en_dat(dat);
}
//***************寫一個(gè)字符串**********
void LCD_write_string(uchar x,uchar y,const uchar *s)
{
LCD_set_xy(x,y);
while(*s)
{
LCD_en_dat(*s);
s++;
}
}
//****************寫一個(gè)無字符整數(shù)***************************************
void LCD_write_int(uchar x,uchar y,uint intdat)
{
uchar i=0,temp[5];
LCD_set_xy(x,y);
if(intdat==0)
{
LCD_en_dat(0x30);
return;
}
while(intdat/10 || intdat%10)
{
temp[i]=intdat%10;
intdat/=10;
i++;
}
while(i)
{
i--;
LCD_en_dat(temp[i]+0x30);
}
}
//*****//LCD的初始化函數(shù)(剛上電的時(shí)候一定要調(diào)用一次)//*********************
void LCD_init(void)
{
LCD_en_com(DATA_MODE); //設(shè)計(jì)四位數(shù)據(jù)線模式
LCD_en_write();
delay(9);
LCD_en_com(DATA_MODE);
LCD_en_com(OPEN_SCREEN); //打開LCD顯示
LCD_en_com(DISPLAY_ADDRESS);//設(shè)計(jì)首地址
CLEARSCREEN; //清屏
delay(9);
}
//***********************18b20************
//*********************11微秒延時(shí)函數(shù)*********************//
void delay1(uint t)
{
for(;t>0;t--);
}
//*********************18B20復(fù)位函數(shù)*********************//
reset(void)
{
char presence=1;
while(presence)
{
while(presence)
{
DQ=1;_nop_();_nop_();//總線上拉
DQ=0; //下拉
delay1(50); //維持550us,最短維持480us
DQ=1; //總線上拉
delay1(6); //檢測總線上升沿,66us
presence=DQ; //DS18B20發(fā)存在低電平信號(hào)(60-240us);presence=0繼續(xù)下一步
}
delay1(30); //延時(shí)300us,總線恢復(fù)高電平,DQ=1;
presence = ~DQ; //取反,跳出循環(huán)
}
DQ=1;
}
//*********************18B20寫命令函數(shù)*********************//
void write_byte1(uchar val)
{
uchar i;
for (i=8; i>0; i--) //定義 8 bit,寫 8 bit
{
DQ=1;_nop_();_nop_();
DQ = 0;_nop_();_nop_();_nop_();_nop_();_nop_();//5us
DQ = val&0x01; //最低位移出,并寫入總線
delay1(6); //66us
val=val/2; //右移一位,倒數(shù)第二位變?yōu)樽畹臀?}
DQ = 1;
delay(1); //高電平維持11us,寫結(jié)束
}
//*********************18B20讀1個(gè)字節(jié)函數(shù)*********************//
uchar read_byte1(void)
{
uchar i;
uchar value = 0;
for (i=8;i>0;i--)
{
DQ=1;_nop_();_nop_();
value>>=1; //右移一位
DQ = 0;
_nop_();_nop_();_nop_();_nop_(); //4us
DQ = 1;_nop_();_nop_();_nop_();_nop_(); //4us ,讀時(shí)隙
if(DQ)value|=0x80; //DQ=1,則寫入為 10000000
delay1(6); //66us
}
DQ=1;
return(value);
}
//*********************讀出溫度函數(shù)*********************//
read_temp()
{
reset(); //總線復(fù)位
write_byte1(0xCC); // 發(fā)Skip ROM命令
write_byte1(0xBE); // 發(fā)讀命令
temp_data[0]=read_byte1(); //溫度低8位
temp_data[1]=read_byte1(); //溫度高8位
reset();
write_byte1(0xCC); // Skip ROM
write_byte1(0x44); // 發(fā)轉(zhuǎn)換命令
}
//*********************溫度數(shù)據(jù)處理函數(shù)*********************//
work_temp()
{
uchar n=0; //存儲(chǔ)符號(hào)
if(temp_data[1]>112)
{temp_data[1]=(0xFF-temp_data[1]);temp_data[0]=(0xFF-temp_data[0]);n=1;} //負(fù)溫度求補(bǔ)碼
display[4]=temp_data[0]&0x0f;display[0]=ditab[display[4]]; //處理小數(shù)位
display[4]=((temp_data[0]&0xf0)>>4)|((temp_data[1]&0x0f)<<4);//低八位屏蔽小數(shù)位后右移四位與高八位屏蔽符號(hào)位后左移四位合并//
if(n){display[4]+=1;display[0]-=9;}
display[3]=display[4]/100; //百位
display[1]=display[4]%100;
display[2]=display[1]/10; //十位
display[1]=display[1]%10; //個(gè)位
LCD_write_int(11,1,display[1]); //顯示溫度的個(gè)位
LCD_en_dat('.');
LCD_write_int(13,1,display[0]); //顯示溫度的小數(shù)位
LCD_en_dat(0x27); //顯示'號(hào)
LCD_en_dat('C'); //顯示C
if(!display[3])
{
LCD_write_string(9,1," ");
if(!display[2])
{
LCD_write_string(10,1," ");
}
else
LCD_write_int(10,1,display[2]);
}
else
{
LCD_write_int(9,1,display[3]);
LCD_write_int(10,1,display[2]);
}
if(n)
{
if(!display[2])
LCD_write_string(10,1,"-");
else
LCD_write_string(9,1,"-");
} //負(fù)溫度時(shí)最高位顯示"-"
}
/*=================================================================*/
/* 從1302的當(dāng)前地址處讀出1字節(jié)數(shù)據(jù) */
/*=================================================================*/
void write_1302_byte(unsigned char d)
{
unsigned char i;
ACC=d; //將8位數(shù)據(jù)寫入ACC
for(i=8;i>0;i--) //循環(huán)8次將ACC8位數(shù)據(jù)從低位到高位寫入1302
{
IO=ACC0; //將ACC的最低位轉(zhuǎn)送到IO口
SCLK=1; //上升沿寫入1位數(shù)據(jù)
SCLK=0;
ACC=ACC>>1; //將ACC次低位變?yōu)樽畹臀? }
}
/*=================================================================*/
/* 從1302的當(dāng)前地址處讀出1字節(jié)數(shù)據(jù) */
/*=================================================================*/
unsigned char read_1302_byte(void)
{
unsigned char i;
for(i=8;i>0;i--)
{
ACC=ACC>>1;
ACC7=IO;
SCLK=1; //時(shí)鐘下降沿讀1位數(shù)據(jù)
SCLK=0;
}
return (ACC);
}
/*=================================================================*/
/* 將數(shù)據(jù)Data寫入1302寄存器add處 */
/*================================================================*/
void write_1302_reg(unsigned char add,unsigned char d)
{
RST= 0;
SCLK=0;
RST= 1;
write_1302_byte(add); //將1302的指針移到add處
write_1302_byte(d); //往add處寫入數(shù)據(jù)
SCLK=1;
RST=0; //停止寫數(shù)據(jù)
}
/*=================================================================*/
/* 讀寄存器add處的值 */
/*==================================================================*/
unsigned char read_1302_reg(unsigned char add)
{
unsigned char d;
RST=0;
SCLK=0;
RST=1;
write_1302_byte(add);
d=read_1302_byte();
SCLK=1;
RST=0;
return (d);
}
/*=================================================================*/
/* 設(shè)置時(shí)間,time[0]-time[6]數(shù)據(jù)存放順序?yàn)?秒,分,時(shí),日,月,星期,年 */
/*===================================================================*/
void write_1302_time(unsigned char *time)
{
unsigned char add=0x80; //地址add初值(秒),偶數(shù)地址為只寫
unsigned char i;
unsigned char timeBCD[7]; //用以存放time數(shù)據(jù)的BCD碼值
bdata unsigned char l,h; //單位變量,分別存放時(shí)間數(shù)據(jù)(8421BCD碼)的低4位和高4位
for(i=0;i<7;i++) //將時(shí)間數(shù)據(jù)轉(zhuǎn)為BCD碼放入timeBCD中
{
l=time[i]%10;
h=time[i]/10;
timeBCD[i]=h*16+l;
}
write_1302_reg(0x8e,0x00); //禁止寫
for(i=0;i<7;i++)
{
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -