洪嵐峰 發表於 2023-4-2 08:58:09

PIC12F508微控制器製作六位數字電子時鐘

以下是使用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;

// 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;

        // 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;

// 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;

        // 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 = hours / 10;
        display_buffer = hours % 10;
        display_buffer = minutes / 10;
        display_buffer = minutes % 10;
        display_buffer = seconds / 10;
        display_buffer = 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引腳的配置。
頁: [1]
查看完整版本: PIC12F508微控制器製作六位數字電子時鐘