#include<16f877a.h> #use delay (clock=4000000) #fuses HS,PUT,NOLVP,NOWDT,BROWNOUT,nowdt #define LCD_ENABLE_PIN PIN_D2 #define LCD_RS_PIN PIN_D0 #define LCD_RW_PIN PIN_D1 #define LCD_DATA0 PIN_D3 #define LCD_DATA1 PIN_D4 #define LCD_DATA2 PIN_D5 #define LCD_DATA3 PIN_D6 #include #ifndef TC_CLK #define TC_CLK PIN_C6 //edit these pins as necessary #endif #ifndef TC_CS #define TC_CS PIN_C5 #endif #ifndef TC_DATA #define TC_DATA PIN_C7 #endif int16 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 } void main () { LCD_INIT(); printf(lcd_putc,"\f ***TERMOMETRO***\n MAX6675 " ); } 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++){ output_high(TC_CLK); delay_us(1); output_low(TC_CLK); shift_left(&data,2,input(TC_DATA)); //reads in 2 bytes to data from the pin TC_DATA } 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_us(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()))); printf(lcd_putc,"\f TEMPERATURA = %3.2f", do_everything() ); delay_ms(100); }