内含1302PDF文件
C语言源程序
单片机源程序如下:
- #include<reg52.h>
- #define uchar unsigned char
- #define unit unsigned int
- #define DS1302_WRITE 0x80
- #define DS1302_READ 0x81
- uchar time[]={0,0,8};
- uchar code tab0[]={"11-21 : : "};
- uchar code tab1[]={"STATE: "};
- sbit en=P3^3;
- static uchar string[3],state[3];
- sbit RS=P1^5;
- sbit RW=P1^6;
- sbit E=P1^7;
- sbit busy=P2^7;
- sbit ce =P1^0;
- sbit scl=P1^1;
- sbit sda=P1^2;
- /****
- ******* 延时函数
- *****/
- void delay(unit x)
- {
- while(x--);
- }
- /****
- ******* 串口初始化函数
- *****/
- void uart_init()
- {
- TMOD=0x20; //9600bps
- SCON=0x50;
- TH1=0xFD;
- TL1=0xFD;
- TR1=1;
- ES=1;
- EA=1;
- en=0; //串口一直处于接收状态
- }
- /****
- ********1602检测忙函数
- *****/
- void check_busy()
- {
- P2=0xff;
- do
- {
- RS=0;
- RW=1;
- E=0;
- E=1;
- } while(busy==1);
- E=0;
- }
- /****
- *******1602写数据函数
- *****/
- void lcd_write_dat(uchar dat)
- {
- check_busy();
- P2=dat;
- delay(5);
- RS=1;
- RW=0;
- E=1;
- delay(5);
- E=0;
- delay(5);
- }
- /****
- *******1602写命令函数
- *****/
- void lcd_write_com(uchar com)
- {
- check_busy();
- P2=com;
- delay(5);
- RS=0;
- RW=0;
- E=1;
- delay(5);
- E=0;
- delay(5);
- }
- /****
- *******1602写字符串函数
- *****/
- void lcd_write_str(uchar add,uchar *dat)
- {
- lcd_write_com(add);
- while(*dat != '\0')
- {
- lcd_write_dat(*dat++);
-
- }
- }
- /****
- *******1602写时间函数
- *****/
- void lcd_write_time(uchar add,uchar date)
- {
- uchar shi,ge;
- shi=date/16;
- ge=date%16;
- lcd_write_com(add);
- lcd_write_dat(shi+'0');
- lcd_write_dat(ge+'0');
- }
- /****
- *******1602初始化函数
- *****/
- void lcd_init()
- {
- lcd_write_com(0x38);
- delay(20000);
- lcd_write_com(0x0c);
- delay(20000);
- lcd_write_com(0x06);
- delay(20000);
- lcd_write_com(0x01);
- delay(20000);
- }
- /****
- ******* DS1302复位函数
- *****/
- void DS1302_ret()
- {
- ce=0;
- scl=0;
- ce=1;
- }
- /****
- ******* DS1302写一个字节函数
- *****/
- void DS1302_write_byte(uchar byte)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- sda=byte&0x01;
- scl=1;
- byte>>=1;
- scl=0;
- }
- }
- /****
- ******* DS1302读一个字节函数
- *****/
- uchar DS1302_read_byte()
- {
- uchar i,byte=0;;
- for(i=0;i<8;i++)
- {
- byte>>=1;
- if(sda)
- byte|=0x80;
- scl=1;
- scl=0;
- }
- return byte;
- }
- /****
- ******* DS1302设置些保护函数
- *****/
- void DS1302_set_WP()
- {
- DS1302_ret();
- DS1302_write_byte(0x8e);
- DS1302_write_byte(0x80);
- sda=0;
- ce=0;
- }
- /****
- ******* DS1302删除保护函数
- *****/
- void DS1302_chear_WP()
- {
- DS1302_ret();
- DS1302_write_byte(0x8e);
- DS1302_write_byte(0);
- sda=0;
- ce=0;
- }
- /****
- ******* DS1302写地址与数据函数
- *****/
- void DS1302_write_date(uchar add,uchar date)
- {
- DS1302_ret();
- DS1302_write_byte(add);
- DS1302_write_byte(date);
- sda=0;
- ce=0;
- }
- /****
- ******* DS1302指定地址读数据函数
- *****/
- uchar DS1302_read_date(uchar add)
- {
- uchar date=0;
- DS1302_ret();
- DS1302_write_byte(add);
- date=DS1302_read_byte();
- sda=0;
- ce=0;
- return (date);
- }
- /****
- ******* DS1302设定时间函数
- *****/
- void DS1302_set_time(uchar *timedate)
- {
- uchar i,temp;
- for(i=0;i<7;i++)
- {
- temp=timedate[i]/10;
- timedate[i]=timedate[i]%10;
- timedate[i]=timedate[i]+temp*16;
- }
- DS1302_chear_WP();
- temp=DS1302_WRITE;
- for(i=0;i<7;i++)
- {
- DS1302_write_date(temp,timedate[i]);
- temp += 2;
- }
- DS1302_set_WP();
- }
- /****
- ******* DS1302读出时间函数
- *****/
- void DS1302_read_time(uchar *timedate)
- {
- uchar i,temp=0;
- temp=DS1302_READ;
- for(i=0;i<7;i++)
- {
- timedate[i]=DS1302_read_date(temp);
- temp += 2;
- }
- }
- /****
- ******* 主函数
- *****/
- int main()
- {
- uart_init();
- lcd_init();
- lcd_write_str(0x80,tab0);
- delay(20000);
- lcd_write_str(0xc0,tab1);
- DS1302_set_time(&time); //设置时钟初值
- while(1)
- {
- DS1302_read_time(&time);
- lcd_write_time(0x87,time[2]); //时
- lcd_write_time(0x8a,time[1]); //分
- lcd_write_time(0x8d,time[0]); //秒
- lcd_write_com(0xc8);
- lcd_write_dat(state[0]);
- lcd_write_dat(state[1]);
- lcd_write_dat(state[2]);
- }
- }
- /****
- ******* 串口中断服务程序函数
- *****/
- void ser() interrupt 4
- {
- uchar i;
- if(RI)
- {
- RI=0;
- string[i++]=SBUF;
- if(i>2)
- i=0;
- }
- state[i]=string[i];
- }
复制代码
所有资料51hei提供下载:
DS1302.rar
(453.71 KB, 下载次数: 12)
|