Work in Progress

Update 11/8/17:

Status report indicating our teams progress in Lab 3 for EE 300:

Loader Loading...
EAD Logo Taking too long?

Reload Reload document
| Open Open in new tab

Download

 

During this semester, I worked on a PID (proportional integral derivative) controller that was implemented through C-Code on the dsPIC33EP64MC502 microcontroller. The code used to implement this controller is shown below:

/*
* File:   Exercise 3.c
* Author: SCHIANO
*
* Created on April 24, 2016, 3:33 PM
*/
/* define FCY before including libpic30.h */
#define FCY 3685000UL
#include <p33EP64MC502.h>
#include <libpic30.h>
#include <stdio.h>
#include <math.h>
#include <xc.h>
#include "EE200_LCD.h"
/* set Configuration Bits */
#pragma config ICS = PGD2    // communicate on PGED2 (pin 14) and PGEC2 (pin 15)
#pragma config JTAGEN = OFF  // disable JTAG inorder to use RB8 through RB11
#pragma config PWMLOCK = OFF // PWM Lock Enabl Bit
/* declare functions */
void Init_ADC(void);
void Init_PWM_module(void);
double Read_ADC(int channel);
void Init_ADC (void) {
ANSELAbits.ANSA0 = 1;          // set pin 2 (ANO) for analog input
TRISAbits.TRISA0 = 1;          // configure for input (disable DO)
ANSELAbits.ANSA1 = 1;          // set pin 3 (AN1) for analog input
TRISAbits.TRISA1 = 1;          // configure for input (disable DO)
AD1CON1bits.ADON = 1;          // turn ADC module on
}
double Read_ADC(int channel) {
double Vmeas;
AD1CHS0 = channel;             // choose CH-0 positive input
AD1CON1bits.SAMP = 1;          // start Sample; place inp signal across cap
__delay_us(10);                // wait for cap voltage to track analog inp
AD1CON1bits.SAMP = 0;          // start SA converter
while (!AD1CON1bits.DONE);     // wait for SA conv to complete
Vmeas = 3.3 * (double) ADC1BUF0 / 1023.0; // convert to units of Volts
return Vmeas;
}
void Init_PWM_Module(void) {
PTCONbits.PTEN = 0;     // disable the PWM module
PTPER = 7370;           // period as a multiple of clock cycles
PDC1  = 3685;           // fract duty cycle as a multiple of clock cycles
IOCON1bits.PMOD  = 1;   // PWM1 I/O pin pair is in redundant mode
IOCON1bits.PENH  = 1;   // PWM module controls pin 25, PWM1H

/* RB10 to RB13 must be configured to control the LCD module */
IOCON2bits.PENL  = 0;   // GPIO module controls pin 24, RB13
IOCON2bits.PENH  = 0;   // GPIO module controls pin 23, RB12
IOCON3bits.PENL  = 0;   // GPIO module controls pin 22, RB11
IOCON3bits.PENH  = 0;   // GPIO module controls pin 21, RB10

FCLCON1bits.FLTMOD = 3; // fault input disabled
PTCONbits.PTEN = 1;     // enable the PWM module
}
int main(void) {
char Line_char_Array[16];
unsigned int TS;
double V_PV, V_SP, TC, TF_PV, TF_SP, KP, KI, x, e, u;
/* Set Parameters */
TS = 500;              // sample period in ms
KP = 0.25;             // proportional gain
KI = 0.05;             // integral gain
x   =  0;              // initial value of integrator
Init_ADC();
Init_PWM_Module();
Init_LCD_Module();

while (1) {
__delay_ms(TS);      // set sample rate to 500 ms
/* Acquire analog input signals */
V_PV = Read_ADC(0); // read PV voltage on pin 2 (ANO)
V_SP = Read_ADC(1); // read SP voltage on pin 3 (AN1)

/* Determine process variable temp in degrees F */
TC = -1481.96 + sqrt(2.1962e6 + (1.8639 - V_PV)/3.88e-6);
TF_PV = (9.0/5.0)*TC + 32.0;
/* Determine setpoint temp in degrees F */
TF_SP = 29.0*V_SP/3.3 + 70.0;
/* Realize PI Controller */
e = TF_SP - TF_PV;
x = x + TS*e/1000;
u = KP*e + KI*x;
/* Limit fractional duty cycle between 0 and 1 */
if (u > 1)
u = 1;
else if (u < 0)
u = 0;
/* Set PWM duty cycle */
PDC1  = floor(7370 * u);
/* Update LCD  */
Position_LCD_Cursor(0x00); // place cursosr at cell 0x00
sprintf(Line_char_Array,"SP %4.1f  e %+4.2f", TF_SP, e);
Write_LCD_String(Line_char_Array);
Position_LCD_Cursor(0x40); // place cursosr at cell 0x40
sprintf(Line_char_Array,"PV %4.1f  D  %4.2f", TF_PV, u);
Write_LCD_String(Line_char_Array);
ClrWdt(); // restart the watchdog timer
}
return 0;
}