This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

My Program

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

  • "So, no one will answer to my problem?"

    Certainly not if you take that attitude!

  • Since output is a 16-bit integer and presuming port 3.10 is a single
    port pin, I guess you have three choices:

    1. If output is greater than some threshold value, set P3.10 high;
      otherwise, set it low.
    2. Pick a bit in output that is of interest to you and duplicate its
      value on P3.10.
    3. Be more specific with your question and state your "association"
      objective, then wait for better answers.

  • Maybe you can use a PWM output (Port 2), generated by means of the CAPCOM unit: take a look at PWM examples published on the Hitex site

    http://www.hitex.co.uk/download/c166examples/examplesrequest.html

    Filtering the generated PWM, via a simple low pass filter, will produce a voltage proportional to your setpoint value.

  • In fact this output will drive a linear motor.The motor is fed with an analog value.What I want to do is that the output of the microcontroller will be converted into anlog values.It may or may not be necessary to use port 3.10 as output.How will I do it?I can use a D/A converter if necessary.But how can this converter read the 16 bit digital value so that it can convert it into a 8 bit analog value?
    Thanks

  • I have written this Function:
    void InitializePWM(void){
    //Initialize
    PWMCON0 = 0x0000;
    PWMCON1 = 0x0000;
    DP7 = 0xFF;
    P7 = 0x00; // Initialize P7

    DP7 = 0x01; // Use P7_0 as output
    PWMCON1 = 0x0001; // using module 0
    PP0 = 3125; // gives us a frequency of approximately 100 Hz
    PW0 = (1-0.68)*PP0; // 68% duty cycle

    PWMCON0 = 0x0033; // choose correct resolution and start PWM

    }

    First,I do not know how PWMCON0 is set.Second,how can the P7.0 port can be mapped with the output variable of the PID()function?
    Pls help me out.

  • On the same Hitex download page there is a full PID example that makes use of CAPCOM1 PWM output to generate the PID analog output value. Filename is PID.ZIP. Please study this example to fully understand how to use a PWM modulator to obtain an analog output value.

    http://www.hitex.co.uk/download/c166examples/download.html

  • It seems people in this forum like to refer and recommend.Why do not you people try to give solution in a straight forward manner? If you have superficial idea then do not reply.I have been member of several c++ forums--people are there so lively and helpful.

    Moreover,I have been noticing that Mr. Neil is always arrogant in replying.You do not believe me? Well, search his postings,it will reveal the fact.

    Well, I would like to know if this problem can be solved without PWM.What about using a D/A converter?Say, if port 8 (from 8.0....8.7)is used for output:
    DP8=0xFF;
    P8=0x00;
    then in PID function:
    P8=output;
    Does it sound ok?By Connecting the pins with a 8 bit D/A converter can't we get an analog outpput?What do u say Mr.Neil?

  • It seems people in this forum like to refer and recommend.Why do not you people try to give solution in a straight forward manner? If you have superficial idea then do not reply.I have been member of several c++ forums--people are there so lively and helpful.

    Well I guess in this field or atleast in this kinda forums people here believe in "Teaching you how to fish...rather than feeding you with a fish every time you ask for..."

    Dont get me wrong try to understand the intentions It helps a lot...

    Moreover,I have been noticing that Mr. Neil is always arrogant in replying.You do not believe me? Well, search his postings,it will reveal the fact.

    And now you talk about Mr.Neil...I guess you dont even know to what extent he goes...he is one of the most helpful person I have ever met anyone online/forums/chat etc.

    And just think about it...why do you think these people have to help you...you are not paying them?

    Rgds
    Raj Shetgar

  • That's what you get for trying to help people... Kinda disappointing :-(

    - mike

  • Anup,

    I really like the fact that Raj brought the "learn to fish" analogy into this.

    The posters getting pi$$ed about the nature of the replies are usually the GIMME CODE types.

    If every poster were spoonfed a solution, who would learn anything and when the generation that answered with the complete code passed on NOBODY would be able to solve any problem.

    Thus, if you do not like the nature of the replies, I can only suggest that you do not like to do your own work and that being the case, who cares what you think.

    Erik

  • "Well, I would like to know if this problem can be solved without PWM. What about using a D/A converter?"

    Well, a PWM (with suitable filtering) is a D-to-A converter!

    Any particular reason why you want to avoid PWM?
    The advantage of PWM is that has virtually zero cost in terms of external components, and requires only a single port pin.

    But sure, you can use a parallel D-to-A if you want, but I expect you'll need more than just the 8 data lines - there'll be at least one extra as a "strobe" to tell the DAC to "Convert Now!"
    You would have to refer to the DAC datasheet to find out exactly what signals are required.

  • "But sure, you can use a parallel D-to-A if you want, but I expect you'll need more than just the 8 data lines..."

    DACs having an I2C (IIC, if you prefer) interface are available, but that's still two wires, not one like the OP wants. If it's to be one wire only, PWM still seems like the way to go.

  • Hello,
    Thank you all for your replies.
    Now,I have the questions:

    How often this interrupt is invoked?If I want to call this function say evrey 10 ms, can I do it?How can I set the period?


    void Interrupt(void) interrupt INT

    {

    sensorValue=ReadSensor(0);

    currentError=setPoint-sensorValue;

    PID(currentError) ;


    }

    I need to read the sensor at specific intervals so that it can process it before the next reading of the sensor.
    Thanks again.

  • How can I set the period?
    Using a timer (interrupt)

    Erik