#INCLUDE <18F4550.h>
#device adc=10
#use delay(clock=400000)
#fuses XT,NOWDT,HS,NOPROTECT
#include <lcd.c>
#use rtos(timer=0,minor_cycle=1ms)
#use standard_io(B)
#use standard_io(c)
#Define TC_CLK PIN_B1
#define TC_CS PIN_A5
#define TC_DATA PIN_C7
#include <max6675.c>
float valor; //lectura de temperatura
float setp=500.0; //temperatura a alcanzar
float yT; //,eT,pT,qT,rT,uT;
float tempera; //Para visualizar la temperatura del horno.
int16 t_l; //Para visualizar la temperatura límite.
int8 sem; //Variable de semáforo.
int16 aux=10;
#task(rate=1ms,max=1ms)
//void pid ( );
void lec();
#task(rate=10ms,max=1ms,queue=2)
void display( );
//#task(rate=10ms,max=1ms)
//void teclado ( );
void main(){
lcd_init();
//setup_timer_2(t2_div_by_4,249,1); //periodo de la señal PWM a 1ms
//setup_ccp1(ccp_pwm); //Módulo CCP a modo PWM
//setup_adc_ports(all_analog); //Puerto A analógico
//setup_adc(ADC_CLOCK_INTERNAL); //reloj convertidor AD interno
//set_adc_channel(0);
sem=1;
rtos_run ( );
}
void lec(void)
{
rtos_wait(sem);
//aux=read_TC();
//aux1=sortout(aux);
delay_ms(800);
valor=do_everything();
//aux1=aux>>4;
//valor=0x0FFF&aux1;
rtos_signal(sem);
rtos_yield();
}
void display()
{
rtos_wait(sem);
//tempera=yT/10;
lcd_gotoxy(1,1);
printf(lcd_putc, "Temp = %f\n", valor);
t_l=setp/10;
printf(lcd_putc, "SO= %Lu", aux);
rtos_signal(sem);
rtos_yield();
}
#ifndef TC_CLK
#define TC_CLK PIN_B1 edit these pins as necessary
#endif
#ifndef TC_CS
#define TC_CS PIN_A5
#endif
#ifndef TC_DATA
#define TC_DATA PIN_C7
#endif
int1 thermocouple_error; //a handy dandy global error flag to tell you if a thermocouple is connected or not
void init_TC(void)
{
output_low(TC_CLK);
output_low(TC_DATA);
output_high(TC_CS); //if we idle high, the chip keeps doing conversions. Change this if you like
}
int16 read_TC(void) //It takes 200ms (ish) for the MAX6675 to perform a conversion
{
int8 i;
int16 data;
output_low(TC_CS); //stop any conversion processes
delay_us(1); //and give it some time to power up (not very much, admittedly)
for (i=0;i<16;i++){
shift_left(&data,2,input(TC_DATA)); //reads in 2 bytes to data from the pin TC_DATA
output_high(TC_CLK);
output_low(TC_CLK);
}
thermocouple_error=bit_test(data,2); //this is the thermocouple status bit
output_high(TC_CS);
return(data);
}
int16 sortout(int16 raw)
{
return(0x0FFF & (raw>>3)); //returns only the bits converning temperature
}
float toFloat_TC(int16 tmp)
{
return((float)tmp/4.0); //adjusts data to floating point format, and accounts for the decimal point
}
float do_everything(void)
{
init_TC();
delay_ms(200); //200ms is a long time to be doing nothing. use a timer interrupt to avoid wasting time here
return(toFloat_TC(sortout(read_TC())));
}