冰楓論壇

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

[討論] PIC30F4013+PIC12F1822製作簡易PLC

[複製鏈接]

2609

主題

0

好友

947

積分

高級會員

Rank: 4

UID
373967
帖子
7447
主題
2609
精華
0
積分
947
楓幣
1268
威望
927
存款
26000
贊助金額
0
推廣
0
GP
1205
閱讀權限
50
在線時間
408 小時
註冊時間
2023-1-12
最後登入
2024-5-6

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

跳轉到指定樓層
1
發表於 2023-4-12 05:54:37 |只看該作者 |倒序瀏覽
PIC30F4013+PIC12F1822製作簡易PLC。
此PLC可以控制3個輸出,並從3個輸入中讀取數據。
在此示例中,PIC30F4013負責控制邏輯,而PIC12F1822則負責輸入/輸出。這只是一個簡單的示例,實際應用時需要根據具體需求進行修改和優化。

零件清單:

PIC30F4013單片機
PIC12F1822單片機
3個LED
3個按鈕
電阻、電容等其它零件
腳位配置:

以下是PIC30F4013和PIC12F1822的腳位配置,其中PIC30F4013使用的是PDIP封裝,而PIC12F1822使用的是SOIC封裝。腳位號碼可能因不同封裝而有所不同,請根據具體封裝和手冊進行配置。

PIC30F4013:

Pin 1 (MCLR)          -> VDD
Pin 2 (RA0/AN0)       -> Button 1
Pin 3 (RA1/AN1)       -> Button 2
Pin 4 (RA2/AN2)       -> Button 3
Pin 5 (VSS)           -> GND
Pin 6 (RB0/INT0)      -> LED 1
Pin 7 (RB1/INT1)      -> LED 2
Pin 8 (RB2/INT2)      -> LED 3
Pin 9 (RB3)           -> PIC12F1822 data out
Pin 10 (RB4)          -> PIC12F1822 data in
Pin 11 (RB5)          -> PIC12F1822 clock
Pin 12 (RB6)          -> PIC12F1822 chip select
Pin 13 (RB7)          -> PIC12F1822 status
Pin 14 (AVDD)         -> VDD
Pin 15 (AVSS)         -> GND
Pin 16 (VDD)          -> VDD

PIC12F1822:

Pin 1 (VPP)           -> VDD
Pin 2 (RA5/ICSPDAT)   -> PIC30F4013 data in
Pin 3 (RA4/ICSPCLK)   -> PIC30F4013 clock
Pin 4 (RA3/T0CKI)     -> Not used
Pin 5 (RC5)           -> Not used
Pin 6 (RC4)           -> Not used
Pin 7 (RC3)           -> Not used
Pin 8 (RC6)           -> Not used
Pin 9 (RC7)           -> Not used
Pin 10 (VSS)          -> GND
Pin 11 (RA2/AN2)      -> Not used
Pin 12 (RA1/AN1)      -> Not used
Pin 13 (RA0/AN0)      -> Not used
Pin 14 (RC0) -> Not used
Pin 15 (RC1) -> Not used
Pin 16 (RC2) -> Not used
Pin 17 (RB7/PGD) -> Not used
Pin 18 (RB6/PGC) -> Not used
Pin 19 (RB5) -> PIC30F4013 status
Pin 20 (RB4) -> PIC30F4013 data out
Pin 21 (RB3) -> PIC30F4013 data in
Pin 22 (RB2) -> Not used
Pin 23 (RB1) -> Not used
Pin 24 (RB0) -> Not used



程式碼:

以下是使用MPLAB X IDE和XC16編譯器開發的程式碼。
程式碼中使用了兩個.c文件和一個.h文件,其中plc.c包含了PLC的主要邏輯,io.c包含了與PIC12F1822進行通信的函數,plc.h包含了一些宏定義和函數聲明。
實際應用時需要根據具體需求進行修改和優化。

plc.h:

#ifndef PLC_H
#define PLC_H

// Macros for LED pins
#define LED_1 RB0
#define LED_2 RB1
#define LED_3 RB2

// Macros for button pins
#define BUTTON_1 RA0
#define BUTTON_2 RA1
#define BUTTON_3 RA2

// Function prototypes
void plc_init(void);
void plc_run(void);

#endif // PLC_Hplc.c:

#include "plc.h"
#include "io.h"

// Global variables
unsigned char inputs = 0;
unsigned char outputs = 0;

// Initialize PLC
void plc_init(void) {
// Configure LED pins as outputs
TRISBbits.TRISB0 = 0; // LED 1
TRISBbits.TRISB1 = 0; // LED 2
TRISBbits.TRISB2 = 0; // LED 3

// Configure button pins as inputs
TRISAbits.TRISA0 = 1;   // Button 1
TRISAbits.TRISA1 = 1;   // Button 2
TRISAbits.TRISA2 = 1;   // Button 3

// Initialize I/O communication with PIC12F1822
io_init();
}

// Main PLC logic
void plc_run(void) {
// Read inputs
inputs = (BUTTON_1 << 0) | (BUTTON_2 << 1) | (BUTTON_3 << 2);

// Update outputs based on inputs
switch (inputs) {
    case 0b000:     // No buttons pressed
        outputs = 0b000;
        break;
    case 0b001:     // Button 1 pressed
        outputs = 0b001;
        break;
    case 0b010:     // Button 2 pressed
        outputs = 0b010;
        break;
    case 0b011:     // Button 1 and 2 pressed
        outputs = 0b011;
        break;
    case 0b100: // Button 3 pressed

outputs = 0b100;
break;
case 0b101: // Button 1 and 3 pressed
outputs = 0b101;
break;
case 0b110: // Button 2 and 3 pressed
outputs = 0b110;
break;
case 0b111: // All buttons pressed
outputs = 0b111;
break;
}

// Write outputs
LED_1 = (outputs >> 0) & 0x01;
LED_2 = (outputs >> 1) & 0x01;
LED_3 = (outputs >> 2) & 0x01;

// Send data to PIC12F1822
io_send_data(outputs);

// Receive data from PIC12F1822
inputs = io_receive_data();

// Update outputs based on received data
LED_1 = (inputs >> 0) & 0x01;
LED_2 = (inputs >> 1) & 0x01;
LED_3 = (inputs >> 2) & 0x01;
}

io.h:

#ifndef IO_H
#define IO_H

// Function prototypes
void io_init(void);
void io_send_data(unsigned char data);
unsigned char io_receive_data(void);

#endif // IO_Hio.c:
#include "io.h"
#include <xc.h>

// Initialize I/O communication with PIC12F1822
void io_init(void) {
// Configure data in pin as input
TRISBbits.TRISB3 = 1;

// Configure data out pin as output
TRISBbits.TRISB4 = 0;

// Configure status pin as input
TRISBbits.TRISB5 = 1;

// Set data out pin low
LATBbits.LATB4 = 0;
}

// Send data to PIC12F1822
void io_send_data(unsigned char data) {
// Set data out pin low
LATBbits.LATB4 = 0;

// Wait for status pin to go low
while (PORTBbits.RB5);

// Send data bit by bit
for (unsigned char i = 0; i < 8; i++) {
    // Set data out pin to next bit
    LATBbits.LATB4 = (data >> i) & 0x01;
   
    // Wait for clock pulse
    __delay_us(1);
   
    // Set clock pin high
    LATBbits.LATB3 = 1;
   
    // Wait for clock pulse
    __delay_us(1);
   
    // Set clock pin low
    LATBbits.LATB3 = 0;
}

// Wait for status pin to go high
while (!PORTBbits.RB5);

// Set data out pin low
LATBbits.LATB4 = 0;
}

// Receive data from PIC12F1822
unsigned char io_receive_data(void) {
// Set data out pin low
LATBbits.LATB4 = 0;

// Wait for status pin to go low
while (PORTBbits.RB5);

// Receive data bit by bit
unsigned char data = 0;
for (unsigned char i = 0; i < 8; i++) {
    // Wait for clock pulse
    __delay_us(1);
   
    // Set clock pin high
    LATBbits.LATB3 = 1;
   
    // Read data in pin
    data |= (PORTBbits.RB3 << i);
   
    // Wait for clock pulse
    __delay_us(1);
   
    // Set clock pin low
    LATBbits.LATB3 = 0;
}

// Wait for status pin to go high
while (!PORTBbits.RB5);

// Set data out pin low
LATBbits.LATB4 = 0;

return data;
}



以上就是這個PLC的程式碼,注意一定要在使用之前先確定每個腳位的連接是否正確,並且調整程式碼中的設定參數以符合你的實際應用需求。
收藏收藏0 推0 噓0


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

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

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

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

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

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

GMT+8, 2024-5-6 13:27

回頂部