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
Why will you not take the time to understand what you are dealing with in, at least, the must basic way before you venture out and start controlling potentially dangerous stuff. codes found in the net and the thread is titled "My program" Please advise where you are going to apply "your code" to the pneumatic system so I can stay at least 1000 miles away. Erik
Erik, Ha ha. You are more than 1000 miles away--the name of that place (where you live) is HELL!!!lol. Ok, Erik, I am trying to understand.I am new in this business, and that is why asking your help..do not be so rude please--I know my questions are elimentary--but you know I have to start from somewhere. Do not worry, my system is alright and will be alright--I have the willingness to learn--but please answer even if it is elimentary. You know,there is no book from where I can learn well.I hope you understood me. Ciao
"the name of that place (where you live) is HELL!!!lol." What on earth is this moron raving about? Stefan