use pid digital +PIC+control brightness

Status
Not open for further replies.

huyvnpte

Newbie level 2
Joined
Oct 4, 2009
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,338
hi everyone. i write code use pid digital control brightness (when sunlight in photosynthesis to turn off the lights,when dark >> light ) but i see program program does not run properly .EVERYONE HELP ME:
#include <16f877a.h>
#include <def_877A.h>
#device *=16 ADC=10
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP, NOCPD, NOWRT
#use delay(clock=20000000)
#use rs232(baud = 9600, xmit = PIN_D0, rcv = PIN_D1)
#INCLUDE <LCD_LIB_4BIT.c>
#include <math.h>

int8 nghin,tram,chuc,donvi,count,duty;
int1 milivolt;
int16 tempt,T1;
float volt,Udat,Uphoi;
unsigned int Kp,Kd,Ki,T;
signed int e0,e1,e2;
signed int u0,u1;
unsigned int heso0,heso1,heso2;
unsigned char value_pwm;
#define Umax 5
#int_timer2
//------------
void Initial(void){



TRISA =0xFF; // PORTA as input

TRISC =0; // PORTC as output

TRISD =0; // PORTD as output

}

//-------------

void convert_bcd(int16 x)
{
nghin=x/1000;
tempt=x%1000;
tram=tempt/100;
tempt=tempt%100;
chuc=tempt/10;
donvi=tempt%10;
}
//------------

void LCD_Display(void){
tempt=read_adc();
volt=(float)*(tempt*5)/1023;
if(volt<1) milivolt=1;
else milivolt=0;
volt=volt*1000;
convert_bcd((int16)volt);
if(!milivolt)
{
lcd_putchar(nghin);
lcd_putchar(".");
}
lcd_putchar(tram);
lcd_putchar(chuc);
lcd_putchar(donvi);
if(milivolt) printf(lcd_putchar("mv"));
else lcd_putchar("v");


}
//----------
void Init_PID(void) {

Kp=3;

Ki=0.1;

Kd=1;

T=1;

heso0=(float)Kp+(float)Kd/(float)T+(float)Ki*(float)T/2; //heso0=Kp+Kd/T+Ki*T/2;

heso1=(float)Ki*(float)T/2-(float)Kp-2*(float)Kd/(float)T; //heso1=-Kp -2*Kd/T+Ki*T/2;

heso2=(float)Kd/(float)T; //heso3=Kd/T;

//SIMPLE CYCLE T=1
u0=0;
u1=0;
e0=0;
e1=0;
e2=0;
}

// Chuong trinh dieu khien vi tich phan ti le //

//----------------------------------------------------------------------------//
void PID_light(void){
float tam;
char tam1;
e0=Udat-Uphoi;
if (e0>-5 && e0<5){
u0=u1+heso0*e0+heso1*e1+heso2*e2;
if (u0>Umax){
u0=Umax;
}
if (u0<=0){
u0=0;
}
else if (e0>=5)
{ u0=Umax; }
else if (e0<=-5)
{ u0=0; }
tam=u0;
tam=tam*1023/Umax;
value_pwm=(char)tam;
set_pwm1_duty(1023-value_pwm);
u1=u0;
e2=e1;
e1=e0;
}
}

void pwm_ccp()
{
set_pwm1_duty(tempt);
while (true) {
tempt = read_ADC();
set_pwm1_duty(1023-tempt);
delay_ms(20);
}
}


//-------------
void main()
{
lcd_init();
Initial();
setup_adc_ports( ALL_ANALOG );
set_adc_channel( 0 );
setup_adc(ADC_CLOCK_INTERNAL );
delay_us(10);
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_1,5, 1);
Printf(LCD_putchar,"lux ");
delay_us(100);
LCD_putcmd(0xC0);
Printf(LCD_putchar,"");
// CREATED FOR INTURRUPS OUTSIDE
enable_interrupts (INT_EXT);
ext_int_edge(H_TO_L);
enable_interrupts (GLOBAL);//
pwm_ccp();
while(1)
{
LCD_Display();
delay_us(10);
PID_light(); //

}
}

:|
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…