Hi all,
I'm trying to communicate with MAX6675 (SPI interface) but still receiving error numbers.I have tried implemetted SPI functions(SPI_INIT,SPI_Read) ,my own procedure with loop but still receiving nonsense numbers...Please,does anyone have functional code and can post it here?Thank you very muchy for any help...I'm totally lost..Thanks
MAX6675 sample code...
bug?
Hi,
i had tried code from Maxiboost for PicBasic (http://www.picbasic.org/forum/showthread.php?t=6642) and it work well in my scheme :-) .I have translated code to MikroC and still not work :-(.Using SPI_Init,SPI_read,tried PIC16F73 and 16F877)
here is my code:
Thanks for replies...
i had tried code from Maxiboost for PicBasic (http://www.picbasic.org/forum/showthread.php?t=6642) and it work well in my scheme :-) .I have translated code to MikroC and still not work :-(.Using SPI_Init,SPI_read,tried PIC16F73 and 16F877)
here is my code:
Code: Select all
int buffer,temp,alarm,;
char ven[32];
void main()
{
Trisa=0x00;
Trisb=0x00;
Trisc=0x10;
lcd_config(&Portb,3,2,1,7,6,5,4);
do{
Porta=0x00;
LCD_Out(1,0,"reading");
delay_ms(500);
Spi_init();
temp=SPI_Read (buffer);
Porta=0xFF;
alarm=temp >> 5;
IntToStr(alarm,ven);
LCD_Out(1,9,ven);
}
while(1);
}
CS of maxim is on Porta - pin0
SCK to SCK
SO to SDI
Hi,
SPI_Read function read 8 bits. You need to read 16 bits !!!
SPI_Read function read 8 bits. You need to read 16 bits !!!
Last edited by yo2lio on 16 Jul 2007 22:13, edited 1 time in total.
Best regards, Florin Andrei Medrea.
http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/
mail : florin@microelemente.ro
http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/
mail : florin@microelemente.ro
This function uses software SPI communication so, you can use any pin on PIC MCU:
in main use this
Don't forget to configure TRIS register, depending which PINs should use.
I hope this is usefull.....
Code: Select all
#define SPI_CLOCK PORTB.F7 // OUT pin Define custom PINs
#define SPI_CS PORTC.F1 // OUT pin
#define SPI_DATA PORTD.F5 // INPUT pin
unsigned long read_MAX6675()
{
unsigned int sekvenca, bits;
unsigned char k;
SPI_CS = 0; // SPI bus is ON
bits = 32768;
sekvenca = 0;
for(k=0; k<=15; k++)
{
if(SPI_DATA) sekvenca += bits; // we
bits >>= 1;
// SPI Clock signal
SPI_CLOCK = 1;
SPI_CLOCK = 0;
}
SPI_CS = 1; // SPI bus is OFF
// check presence of THERMOCOUPLE
if(sekvenca & 0b100)
{
return(0xFFFFFFFF); // return MAX value if THERMOCOOUPLE is NOT present
}
else
{
return((sekvenca >> 3) * 25); // return measured temperature
}
}
Code: Select all
unsigned char accumulation_count;
unsigned long accumulation;
unsigned long temperature_raw;
unsigned long measured_temperature;
unsigned int temperatura_za_prikaz;
temperature_raw = read_MAX6675();
// if sensor is not open-circuit, we do calculation
if(temperature_raw != 0xFFFFFFFF)
{
// we make a
accumulation += temperature_raw;
if(++accumulation_count > 3) // if we colected a four samples of
{ // theperature, we divide this by 4
accumulation_count = 0;
accumulation >>= 2; // divide by 4
measured_temperature = accumulation;
measured_temperature /= 100; // remove decimals! 348.32 -> 348
temperatura_za_prikaz = measured_temperature;
accumulation = 0;
if(temperatura_za_prikaz > 999)
{
sensor_error = 1;
}
else
{
sensor_error = 0;
}
}
}
else
{
// put error message on LED screen
// !!! Thermocouple is open-circuit ;
sensor_error = 1;
}
I hope this is usefull.....
Nebojsa Pejcic
www.ePraktikum.co.yu
www.ePraktikum.co.yu