Copy from Proteus source code
CCS paste c
is working
#include <Ran_Num.h>
#include <flex_lcd.c>
#include <string.h>
#use rtos(timer=0, minor_cycle=500us)
int count;
float Tex, Ksens_1, Tref, ek, ek_1, ek_2, uk, uk_1;
float Kp, Ki, Kd, a0, a1, a2, p0, p00,T,Tf;
int16 value;
float Consigne;
char BufferLCD1[16]; // Tampon pour écrire sur la 1ère ligne de l'écran LCD
char BufferLCD2[16]; // Tampon pour écrire sur la 2ème ligne de l'écran LCD
//int LengthMsj1, LengthMsj2; // Longueur de chaîne
//char String[6]; // Tampon pour stocker la conversion à imprimer
int8 sem;
//----------Declaratiion des Taches----------------------------------------------------------------
#task(rate=200ms, max=500us)
void Live() ;
#task(rate=10ms, max=500us, queue=1)
void Display() ;
#task(rate=10ms, max=500us, queue=1)
void Display_LCD() ;
#task(rate=1ms, max=500us)
void Generator() ;
#task(rate=10ms, max=500us, queue=1)
void Terminal() ;
#task(rate=1ms, max=500us)
void PID_Crt() ;
//----------Fin Declaratiion des Taches----------------------------------------------------------------
//----------Definitiion des fonctions-------------------------------------------------------------------
void Inf_To_LCD();
//----------Fin Definitiion des fonctions---------------------------------------------------------------
//----------Implementation des Taches----------------------------------------------------------------
void Live() {
output_toggle(LED_Liv);
}
void Display() {
if(rtos_msg_poll() > 0){ // Is there a message ?
output_d(rtos_msg_read()); // Send to PORTD
}
}
void Display_LCD() {
/*if(rtos_msg_poll() > 0){ // Is there a message ?
printf(lcd_putc,"\fCur Numb: %U\n", rtos_msg_read()); // Send to PORTB
}*/
rtos_wait(sem);
printf(lcd_putc,"\f");
printf(lcd_putc,BufferLCD1);
lcd_gotoxy(1,2);
printf(lcd_putc,BufferLCD2);
}
void Generator() {
char Car;
count++;
if(input(BP) == 0) {
rtos_await(input(BP) == 1);
Inf_To_LCD();
rtos_signal(sem);
}
if(kbhit()){
Car = getch();
if(Car == 'N'){
Inf_To_LCD();
rtos_signal(sem);
}
if(Car == 'C'){
scanf("%f",&Tref);
sprintf(BufferLCD2,"SetPoint = %3.1f",Tref);
rtos_signal(sem);
}
}
}
void Terminal(){
if(rtos_msg_poll() > 0){ // Is there a message ?
printf("\n\rNumb = %u \n\r",rtos_msg_read());
}
}
void PID_Crt() { // Controlleur PID
float PWM_Out;
value = read_adc();
Tex = value;
Tex = Tex * 0.004883 * Ksens_1; // Tex = Uin * Quantum * (1/Ksensor)
ek = Tref - Tex ; // Erreur de Commande
uk = uk_1 + a0*ek - a1*ek_1 + a2*ek_2; // Calcul de la commande
PWM_Out = uk*800; // Conversion en alpha pour la commande PWM
if(uk < 0){ // Gestion de la saturation de la commande
uk = uk*p00;
PWM_Out = 0;}
if(uk > 1){
uk = (uk + p0)*p00;
PWM_Out = 800;}
value = PWM_Out;
set_pwm1_duty(value); // Application de la commande
uk_1 = uk; // Mise a jour des futur valeurs
ek_2 = ek_1;
ek_1 = ek;
}
//----------Fin Implementation des Taches----------------------------------------------------------------
void Inf_To_LCD(){ //---Utiilise par la Tache Generator----------
rtos_msg_send(Display,count);
rtos_msg_send(Terminal,count);
sprintf(BufferLCD1,"Count = %U",count);
sprintf(BufferLCD2,"SetPoint = %3.1f",Tref);
}
void Init_PID_Par(){ //--Init des parametres du PID-------------
Ksens_1 = 74.1697;
Tref = 50.0;
Kp = 0.0909;
Ki = 2.502;
Kd = 0.0006139;
T = 0.001;
uk_1 = 0.0;
ek_1 = 0.0;
ek_2 = 0.0;
Tf = 0.02599;
a0 = Kp + Ki*T + Kd/T;
a1 = Kp + 2*Kd/T;
a2 = Kd / T;
p0 = kp*T/ ki;
p00 = 1 / (1 + p0);
}
void main()
{
setup_adc_ports(sAN0, VSS_VDD);
//setup_adc(ADC_CLOCK_DIV_4 | ADC_TAD_MUL_4);
setup_adc(ADC_CLOCK_INTERNAL); // Built-in A/D setup function
setup_timer_2(T2_DIV_BY_4,199,1); //250 us overflow, 250 us interrupt setup_timer_2(T2_DIV_BY_16,249,1);
setup_ccp1(CCP_PWM);
//set_pwm1_duty((int16)0);
lcd_init();
set_adc_channel(0);
Init_PID_Par();
sem = 0;
while(TRUE)
{
//TODO: User Code
rtos_run();
}
}