Main Menu
Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Messages - zombies

#1
อันนี้เป็นโค้ด DS1307 ไม่รู้เขียนผิดในส่วนไหน เวลามันไม่ตรงกับเวลาปัจจุบันค่ะ


void   DS1307Int()
{
   send_ds1307(0x07,0x91);
   read_sec();
   read_min();
   read_hour();
   read_date();
   read_mount();
   read_year();
}

void   send_ds1307(int ad,int data)
{
   
   i2c_start();
   i2c_write(0xD0);
   i2c_write(ad);
   i2c_write(data);
   i2c_stop();

}

int    read_ds1307(int ad)
{
   int   x;
   
   i2c_start();
   i2c_write(0xD0);
   i2c_write(ad);
   i2c_start();
   i2c_write(0xD1);
   x=i2c_read(0);
   i2c_stop();
   return(x);
}

void   read_sec()
{
   int   x;
      x=read_ds1307(0x00);
      DS_SEC=(x/0x10)*10;
      DS_SEC+=(x%0x10);
     
}

void   read_min()
{
   int   x;
      x=read_ds1307(0x01);
      DS_MIN=(x/0x10)*10;
      DS_MIN+=(x%0x10);
     
}
void   read_hour()
{
   int   x;
      x=read_ds1307(0x02);
      DS_HOUR=(x/0x10)*10;
      DS_HOUR+=(x%0x10);
}
void   read_day()
{
   int   x;
      x=read_ds1307(0x03);
      DS_DAY=x;
}
void   read_date()
{
   int   x;
      x=read_ds1307(0x04);
      DS_DATE=(x/0x10)*10;
      DS_DATE+=(x%0x10);
}
void   read_mount()
{
   int   x;
      x=read_ds1307(0x05);
      DS_MOUNT=(x/0x10)*10;
      DS_MOUNT+=(x%0x10);
}
void   read_year()
{
   int   x;
      x=read_ds1307(0x06);
      DS_YEAR=(x/0x10)*10;
      DS_YEAR+=(x%0x10);//+2000;
      TH_YEAR=DS_YEAR+2543;
}
void   Ds1307_read()
{
   read_day();
   read_sec();
   read_min();
   read_hour();
   read_date();
   read_mount();
   read_year();
}

void   save_day(int x)
{
   send_ds1307(0x03,x);
}
void   save_date(int x)
{
   int y;
   
   y=((x/10)<<4);
   y|=x%10;
   send_ds1307(0x04,y);
}
void   save_mount(int x)
{
   int y;
   
   y=((x/10)<<4);
   y|=x%10;
   send_ds1307(0x05,y);
}
void   save_year(int x)
{
   int y;
   
   y=((x/10)<<4);
   y|=x%10;
   send_ds1307(0x06,y);
}
void   save_sec(int x)
{
   int y;
   
   y=((x/10)<<4);
   y|=x%10;
   send_ds1307(0x00,y);
}

void   save_min(int x)
{
   int y;
   
   y=((x/10)<<4);
   y|=x%10;
   send_ds1307(0x01,y);
}
void   save_hour(int x)
{
   int y;
   
   y=((x/10)<<4);
   y|=x%10;
   send_ds1307(0x02,y);
}

void  set_time_ds(int   hh,int mm,int ss)
{
   save_hour(hh);
   save_min(mm);
   save_sec(ss);
}
void  set_date_ds(int   dd,int mm,int yy)
{
   save_date(dd);
   save_mount(mm);
   save_year(yy);
}

//31  // 1 3 5 7 8 10 12
//30 // 4 6 9 11