Advertisement
Guest User

TCN75A I2C Code

a guest
Aug 26th, 2011
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.89 KB | None | 0 0
  1. #include "tcn75a.h"
  2. #include <p18f2458.h>
  3. #include <delays.h>
  4.  
  5. #define SCL_TRIS TRISBbits.TRISB1
  6. #define SDA_TRIS TRISBbits.TRISB0
  7.  
  8. #define SCL LATBbits.LATB1
  9. #define SDA LATBbits.LATB0
  10.  
  11. int read_tcn75(void){
  12.     int data;
  13.     short msb, lsb;
  14.    
  15.     SWStartI2C();               // Start
  16.  
  17.     SWPutcI2C(0x91);            // Address byte (read)
  18.     SWAckI2C();             // Receive ACK
  19.     msb = SWGetcI2C();          // Get MSB
  20.     i2csendack();               // Send ACK
  21.     lsb = SWGetcI2C();          // Get LSB
  22.     i2csendnack();              // Send NACK
  23.  
  24.     SWStopI2C();                // Stop
  25.    
  26. //  data = lsb | (msb << 8);
  27.     return lsb;             // Always returns 255, MSB is returned as expected
  28. }
  29.  
  30. void tcn75_init(void){
  31. /*
  32. 00 - temp
  33. 01 - config (0x60)
  34. 10 - hyst
  35. 11 - temp limit
  36. */
  37.  
  38.     // Setup Config reg for 12bit A/D conversion
  39.     SWStartI2C();               // Start
  40.     SWPutcI2C(0x90);            // Address byte (write)
  41.     SWAckI2C();
  42.     SWPutcI2C(0x01);            // Pointer (config)
  43.     SWAckI2C();
  44.     SWPutcI2C(0x60);            // Data (12bit A/D conversion)
  45.     SWAckI2C();
  46.     SWStopI2C();                // Stop
  47.  
  48.     // Set pointer to temperature reg
  49.     SWStartI2C();               // Start
  50.     SWPutcI2C(0x90);            // Address byte (write)
  51.     SWAckI2C();
  52.     SWPutcI2C(0x00);            // Pointer (temp)
  53.     SWAckI2C();
  54.     SWStopI2C();                // Stop
  55. }
  56.  
  57. /**** Code from Mike Pierce's Routines with delays calculated for 48MHz FOSC ****/
  58.  
  59. void i2csendack(void)
  60. {
  61.  SDA=0;
  62.  SDA_TRIS=0; //-- Send ACK
  63.  Delay10TCYx(24);
  64.  i2cclock(); //-- Pulse the clock
  65.  SDA_TRIS=1; //-- Release ACK
  66.  Delay10TCYx(24);
  67. }
  68.  
  69. void i2csendnack(void)
  70. {
  71.  SDA=0;
  72.  SDA_TRIS=1; //-- Send NACK
  73.  Delay10TCYx(24);
  74.  i2cclock(); //-- Pulse the clock
  75.  SDA_TRIS=1; //-- Release ACK
  76.  Delay10TCYx(24);
  77. }
  78.  
  79. void i2cclock(void)
  80. {
  81.  Delay10TCYx(24);       //-- Minimum Clock Low Time
  82.  SCL_TRIS=1;              //-- Release clock
  83.  Delay100TCYx(12);        //-- Minimum Clock High Time
  84. // SCL = 0;
  85.  SCL_TRIS=0;               //-- Lower the clock
  86.  Delay100TCYx(12);         //-- Minimum Clock Low Time
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement