冰楓論壇

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

[討論] PIC12F508微控制器製作六位數字電子時鐘

[複製鏈接]

2609

主題

0

好友

945

積分

高級會員

Rank: 4

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

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

跳轉到指定樓層
1
發表於 2023-4-2 08:58:09 |只看該作者 |倒序瀏覽
以下是使用PIC12F508微控制器製作六位數字電子時鐘的零件清單:

PIC12F508微控制器
32.768 kHz時鐘晶振
6個共陽極七段LED顯示器
6個PNP型晶體管(用於控制LED顯示器)
6個330歐姆電阻
1個10K歐姆電阻
1個電解電容(1uF)
1個陶瓷電容(0.1uF)
1個7805穩壓器
1個9V電源適配器
杜邦線和麵包板
下面是PIC12F508微控制器的引腳分配:

PIC12F508 Pinout

下面是完整的PIC12F508電子時鐘代碼:


#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Configuration bits
#pragma config FOSC = INTRCIO, WDTE = OFF, PWRTE = ON, MCLRE = OFF

// Global variables
unsigned char seconds = 0;
unsigned char minutes = 0;
unsigned char hours = 0;
unsigned char display_buffer[6];

// Function prototypes
void init(void);
void display_time(void);
void update_time(void);

// Main function
void main(void) {
    init();

    while (1) {
        update_time();
        display_time();
    }
}

// Initialization function
void init(void) {
    // Set internal oscillator frequency to 4 MHz
    OSCCON = 0b01110000;

    // Set GPIO directions
    TRISIO = 0b00000000;

    // Set timer0 to 8-bit mode with prescaler of 128
    OPTION_REG = 0b11000111;

    // Clear display buffer
    memset(display_buffer, 0, sizeof(display_buffer));
}

// Display time function
void display_time(void) {
    // Set digit select lines to display each digit in turn
    for (unsigned char i = 0; i < 6; i++) {
        // Turn off all digits
        GPIO = 0b00000000;

        // Set the appropriate digit high
        GPIO = 1 << i;

        // Output the appropriate segment pattern
        GPIO = display_buffer[i];

        // Delay for a short period to allow the display to update
        __delay_us(100);
    }
}

// Update time function
void update_time(void) {
    // Check if timer0 has overflowed
    if (TMR0IF) {
        // Reset timer0 and set the appropriate flag
        TMR0 = 6;
        TMR0IF = 0;

        // Increment seconds counter
        seconds++;

        // Check if seconds counter has reached 60
        if (seconds == 60) {
            // Reset seconds counter and increment minutes counter
            seconds = 0;
            minutes++;

            // Check if minutes counter has reached 60
            if (minutes == 60) {
                // Reset minutes counter and increment hours counter
                minutes = 0;
                hours++;

                // Check if hours counter has reached 24
                if (hours


#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Configuration bits
#pragma config FOSC = INTRCIO, WDTE = OFF, PWRTE = ON, MCLRE = OFF

// Global variables
unsigned char seconds = 0;
unsigned char minutes = 0;
unsigned char hours = 0;
unsigned char display_buffer[6];

// Function prototypes
void init(void);
void display_time(void);
void update_time(void);

// Main function
void main(void) {
    init();

    while (1) {
        update_time();
        display_time();
    }
}

// Initialization function
void init(void) {
    // Set internal oscillator frequency to 4 MHz
    OSCCON = 0b01110000;

    // Set GPIO directions
    TRISIO = 0b00000000;

    // Set timer0 to 8-bit mode with prescaler of 128
    OPTION_REG = 0b11000111;

    // Clear display buffer
    memset(display_buffer, 0, sizeof(display_buffer));
}

// Display time function
void display_time(void) {
    // Set digit select lines to display each digit in turn
    for (unsigned char i = 0; i < 6; i++) {
        // Turn off all digits
        GPIO = 0b00000000;

        // Set the appropriate digit high
        GPIO = 1 << i;

        // Output the appropriate segment pattern
        GPIO = display_buffer[i];

        // Delay for a short period to allow the display to update
        __delay_us(100);
    }
}

// Update time function
void update_time(void) {
    // Check if timer0 has overflowed
    if (TMR0IF) {
        // Reset timer0 and set the appropriate flag
        TMR0 = 6;
        TMR0IF = 0;

        // Increment seconds counter
        seconds++;

        // Check if seconds counter has reached 60
        if (seconds == 60) {
            // Reset seconds counter and increment minutes counter
            seconds = 0;
            minutes++;

            // Check if minutes counter has reached 60
            if (minutes == 60) {
                // Reset minutes counter and increment hours counter
                minutes = 0;
                hours++;

                // Check if hours counter has reached 24
                if (hours == 24) {
                    // Reset hours counter
                    hours = 0;
                }
            }
        }

        // Update display buffer
        display_buffer[0] = hours / 10;
        display_buffer[1] = hours % 10;
        display_buffer[2] = minutes / 10;
        display_buffer[3] = minutes % 10;
        display_buffer[4] = seconds / 10;
        display_buffer[5] = seconds % 10;
    }
}

PIC12F508是一款8位微控制器,擁有8個引腳。以下是其引腳分配:

GP5 - 引腳5,數字I/O引腳,可用於輸入或輸出數字信號。
GP4 - 引腳4,數字I/O引腳,可用於輸入或輸出數字信號。
GP3/MCLR - 引腳3,數字I/O引腳或複位引腳。在MCLR模式下,可用於復位控制器。
GP2 - 引腳2,數字I/O引腳,可用於輸入或輸出數字信號。
GP1/CLKIN - 引腳1,數字I/O引腳或時鐘輸入引腳。在CLKIN模式下,可用於外部時鐘輸入。
GP0/OSC1/CLKOUT - 引腳0,數字I/O引腳、振盪器輸入引腳或時鐘輸出引腳。在OSC1/CLKOUT模式下,可用於外部振盪器輸入或時鐘輸出。
VSS - 引腳6,接地引腳,必須接地。
VDD - 引腳7,電源引腳,必須連接正電源。
需要注意的是,GP3引腳在復位模式下有特殊功能,用於控制控制器的複位。如果不使用複位功能,可以將GP3引腳用作數字I/O引腳。此外,GP0引腳還具有振盪器輸入和時鐘輸出的功能,因此在使用外部振盪器時,需要特別注意GP0引腳的配置。
收藏收藏0 推0 噓0


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

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

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

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

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

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

GMT+8, 2024-4-29 20:25

回頂部