#include <16F876.h>
#device adc=10
#use delay(clock=20000000)
#include <math.h>
#include "20X2.c"
#fuses HS,NOWDT,NOPROTECT
#use fast_io(A)
#use fast_io(B)
//#use fast_io(C)
#BYTE TRISA=0x85
#BYTE PORTA=0x05
#BYTE TRISB=0x86
#BYTE PORTB=0x06
#BYTE TRISC=0x87
#BYTE PORTC=0x07
//#byte INTCON = 0x0B
#define DOWN PIN_B7
#define UP PIN_B6
#define DOWN1 PIN_B5
#define UP1 PIN_B4
//#bit TMR0IF = INTCON.2
#byte porta=5 //#byte INTCON = 0x0B
#byte portb=7 //#bit TMR0IF = INTCON.2
#byte portc=7
#define minPWM 0
#define maxPWM 100 //15
unsigned int16 preheatset=40,topheatset=45; //,cupla_gpu,uk,tu,pre,top; //preheatset=100
long int uk,pre;
float cupla_pcb;
unsigned int16 q,v;
unsigned int8 msec=0;
int8 sec=0,min=0,count=0;
unsigned int16 outPWM,pwm,pwmValue = 0;
int1 bit_zc=0,swicht;
unsigned Pk =15; // Pk =3.500
unsigned Ik =0; //Ik =1.875
unsigned Dk =5; //Dk =3.250
unsigned PTerm = 0;
unsigned ITerm = 0;
unsigned DTerm = 0;
unsigned long int millisec,flag;
//void pidCompute(void);
#INT_RB
void keyboard() //keyboard
{
{
if(!input(UP)){preheatset=preheatset+1; if (preheatset>350) {preheatset=350;}}
if(!input(DOWN)){preheatset=preheatset-1; if (preheatset>350) {preheatset=0;}}
if(!input(UP1)){topheatset=topheatset+1; if (topheatset>350) {topheatset=350;}}
if(!input(DOWN1)){topheatset=topheatset-1; if (topheatset>350) {topheatset=0;}}
//y=PORTB;
}
}
#INT_EXT // Interrupt
void EXT_isr(void)
{
//float pre,top;
bit_zc=1;
setup_ccp1(CCP_OFF);
set_timer1(T1_DISABLED|T1_DIV_BY_4);
output_low(PIN_C2);
if (outPWM>0){
set_timer1(0);
setup_ccp1(CCP_COMPARE_CLR_ON_MATCH);
CCP_1=0;
uk=outPWM;
pre=13500-uk*135; //maxpwm=15--pre=15000-(uk*1000);
CCP_1=(long int)pre; //(long int)pre
setup_ccp1(CCP_COMPARE_SET_ON_MATCH);
set_timer1(0);
}
}
////////starting system////////
void intro(){
int i;
for (i=3;i>0;i--){
lcd_gotoxy(5,1); printf(lcd_putc," Start");
lcd_gotoxy(8,2); printf(lcd_putc,"%u",i);
delay_ms(500);
lcd_putc("\f");
delay_ms(500);
}
}
void countertime(){
if(swicht==1){
lcd_gotoxy(17,2);
printf(lcd_putc,"-On");
if(bit_zc==1){
millisec++;
//msec++;
bit_zc=0;
if(msec++>=99){ //incrementa el valor de msec y si es mayor a 999
msec=0; //pone msec a 0.
lcd_putc('\f');
if(sec++>=59){ sec=0;
if(min++>60){min=0;}
}
}
}
}
else {
//if(swicht==0){
setup_ccp1(CCP_OFF);
set_timer1(T1_DISABLED|T1_DIV_BY_4);
CCP_1=0;
outPWM=0;
lcd_gotoxy(17,2);
printf(lcd_putc,"-Off");
if(bit_zc==1){
flag++;
if(flag++>=99){
flag=0;
bit_zc=0;
msec=0;
sec=0;
min=0;
lcd_putc('\f');}
}
//}
}
}
/////switch/////
void button (void){ //Rutina para transformar un pulsador en un pulsador con enclavamiento
int limit=3;
if(input(PIN_C5)==0){ //Si la entrada A4 está a 0
swicht++; //Se incrementa la variable swicht
delay_ms(20); //Un retraso para evitar rebotes del pulsador
}
else if(swicht > (limit-1)){ //Si la variable swicht es mayor que límite -1
swicht = 0; // se pone la variable swicht a 0
}
}
////////////////
void lectura(void){
q=0,v=0;
set_adc_channel(0);
delay_us(20);
q = read_adc();
//pretemp=q; //*0.178005
cupla_pcb = (500.0 * q) / 1023.0;
//lcd_gotoxy(4,1);
//lcd_putc('\f');
lcd_gotoxy(1,1);
printf(lcd_putc,"Pre=%02.1fC ", cupla_pcb);
lcd_gotoxy(1,2);
printf(lcd_putc,"Set=");
//printf(lcd_putc, ">%02.1fC ", cupla_pcb);
}
///
void pidCompute(){
float error;
float olderror=0;
unsigned long int now,oldTime = 0;
now = millisec;
if ((now - oldTime) >= 99){
error=preheatset-cupla_pcb;
if (error < 0) {PTerm = 0;outPWM=0;} //error <= 0
else {
PTerm = Pk * error ;
ITerm = ITerm + (Ik* error);
DTerm = Dk* (error - olderror);
olderror = error;
outPWM = PTerm + ITerm + DTerm;
if (outPWM > maxPwm){outPWM=maxPWM;}
if (outPWM < minPWM){outPWM=minPWM;}
pwmValue = outPWM;
}
} //ClrWdt();
}
//#org DEFAULT
void main() {
lcd_init();
//delay_ms(10);
setup_adc_ports(AN0_AN1_AN3);
setup_adc(ADC_CLOCK_INTERNAL);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_4);
ENABLE_INTERRUPTS(INT_RB);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
set_tris_a(0b11111);
set_tris_b(0b11110001);
PORTB =0b00000000;
set_tris_c(0b00100000);
PORTC =0b11010000;
intro();
do {
button();
pidCompute();
countertime();
lectura();
pwm=outPWM;
lcd_gotoxy(13,1);
printf(lcd_putc"Pwm=%3.0ld%C",pwm,0x25); //"PRE>%02.1fC PWM=%2.0f", cupla_pcb,pwm //Pwm=%ld%C
lcd_gotoxy(5,2);
printf(lcd_putc,"%3ld%C%C", preheatset,0xDF,0x43); //lcd_putc,"%2.0f%C%C time=%02u:%02u", preheatset,0xDF,0x43,min,sec
/*
if(swicht==1){
lcd_gotoxy(17,2);
printf(lcd_putc,"-On");
}
else{
lcd_gotoxy(17,2);
printf(lcd_putc,"-Off");
}
*/
// printf(lcd_putc," uk:%1.2f %02u:%02u",uk,min,sec); //lcd_putc," uk:%1.3f %02u:%02u"
}
while(1);
}
Este es el código que me funciono. El sensor de temperatura en un LM35