Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "tcn75a.h"
- #include <p18f2458.h>
- #include <delays.h>
- #define SCL_TRIS TRISBbits.TRISB1
- #define SDA_TRIS TRISBbits.TRISB0
- #define SCL LATBbits.LATB1
- #define SDA LATBbits.LATB0
- int read_tcn75(void){
- int data;
- short msb, lsb;
- SWStartI2C(); // Start
- SWPutcI2C(0x91); // Address byte (read)
- SWAckI2C(); // Receive ACK
- msb = SWGetcI2C(); // Get MSB
- i2csendack(); // Send ACK
- lsb = SWGetcI2C(); // Get LSB
- i2csendnack(); // Send NACK
- SWStopI2C(); // Stop
- // data = lsb | (msb << 8);
- return lsb; // Always returns 255, MSB is returned as expected
- }
- void tcn75_init(void){
- /*
- 00 - temp
- 01 - config (0x60)
- 10 - hyst
- 11 - temp limit
- */
- // Setup Config reg for 12bit A/D conversion
- SWStartI2C(); // Start
- SWPutcI2C(0x90); // Address byte (write)
- SWAckI2C();
- SWPutcI2C(0x01); // Pointer (config)
- SWAckI2C();
- SWPutcI2C(0x60); // Data (12bit A/D conversion)
- SWAckI2C();
- SWStopI2C(); // Stop
- // Set pointer to temperature reg
- SWStartI2C(); // Start
- SWPutcI2C(0x90); // Address byte (write)
- SWAckI2C();
- SWPutcI2C(0x00); // Pointer (temp)
- SWAckI2C();
- SWStopI2C(); // Stop
- }
- /**** Code from Mike Pierce's Routines with delays calculated for 48MHz FOSC ****/
- void i2csendack(void)
- {
- SDA=0;
- SDA_TRIS=0; //-- Send ACK
- Delay10TCYx(24);
- i2cclock(); //-- Pulse the clock
- SDA_TRIS=1; //-- Release ACK
- Delay10TCYx(24);
- }
- void i2csendnack(void)
- {
- SDA=0;
- SDA_TRIS=1; //-- Send NACK
- Delay10TCYx(24);
- i2cclock(); //-- Pulse the clock
- SDA_TRIS=1; //-- Release ACK
- Delay10TCYx(24);
- }
- void i2cclock(void)
- {
- Delay10TCYx(24); //-- Minimum Clock Low Time
- SCL_TRIS=1; //-- Release clock
- Delay100TCYx(12); //-- Minimum Clock High Time
- // SCL = 0;
- SCL_TRIS=0; //-- Lower the clock
- Delay100TCYx(12); //-- Minimum Clock Low Time
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement