冰楓論壇

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

[討論] PIC16F1825和PIC16F1786

[複製鏈接]

2609

主題

0

好友

950

積分

高級會員

Rank: 4

UID
373967
帖子
7652
主題
2609
精華
0
積分
950
楓幣
8206
威望
929
存款
20000
贊助金額
0
推廣
0
GP
1205
閱讀權限
50
在線時間
415 小時
註冊時間
2023-1-12
最後登入
2024-5-29

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

跳轉到指定樓層
1
發表於 2023-4-17 03:57:51 |只看該作者 |倒序瀏覽
製作早晨鬧鐘及自動拉窗簾需要一些硬件設備,例如RTC模塊、步進馬達驅動器、光線感應器、LCD顯示屏等等。以下是一個基於PIC16F1825和PIC16F1786,該示例使用RTC模塊、步進馬達驅動器和光線感應器來控制早晨鬧鐘和自動拉窗簾。

所需零件:

PIC16F1825微型控制器
PIC16F1786微型控制器
RTC模塊
步進馬達驅動器
光線感應器
LCD顯示屏
電位器
陶瓷電容和電解電容
電阻
按鈕開關
連接線和面包板
連結腳位:

以下是PIC16F1825和PIC16F1786的連結腳位示意圖:


PIC16F1825               PIC16F1786
     RA0  <---->   SDA
     RA1  <---->   SCL
     RC0  <---->   STEP
     RC1  <---->   DIR
     RC2  <---->   EN
     RC3  <---->   LCD_RS
     RC4  <---->   LCD_EN
     RC5  <---->   LCD_D4
     RC6  <---->   LCD_D5
     RC7  <---->   LCD_D6
     RA4  <---->   LIGHT_SENSOR
     RA5  <---->   BUTTON
程式碼:

其中使用了RTC庫、步進馬達驅動器庫、LCD顯示屏庫和光線感應器庫。

您需要自行安裝這些庫,並將代碼進行修改以適應您的硬件設備和需求。

#include <pic16f1825.h>
#include <pic16f1786.h>
#include <stdlib.h>
#include "RTC.h"
#include "StepperMotor.h"
#include "LCD.h"
#include "LightSensor.h"

#define _XTAL_FREQ 8000000

#pragma config FOSC = INTOSC, WDTE = OFF, PWRTE = OFF, MCLRE = OFF, CP = OFF, BOREN = OFF, CLKOUTEN = OFF, IESO = OFF, FCMEN = OFF

void main() {
    OSCCONbits.IRCF = 0b111; // Set internal oscillator frequency to 8MHz
   
    TRISCbits.TRISC0 = 0; // Set STEP pin as output
    TRISCbits.TRISC1 = 0; // Set DIR pin as output
    TRISCbits.TRISC2 = 0; // Set EN pin as output
    TRISCbits.TRISC3 = 0; Set LCD_RS pin as output

TRISCbits.TRISC4 = 0; // Set LCD_EN pin as output
TRISCbits.TRISC5 = 0; // Set LCD_D4 pin as output
TRISCbits.TRISC6 = 0; // Set LCD_D5 pin as output
TRISCbits.TRISC7 = 0; // Set LCD_D6 pin as output
TRISAbits.TRISA4 = 1; // Set LIGHT_SENSOR pin as input
TRISAbits.TRISA5 = 1; // Set BUTTON pin as input

ANSELA = 0b00000000; // Set all PORTA pins as digital
ANSELC = 0b00000000; // Set all PORTC pins as digital

// Initialize RTC
rtc_init();

// Initialize stepper motor driver
stepper_motor_init();

// Initialize LCD
lcd_init();

// Initialize light sensor
light_sensor_init();

while(1) {
    // Get current time from RTC
    rtc_time_t time = rtc_get_time();
   
    // Check if it is time to ring the alarm
    if(time.hour == 7 && time.minute == 0 && time.second == 0) {
        // Ring the alarm by rotating the stepper motor
        for(int i = 0; i < 100; i++) {
            stepper_motor_step(1);
            __delay_ms(10);
        }
    }
   
    // Check if it is time to open the curtain
    if(time.hour == 7 && time.minute == 30 && time.second == 0) {
        // Check if it is dark outside
        if(light_sensor_read() < 500) {
            // Open the curtain by rotating the stepper motor
            for(int i = 0; i < 1000; i++) {
                stepper_motor_step(1);
                __delay_ms(10);
            }
        }
    }
   
    // Display current time on LCD
    lcd_clear();
    lcd_print("Time: %02d:%02d:%02d", time.hour, time.minute, time.second);
   
    // Check if button is pressed to set the time
    if(!PORTAbits.RA5) {
        // Wait for button to be released
        while(!PORTAbits.RA5);
        
        // Set current time on RTC
        rtc_set_time(8, 0, 0);
    }
   
    __delay_ms(1000);
}
}
[發帖際遇]: 洪嵐峰 因為參加「太陽花學運」被黨看見,而成功加入DDP側翼網軍,領取網軍獎勵 1 楓幣 幸運榜 / 衰神榜
收藏收藏0 推0 噓0


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

廣告刊登意見回饋關於我們管群招募本站規範DMCA隱私權政策

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

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

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

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

GMT+8, 2024-5-29 19:44

回頂部