|
#include <iom16v.h>
#include <macros.h>
#define uint unsigned int
#define uchar unsigned char
#define RS_0 PORTD&0xfe
#define RS_1 PORTD|0x01
#define E_0 PORTD&0xfd
#define E_1 PORTD|0x02
#define RW_0 PORTD&0xfb
#define RW_1 PORTD|0x04
/*读写控制*/
#define cmd_read 0x01
#define cmd_write 0x00
/*时钟日期命令*/
#define cmd_second 0x80
#define cmd_minute 0x82
#define cmd_hour 0x84
#define cmd_day 0x86
#define cmd_month 0x88
#define cmd_week 0x8a
#define cmd_year 0x8c
#define cmd_control 0x8e
//写保护
#define cmd_trickle_charge 0x90
//充电控制
#define cmd_clocl_burst 0xbe
//连续写
/*时钟配置位*/
#define clock_halt 0x80
#define _12-24_slect 0x80
#define AM_PM_slect 0x20
#define protect 0x80
#define unprotect 0x00
/*充电控制*/
#define CFG_TC_D1R2 0xA5 //high 1 Diode +2K Resistors
#define CFG_TC_D2R8 0xAB //low 2 Diodes+8K Resistors
#define CFG_TC_DISABLED 0x00 //Disabled(TCS<>1010 or DS=00 or RS=00
/*RAM命令*/
#define cmd_RAM_base 0xc0
#define cmd_RAM_burst 0xfe
/*管脚定义*/
#define CE 4
#define MOSI 5
#define MISO 6
#define SCK 7
#define DS1302_ON PORTB|=(1<<4)
//使能DS1302
#define DS1302_OFF PORTB&=~(1<<4)
//关闭DS1302
uchar CURDATE[7];
uchar Timer[2]={0,0};
uchar DS1302_EXIST;
uchar table[10]={0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f};
uchar DATA[4]={0,0,0,0};
uchar init_data[8]={0x50,0x59,0x23,0x06,0x10,0x04,0x05,0x80};
/*************************************************************************************/
void delay(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
{
for(j=1141;j!=0;j--)
{;}
}
}
/****************************************************************************************/
void init_SPI(void) //初始化SPI
{
SPCR=(1<<SPIE)|(1<<MSTR)|(1<<DORD)|(1<<SPR0);
}
/****************************************************************************************/
uchar Write_data_DS1302(uchar command,uchar wdata) // <------向DS1302读写数据
{
uchar rdata;
DS1302_ON;
delay(10);
SPDR=command;
while(!(SPSR&(1<<SPIF)));
delay(10);
SPDR=wdata;
while(!(SPSR&(1<<SPIF)));
rdata=SPDR;
DS1302_OFF;
return rdata;
}
/*******************************************************************************************/
void Burst_data(uchar CMD,uchar len,uchar *pwdata,uchar *prdata) //<-----读写一批数据到DS1302
{
DS1302_ON;
delay(10);
SPDR=CMD;
while(!(SPSR&(1<<SPIF)));
delay(10);
while(len--)
{
SPDR=*pwdata++;
while(!(SPSR&(1<<SPIF)));
*prdata++=SPDR;
}
DS1302_OFF;
delay(10);
}
/************************************************************************************/
uchar check_RTC(void) //<---------------检测DS1302
{
uchar i;
Write_data_DS1302(cmd_control|cmd_write,unprotect);
Write_data_DS1302(cmd_RAM_base|cmd_write,0xA5);
Write_data_DS1302(cmd_control|cmd_write,protect);
i=Write_data_DS1302(cmd_RAM_base|cmd_read,0x00);
if(i==0xA5) return 1;
else return 0;
}
/***************************************************************************************/
void init_RTC(void) //<----------------初始化DS1302
{
uchar temp[8];
Write_data_DS1302(cmd_control|cmd_write,unprotect);
Burst_data(cmd_clocl_burst|cmd_write,8,&init_data[0],&temp[0]);
Write_data_DS1302(cmd_trickle_charge|cmd_write,0xA5);
Write_data_DS1302(cmd_control|cmd_write,protect);
}
/**************************************************************************************/
void BCD_to_ASCII(uchar cBCD,uchar *pstr)
{
uchar i,j;
i=cBCD>>4;
j=cBCD&0x0f;
*pstr++=i+0x30;
*pstr=j+0x30;
}
/*************************************************************************************
void decodeWEEK(uchar WEEK,uchar *pstr)
{
uchar i;
i=WEEK;
i&=0x07;
i=i<<1;
*pstr++=sWEEK[i++];
*pstr=sWEEK;
}
/*****************************************************************************/
void read_date(void)
{
Burst_data(cmd_clocl_burst|cmd_read,7,0,CURDATE); //读取当前时间
BCD_to_ASCII(CURDATE[0],&Timer[0]); //分 ,00~59
BCD_to_ASCII(CURDATE[1],&Timer[1]); //秒 ,00~59
}
void display(uchar *p)
{
uchar i;
uchar sel=0x01;
for(i=0;i<4;i++)
{
PORTC=sel;
PORTD=table[p];
delay(2);
sel=sel<<1;
}
}
/**************************************************************************************/
void process(uchar *p1,uchar *p2)
{
p2[0]=p1[0]/10;
p2[1]=p1[0]%10;
p2[2]=p1[1]/10;
p2[3]=p1[1]%10;
}
/****************************************************************************************/
void main(void)
{
DDRC=0xff;
PORTC=0xff;
DDRD=0xff;
PORTD=0xff;
DDRB =(1<<4)|(1<<7)|(1<<6);
PORTB=~((1<<4)|(1<<5)|(1<<6)|(1<<7));
init_SPI();
delay(100);
init_RTC();
delay(500);
while(1)
{
read_date();
process(Timer,DATA);
display(DATA);
}
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)
|