PIC16F690
製作PID控制器需要以下零件:PIC16F690 微控制器
10K歐姆電阻器
0.1微法電容器
16x2 LCD顯示屏
L293D 馬達驅動模組
直流馬達
電源供應器
杜邦線
PIC16F690 的連接腳位:
RA0 - ADC輸入1
RA1 - ADC輸入2
RA2 - ADC輸入3
RA4 - 馬達正轉
RA5 - 馬達反轉
RC0 - LCD D4
RC1 - LCD D5
RC2 - LCD D6
RC3 - LCD D7
RC4 - LCD RS
RC5 - LCD E
RC6 - 調節器PWM輸出
RC7 - 馬達使能
以下是 PIC16F690 的 PID 控制器程式碼:
#include <xc.h>
#include <stdio.h>
#include "lcd.h"
#pragma config FOSC = INTOSCIO
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config MCLRE = OFF
#pragma config CP = OFF
#pragma config CPD = OFF
#pragma config BOREN = OFF
#pragma config IESO = OFF
#pragma config FCMEN = OFF
#pragma config LVP = OFF
#define _XTAL_FREQ 8000000
int kp = 10;
int ki = 5;
int kd = 2;
int setpoint = 50;
int error = 0;
int prev_error = 0;
int integral = 0;
int derivative = 0;
int output = 0;
void init_ADC(void)
{
ADCON0 = 0b00000001;
ADCON1 = 0b00000000;
ANSEL = 0b00000111;
TRISA = 0b00000111;
}
void init_PWM(void)
{
PR2 = 249;
T2CON = 0b00000101;
CCP1CON = 0b00111100;
}
void init_LCD(void)
{
lcd_init();
lcd_puts("PID Controller");
lcd_gotoxy(0, 1);
lcd_puts("Setpoint: ");
lcd_putc(setpoint + '0');
}
int read_ADC(int channel)
{
ADCON0 &= 0b11000101;
ADCON0 |= (channel << 3);
__delay_us(10);
GO_nDONE = 1;
while (GO_nDONE);
return (ADRESH << 8) | ADRESL;
}
int compute_PID(int input)
{
error = setpoint - input;
integral += error;
derivative = error - prev_error;
prev_error = error;
output = kp * error + ki * integral + kd * derivative;
return output;
}
void set_motor_speed(int speed)
{
if (speed >= 0)
{
RA4 = 1;
RA5 = 0;
CCPR1L = speed >> 2;
CCP1CON &= 0b11001111;
CCP1CON |= (speed<< 2) & 0b00110000;
}
{
RA4 = 0;
RA5 = 1;
CCPR1L = (-speed) >> 2;
CCP1CON &= 0b11001111;
CCP1CON |= ((-speed) << 2) & 0b00110000;
}
}
void main(void)
{
OSCCON = 0b01110000;
init_ADC();
init_PWM();
init_LCD();
while (1)
{
int input = read_ADC(0);
int speed = compute_PID(input);
set_motor_speed(speed);
lcd_gotoxy(9, 1);
lcd_putc((input / 100) + '0');
lcd_putc(((input % 100) / 10) + '0');
lcd_putc((input % 10) + '0');
__delay_ms(100);
}
}
這個程式使用 ADC 讀取輸入,計算 PID 控制器輸出,並通過 PWM 控制馬達速度。
它還使用 LCD 顯示輸入和設置點,以及用於計算 PID 的係數 kp、ki 和 kd。
頁:
[1]