MCP9804 Software issues

Discussion in 'Embedded Systems and Microcontrollers' started by d333b, Mar 23, 2011.

  1. d333b

    Thread Starter New Member

    Nov 1, 2010
    Hi all,

    So I have been working a project for the home for a while. It is going to be a snake cage controller (for my red tail). In this project I was hoping to utilize I2C and the MCP9804 (for its accuracy). I have successfully used my 18f4550 to talk to EEPROMs via the I2C, but I seem to be having a large issue with this temp sensor. I have verified that all the pins are where they are supposed to be (no shorts or opens).

    The situation:

    Using the C18 provided CDC COM port emulator and my/microchips 18f4550 software I have been polling this chip with read commands (thru a LabVIEW interface). The results are bizarre. My read (i2c) code is below:

    Code ( (Unknown Language)):
    2. unsigned char gettemp(unsigned char chip)
    3. {
    4. char arr[2];
    5. unsigned char a;
    6. IdleI2C();
    7. StartI2C();
    8. while ( SSPCON2bits.SEN );
    9. WriteI2C( chip ); // write 1 byte
    10. IdleI2C();
    11. WriteI2C(USB_Out_Buffer[2]); // WRITE word address to EEPROM
    12. IdleI2C(); // ensure module is idle
    14. [I]StartI2C(); // initiate START condition[/I]
    15. [I]while ( SSPCON2bits.SEN ); [/I]
    16. [I]// RestartI2C(); // generate I2C bus restart condition[/I]
    17. [I]// while ( SSPCON2bits.RSEN ); // wait until re-start condition is over [/I]
    19. WriteI2C( chip | 0x01 ); // WRITE 1 byte - R/W bit should be 1 for read
    20. IdleI2C();
    21. getsI2C(arr, 2);
    22. USB_In_Buffer[0] = (arr[0] & 0x1F); //this is supposed to eliminate the register flags
    23. USB_In_Buffer[1] = arr[1];
    24. NotAckI2C();
    25. while ( SSPCON2bits.ACKEN ); // wait until ACK sequence is over
    26. StopI2C(); // send STOP condition
    27. while ( SSPCON2bits.PEN );//*/
    28. a = 1;
    29. return a;
    30. }
    The result usually winds up being 0x00FF or 0x0FFF and sometimes 0x001D and other times 0x0000. With my trusty work Oscope I can see that these results are right however sometimes the device is sending NACK(it pulls the SDA line low if that’s right). I'm really lost, I can't even get it to work using simple code to light up LEDS, it looks like the MCP9804 is NAKing after each command byte and with the 18f2221 it's just spitting back 0xFFFF (without the flag bits removed).

    If anyone has worked with this aggravating chip before please let me know, or if you have a better solution for me (as a temp sensor accurate to .5dF). I would be willing to work with an ADC instead if necessary but I was going to be implementing the I2C line anyways so this looked like a perfect option for me. I would love to hear what you guys think.

    p.s. the code that I spaced out and italics i was curious if a restart is the same a start? it didn't seem to make any difference, but i am curious.

    Last edited by a moderator: Mar 23, 2011
  2. d333b

    Thread Starter New Member

    Nov 1, 2010
    Hello all again,

    So with some experimenting and a new serf board I have discovered that my chip does work but the length (~1ft) of ribbon cable I was using to connect to it must be introducing too much noise. When I connect the MCP9804 directly to my breadboard the program (on my 18f2221) functions perfectly, however when I separate the MCP9804 with the cable the program returns incorrect values (telling me my desk is 224dC), and then will quickly freeze (I have a second pin strobing an LED to verify proper function). I've also tried with some cat5 cable and same thing occurs. So maybe I will stick to my current set up with LM335's, and run an external ADC that is I2C. If anyone has any suggestions please share.

    Thanks again.