內含1302PDF文件
C語言源程序
0.png (11.15 KB, 下載次數: 21)
下載附件
2018-11-27 03:08 上傳
單片機源程序如下:
- #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)
2018-11-26 23:36 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|