发布论文 | 上传资料 | 发布供求 | 发布求职 | 发布项目 | 加入收藏 | RSS
您当前的位置:首页 > 文章中心 > 单 片 机 > 51单片机

DS1302 C语言驱动程序

时间:2008-08-02 10:51:02  来源:  作者: 点击:6

DS1302 C语言驱动程序--LCD1602 显示    

//DS1302 RTC drive program
//for 51 mcu with LCD1602 display
//designed by zhaoliang
//2005-6-16 18:30
#i nclude <reg51.h>
#i nclude <intrins.h>

//lcd part
/***********************************************************************/
#define LINE1     0
#define LINE2     1
#define LINE1_HEAD    0x80
#define LINE2_HEAD    0xC0
#define LCD_DELAY_TIME   40
#define DATA_MODE    0x38
#define OPEN_SCREEN    0x0C
#define DISPLAY_ADDRESS   0x80
#define CLEARSCREEN    LCD_en_command(0x01)
// common PreDefinition
#define HIGH     1
#define LOW      0
#define TRUE      1
#define ZERO      0
#define MSB       0x80
#define LSB       0x01
// ds1302 PreDefinition
#define DS1302_WP    0x8E
#define DS1302_RESET   RST=LOW;SCL=LOW;RST=HIGH
#define DS1302_WP_ENABLE Write_Ds1302(DS1302_WP,0X00)
#define DS1302_WP_DISENABLE Write_Ds1302(DS1302_WP,0x80)

/***********************************************************************/
#define LCDIO     P2
sbit LCD1602_RS=P0^7;   //data command select 1 data 0 command pin 4
sbit LCD1602_RW=P0^6;   //read write select   1 read   0 write     pin 5
sbit LCD1602_EN=P0^5;   //LCD enable signal             pin 6

sbit SCL = P1^6;// DS1302 Serial-Clock Input pin 7
sbit SDA = P1^7;// DS1302 Serial-Data Input pin 6
sbit RST = P1^4;// DS1302 Chip-Seclet Input pin 5
/***********************************************************************/    
void LCD_delay(void);//lcd delay function
void LCD_en_command(unsigned char command);//write command function
void LCD_en_dat(unsigned char temp);//write data function
void LCD_set_xy( unsigned char x, unsigned char y );//set display address function
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat);//write lcd a character function
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);//write lcd string function
void LCD_init(void);//lcd initize function

void Write_Ds1302_byte(unsigned char temp);
void Write_Ds1302( unsigned char address,unsigned char dat );
unsigned char Read_Ds1302 ( unsigned char address );

void Read_RTC(void);//read RTC
void Set_RTC(void);//set RTC

void Initial(void);//system initize function
void Display(void);//RTC display function

/***********************************************************************/
code unsigned char set_rtc_code[7]={0x00,0x30,0x18,0x04,0x16,0x06,0x05};
code unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
code unsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
code unsigned char *day[7]={"Mon","Tue","Wen","Thu","Fri","Sat","Sun"};
unsigned char read_rtc_code[7];

/***********************************************************************/


/***********************************************************************/
/***********************************************************************/
void main(void)
{
Initial();
while(1)
{
Read_RTC();
Display();
}
}

/****************************************************************************/
/****************************************************************************/
void Initial(void)     
{
LCD_init();
DS1302_WP_ENABLE;
Set_RTC();
DS1302_WP_DISENABLE;
}
/****************************************************************************/

/****************************************************************************/
/****************************************************************************/
void Write_Ds1302_Byte(unsigned char temp)
{
unsigned char i;
for (i=0;i<8;i++)    
{
   SCL=LOW;
     SDA=temp&LSB;     
     temp>>=1;
     SCL=HIGH;
   }
}  
/****************************************************************************/
void Write_Ds1302( unsigned char address,unsigned char dat )    
{
DS1302_RESET;
Write_Ds1302_Byte(address);
Write_Ds1302_Byte(dat);
RST=LOW;
}
/****************************************************************************/
unsigned char Read_Ds1302 ( unsigned char address )
{
unsigned char i,temp=0x00,temp_temp;
DS1302_RESET;
Write_Ds1302_Byte(address);
for (i=0;i<8;i++)
{
if(SDA)
   temp|=0x80;
SCL=LOW;
   temp>>=1;
SCL=HIGH;
}
RST=LOW;
temp_temp=temp/16;
temp=temp%16;
temp=temp+temp_temp*10;
return (temp);
}
/****************************************************************************/
void Read_RTC(void)
{
unsigned char i,*p;
p=read_rtc_address;
for(i=0;i<7;i++)
{
read_rtc_code[i]=Read_Ds1302(*p);
p++;
}
}
/***********************************************************************/
void Set_RTC(void)
{
unsigned char i,*p;
p=write_rtc_address;
for(i=0;i<7;i++)
{
Write_Ds1302(*p,set_rtc_code[i]);
p++;
}
}

/***********************************************************************/
/******************** LCD PART START *******************************/
void LCD_delay(void)  
{
unsigned char i;
for(i=LCD_DELAY_TIME;i>ZERO;i--);
}
/********************************************************************/
void LCD_en_command(unsigned char command)
{
LCDIO=command;
LCD1602_RS=LOW;  
LCD1602_RW=LOW;
LCD1602_EN=LOW;
LCD_delay();
LCD1602_EN=HIGH;
}
/********************************************************************/
void LCD_en_dat(unsigned char dat)
{
LCDIO=dat;
LCD1602_RS=HIGH;
LCD1602_RW=LOW;
LCD1602_EN=LOW;
LCD_delay();
LCD1602_EN=HIGH;
}
/********************************************************************/
void LCD_set_xy( unsigned char x, unsigned char y )
{
unsigned char address;
if (y == LINE1)
address = LINE1_HEAD + x;
else
     address = LINE2_HEAD + x;
LCD_en_command(address);
}
/********************************************************************/
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat)
{
LCD_set_xy( x, y );
LCD_en_dat(dat);
}
/********************************************************************/
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)
{
    LCD_set_xy( X, Y ); //set address
    while (*s) // write character
    {
     LCDIO=*s;
        LCD_en_dat(*s);  
s ++;
    }
}
/********************************************************************/
void LCD_init(void)
{
CLEARSCREEN;//clear screen
LCD_en_command(DATA_MODE);//set 8 bit data transmission mode
LCD_en_command(OPEN_SCREEN);//open display (enable lcd display)
LCD_en_command(DISPLAY_ADDRESS);//set lcd first display address
CLEARSCREEN;//clear screen
}
/******************** LCD PART STOP *******************************/
/********************************************************************/
/******************** OTHER PART   ******************************/
void Display(void)     
{
LCD_write_char(0x0f,LINE2,(read_rtc_code[0]%10)|0x30);
LCD_write_char(0x0e,LINE2,(read_rtc_code[0]/10)|0x30);
LCD_write_char(0x0c,LINE2,(read_rtc_code[1]%10)|0x30);
LCD_write_char(0x0b,LINE2,(read_rtc_code[1]/10)|0x30);
LCD_write_char(0x09,LINE2,(read_rtc_code[2]%10)|0x30);
LCD_write_char(0x08,LINE2,(read_rtc_code[2]/10)|0x30);
LCD_write_char(0x09,LINE1,(read_rtc_code[4]%10)|0x30);
LCD_write_char(0x08,LINE1,(read_rtc_code[4]/10)|0x30);
LCD_write_char(0x06,LINE1,(read_rtc_code[5]%10)|0x30);
LCD_write_char(0x05,LINE1,(read_rtc_code[5]/10)|0x30);
LCD_write_char(0x03,LINE1,(read_rtc_code[6]%10)|0x30);
LCD_write_char(0x02,LINE1,(read_rtc_code[6]/10)|0x30);

LCD_write_string(0x0d,LINE1,day[read_rtc_code[3]-1]);
LCD_write_string(0x0d,LINE2,":");
LCD_write_string(0x0a,LINE2,":");
LCD_write_string(0x07,LINE1,"-");
LCD_write_string(0x04,LINE1,"-");
LCD_write_string(0x00,LINE1,"20");
}

来顶一下
近回首页
返回首页
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表
推荐资讯
音乐程序的设计原理之单片机
音乐程序的设计原理之
FPGA的可编程全数字锁相环路实现
FPGA的可编程全数字锁
什么是模拟电路?
什么是模拟电路?
挪威发明蛇形消防机器人
挪威发明蛇形消防机器
相关文章
栏目更新
栏目热门