#include "at32l021_board.h"
#include "at32l021_clock.h"
#include "OLED.h"
/** @addtogroup AT32L021_periph_examples
* @{
*/
#define uint unsigned int
#define u8 unsigned char
u8 const write_addr[]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; //写的地址
u8 const read_addr[]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d}; //读的地址
u8 const read[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d}; //读的地址
u8 const write[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};//秒分时日月周年,写的地址
u8 time[7]={0x40,0x59,0x23,0x31,0x12,0x07,0x23};//存读的数据
//MS延时函数
void MS_delay(uint MS)
{
uint x,y;
for(y=MS;y>0;y--)
{
for(x=796;x>0;x--);
}
}
//---存储顺序是秒分时日月周年,存储格式是用BCD码---//
//uchar time[7]={0,0,0x12,0x12,0x12,0x20,0x15}; //初始化的时间为2015年12月12日12:00
void Ds1302Write(u8 address,u8 date)
{
u8 n;
//ce=0;
at32_ds1302_off(RST1);
//sclk=0;//
at32_ds1302_off(CLK);
//ce=1;
at32_ds1302_on(RST1);
for (n=0;n<8;n++) //写地址
{
//io=address & 0x01 ; //只保留1位
if(address & 0x01)
at32_ds1302_on(DAT);
else
at32_ds1302_off(DAT);
address>>=1; //右移一位
//sclk=1; //写
at32_ds1302_on(CLK);
//sclk=0;
at32_ds1302_off(CLK);
}
for (n=0; n<8; n++)//写数据
{
//io=date & 0x01;
if(date & 0x01)
at32_ds1302_on(DAT);
else
at32_ds1302_off(DAT);
date>>=1;
//sclk=1;
at32_ds1302_on(CLK);
//sclk=0;
at32_ds1302_off(CLK);
}
//ce=0;
at32_ds1302_off(RST1);
}
u8 Ds1302Read(u8 address)
{
u8 n,date,date1;
//ce=0;
//sclk=0;
//ce=1;
at32_ds1302_off(RST1);
//sclk=0;//
at32_ds1302_off(CLK);
//ce=1;
at32_ds1302_on(RST1);
for(n=0;n<8;n++) //发地址
{
//io=address & 0x01;
if(address & 0x01)
at32_ds1302_on(DAT);
else
at32_ds1302_off(DAT);
address>>= 1;
//sclk= 1;
//sclk= 0;
at32_ds1302_on(CLK);
at32_ds1302_off(CLK);
}
//TRISCbits.TRISC5=1;
IIC_INPUT_MODE_SET();
for(n=0;n<10;n++);
for(n=0;n<8;n++) //收数据
{
//date1=PORTCbits.RC5;
date1=at32_datin_state();
date=(date>>1)|(date1<<7);//存入date中
//sclk=1;
at32_ds1302_on(CLK);
at32_ds1302_off(CLK);
//sclk=0;
}
//ce=0;
//sclk=1;//保证1302稳定,必须的
//io=0;
//io=1;
at32_ds1302_off(RST1);
at32_ds1302_on(CLK);
at32_ds1302_off(DAT);
at32_ds1302_on(DAT);
//TRISCbits.TRISC5=0;
at32_ds1302_init(DAT);
for(n=0;n<10;n++);
return date;
}
void Ds1302ReadTime()//读时间
{
u8 n;
for(n=0;n<7;n++)//读取7个字节的时钟信号:分秒时日月周年
{
time[n] =Ds1302Read(read[n]);
}
}
void Ds1302Init()
{
u8 n;
Ds1302Write(0x8E,0x00); //8E为写保护寄存器地址,00为不写保护
for(n=0;n<7;n++) //
{
Ds1302Write(write[n],time[n]); //地址加数据
}
Ds1302Write(0x8E,0x80); //开写保护
}
/**
* [url=home.php?mod=space&uid=247401]@brief[/url] main function.
* @param none
* @retval none
*/
int main(void)
{
u8 i,j;
system_clock_config();
at32_board_init();
LCD_Init();
LCD_CLS();
Ds1302Init();
//LCD_P8x16Str(45,5,(u8*)"EASEAR");
//LCD_P6x8Str(0,7,(u8*)"www.holteksupport.com");
LCD_P8x16Str(0,0,(u8*)" : : ");
while(1)
{
Ds1302ReadTime();
i=(time[0]>>4)+'0';
j=(time[0]&0x0f)+'0';
LCD_P8x16Char(68,6,i);
LCD_P8x16Char(76,6,j);
i=(time[1]>>4)+'0';
j=(time[1]&0x0f)+'0';
LCD_P8x16Char(44,6,i);
LCD_P8x16Char(52,6,j);
i=(time[2]>>4)+'0';
j=(time[2]&0x0f)+'0';
LCD_P8x16Char(20,6,i);
LCD_P8x16Char(28,6,j);
i=(time[3]>>4)+'0';
j=(time[3]&0x0f)+'0';
LCD_P8x16Char(84,4,i);
LCD_P8x16Char(92,4,j);
i=(time[4]>>4)+'0';
j=(time[4]&0x0f)+'0';
LCD_P8x16Char(60,4,i);
LCD_P8x16Char(68,4,j);
i=(time[6]>>4)+'0';
j=(time[6]&0x0f)+'0';
LCD_P8x16Char(36,4,i);
LCD_P8x16Char(44,4,j);
i=time[5]+'0';
LCD_P8x16Char(36,2,i);
MS_delay(500);
// at32_led_toggle(CLK);
// at32_led_toggle(RST1);
// at32_led_toggle(DAT);
delay_ms(200);
//delay_ms(200);
}
}
运行效果图: