PLC Makes Microcontroller Programming More Professional!
Hello everyone, today I want to talk about the relationship between PLC and microcontrollers. Many friends might think these are two completely different fields, but that’s not the case. The programming mindset of PLC can help us write more stable and industrial-grade microcontroller programs.
From Relay to PLC Programming Mindset
I remember visiting an old factory once, and I saw walls full of relay control cabinets with tangled wires that made one dizzy. Now, a small PLC can accomplish the same work. This evolution inspires us: The core of control systems is logical relationships, not specific implementations.
PLC State Machine Thinking
The most basic characteristic of PLC is polling execution. It’s like an indefatigable worker checking each process in a fixed order:
-
1. Read inputs
-
2. Execute program
-
3. Update outputs
-
4. Repeat
Isn’t this the best practice of while(1) in microcontrollers?
while(1) {
// Read input status
input_status = READ_GPIO();
// Execute control logic
process_control();
// Update output status
UPDATE_GPIO();
// System state maintenance
watch_dog_feed();
delay_ms(10); // Control scan cycle
}
Anti-Interference Is Not Just Talk
The input anti-interference design of PLC is worth learning from. For example: Debouncing input signals.
// Debounce input function
bool debounce_input(uint8_t pin) {
static uint8_t count[8] = {0}; // Assume there are 8 input ports
static uint8_t state[8] = {0};
// Sample current state
bool current = READ_PIN(pin);
// State counting
if(current != state[pin]) {
count[pin]++;
if(count[pin] >= 5) { // Stable state for 5 consecutive samples to consider state change
state[pin] = current;
count[pin] = 0;
}
} else {
count[pin] = 0;
}
return state[pin];
}
Power Failure Protection and State Recovery
Power outages often occur in industrial sites, and PLCs have power-holding functions. In microcontrollers, we can implement it like this:
typedef struct {
uint16_t production_count;
uint8_t machine_state;
uint8_t alarm_flags;
} SystemData;
// Regularly save system state to EEPROM
void save_system_state(void) {
static uint32_t last_save = 0;
uint32_t now = get_system_tick();
if(now - last_save > 60000) { // Save every minute
EEPROM_Write(&system_data, sizeof(SystemData));
last_save = now;
}
}
Common Problems and Solutions
-
1. Unstable input signals
-
• Reason: Field interference, poor contact
-
• Solution: Increase hardware filtering, software debouncing
-
2. Program runaway
-
• Reason: Improper interrupt handling, stack overflow
-
• Solution: Add a watchdog, check the validity of critical variables
-
3. State confusion
-
• Reason: Inadequate logic for multiple state transitions
-
• Solution: Use state machine management, clarify state transition conditions
Precautions
-
1. Avoid performing time-consuming operations in interrupts, as it can lead to loss of control in the main loop
-
2. Critical data must be validated
-
3. The system state should have a power failure protection mechanism
-
4. The watchdog is not a panacea, find the root cause of program runaway
-
5. Input signals should consider anti-interference design
Practical Suggestions
-
1. Build a minimal system test platform to simulate industrial site interference
-
2. Implement a simple PLC function on an experimental board
-
3. Use an oscilloscope to observe the actual situation of input signals
-
4. Test power failure protection function
-
5. Conduct EMC testing
By borrowing the design concepts of PLC, microcontroller programs can become more stable and reliable. Remember: The reliability of industrial-grade products comes from the extreme pursuit of details.
It is recommended to prepare the following equipment for practice:
-
• Development board
-
• Oscilloscope
-
• Signal generator
-
• DC power supply
-
• Various sensors
Would you like me to explain or break down any of the code examples?