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

Parents Reply Children
  • 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

  • Ok Erik, thanks.
    I have used timer interrupt.
    I used this:
    T3 = 0x9E58;

    What does it mean?what is overflow?



    Thanks

  • 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.

    I have used timer interrupt.
    I used this:
    T3 = 0x9E58;

    What does it mean?what is overflow?


    STOP!

    without even a rudimentary knowledge of the '51 DO NOT make anything for "controlling pressure in a pneumatic system"

    Find some basic stuff to get the experience with then, after a while, you may be capable of the above without blowing the whole darn thing up.

    Erik

  • "without even a rudimentary knowledge of the '51 DO NOT make anything for "controlling pressure in a pneumatic system""

    Same applies for '166, etc

  • Anup Saha asked:
    "Why do not you people try to give solution in a straight forward manner?"

    Well, arrogant as I may be, I think this thread is a perfect illustration of the fallacy of just handing out blobs of code.

    Anuraag Gunjan has now admitted at least twice to writing stuff with no understanding at all of what he's doing:

    "I have written this Function ... PWMCON0 = 0x0000; ... I do not know how PWMCON0 is set..."

    "I used this: T3 = 0x9E58; What does it mean? what is overflow?"

    This clearly demonstrates, I think, that simply dumping code on him has not actually helped him at all.

    Providing links to datasheets and other instructive materials gives the opportunity to gain the necessary understanding to make effective use of sample code - but it is up to the student to take that opportunity and gain the understanding.

    Understanding cannot be transferred simply by posting sample code!

  • So, Mr. Neil what Mr. Anup said about you is absolutely true.And the other accomplice Mr.Erik is no less than you.Poor you!Pundits are increasing in numbers day by day!If you do not want to reply just shut up!

    As I said in my posting, I have used the codes I found in net.So, what is your problem?

    There is no english book on C167.And many of this forum have given some links regarding the codes of c167.So, what is wrong if I ask when I can not make it out?

    Instead of telling the answer, you are railing at me,ridiculous!!

    Thank you Dr. Neil and Dr. Erik.Continue it,at least do something, do not keep your brains idle!