📄 main.cpp
字号:
send_com(0x8e);
send_data(0x80);
}
void wp_off() //写保护禁止
{
send_com(0x8e);
send_data(0x00);
}
//////////////////////////////////////////////
void write_sec(unsigned int sec) //写秒
{
wp_off();
send_com(0x80);
send_data(sec);
wp_on();
}
unsigned int read_sec() //读秒
{
send_com(0x81);
read_data();
unsigned int a,b;
a=y[0]+y[1]*2+y[2]*4+y[3]*8;
b=y[4]+y[5]*2+y[6]*4;
sec=b*10+a;
return sec;
}
////////////////////////////////////////////
void write_min(unsigned int min) //写分
{ wp_off();
send_com(0x82);
send_data(min);
wp_on();
}
unsigned int read_min() //读分
{
send_com(0x83);
read_data();
unsigned int a,b;
a=y[0]+y[1]*2+y[2]*4+y[3]*8;
b=y[4]+y[5]*2+y[6]*4;
min=b*10+a;
return min;
}
//////////////////////////////////////////
void write_hr(unsigned int hr) // 写小时
{ wp_off();
send_com(0x84);
send_data(hr);
wp_on();
}
unsigned int read_hr() //读小时
{
send_com(0x85);
read_data();
unsigned int a,b;
a=y[0]+y[1]*2+y[2]*4+y[3]*8;
b=y[4]+y[5]*2;
hr=b*10+a;
return hr;
}
////////////////////////////////////////
void write_date(unsigned int date) //写日期
{ wp_off();
send_com(0x86);
send_data(date);
wp_on();
}
unsigned int read_date() //读日期
{
send_com(0x87);
read_data();
unsigned int a,b;
a=y[0]+y[1]*2+y[2]*4+y[3]*8;
b=y[4]+y[5]*2;
date=b*10+a;
return date;
}
//////////////////////////////////////////
void write_month(unsigned int month) //写月
{ wp_off();
send_com(0x88);
send_data(month);
wp_on();
}
unsigned int read_month() //读月
{
send_com(0x89);
read_data();
unsigned int a,b;
a=y[0]+y[1]*2+y[2]*4+y[3]*8;
b=y[4];
month=b*10+a;
return month;
}
////////////////////////////////////////////////////
void write_day(unsigned int day) //写星期
{ wp_off();
send_com(0x8a);
send_data(day);
wp_on();
}
unsigned int read_day() //读星期
{
send_com(0x8b);
read_data();
unsigned int a;
a=y[0]+y[1]*2+y[2]*4;//+y[3]*8;
// b=y[4]+y[5]*2+y[6]*4+y[7]*8;
day=a;
return day;
}
///////////////////////////////////////
void write_year(unsigned int year) //写年
{ wp_off();
send_com(0x8c);
send_data(year);
wp_on();
}
unsigned int read_year() //读年
{
send_com(0x8d);
read_data();
unsigned int a,b;
a=y[0]+y[1]*2+y[2]*4+y[3]*8;
b=y[4]+y[5]*2+y[6]*4+y[7]*8;
year=b*10+a;
return year;
}
void shijianjishuan()
{
year1=year/10;
year2=year%10;
date1=date/10;
date2=date%10;
month1=month/10;
month2=month%10;
hr1=hr/10;
hr2=hr%10;
min1=min/10;
min2=min%10;
sec1=sec/10;
sec2=sec%10;
}
void read_all()
{
sec=read_sec();
min=read_min();
hr=read_hr();
date=read_date();
month=read_month();
day=read_day();
year=read_year();
shijianjishuan();
}
void weishu(float k)
{
int q=k;
a=q/10;
b=q%10;
float p=k-a*10-b;
c=p*10;
}
//=======================================================================================
//============功能:写18B20 =============================================================
//=======================================================================================
void Write_18B20(unsigned char n)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ0;
_NOP();_NOP(); //==延时5us===
_NOP();_NOP();_NOP();
if((n&0X01)==0X01)
DQ1;
else DQ0;
n=n>>1;
delay_us(60); //==延时50us 以上===
DQ1;
}
}
//=======================================================================================
//============功能:读取18B20 ===========================================================
//=======================================================================================
unsigned char Read_18B20(void)
{
unsigned char i;
unsigned char temp;
for(i=0;i<8;i++)
{
temp=temp>>1;
DQ0;
_NOP(); //==延时1us===
DQ1;
_NOP();_NOP(); //==延时5us===
_NOP();_NOP();_NOP();
P2DIR&=~0x01;
if((P2IN&0x01)==0)
{
temp=temp&0x7F;
}else
{
temp=temp|0x80;
}
delay_us(45); //==延时40us===
P2DIR|=0x01;
DQ1;
}
return temp;
}
//=======================================================================================
//============DS18B20的初始化 ==========================================================
//=======================================================================================
void Init (void)
{
DQ0;
delay_us(600); //==延时500us=================
DQ1;
delay_us(50); //==延时16~60us==============
P2DIR&=~0x01;
if((P2IN&0x01)==0x01) //==0001 1111b=1f ===========
{
Error=1; //==失败1=====================
P2DIR|=0x01;
DQ0;
}else
{
Error=0; //==初始化成功================
P2DIR|=0x01;
DQ1;
delay_us(500);
}
}
//=======================================================================================
//======指令描述:跳过ROM命令,指定代码为CCH,忽略64位ROM地址,直接向DS1820发温度变换命令,===
// 适用于单片机工作.
//=======================================================================================
void Skip(void)
{
Write_18B20(0xcc);
}
//=======================================================================================
//========== 指令描述:温度转换命令,指定代码为44H.启动DS1820进行温度转换,12位转换时最长===
// 为750ms(9位为93.75ms).结果存入内部9字节RAM中.
//=======================================================================================
void Convert (void)
{
Write_18B20(0x44);
}
//=======================================================================================
//================指令描述:读暂存器,指定代码为BEH.读内部RAM中9字节的内容.================
//=======================================================================================
void ReadDo (void)
{
Write_18B20(0xbe);
}
//=======================================================================================
void ReadTemp (void)
{
char temp_low,temp_high; //== 温度值 =====
unsigned int temperature;
temp_low=Read_18B20(); //== 读低位 =====
temp_high=Read_18B20(); //== 读高位 =====
temperature=(temp_high&0x0f); //=== 屏蔽高4位==
temperature<<=8; //=== 将temp_high部分数据移到temperature高8位===
temperature|=temp_low; //=== 将高低两字节内部合并成一个16位数据===
Temper=temperature*0.0625; //=== 计算真实温度值===
}
//=======================================================================================
//============获取DS18B20的温度值========================================================
//=======================================================================================
//=== MCU对DS18B20进行温度转换时,其操作必须满足以下过程:
//=== 1- 每一次读写之前都要对DS18B20进行复位.
//=== 2- 完成复位后发送一条ROM命令到DS18B20.
//=== 3- 最后发送一条RAM命令到DS18B20.
void GetTemp(void)
{
Init(); //=== DS1820初始化===
Skip(); //=== 跳过64位ROM(ROM命令)===
Convert(); //=== 转换(RAM命令)===
// delay_ms(300); //=== 60000x5us=0.3s===
Init(); //=== DS1820初始化===
Skip(); //=== 跳过64位ROM===
ReadDo(); //=== 读暂存器===
ReadTemp(); //=== 读取温度值===
weishu(Temper);
}
main()
{
WDTCTL = WDTPW + WDTHOLD;
DCOCTL=CALDCO_1MHZ;
BCSCTL1=CALBC1_1MHZ;
P4DIR |=BIT2; //DS1302 CS
P4OUT &=~BIT2;
P4DIR |=BIT0; //CLOCK
P4OUT &=~BIT0;
P4DIR |=BIT1; //I/O
P4OUT &=~BIT1; //23时59分58秒 星期日
P1DIR=0XFF;
res0;
delay_us(100);
res1;
LCD_init(); //初始化LCD模块
LCD_clear(); //清屏幕
write_sec(0x27);
write_min(0x34);
write_hr(0x19);
write_date(0x24); //写进时间为09年10月31日
write_month(0x05);
write_year(0x09);
write_day(0x07);
LCD_write_shu(0,0,11);//
LCD_write_shu(1,0,0);//
LCD_write_shu(2,0,0);//
LCD_write_hanzi(3,0,8);// 年
LCD_write_shu(5,0,0);//
LCD_write_shu(6,0,0);//
LCD_write_hanzi(7,0,9);//月
LCD_write_shu(9,0,0);//
LCD_write_shu(10,0,0);
LCD_write_hanzi(11,0,10);//日
LCD_write_shu(13,0,11);
LCD_write_hanzi(0,2,11);//星
LCD_write_hanzi(2,2,12);//期
LCD_write_hanzi(4,2,7);//日
LCD_write_shu(6,2,0);//
LCD_write_shu(7,2,0);
LCD_write_shu(8,2,10);
LCD_write_shu(9,2,0);
LCD_write_shu(10,2,0);
LCD_write_shu(11,2,10);
LCD_write_shu(12,2,0);
LCD_write_shu(13,2,0);
LCD_write_shu(0,4,11);
LCD_write_shu(1,4,11);
LCD_write_hanzi(1,4,0);//温
LCD_write_hanzi(3,4,13);//度
LCD_write_shu(5,4,11);
LCD_write_shu(6,4,11);
LCD_write_shu(7,4,0);
LCD_write_shu(8,4,0);
LCD_write_shu(9,4,12);
LCD_write_shu(10,4,0);
LCD_write_shu(11,4,13);
LCD_write_shu(12,4,14);
while(1)
{
read_all();
LCD_write_shu(1,0,year1);//
LCD_write_shu(2,0,year2);//
LCD_write_shu(5,0,month1);//
LCD_write_shu(6,0,month2);//
LCD_write_shu(9,0,date1);//
LCD_write_shu(10,0,date2);
LCD_write_hanzi(4,2,day);//日
LCD_write_shu(6,2,hr1);//
LCD_write_shu(7,2,hr2);
LCD_write_shu(9,2,min1);
LCD_write_shu(10,2,min2);
LCD_write_shu(12,2,sec1);
LCD_write_shu(13,2,sec2);
GetTemp();
LCD_write_shu(7,4,a);
LCD_write_shu(8,4,b);
LCD_write_shu(10,4,c);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -