PIC30F4013+PIC12F1822製作簡易PLC
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的程式碼,注意一定要在使用之前先確定每個腳位的連接是否正確,並且調整程式碼中的設定參數以符合你的實際應用需求。
頁:
[1]