?? ds1337.h
字號:
#include "excalibur.h"
#define ADDRTC 0xd0
typedef unsigned char uchar;
np_pio *clk1337=na_clk1337;
np_pio *dio1337=na_dio1337;
uchar sec, min, hr, dy, dt, mn, yr;
uchar display1337[6];
uchar displaycalendar[6];
void delay1337(uchar a)
{
int i=0;
for(i=0;i<a;i++){;}
}
void I2C_start()
{
dio1337->np_piodata=1;
clk1337->np_piodata=1;
dio1337->np_piodata=0;
}
void I2C_stop()
{
dio1337->np_piodata=0;
clk1337->np_piodata=1;
dio1337->np_piodata=1;
}
void ack()
{
clk1337->np_piodata=0;
dio1337->np_piodata=0;
clk1337->np_piodata=1;
clk1337->np_piodata=0;
}
void I2C_write(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
delay1337(1);
clk1337->np_piodata=0;
dio1337->np_piodata=dat>>7;
dat=dat<<1;
delay1337(1);
clk1337->np_piodata=1;
}
ack();
}
uchar I2C_read()
{
uchar d=0, i;
dio1337->np_piodirection=0;
for (i = 1; i <= 8; i++)
{
delay1337(1);
clk1337->np_piodata = 0;
delay1337(1);
clk1337->np_piodata = 1;
d = d << 1;
d |=dio1337->np_piodata;
}
dio1337->np_piodirection = 1;
return d;
}
void settime(uchar hour,uchar minute,uchar second) // -- initialize the time and date using entries from stdin --
{
uchar a=0;
//I2C_start(); // The following Enables the Oscillator
//I2C_write(ADDRTC); // address the part to write
//I2C_write(0x00); // position the address pointer to 0
//I2C_write(0x00); // write 0 to the seconds register, clear the CH bit
//I2C_stop();
I2C_start();
I2C_write(ADDRTC); // write slave address + write
I2C_write(0x00); // write register address, 1st clock register
a|=(second%10)&0x0f;
a|=(second/10)<<4;
I2C_write(a);
a=0;
a|=(minute%10)&0x0f;
a|=(minute/10)<<4;
I2C_write(a);
a=0;
a|=(hour%10)&0x0f;
a|=(hour/10)<<4;
I2C_write(a);
//I2C_start();
//I2C_write(ADDRTC);
//I2C_write(0x0e);
//I2C_write(0x20);
//I2C_write(0);
I2C_stop();
}
void set(uchar year,uchar month,uchar date,uchar day,uchar hour,uchar minute,uchar second) // -- initialize the time and date using entries from stdin --
{
uchar a=0;
//I2C_start(); // The following Enables the Oscillator
//I2C_write(ADDRTC); // address the part to write
//I2C_write(0x00); // position the address pointer to 0
//I2C_write(0x00); // write 0 to the seconds register, clear the CH bit
//I2C_stop();
I2C_start();
I2C_write(ADDRTC); // write slave address + write
I2C_write(0x00);
a|=(second%10)&0x0f;
a|=(second/10)<<4;
I2C_write(a);
a=0;
a|=(minute%10)&0x0f;
a|=(minute/10)<<4;
I2C_write(a);
a=0;
a|=(hour%10)&0x0f;
a|=(hour/10)<<4;
I2C_write(a);
I2C_write(day);
a=0;
a|=(date%10)&0x0f;
a|=(date/10)<<4;
I2C_write(a);
a=0;
a|=(month%10)&0x0f;
a|=(month/10)<<4;
I2C_write(a);
a=0;
a|=(year%10)&0x0f;
a|=(year/10)<<4;
I2C_write(a);
//I2C_start();
//I2C_write(ADDRTC);
//I2C_write(0x0e);
//I2C_write(0x20);
//I2C_write(0);
I2C_stop();
}
void setcalendar(uchar year,uchar month,uchar day) // -- initialize the time and date using entries from stdin --
{
uchar a=0;
//I2C_start(); // The following Enables the Oscillator
//I2C_write(ADDRTC); // address the part to write
//I2C_write(0x00); // position the address pointer to 0
//I2C_write(0x00); // write 0 to the seconds register, clear the CH bit
//I2C_stop();
I2C_start();
I2C_write(ADDRTC); // write slave address + write
I2C_write(4);
a=0;
a|=(day%10)&0x0f;
a|=(day/10)<<4;
I2C_write(a);
a=0;
a|=(month%10)&0x0f;
a|=(month/10)<<4;
I2C_write(a);
a=0;
a|=(year%10)&0x0f;
a|=(year/10)<<4;
I2C_write(a);
//I2C_start();
//I2C_write(ADDRTC);
//I2C_write(0x0e);
//I2C_write(0x20);
//I2C_write(0);
I2C_stop();
}
unsigned char readany(unsigned char b)//b為0,1,2時分別讀取秒。分時
{
unsigned char a;
I2C_start();
I2C_write(ADDRTC);
I2C_write(b);
I2C_start();
I2C_write(ADDRTC | 1);
a=I2C_read();
I2C_stop();
return a;
}
void initial1337()
{
dio1337->np_piodirection=1;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -