PIC16F877A DS1302 chip driver

Publisher:平静宁静Latest update time:2016-11-04 Source: eefocusKeywords:PIC16F877A  DS1302 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
  1. #include  
  2. typedef unsigned char uchar;  
  3. typedef unsigned int uint;  
  4. #define rs_h PORTC|=0x01  
  5. #define rs_l PORTC&=0xfe  
  6. #define rw_h PORTC|=0x02  
  7. #define rw_l PORTC&=0xfd  
  8. #define en_h PORTC|=0x04   
  9. #define en_l PORTC&=0xfb  
  10. #define rst_h PORTC|=0x08  
  11. #define rst_l PORTC&=0xf7  
  12. #define sck_h PORTC|=0x10  
  13. #define sck_l PORTC&=0xef  
  14. #define io_h PORTC|=0x20  
  15. #define io_l PORTC&=0xdf  
  16. #define SECADD 0X80 //Second register address  
  17. #define MINADD 0x82  
  18. #define HRADD 0x84  
  19. #define DATEADD 0x86  
  20. #define MONTHADD 0x88  
  21. #define DAYADD 0x8a  
  22. #define YEARADD 0x8c  
  23. #define CONTROLADD 0x8e  
  24. #define PORT (PORTB)  
  25. void delayms(uint x);  
  26. void lcd_com(uchar com);  
  27. void lcd_dat(uchar dat);  
  28. void lcd_write(fly c,fly r,fly dat);  
  29. void lcd_init(void);  
  30. void Ds1302Init(void);  
  31. void WriteByte(volatile dat);  
  32. void ReadByte(void);  
  33. float BCDtoDex(float dat);  
  34. float DextoBCD(float dat);  
  35. void ReadByte(void);  
  36. void Ds1302Write(uchar add,uchar dat);  
  37. fly Ds1302Read(fly add);  
  38.   
  39. void main(void)  
  40. {  
  41.     uchar sec,min,hr,date,month,day,year;  
  42.     TRISB=0x00;  
  43.     TRISC&=0xc0;  
  44.     lcd_init();  
  45.     Ds1302Init();  
  46.     while(1)  
  47.     {  
  48.         sec=Ds1302Read(SECADD);  
  49.         min=Ds1302Read(MINADD);  
  50.         hr=Ds1302Read(HRADD);  
  51.         date=Ds1302Read(DATEADD);  
  52.         month=Ds1302Read(MONTHADD);  
  53.         day=Ds1302Read(DAYADD);  
  54.         year=Ds1302Read(YEARADD);  
  55.   
  56.         sec=BCDtoDex(sec);  
  57.         min=BCDtoDex(min);  
  58.         hr=BCDtoDex(hr);  
  59.         date=BCDtoDex(date);  
  60.         month=BCDtoDex(month);  
  61.         day=BCDtoDex(day);  
  62.         year=BCDtoDex(year);  
  63.   
  64.         lcd_write(0,1,0x32);  
  65.         lcd_write(0,2,0x30);  
  66.         lcd_write(0,3,0x30+year/10);  
  67.         lcd_write(0,4,0x30+year%10);  
  68.         lcd_write(0,6,0x30+month/10);  
  69.         lcd_write(0,7,0x30+month%10);  
  70.         lcd_write(0,9,0x30+date/10);  
  71.         lcd_write(0,10,0x30+date%10);  
  72.         lcd_write(0,14,0x30+day);  
  73.         lcd_write(1,4,0x30+hr/10);  
  74.         lcd_write(1,5,0x30+hr%10);  
  75.         lcd_write(1,7,0x30+min/10);  
  76.         lcd_write(1,8,0x30+min%10);  
  77.         lcd_write(1,10,0x30+sec/10);  
  78.         lcd_write(1,11,0x30+sec%10);  
  79.         delayms(1005);//1ms  
  80.     }  
  81.   
  82. }  
  83. void Ds1302Init(void)  
  84. {  
  85.     rst_l;  
  86.     sck_l;  
  87.     Ds1302Write(CONTROLADD,0); //turn off write protection  
  88.     Ds1302Write(MINADD,DextoBCD(06));  
  89.     Ds1302Write(HRADD,DextoBCD(19));  
  90.     Ds1302Write(DATEADD,DextoBCD(20));  
  91.     Ds1302Write(MONTHADD,DextoBCD(5));  
  92.     Ds1302Write(ONEADD,DextoBCD(7));  
  93.     Ds1302Write(YEARADD,DextoBCD(12));  
  94.     Ds1302Write(SECADD,DextoBCD(30)); //Write seconds and start the clock at the same time  
  95. }  
  96. void WriteByte(uchar dat)//Rising edge output  
  97. {  
  98.     flying i;  
  99.     TRISC&=0xdf;  
  100.     for(i=8;i>0;i--)  
  101.     {  
  102.         sck_l; //The data line is variable when the level is low  
  103.         if(dat&0x01)  
  104.             i_h;  
  105.         else  
  106.             I l;  
  107.         sck_h;  
  108.         that>>=1;  
  109.     }  
  110. }  
  111. uchar BCDtoDex(uchar dat) //BCD code to decimal  
  112. {  
  113.     flying i;  
  114.     i=that/16;  
  115.     that%=16;  
  116.     i=i*10+dat;  
  117.     return i;  
  118. }  
  119. uchar DextoBCD(uchar dat) //Decimal to BCD code  
  120. {  
  121.     flying i;  
  122.     i=that/10;  
  123.     that%=10;  
  124.     i=i*16+dat;  
  125.     return i;  
  126. }  
  127. uchar ReadByte(void) // falling edge output  
  128. {  
  129.     flying i;  
  130.     flying dat;  
  131.     TRISC|=0x20;  
  132.     for(i=8;i>0;i--)//Consider the state after the previous writing. The first time entering this loop body, there is a falling edge.  
  133.     {  
  134.         that>>=1;  
  135.         sck_l;  
  136.         if(PORTC&0x20)  
  137.             that|=0x80;  
  138.         sck_h;    
  139.     }  
  140.     return that;  
  141. }  
  142. void Ds1302Write(uchar add,uchar dat)  
  143. {  
  144.     rst_l;  
  145.     sck_l;  
  146.     rst_h;  
  147.     WriteByte(add); //The lowest position 0 means write  
  148.     WriteByte(that);  
  149.     rst_l;  
  150. }  
  151. fly Ds1302Read(fly add)  
  152. {  
  153.     flying dat;  
  154.     rst_l;  
  155.     sck_l;  
  156.     rst_h;  
  157.     WriteByte(add+1); //The lowest address position 1 indicates read  
  158.     dat=ReadByte();  
  159.     rst_l;  
  160.     return that;  
  161. }  
  162. void delayms(uint x) //8M crystal oscillator, delay 1ms  
  163. {  
  164.     uint y,z;  
  165.     for(y=x;y>0;y--)  
  166.         for(z=220;z>0;z--);  
  167. }  
  168. void lcd_init(void) //LCD1602 initialization  
  169. {  
  170.     lcd_com(0x38);  
  171.     lcd_com(0x0c);  
  172.     lcd_com(0x06);  
  173.     lcd_write(0,5,0xb0);  
  174.     lcd_write(0,8,0xb0);  
  175.     lcd_write(1,6,0x3a);  
  176.     lcd_write(1,9,0x3a);  
  177. }  
  178. void lcd_com(uchar com) //write command to LCD1602  
  179. {  
  180.     rs_l;  
  181.     rw_l;  
  182.     PORT=com;  
  183.     delayms(1);  
  184.     and_h;  
  185.     delayms(1);   
  186.     en_l;  
  187. }  
  188. void lcd_dat(uchar dat) //write data to LCD1602  
  189. {  
  190.     rs_h;  
  191.     rw_l;  
  192.     PORT=that;  
  193.     delayms(1);  
  194.     and_h;  
  195.     delayms(1);  
  196.     en_l;  
  197. }  
  198. void lcd_write(uchar c,uchar r,uchar dat) // specify row, specify column, write data to LCD1602  
  199. {  
  200.     lcd_com(0x80+0x40*c+r);  
  201.     lcd_dat(dat);  
  202.     delayms(1);  
  203. }  

Keywords:PIC16F877A  DS1302 Reference address:PIC16F877A DS1302 chip driver

Previous article:PIC16F877A sleep mode
Next article:PIC16F877A external interrupt RB0, RA0 lights up the LED

Recommended ReadingLatest update time:2024-11-16 21:23

[C51 code] DS1302 driver
/*********************ds1302.c**********************/ #include "Atmel/AT89X51.h" #include "link.h" #include "ds1302.h" /*******************Variable definition********************/ uchar code WeekTab = { //Leap year month and week table                          (3 5) + 31, //January                           (6
[Microcontroller]
Voltage output and display based on pic16f877a
  The program source code is as follows #include pic.h //Pin definition #define din RC7 //Display serial data input terminal #define load RC6 //Display data latch terminal #define clk RC5 //Display clock input #define DecodeMode 0x09 //Decoding mode #define Intensity 0x0a //Brightness #define ScanLimit 0x0b //Sca
[Microcontroller]
Voltage output and display based on pic16f877a
ATmega128(DS1302)
//DS1302 real-time clock chip driver for ATmega128 //Compilation environment AVR Studio 4.17/AVR GCC //System clock 7.3728MHZ, set fuse bits to external high-frequency quartz crystal oscillator, startup time 4.1ms //Author: Zhang  Qizhangqi123zq@126.com //Date: 2010.02.20 //**************************************
[Microcontroller]
ds1302 real time clock
ds1302 real time clock   There are many popular serial clock circuits now, such as DS1302, DS1307, PCF8485, etc. These circuits have simple interfaces, low prices, and are easy to use, and are widely used. The real-time clock circuit DS1302 introduced in this article is a circuit with trickle current charging capabi
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号