MAX6675 sample code...

General discussion on mikroC.
Post Reply
Author
Message
Karm
Posts: 6
Joined: 29 May 2007 21:37

MAX6675 sample code...

#1 Post by Karm » 14 Jul 2007 10:48

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

Karm
Posts: 6
Joined: 29 May 2007 21:37

bug?

#2 Post by Karm » 16 Jul 2007 19:27

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:

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
Thanks for replies...

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#3 Post by yo2lio » 16 Jul 2007 19:44

Hi,

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

Karm
Posts: 6
Joined: 29 May 2007 21:37

#4 Post by Karm » 16 Jul 2007 20:38

ou what a stupid mistake.Please,have you any idea how to solve it?Thanks

edit:
Solved :-)

Porta=0x00;//select chip
temp1=SPI_Read (buffer);
temp2=SPI_Read (buffer);
Porta=0xFF;//deselect chip

npejcic
Posts: 7
Joined: 27 Sep 2004 11:25
Location: Nis, Serbia
Contact:

#5 Post by npejcic » 17 Jul 2007 12:12

This function uses software SPI communication so, you can use any pin on PIC MCU:

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
  }                       
}
in main use this

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;
      }
Don't forget to configure TRIS register, depending which PINs should use.
I hope this is usefull.....
Nebojsa Pejcic
www.ePraktikum.co.yu

Post Reply

Return to “mikroC General”