基于51单片机的DS1302+LCD12864+DS18B20小综合 下载本文

minute=59; hour=23; day=30; month=3; week=5; year=16;

write_DS1302(0x8E,0x00);//NWP write_DS1302(0x80,(second/10)<<4|(second)); write_DS1302(0x82,(minute/10)<<4|(minute)); write_DS1302(0x84,(hour/10)<<4|(hour)); write_DS1302(0x86,(day/10)<<4|(day)); write_DS1302(0x88,(month/10)<<4|(month)); write_DS1302(0x8A,(week/10)<<4|(week)); write_DS1302(0x8C,(year/10)<<4|(year)); write_DS1302(0x8E,0x01);//WP }

void write_time(uchar addr,uchar num) {

write_command(second_line+addr); delay_ms(5);

write_data(0x30+num/10); delay_ms(5);

write_data(0x30+num); delay_ms(5); }

uchar BCD_to_Dec(uchar num) {

uchar NUM_H,NUM_L,NUM; NUM_H=num>>4; NUM_L=num&0x0f;

NUM=((NUM_H*10))+NUM_L; return NUM; }

void write_week(uchar num) {

uchar i;

switch(num){

case 1:write_command(third_line+wek_addr_pos); for(i=0;i<2;i++){

write_data(str_wek[i]); delay_ms(5); } break;

case 2:write_command(third_line+wek_addr_pos); for(i=2;i<4;i++){

write_data(str_wek[i]); delay_ms(5); } break;

case 3:write_command(third_line+wek_addr_pos); for(i=4;i<6;i++){

write_data(str_wek[i]); delay_ms(5); } break;

case 4:write_command(third_line+wek_addr_pos); for(i=6;i<8;i++){

write_data(str_wek[i]); delay_ms(5); } break;

case 5:write_command(third_line+wek_addr_pos); for(i=8;i<10;i++){

write_data(str_wek[i]); delay_ms(5); } break;

case 6:write_command(third_line+wek_addr_pos); for(i=10;i<12;i++){

write_data(str_wek[i]); delay_ms(5); } break;

case 7:write_command(third_line+wek_addr_pos); for(i=12;i<14;i++){

write_data(str_wek[i]); delay_ms(5); }

break; } }

void write_date(uchar addr,uchar num) {

write_command(first_line+addr); delay_ms(5);

write_data(0x30+num/10); delay_ms(5);

write_data(0x30+num); delay_ms(5); }

/************************读出所有时间**********************/ void readtime(void) {

readsecond(); readminute(); readhour(); readday(); readmonth(); readweek(); readyear(); }

/*****************读出秒的十进制数***************************/

uchar readsecond(void) {

uchar dat;

dat=Read_DS1302(0x81);

second=((dat&0x70)>>4)*10+(dat&0x0f); return second; }

/*****************读出分的十进制数***************************/

uchar readminute(void) {

uchar dat;

dat=Read_DS1302(0x83);

minute=((dat&0x70)>>4)*10+(dat&0x0f); return minute; }

/*****************读出小时的十进制数***************************/

uchar readhour(void) {

uchar dat;

dat=Read_DS1302(0x85);

hour=((dat&0x70)>>4)*10+(dat&0x0f); return hour; }

/*****************读出天的十进制数***************************/

uchar readday(void) {

uchar dat;

dat=Read_DS1302(0x87);

day=((dat&0x70)>>4)*10+(dat&0x0f); return day; }

/*****************读出月的十进制数***************************/

uchar readmonth(void) {

uchar dat;

dat=Read_DS1302(0x89);

month=((dat&0x70)>>4)*10+(dat&0x0f); return month; }

/*****************读出周的十进制数***************************/

uchar readweek(void) {

uchar dat;

dat=Read_DS1302(0x8b);

week=((dat&0x70)>>4)*10+(dat&0x0f); return week; }