9 years, 7 months ago.

Problem with MCP3551 and PT100

HI I have a problem with read ADC from MCP3551. MCP is using SPI. I want to use MCP3551 and pt100 to read temperature.

I can't read right values from MCP3551.

Devices is good, because I checked in Atmel Studio

This is my C -code.

include the mbed library with this snippet

#include "mbed.h"
unsigned char dd_zmien_kolejnosc_bitow(char a);
uint8_t LsbMsb( uint8_t data);
uint8_t REVERSE_BITS(uint8_t a);

SPI spi(NC, D12, D13); // mosi(out), miso(in), sclk(clock)
DigitalOut cs(D8); // cs (the chip select signal)
Timer timer;
Serial pc(USBTX, USBRX); // tx, rx ( the usb serial communication )
uint8_t aByte[4],rByte[4],oByte[4];


bool OVH, OVL, rOVH, rOVL;
unsigned long ends=0,delta=0;
uint32_t adc,radc;


int main() {
timer.start();

   spi.format(8,0);
   spi.frequency(5000000);
pc.baud(19200);


	cs = 1;
    wait_ms(300);
    // lets just do this forever
    while (1) {




    delta = timer.read_ms()-ends;
    if(delta > 75)
       {
        cs = 0;

       	wait_us(737400);

        aByte[3] = spi.write(0xFF);
        aByte[2] = spi.write(0xFF);
        aByte[1] = spi.write(0xFF);
        aByte[0] = spi.write(0xFF);
        cs = 1;
        pc.printf("senByte3=== %u    ", aByte[3]);
        pc.printf("senByte2=== %u    ", aByte[2]);
        pc.printf("senByte1=== %u    ", aByte[1]);
        pc.printf("senByte0=== %u\n", aByte[0]);

        rByte[3]=REVERSE_BITS(aByte[3]);
        rByte[2]=REVERSE_BITS(aByte[2]);
        rByte[1]=REVERSE_BITS(aByte[1]);
        rByte[0]=REVERSE_BITS(aByte[0]);


  		      ////////////////////////////////////////////////////////////////////////////////////
  		        ///////////////////////////////////////////
  		          pc.printf("senByte3r=== %u     ", rByte[3]);
  		          pc.printf("senByte2r=== %u     ", rByte[2]);
  		          pc.printf("senByte1r=== %u     ", rByte[1]);
  		          pc.printf("senByte0r=== %u\n   ", rByte[0]);

  		          radc=(aByte[2]<<16) + (aByte[1]<<8) + aByte[0];
  		          pc.printf("przedkonwerscji %d\n", radc);

  		          rOVL = ((aByte[2] & 0b10000000));
  		          rOVH = ((aByte[2] & 0b01000000));

  		          	  if(rOVH==1){ pc.printf("ovh=1\n");};
  		              if(rOVL==1){ pc.printf("ovl =1\n");};


  		        		  //will not touch the outgoing code. Leaves the last valid.
  		        		if (rOVH && !rOVL)
  		        			{
  		        			aByte[2] &= 0b00111111; // clear both flags from byte when positive
  		        			}

  		        		if ((!rOVH && (aByte[2] & 0b00100000)) || rOVL ) //when OVH is not set and signed (21th) bit is set we have negative value
  		        			{
  		        				aByte[2] |= 0b11000000; //set remaining bits high
  		        				aByte[3] =0xFF; //sets high order byte, value is then negative.
  		        			}
  		        						float calRAdevice1 = 13607; //stores the series resistor value
  		        		  		    	//resistor of RTD
  		        		  		    	const int RZero = 100; //resistance at 0°C
  		        		 	 	 	 	radc=(aByte[2]<<16) + (aByte[1]<<8) + aByte[0];
  		        		    		    pc.printf("po konwerscji %d\n", radc);



  		        		    		   float rRTD = calRAdevice1 * (radc / ( 2097152 - radc));
  		        		    		//  pc.printf("kontrol 2 %f\n", rRTD);
  		        		    		    rRTD = (rRTD / RZero) - 1;
  		        		    		//  pc.printf("kontrol 3 %f\n", rRTD);
  		        		    		    float rtemperature = (rRTD * (255.8723 + rRTD * (9.6 + rRTD * 0.878)));
  		        		    		    char buffer[10];
  		        		    		    sprintf (buffer, "%4.0f", rtemperature);


  		        		    	   pc.printf("temperatura %d\n", buffer);





pc.printf("////////////////////////////////////////////////////////////////////////////////////////\n");


        // wait_ms(74);
       // wait_ms(1);
        //ends = timer.read_us();
        }

        else
        {
    	  ends = timer.read_ms();
    	  cs =1;
      	 }

	}







    }




bool MCP3551_ready(void)
{	DigitalIn miso(D12);
//int ALA= (D12);
	int end;
	if((timer.read_ms() - end)> 74)
	{
		cs = 0;

		wait_us(1);
		if(miso == 0)
		{
			return 1;

		}
		else {

				end = timer.read_ms();
				cs =1;
				return 0;
			}
		}
		else return 0;
	}




uint8_t REVERSE_BITS(uint8_t a)
{	static const uint8_t reverse_lookup[] = { 0, 8,  4, 12, 2, 10, 6, 14,1, 9, 5, 13,3, 11, 7, 15 };
	//int a;
	a= ((reverse_lookup[(a & 0x0F)]) << 4) + reverse_lookup[((a & 0xF0) >> 4)];
return a;
}



uint8_t LsbMsb( uint8_t data)
{
	uint8_t i,bit,buffer=0;

	for(i=0;i<8;i++)
	{
		bit = data & (1 << i);
		buffer = (buffer << 1) | bit;
	}
	return( buffer );
}

//Funkcja zamiany kolejności bitów LSB <-> MSB

unsigned char dd_zmien_kolejnosc_bitow(char a)
{
int i, c=0;
for(i=0; i<8; i++)
{
if(a&(1<<i))
c |= (1<<7-i);
}
return c;
}



Could you help me find a mistakes or might by somebody have good code to read MCP3551.

Be the first to answer this question.