找回密码
 立即注册

QQ登录

只需一步,快速开始

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

mickoC 编写pic18f45k22读取i2c接口RTC pcf8583时间显示在数码管,附仿真。

[复制链接]
跳转到指定楼层
楼主
mickoC 编写pic18f45k22读取i2c接口RTC pcf8583时间显示在数码管,附仿真。
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)


单片机源程序如下:
  1. /*
  2. * Project name:
  3.      Seven Segment Display (The 'Hello World' example for the Seven Segment Display)
  4. * Copyright:
  5.      (c) Mikroelektronika, 2011.
  6. * Revision History:
  7.      20110929:
  8.        - initial release (FJ);
  9. * Description:
  10.      This code demonstrates how to display number on one 7-segment display
  11.      (common cathode). Display is connected to PORTD (RD0..RD7, segment A to
  12.      RD0, segment B to RD1, etc); common cathode is connected to the pin RA0 on
  13.      PORTA. Number is incremented every 1s.

  14. * NOTES:
  15.      - Turn on Seven Segment Display switches SW4.1, SW4.2, SW4.3 and SW4.4. (board specific)
  16. */

  17. #include "Display_Utils.h"

  18. //unsigned short  portd_index;

  19. char seconds, minutes, hours, day, month, year; // Global date/time variables
  20. char i;
  21. // Software I2C connections
  22. /*sbit Soft_I2C_Scl           at RC3_bit;
  23. sbit Soft_I2C_Sda           at RC4_bit;
  24. sbit Soft_I2C_Scl_Direction at TRISC3_bit;
  25. sbit Soft_I2C_Sda_Direction at TRISC4_bit;
  26. */
  27. // End Software I2C connections


  28. //--------------------- Reads time and date information from RTC (PCF8583)
  29. void Read_Time() {

  30.   I2C1_Start();               // Issue start signal
  31.   I2C1_Wr(0xA0);           // Address PCF8583, see PCF8583 datasheet
  32.   I2C1_Wr(2);              // Start from address 2
  33.   I2C1_Repeated_Start();     // issue I2C signal repeated start
  34.   //I2C1_Start();               // Issue repeated start signal
  35.   I2C1_Wr(0xA1);           // Address PCF8583 for reading R/W=1

  36.   seconds = I2C1_Rd(1);     // Read seconds byte
  37.   minutes = I2C1_Rd(1);     // Read minutes byte
  38.   hours = I2C1_Rd(1);       // Read hours byte
  39.   day = I2C1_Rd(1);         // Read year/day byte
  40.   month = I2C1_Rd(0);       // Read weekday/month byte
  41.   I2C1_Stop();                // Issue stop signal

  42. }

  43. //-------------------- Formats date and time
  44. void Transform_Time() {
  45.   seconds  =  ((seconds & 0xF0) >> 4)*10 + (seconds & 0x0F);  // Transform seconds
  46.   minutes  =  ((minutes & 0xF0) >> 4)*10 + (minutes & 0x0F);  // Transform months
  47.   hours    =  ((hours & 0xF0)  >> 4)*10  + (hours & 0x0F);    // Transform hours
  48.   year     =   (day & 0xC0) >> 6;                             // Transform year
  49.   day      =  ((day & 0x30) >> 4)*10    + (day & 0x0F);       // Transform day
  50.   month    =  ((month & 0x10)  >> 4)*10 + (month & 0x0F);     // Transform month
  51. }

  52. /*
  53. void interrupt() {
  54.   LATD = 0;                             // Turn off all 7seg displays
  55.   LATD = mask(hours/10u);      // bring appropriate value to PORTD
  56.   LATA = 0b011111;                       // turn on appropriate 7seg. display
  57.   Delay_ms(2);
  58.   LATD=0;
  59.   LATD = mask(hours%10u);
  60.   LATA = 0b101111;
  61.   Delay_ms(2);
  62.   LATD=0;
  63.   LATD = mask(minutes/10u);
  64.   LATA = 0b110111;
  65.   Delay_ms(2);
  66.   LATD=0;
  67.   LATD = mask(minutes%10u);
  68.   LATA = 0b111011;
  69.   Delay_ms(2);
  70.   LATD=0;
  71.   LATD = mask(seconds/10u);
  72.   LATA = 0b111101;
  73.   Delay_ms(2);
  74.   LATD=0;
  75.   LATD = mask(seconds%10u);
  76.   LATA = 0b111110;
  77.   Delay_ms(2);
  78.   LATD=0;

  79.   TMR0L  =   0;                  // reset TIMER0 value
  80.   TMR0IF_bit = 0;                // Clear TMR0IF
  81. } */

  82. void main() {
  83.   ANSELA = 0;                    // Configure PORTA pins as digital
  84.   ANSELD = 0;                    // Configure PORTD pins as digital

  85.   TRISA = 0;                     // Configure PORTA as output
  86.   LATA  = 0;                     // Clear PORTA
  87.   TRISD = 0;                     // Configure PORTD as output
  88.   LATD  = 0;                     // Clear PORTD

  89.   //T0CON = 0xC4;                  // Set TMR0 in 8bit mode, assign prescaler to TMR0
  90.   //TMR0L = 0;                     // clear TMROL

  91.   //GIE_bit = 1;
  92.   //TMR0IE_bit = 1;

  93.   I2C1_Init(100000);         // initialize I2C communication

  94.   //Soft_I2C_Init();           // Initialize Soft I2C communication

  95.   do {
  96.      Read_Time();             // Read time from RTC(PCF8583)
  97.      Transform_Time();        // Format date and time
  98.      for (i=0;i<32;i++)
  99.       {
  100.       LATD = 0;                             // Turn off all 7seg displays
  101.       LATD = mask(hours/10u);      // bring appropriate value to PORTD
  102.       LATA = 0b011111;                       // turn on appropriate 7seg. display
  103.       Delay_ms(20);
  104.       LATD=0;
  105.       LATD = mask(hours%10u);
  106.       LATA = 0b101111;
  107.       Delay_ms(20);
  108.       LATD=0;
  109.       LATD = mask(minutes/10u);
  110.       LATA = 0b110111;
  111.       Delay_ms(20);
  112.       LATD=0;
  113.       LATD = mask(minutes%10u);
  114.       LATA = 0b111011;
  115.       Delay_ms(20);
  116.       LATD=0;
  117.       LATD = mask(seconds/10u);
  118.       LATA = 0b111101;
  119.       Delay_ms(20);
  120.       LATD=0;
  121.       LATD = mask(seconds%10u);
  122.       LATA = 0b111110;
  123.       Delay_ms(20);
  124.       LATD=0;
  125.       }
  126.      //Delay_ms(4000);                      // one second delay

  127.   } while(1);                            // endless loop
  128. }
复制代码

所有资料51hei提供下载:
pic18f45k22 led rtc.zip (61.09 KB, 下载次数: 15)


评分

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

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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