We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hello Everyone, with the help of codes found in the net, I have written these codes for controlling pressure in a pneumatic system.The pressure sensor has the range 2.5-4.5 volts. The set point is 3.5 volts. [CODE] #include <stdio.h> #include <reg167.h> // **************** GLOBAL VARIABLES AND CONSTANTS ************************** // for sensors int sensorValue ; // for PID control int output ; float currentError=0; float setPoint=512 ; #define maxValue 1023 #define minValue 0 //ISR address #define INT 0x3F //**************** FUNCTION PROTOTYPES ************************* // General function void Initialize(void); // control function void PID(float); // A/D converter void AToDInitialize(void); unsigned int ReadSensor(unsigned int); // Interrupt routine void Interrupt(void); // ********************* MAIN PROGRAM ****************************** void main(void) { // ALWAYS DO THIS FIRST! Initialize(); while(1) ; } // *************************** FUNCTION DEFINITIONS ************************* // Function: Initialize // Purpose: Avoid floating pins by setting all of them to GND. // Input conditions: None // Arguments: None // Return value(s): None // Destroys: ALL REGISTERS - DPx and Px! void Initialize(void) { P3 |= 0x0400; //set port 3.10 output latch (TXD) DP3 |= 0x0400; // configure port 3.10 for output // Initialize Analog/Digital AToDInitialize(); // GLOBALLY ENABLE INTERRUPTS IEN = 1; } // Function: PID // Purpose: controller for driving the actuator // Input conditions: None // Arguments: Kp, Kd, Ki // Return value(s): None // Destroys: None void PID(float currentError) { static float oldError = 0 ; static float integralError = 0; int temp ; static float Kp = 1.2; static float Ki = 9.5; static float Kd = 0.05; // Accumulate error for integral control integralError += currentError; temp=(currentError) * Kp + (currentError - oldError)* Kd+Ki * integralError ; // Sanity checks if(output + temp > minValue) output = minValue ; else if(output + temp < maxValue) output = maxValue; else output += temp ; //P3.10=output; // Update your error oldError = currentError ; } /************* Sensor functions - AToD module *******************/ // Function: AToDInitialize // Purpose: Initialize AToD converter module // Input conditions: None // Arguments: None // Return values: None // Destroys: ADCON void AToDInitialize(void) { ADCON = 0x0000; // ADM = 0x00, fixed channel SINGLE conversion } // Function: ReadSensor // Purpose: Reads a sensor value from the specified analog input port (Port P5_0 to P5_15). // Input conditions: None // Arguments: input analog port to be read (0 to 15) // Return values: converted digital value // Destroys: no registers are destroyed unsigned int ReadSensor(unsigned int port_no) { ADCON = (ADCON & 0xFFF0) | port_no; ADST = 1; // start conversion while(ADBSY) // wait for conversion ; ADCIR = 0; // stop it return (ADDAT & 0x0FFF); } //************ INTERRUPT ROUTINE ******* void Interrupt(void) interrupt INT { sensorValue=ReadSensor(0); currentError=setPoint-sensorValue; PID(currentError) ; } [/CODE] As you can see 'output' is the output of PID contrller. I have set 3.10 port as output. How can I associate it with the output from the PID function? Thanks