冰楓論壇

 找回密碼
 立即註冊
ads_sugarbook
搜索
查看: 586|回覆: 0
打印 上一主題 下一主題

[討論] PIC16F886

[複製鏈接]

2609

主題

0

好友

945

積分

高級會員

Rank: 4

UID
373967
帖子
7392
主題
2609
精華
0
積分
945
楓幣
962
威望
925
存款
26000
贊助金額
0
推廣
0
GP
1205
閱讀權限
50
在線時間
405 小時
註冊時間
2023-1-12
最後登入
2024-4-29

2023端午節紀念勳章 2023中秋節紀念勳章 2023聖誕節紀念勳章

跳轉到指定樓層
1
發表於 2023-4-23 13:40:39 |只看該作者 |倒序瀏覽
PIC16F886微控制器和RTC模組製作萬年曆電子鐘
程式碼:


#include <16F886.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)

// RTC I2C Address
#define RTC_ADDRESS 0xD0

// Define RTC Register Addresses
#define SEC_ADDRESS 0x00
#define MIN_ADDRESS 0x01
#define HOUR_ADDRESS 0x02
#define DAY_ADDRESS 0x04
#define MONTH_ADDRESS 0x05
#define YEAR_ADDRESS 0x06

// Define the 7-segment display patterns for the digits 0-9
const int8 patterns[10] = {
   0b00111111, // 0
   0b00000110, // 1
   0b01011011, // 2
   0b01001111, // 3
   0b01100110, // 4
   0b01101101, // 5
   0b01111101, // 6
   0b00000111, // 7
   0b01111111, // 8
   0b01101111 // 9
};

// Define the 7-segment display patterns for the days of the week (0-6)
const int8 days[7] = {
   0b00001110, // Sunday
   0b01100110, // Monday
   0b01001111, // Tuesday
   0b01101101, // Wednesday
   0b00110110, // Thursday
   0b01111101, // Friday
   0b01110101 // Saturday
};

void i2c_start() {
   i2c_start();
   i2c_write(RTC_ADDRESS);
}

void i2c_stop() {
   i2c_stop();
}

void i2c_write(int8 data) {
   i2c_write(data);
}

int8 i2c_read(int1 ack) {
   return i2c_read(ack);
}

void main() {
   int8 sec, min, hour, day, month, year;
   int8 weekday;
   
   // Initialize the I2C bus
   i2c_init();
   
   while (1) {
      // Read the time and date from the RTC
      i2c_start();
      i2c_write(RTC_ADDRESS);
      i2c_write(SEC_ADDRESS);
      i2c_start();
      i2c_write(RTC_ADDRESS | 0x01);
      sec = i2c_read(TRUE);
      min = i2c_read(TRUE);
      hour = i2c_read(TRUE);
      weekday = i2c_read(TRUE);
      day = i2c_read(TRUE);
      month = i2c_read(TRUE);
      year = i2c_read(FALSE);
      i2c_stop();
      
      // Display the time and date on the 7-segment display
      output_high(PIN_A0); // select the first digit
      output_d(patterns[hour/10]); // display the tens digit of the hour
      delay_ms(5);
      output_low(PIN_A0);
      
      output_high(PIN_A1); // select the second digit
      output_d(patterns[hour%10]); // display the ones digit of the hour
      delay_ms(5);
      output_low(PIN_A1

  output_high(PIN_A2); // select the third digit
  output_d(patterns[min/10]); // display the tens digit of the minute
  delay_ms(5);
  output_low(PIN_A2);
  
  output_high(PIN_A3); // select the fourth digit
  output_d(patterns[min%10]); // display the ones digit of the minute
  delay_ms(5);
  output_low(PIN_A3);
  
  // Display the day of the week on the 7-segment display
  output_high(PIN_A4); // select the fifth digit
  output_d(days[weekday]); // display the day of the week
  delay_ms(5);
  output_low(PIN_A4);
  
  // Display the date on the 7-segment display
  output_high(PIN_A5); // select the sixth digit
  output_d(patterns[day/10]); // display the tens digit of the day
  delay_ms(5);
  output_low(PIN_A5);
  
  output_high(PIN_A6); // select the seventh digit
  output_d(patterns[day%10]); // display the ones digit of the day
  delay_ms(5);
  output_low(PIN_A6);
}
}



上述程式碼使用了PIC16F886微控制器和RTC模組,並且在7段顯示器上顯示時間、日期和星期幾。

其中,程式碼包括I2C通訊的初始化和讀取RTC數據的代碼,以及在7段顯示器上顯示數據的代碼。
收藏收藏0 推0 噓0


把本文推薦給朋友或其他網站上,每次被點擊增加您在本站積分: 1骰子
複製連結並發給好友,以賺取推廣點數
簡單兩步驟,註冊、分享網址,即可獲得獎勵! 一起推廣文章換商品、賺$$
高級模式
B Color Image Link Quote Code Smilies |上傳

廣告刊登意見回饋關於我們職位招聘本站規範DMCA隱私權政策

Copyright © 2011-2024 冰楓論壇, All rights reserved

免責聲明:本網站是以即時上載留言的方式運作,本站對所有留言的真實性、完整性及立場等,不負任何法律責任。

而一切留言之言論只代表留言者個人意見,並非本網站之立場,用戶不應信賴內容,並應自行判斷內容之真實性。

小黑屋|手機版|冰楓論壇

GMT+8, 2024-4-29 01:19

回頂部