找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1917|回复: 0
打印 上一主题 下一主题
收起左侧

PIC16F877A单片机+DS1307实时时钟RTC程序与Proteus仿真图

[复制链接]
跳转到指定楼层
楼主
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)


单片机源程序如下:
  1. // LCD module connections
  2. sbit LCD_RS at RB2_bit;
  3. sbit LCD_EN at RB3_bit;
  4. sbit LCD_D4 at RB4_bit;
  5. sbit LCD_D5 at RB5_bit;
  6. sbit LCD_D6 at RB6_bit;
  7. sbit LCD_D7 at RB7_bit;

  8. sbit LCD_RS_Direction at TRISB2_bit;
  9. sbit LCD_EN_Direction at TRISB3_bit;
  10. sbit LCD_D4_Direction at TRISB4_bit;
  11. sbit LCD_D5_Direction at TRISB5_bit;
  12. sbit LCD_D6_Direction at TRISB6_bit;
  13. sbit LCD_D7_Direction at TRISB7_bit;
  14. // End LCD module connections



  15. unsigned short read_ds1307(unsigned short address)
  16. {
  17.   unsigned short r_data;
  18.   I2C1_Start();
  19.   I2C1_Wr(0xD0); //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
  20.   I2C1_Wr(address);
  21.   I2C1_Repeated_Start();
  22.   I2C1_Wr(0xD1); //0x68 followed by 1 --> 0xD1
  23.   r_data=I2C1_Rd(0);
  24.   I2C1_Stop();
  25.   return(r_data);
  26. }


  27. void write_ds1307(unsigned short address,unsigned short w_data)
  28. {
  29.   I2C1_Start(); // issue I2C start signal
  30.   //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0
  31.   I2C1_Wr(0xD0); // send byte via I2C (device address + W)
  32.   I2C1_Wr(address); // send byte (address of DS1307 location)
  33.   I2C1_Wr(w_data); // send data (data to be written)
  34.   I2C1_Stop(); // issue I2C stop signal
  35. }


  36. unsigned char BCD2UpperCh(unsigned char bcd)
  37. {
  38.   return ((bcd >> 4) + '0');
  39. }


  40. unsigned char BCD2LowerCh(unsigned char bcd)
  41. {
  42.   return ((bcd & 0x0F) + '0');
  43. }


  44. int Binary2BCD(int a)
  45. {
  46.    int t1, t2;
  47.    t1 = a%10;
  48.    t1 = t1 & 0x0F;
  49.    a = a/10;
  50.    t2 = a%10;
  51.    t2 = 0x0F & t2;
  52.    t2 = t2 << 4;
  53.    t2 = 0xF0 & t2;
  54.    t1 = t1 | t2;
  55.    return t1;
  56. }


  57. int BCD2Binary(int a)
  58. {
  59.    int r,t;
  60.    t = a & 0x0F;
  61.    r = t;
  62.    a = 0xF0 & a;
  63.    t = a >> 4;
  64.    t = 0x0F & t;
  65.    r = t*10 + r;
  66.    return r;
  67. }



  68. int second;
  69. int minute;
  70. int hour;
  71. int hr;
  72. int day;
  73. int dday;
  74. int month;
  75. int year;
  76. int ap;

  77. unsigned short set_count = 0;
  78. short set;

  79. char time[] = "00:00:00 PM";
  80. char date[] = "00-00-00";

  81. void main()
  82. {
  83.    I2C1_Init(100000); //DS1307 I2C is running at 100KHz

  84.    CMCON = 0x07;   // To turn off comparators
  85.    ADCON1 = 0x06;  // To turn off analog to digital converters

  86.    TRISA = 0x07;
  87.    PORTA = 0x00;

  88.    Lcd_Init();                        // Initialize LCD
  89.    Lcd_Cmd(_LCD_CLEAR);               // Clear display
  90.    Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  91.    Lcd_out(1,1,"Time:");
  92.    Lcd_out(2,1,"Date:");
  93.    
  94.    do
  95.    {
  96.      set = 0;
  97.      if(PORTA.F0 == 0)
  98.      {
  99.          Delay_ms(100);
  100.          if(PORTA.F0 == 0)
  101.          {
  102.              set_count++;
  103.              if(set_count >= 7)
  104.              {
  105.                 set_count = 0;
  106.              }
  107.          }
  108.      }
  109.      if(set_count)
  110.      {
  111.         if(PORTA.F1 == 0)
  112.         {
  113.           Delay_ms(100);
  114.           if(PORTA.F1 == 0)
  115.               set = 1;
  116.         }
  117.         
  118.         if(PORTA.F2 == 0)
  119.         {
  120.           Delay_ms(100);
  121.           if(PORTA.F2 == 0)
  122.               set = -1;
  123.         }
  124.         if(set_count && set)
  125.         {
  126.           switch(set_count)
  127.           {
  128.             case 1:
  129.                     hour = BCD2Binary(hour);
  130.                     hour = hour + set;
  131.                     hour = Binary2BCD(hour);
  132.                     if((hour & 0x1F) >= 0x13)
  133.                     {
  134.                       hour = hour & 0b11100001;
  135.                       hour = hour ^ 0x20;
  136.                     }
  137.                     else if((hour & 0x1F) <= 0x00)
  138.                     {
  139.                       hour = hour | 0b00010010;
  140.                       hour = hour ^ 0x20;
  141.                     }
  142.                     write_ds1307(2, hour); //write hour
  143.                     break;
  144.             case 2:
  145.                      minute = BCD2Binary(minute);
  146.                      minute = minute + set;
  147.                      if(minute >= 60)
  148.                         minute = 0;
  149.                      if(minute < 0)
  150.                         minute = 59;
  151.                      minute = Binary2BCD(minute);
  152.                      write_ds1307(1, minute); //write min
  153.                      break;
  154.             case 3:
  155.                     if(abs(set))
  156.                       write_ds1307(0,0x00); //Reset second to 0 sec. and start Oscillator
  157.                     break;
  158.             case 4:
  159.                      day = BCD2Binary(day);
  160.                      day = day + set;
  161.                      day = Binary2BCD(day);
  162.                      if(day >= 0x32)
  163.                         day = 1;
  164.                      if(day <= 0)
  165.                         day = 0x31;
  166.                      write_ds1307(4, day); // write date 17
  167.                      break;
  168.             case 5:
  169.                     month = BCD2Binary(month);
  170.                     month = month + set;
  171.                     month = Binary2BCD(month);
  172.                     if(month > 0x12)
  173.                       month = 1;
  174.                     if(month <= 0)
  175.                       month = 0x12;
  176.                     write_ds1307(5,month); // write month 6 June
  177.                     break;
  178.             case 6:
  179.                     year = BCD2Binary(year);
  180.                     year = year + set;
  181.                     year = Binary2BCD(year);
  182.                     if(year <= -1)
  183.                        year = 0x99;
  184.                     if(year >= 0x50)
  185.                        year = 0;
  186.                     write_ds1307(6, year); // write year
  187.                     break;
  188.           }
  189.         }
  190.      }

  191.       second = read_ds1307(0);
  192.       minute = read_ds1307(1);
  193.       hour = read_ds1307(2);
  194.        hr = hour & 0b00011111;
  195.        ap = hour & 0b00100000;
  196.       dday = read_ds1307(3);
  197.       day = read_ds1307(4);
  198.       month = read_ds1307(5);
  199.       year = read_ds1307(6);


  200.       time[0] = BCD2UpperCh(hr);
  201.       time[1] = BCD2LowerCh(hr);
  202.       time[3] = BCD2UpperCh(minute);
  203.       time[4] = BCD2LowerCh(minute);
  204. ……………………

  205. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei附件下载:
Digital-Clock.zip (308.52 KB, 下载次数: 31)

51hei.png (14.7 KB, 下载次数: 76)

51hei.png

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶1 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表