?? tmp100.c
字號:
/************************************************************************************************
*Filename :TMP100.C
*Programmer :nm
*ProgramDate:09.27.2006
*************************************************************************************************/
#include <math.h>
#include <regxag49.sfr>
#include <stdio.h>
#include <cxa.h>
#include <string.h>
#define FALSE 0
#define TRUE 1
#define CtrlRegAddr 0x01
#define CtrlRegData 0x0e0 //control register initial set:SD=0,TM=0,POL=0,(F1F0)=00,(R!R0)=11, OS=1
#define TMP0addrW 0x90 /*slave address + write*/
#define TMP0addrR 0x91 /*slave address + read*/
#define TMP1addrW 0x98 /*slave address + write*/
#define TMP1addrR 0x99 /*slave address + read*/
#define TMP_CLK P1_0
#define TMP_SDA P1_2
#define SDA1 P4OUT |= BIT0
#define SDA0 P4OUT &= ~BIT0
#define SCL1 P2OUT |= BIT2
#define SCL0 P2OUT &= ~BIT2
void TMP_IIC_Initial(void);
void TMP_Read_Temp(void);
unsigned char TMP_ErrFlg;//BIT0--3:TEMP0 0000:ok BIT0=1:err@config BIT1=1:err@data
//BIT4--7:TEMP1 0000:ok BIT4=1:err@config BIT1=5:err@data
unsigned int TempCold0;
unsigned int TempCold1;
_near unsigned int Cold_data[2]_at(0x50);
void delay10us(unsigned char loop)
{
unsigned char i,j;
for(i=0;i<loop;i++)
{
for(j=0;j<10;j++)_nop();
}
}
void TMP_Set_SDA_high(void)
{
TMP_SDA = 1;;
delay10us(1);
}
void TMP_Set_SDA_low(void)
{
TMP_SDA = 0;;
delay10us(1);
}
void TMP_Set_sck_high(void)
{
TMP_CLK = 1;
delay10us(1);
}
void TMP_Set_sck_low(void)
{
TMP_CLK = 0;
delay10us(1);
}
// -------------------------------------------------------------------
// TMP_IIC_GetACK
// 1) The acknowledge-related clock pulse is generated by the master.
// 2) The transmitter (Tx) release the SDA line (HIGH) during the
// acknowledge clock pulse.
// 3) The receiver (Rx) must pull down the SDA line during the
// acknowledge clock pulse so that it remains stable LOW during
// the HIGH period of this clock pulse.
//
// Return:
// LOW if OK
// -------------------------------------------------------------------
unsigned char TMP_IIC_GetACK (void)
{
unsigned char i;
unsigned char bResult;
TMP_Set_SDA_high();//轉輸入
TMP_Set_sck_high();
bResult = 1;
for(i=0;i<250;i++)
{
if(TMP_SDA == 0)
{
bResult = 0;
i = 250;//OK立即退出
}
}
TMP_Set_sck_low();
return ( bResult );
} // TMP_IIC_GetACK
// -------------------------------------------------------------------
// TMP_IIC_SetACK
// __ ____
// SDA: \________/
// ____
// SCL: ____/ \____
//
// -------------------------------------------------------------------
void TMP_IIC_SetACK (void)
{
TMP_Set_sck_low();
TMP_Set_SDA_low();
TMP_Set_sck_high();
TMP_Set_sck_low();
} // TMP_IIC_SetACK
// ---------------------------------------------------------------------
// TMP_IIC_SetNAK
// ____
// SCK: ___/ \____
// _____________
// SDA:
// -------------------------------------------------------------------
void TMP_IIC_SetNAK (void)
{
TMP_Set_sck_low();
TMP_Set_SDA_high();
TMP_Set_sck_high();
TMP_Set_sck_low();
} // TMP_IIC_SetNAK
// -------------------------------------------------------------------
// TMP_IIC_START - START condition (SDA falling edge).
// ____
// SDA: \_____
// _____
// SCL: \__
//
// -------------------------------------------------------------------
void TMP_IIC_START (void)
{
_nop();
_nop();
_nop();
_nop();
TMP_Set_SDA_high();
TMP_Set_sck_high();//prepare for start condition
TMP_Set_SDA_low();
TMP_Set_sck_low();
} // TMP_IIC_START
// -------------------------------------------------------------------
// TMP_IIC_STOP - STOP condition (SDA rising edge).
// ____ __
// SDA: ____\_____/
// __ _______
// SCL: __\___/
//
// -------------------------------------------------------------------
void TMP_IIC_STOP (void)
{
_nop();
_nop();
_nop();
_nop();
TMP_Set_sck_low();
TMP_Set_SDA_low();//prepare for stop condition
TMP_Set_sck_high();
TMP_Set_SDA_high();
delay10us(255);//delay time from stop to start time must >10mS
} // TMP_IIC_STOP
// -------------------------------------------------------------------
// TMP_IIC_TxByte - Parallel serial conversion.
// __ ________
// SDA: __X_state__
// ________
// SCL: _____/ \__
//
// -------------------------------------------------------------------
void TMP_IIC_TxByte (unsigned char bValue)
{
unsigned char i ;
for(i = 0 ; i < 8 ; i++)
{
if( bValue & 0x80)
TMP_Set_SDA_high();
else
TMP_Set_SDA_low();
TMP_Set_sck_high();
TMP_Set_sck_low();
bValue = bValue << 1 ;
}
} //TMP_IIC_TxByte
// -------------------------------------------------------------------
// TMP_IIC_RxByte -
// ______
// SDA: --<_read_>--
// _________
// SCL: __/ \__
//
// -------------------------------------------------------------------
unsigned char TMP_IIC_RxByte (void)
{
unsigned char bResult;
unsigned char i;
bResult = 0x00; // clear value //
TMP_Set_SDA_high();//轉輸入
delay10us(1);
for ( i=0;i<8;i++) // Read all bits //
{
TMP_Set_sck_high();
bResult <<=1; // Set Result to correct value //
if (TMP_SDA == 1) // Sampling SDA input level //
bResult |= 0x01;
else
bResult &= 0xFE;
TMP_Set_sck_low();
}
return( bResult );
} // TMP_IIC_RxByte
// *********************************************************************
// Function : TMP_Read_Data
// Description :
// Arguments : DevSel : Device ID
// addr : read address
// Return : data in the address
// Side Effect :
// *********************************************************************
unsigned char TMP_Read_Data ( unsigned char devnum,unsigned char addr,unsigned char *val)
{
unsigned char bError;
unsigned char DevSel;
if(devnum==0)DevSel = TMP0addrW;
else DevSel = TMP1addrW;
TMP_IIC_START(); // START condition
TMP_IIC_TxByte((char)(DevSel&0x0fe)); //write DevEel adress
bError = TMP_IIC_GetACK();
if ( bError )
return FALSE;
TMP_IIC_TxByte ( addr ); // write addr
bError = TMP_IIC_GetACK();
if ( bError )
return FALSE;
TMP_IIC_START();
TMP_IIC_TxByte((char)(DevSel|0x01)); //read DevEel adress
bError = TMP_IIC_GetACK();
if ( bError )
return FALSE;
*val=TMP_IIC_RxByte(); //read data1 & Set_sck_low
val++;
*val=TMP_IIC_RxByte(); //read data2 & Set_sck_low
TMP_IIC_SetNAK ();
TMP_IIC_STOP(); // STOP condition
return TRUE;
}
// *********************************************************************
// Function : TMP_IIC_Write
// Description :
// Arguments : DevSel : Device ID
// AS pin '0' : ID is 40h
// AS pin '1' : ID is 42h
// * pBuffer : Buffer pointer
// but normal is not more than 8)
// count : size of buffer normal is less than 8
// Return :
// Side Effect :
// *********************************************************************
unsigned char TMP_Write_Config ( unsigned char devnum, unsigned char addr,unsigned char data )
{
//unsigned char idx;
unsigned char bError;
unsigned char DevSel;
if(devnum==0)DevSel = TMP0addrW;
else DevSel = TMP1addrW;
TMP_IIC_START(); // START condition
TMP_IIC_TxByte ( DevSel ); // write DevEel adress
bError = TMP_IIC_GetACK();
if ( bError )
return FALSE;
TMP_IIC_TxByte ( addr ); // write addr
bError = TMP_IIC_GetACK();
if ( bError )
return FALSE;
TMP_IIC_TxByte(data); // write Data
bError = TMP_IIC_GetACK();
if (bError )
return FALSE;
TMP_IIC_STOP(); // STOP condition
return(TRUE);
}
void TMP_Cofig(unsigned char devnum)
{
unsigned char flg;
unsigned char wdata;
wdata = CtrlRegData;
flg=TMP_Write_Config(devnum,CtrlRegAddr,wdata);
if(devnum==0)
{
if(flg==TRUE)TMP_ErrFlg&=0x0fe;
else TMP_ErrFlg|=0x01;
}
else
{
if(flg==TRUE)TMP_ErrFlg&=0x0ef;
else TMP_ErrFlg|=0x10;
}
}
// -------------------------------------------------------------------
// TMP_IIC_Initial
// initial IIC bus before use
// look for SDA high in each cycle while SCL is high
//--------------------------------------------------------------------
void TMP_IIC_Initial(void)
{
TMP_Set_sck_low();
TMP_IIC_STOP();
delay10us(255);
TMP_Cofig(0);
TMP_Cofig(1);
TMP_Cofig(0);
}
void TMP_Read_Temp(void)
{
unsigned char flg;
unsigned char data[2];
unsigned int tempu16;
TempCold0 = 0xffff;
flg = TMP_Read_Data(0,0,&data[0]);
if(flg==TRUE)
{
TMP_ErrFlg&=0x0fe;
tempu16=data[0];
tempu16<<=8;
tempu16 &= 0xff00;
tempu16+=((unsigned int)data[1])&0x00ff;
tempu16>>=4;
if((tempu16&0x0800)!=0)tempu16|=0xf000;
TempCold0 = tempu16;
}
else TMP_ErrFlg|=0x01;
TempCold1 = 0xffff;
flg = TMP_Read_Data(1,0,&data[0]);
if(flg==TRUE)
{
TMP_ErrFlg&=0x0ef;
tempu16=data[0];
tempu16<<=8;
tempu16 &= 0xff00;
tempu16+=((unsigned int)data[1])&0x00ff;
tempu16>>=4;
if((tempu16&0x0800)!=0)tempu16|=0xf000;
TempCold1 = tempu16;
}
else TMP_ErrFlg|=0x10;
Cold_data[0] = TempCold0;
Cold_data[1] = TempCold1;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -