- UID
- 373967
- 帖子
- 8882
- 主題
- 2609
- 精華
- 0
- 積分
- 1000
- 楓幣
- 0
- 威望
- 977
- 存款
- 34955
- 贊助金額
- 0
- 推廣
- 0
- GP
- 1205
- 閱讀權限
- 70
- 在線時間
- 455 小時
- 註冊時間
- 2023-1-12
- 最後登入
- 2024-11-25
|
製作PID溫控上下爐火烤箱需要以下零件:
ATmega8A-PU微控制器
MAX6675熱電偶模組
SSD1306 OLED顯示屏
杜邦線
K型熱電偶線
綠色LED指示燈
紅色LED指示燈
蜂鳴器
雙極性電容器
NPN晶體管
電位器
電源開關
電源插座
電源轉換器
烤箱熱插頭
AC插頭
2路繼電器模塊
PID控制器模組
以下是ATmega8A-PU的連結腳位:
rust
Copy code
MAX6675模組:
VCC -> 5V
GND -> GND
SCK -> PB5
CS -> PB4
SO -> PB3
SSD1306顯示屏:
VCC -> 5V
GND -> GND
SCL -> PC5
SDA -> PC4
綠色LED指示燈:
正極 -> PD0
負極 -> GND
紅色LED指示燈:
正極 -> PD1
負極 -> GND
蜂鳴器:
正極 -> PD2
負極 -> GND
繼電器模塊:
VCC -> 5V
GND -> GND
IN1 -> PD3
IN2 -> PD4
PID控制器模組:
OUT -> PD6
GND -> GND
VIN -> 5V
AC插頭:
N -> 接地線
L -> VCC
V -> 繼電器模塊COM
下面是ATmega8A-PU製作PID溫控上下爐火烤箱的程式碼。這段程式碼使用PID控制器來控制烤箱溫度,並顯示溫度信息在OLED顯示屏上,同時使用蜂鳴器和LED燈來指示烤箱狀態。
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "Max6675.h"
#include "SSD1306.h"
#define SSR_DDR DDRD
#define SSR_PORT PORTD
#define SSR_PIN PD6
#define BUZZER_DDR DDRD
#define BUZZER_PORT PORTD
#define BUZZER_PIN PD2
#define GREEN_LED_DDR DDRD
#define GREEN_LED_PORT PORTD
#define GREEN_LED_PIN PD0
#define RED_LED_DDR DDRD
#define RED_LED_PORT PORTD
#define RED_LED
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#define HEATER_DDR DDRB
#define HEATER_PORT PORTB
#define HEATER_PIN PB1
#define TEMP_SENSOR_PIN PINC0
double Setpoint, Input, Output;
double Kp=2, Ki=5, Kd=1;
unsigned long lastTime;
double elapsedTime, cumError, rateError;
void setup() {
HEATER_DDR |= (1 << HEATER_PIN); // set heater pin as output
ADC_init(); // initialize ADC
PWM_init(); // initialize PWM
Timer_init(); // initialize timer
}
void loop() {
// compute PID output and adjust PWM duty cycle
unsigned long now = millis();
elapsedTime = (double)(now - lastTime);
Input = readTemperature();
cumError += (Input - Setpoint) * elapsedTime;
rateError = (Input - Setpoint) / elapsedTime;
Output = Kp * (Input - Setpoint) + Ki * cumError + Kd * rateError;
if (Output < 0) {
Output = 0;
} else if (Output > 255) {
Output = 255;
}
analogWrite(HEATER_PIN, Output);
lastTime = now;
}
double readTemperature() {
// read temperature from LM35 sensor
int val = analogRead(TEMP_SENSOR_PIN);
double mv = (val / 1024.0) * 5000;
double cel = mv / 10;
return cel;
}
void ADC_init() {
// initialize ADC
ADMUX |= (1 << REFS0); // use AVcc as reference voltage
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) | (1 << ADPS0); // set prescaler to 128
ADCSRA |= (1 << ADEN); // enable ADC
}
void PWM_init() {
// initialize PWM
TCCR1A |= (1 << COM1A1) | (1 << WGM11); // set non-inverting mode and Fast PWM mode with TOP=ICR1
TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS11); // set Fast PWM mode with TOP=ICR1 and prescaler to 8
ICR1 = 255; // set TOP value
}
void Timer_init() {
// initialize timer
TCCR0 |= (1 << CS02) | (1 << CS00); // set prescaler to 1024
TIMSK |= (1 << TOIE0); // enable timer overflow interrupt
sei(); // enable global interrupts
}
ISR(TIMER0_OVF_vect) {
// interrupt service routine for timer overflow
loop();
} |
|