date.c
上传用户:shyuanyi
上传日期:2008-05-24
资源大小:69k
文件大小:5k
- #include "my_head.h"
- #include "DS_addr.h"
- /*************************************/
- #define SCL P2_2
- #define SDA P2_1
- #define RST P2_0
- //-------------------------
- #define CLOCK_FLAG 0
- #define RAM_FLAG 1
- /**************************************/
- void I_send(BYTE I_data)
- {
- BYTE i;
- for(i=0;i<8;i++ )
- {
- SCL = 0;
- SDA = (bit)(I_data & 0x1);
- I_data>>=1;do_nop(1);
- SCL = 1; do_nop(1);
- }
- }
- BYTE I_receive( void )
- {
- BYTE i,I_data;
- for(I_data=i=0;i<8;i++)
- {
- SCL = 0;do_nop(1);
- I_data >>= 1;
- if(SDA==1)I_data|=0X80;
- SCL = 1; do_nop(1);
- }
- return(I_data);
- }
- BYTE ReadByte(BYTE addr,bit Clock_Ram) //Clock_Ram:0-clock,1-ram.
- {
- BYTE i;
- SCL = 0; //must
- addr <<= 1;
- addr &= ~0xC1;
- addr |= 0x81;
- if(Clock_Ram)addr |= 0x40;
- RST = 1;
- I_send(addr);
- i=I_receive();
- RST = 0;
- return(i);
- }
- void WriteByte(BYTE addr,BYTE Dat,bit Clock_Ram)//Clock_Ram:0-clock,1-ram.
- {
- SCL = 0; //must
- addr <<= 1;
- addr &= ~0xC1;
- addr |= 0x80;
- if(Clock_Ram)addr |= 0x40;
- RST = 1;
- I_send(addr);
- I_send(Dat);
- RST = 0;
- }
- //ClockData[7],时钟数据,格式:YY MM DD HH MM SS WW
- void ReadClock(unsigned char ClockData[])
- {
- WriteByte(CONTRAL_ADDR,0X00,CLOCK_FLAG);//WP = 0
- ClockData[0]=ReadByte(YEAR_ADDR,CLOCK_FLAG);
- ClockData[1]=ReadByte(MOM_ADDR,CLOCK_FLAG);
- ClockData[2]=ReadByte(DATE_ADDR,CLOCK_FLAG);
- ClockData[3]=ReadByte(HOUR_ADDR,CLOCK_FLAG);
- ClockData[4]=ReadByte(MIN_ADDR,CLOCK_FLAG);
- ClockData[5]=ReadByte(SEC_ADDR,CLOCK_FLAG);
- ClockData[6]=ReadByte(DATE_ADDR,CLOCK_FLAG);
- WriteByte(CONTRAL_ADDR,0X80,CLOCK_FLAG); //WP = 1
- }
- void Do_WriteClock(BYTE buf[7])
- {
- BYTE i;
- WriteByte(CONTRAL_ADDR,0X00,CLOCK_FLAG); //WP = 0
- for(i=0;i<7;i++)
- {
- WriteByte(i,buf[i],CLOCK_FLAG);
- }
- WriteByte(CONTRAL_ADDR,0X80,CLOCK_FLAG); //WP = 1
- }
- void Do_ReadClock(BYTE buf[7])
- {
- BYTE i;
- WriteByte(CONTRAL_ADDR,0X00,CLOCK_FLAG); //WP = 0
- for(i=0;i<7;i++)
- {
- buf[i]=ReadByte(i,CLOCK_FLAG);
- }
- WriteByte(CONTRAL_ADDR,0X80,CLOCK_FLAG); //WP = 1
- }
- void Init_DateIC(void) //DS1302
- {
- BYTE i;//,buf[7] = {0x00,0x45,0x23,0x16,0x06,0x03,0x04};
- SCL = SDA =1;
- RST = 0; //P3.0 = 0;
- //I2CON = 0x44;
- //I2SCLH = 0x10;
- //I2SCLH = 0x10;
- //Do_WriteClock(buf); //this is a test!
-
- WriteByte(CONTRAL_ADDR,0X00,CLOCK_FLAG); //WP = 0
- i=ReadByte(SEC_ADDR,CLOCK_FLAG);
- if(i>0x7f)
- {
- WriteByte(SEC_ADDR,i&0x7F,CLOCK_FLAG);//startup clock;
- }
- i=ReadByte(HOUR_ADDR,CLOCK_FLAG);
- if(i>0x7f)WriteByte(HOUR_ADDR,i&0X7F,CLOCK_FLAG);//set 24hour/day;
- WriteByte(TRCH_ADDR,0XA5,CLOCK_FLAG); //充电备置
- WriteByte(CONTRAL_ADDR,0X80,CLOCK_FLAG); //WP = 1
- }
- void DisplayClock(void)
- {
- BYTE i,buf[8];
- WriteByte(CONTRAL_ADDR,0X00,CLOCK_FLAG);//WP = 0
- i=ReadByte(HOUR_ADDR,CLOCK_FLAG);
- buf[0] = ~D_DISPLAY[i>>4];
- buf[1] = ~D_DISPLAY[i&0XF];
- i=ReadByte(MIN_ADDR,CLOCK_FLAG);
- buf[3] = ~D_DISPLAY[i>>4];
- buf[4] = ~D_DISPLAY[i&0XF];
- i=ReadByte(SEC_ADDR,CLOCK_FLAG);
- buf[6] = ~D_DISPLAY[i>>4];
- buf[7] = ~D_DISPLAY[i&0XF];
- buf[5]=buf[2]= ~D_DISPLAY[16];
- i=ReadByte(DATE_ADDR,CLOCK_FLAG);
- WriteByte(CONTRAL_ADDR,0X80,CLOCK_FLAG);//WP = 1
- //Send_Str(buf,10);//for test!
- Display_str(buf);//Do_Led(buf[0]);
- if(stOtherTableInfo.save_date != i)
- {
- stOtherTableInfo.save_date=i;
- con_statusinfo.con_daymoney=0L; //当天消费总金额;
- con_statusinfo.con_dayrecords=0; //当天消费记录数;
- }
- }
- void DisplayDate(void)
- {
- BYTE i,buf[8];
- WriteByte(CONTRAL_ADDR,0X00,CLOCK_FLAG);//WP = 0
- i=ReadByte(YEAR_ADDR,CLOCK_FLAG);
- buf[0] = ~D_DISPLAY[i>>4];
- buf[1] = ~D_DISPLAY[i&0XF];
- i=ReadByte(MOM_ADDR,CLOCK_FLAG);
- buf[3] = ~D_DISPLAY[i>>4];
- buf[4] = ~D_DISPLAY[i&0XF];
- i=ReadByte(DATE_ADDR,CLOCK_FLAG);
- buf[6] = ~D_DISPLAY[i>>4];
- buf[7] = ~D_DISPLAY[i&0XF];
- buf[5]=buf[2]= ~D_DISPLAY[16];
- WriteByte(CONTRAL_ADDR,0X80,CLOCK_FLAG);//WP = 1
- //Send_Str(buf,10);//for test!
- Display_str(buf);//Do_Led(buf[0]);
- }
- /************************************************************
- void Save_My_TestData(BYTE Data) //this is a test!
- {
- static WORD idata test_addr = MY_TEST_ADDR;//this is a test!
- WriteByte(test_addr,Data,RAM_FLAG);
- if(++test_addr>=ROM_MAXSIZE)test_addr = MY_TEST_ADDR;
- }
- /************************************************************/