温度传感器AD7416接口程序 - Copywright www.dailzh.com -------------------------------------------------------------------------------- /*************************************************** * License: Full Free under GPL * * Author: Dailizhou in HIT * * URL: http://www.dailzh.com * ****************************************************/ #pragma small #include #include sbit Second_Led=P2^4; sbit Power_OnOff=P2^5; sbit Led1=P2^0; sbit Led2=P2^1; sbit Led3=P2^2; sbit Led4=P2^3; sbit ks=P1^0; sbit ku=P1^1; sbit kd=P1^2; sbit LE=P0^6; sbit SDA = P3^5; sbit SCL = P3^4; sbit DS_CLK = P1^3; sbit DS_IO = P1^4; sbit DS_RST = P1^5; char cp=0,cp1=0,di=0,cps=0,cpt,cps1; char buf[4],d=0,c,dt[7]; char disp_mode; typedef struct { unsigned char h,m,t; }SEG_CT; SEG_CT seg_ct[10]; /**************** IIC Access Function *******************/ /* Author: Dai LiZHou */ /* Date: 1999.8.24 */ /* Last Date: 1999.8.24 */ /********************************************************/ /********** Function Declare Begin **********************/ char Start(); /* Start IIC bus for reading or writing*/ void Stop(); /* Stop IIC bus */ char Shin(); /* Shift a byte from EEPROM, */ /* most significant first */ void Shout(char Byte); /* Shift a byte to EEPROM */ char TestAck(); /* check if Acknowledge bit is valid */ /********** Function Declare End *************************/ char Start() { SCL=1; /* release SCL */ SDA=1; /* release SDA */ _nop_(); _nop_(); _nop_(); _nop_(); if( SCL==0 || SDA==0 ) return 0; /* IIC bus not available, Start false */ else { SCL=1; /* Send start operation */ _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); SDA=0; /* SDA high-to-low when SCL high */ _nop_(); _nop_(); _nop_(); /*one cycle delay function */ _nop_(); /*declare in */ _nop_(); SCL=0; _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); SDA=1; /*release SDA */ } } /*return with SDA high, SCL low */ void Stop() { SDA=0; _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); SCL=1; _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); SDA=1; /* SDA low-to-high when SCL high, return with SCL SDA high */ } char Shin() { char i,RetByte; for(i=0;i<8;i++) { SCL=0; _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); SCL=1; _nop_(); _nop_(); RetByte=RetByte<<1; if( SDA==1 ) RetByte=RetByte|0x01; } SCL=0; _nop_(); _nop_(); /* return with SCL low, SDA high */ SDA=1; return RetByte; } void Shout( char Byte ) { /* Send one byte to EEPROM */ /* most significant */ char i; for(i=0;i<8;i++) { SCL=0; _nop_(); _nop_(); if( Byte&0x80 ) SDA=1; else SDA=0; _nop_(); _nop_(); Byte=Byte<<1; SCL=1; _nop_(); _nop_(); _nop_(); _nop_(); _nop_(); } SCL=0; _nop_(); _nop_(); _nop_(); _nop_(); SDA=1; /* return with SCL low , SDA high */ } char TestAck() { char rf=0; /* return flag */ SCL=0; _nop_(); _nop_(); _nop_(); SDA=1; /* release SDA */ _nop_(); _nop_(); SCL=1; _nop_(); _nop_(); _nop_(); _nop_(); if( SDA==1 ) rf=1; /* Acknowledge bit is invalid*/ else rf=0; SCL=0; _nop_(); _nop_(); _nop_(); return rf; } unsigned int LM74_Read(void) { unsigned char rc_h=0, rc_l=0,i; unsigned int bbData,sum; Start(); Shout( 0x90 ); /* send device address & write command */ if( !TestAck() ) { Shout(0); TestAck(); } Stop; sum=0; for(i=0;i<10;i++) { Start(); Shout( 0x91 ); /* send device address & write command */ if( !TestAck() ) { rc_h=Shin(); if( !TestAck() ) rc_l=Shin(); TestAck(); } Stop(); bbData=rc_h; bbData=_irol_(bbData,8); bbData=bbData|rc_l; bbData=_iror_(bbData,6); bbData=bbData&0x0FFF; sum+=bbData; } bbData=sum/10; return (bbData); } void LM74_Set(void) { unsigned char rc_h, rc_l; unsigned int bbData; Start(); Shout( 0x90 ); /* send device address & write command */ if( !TestAck() ) { Shout( 1 ); /* send byte address to read */ if( !TestAck() ) { Shout(0x0); TestAck(); } } Stop(); Start(); Shout( 0x91 ); /* send device address & write command */ if( !TestAck() ) { rc_l=Shin(); TestAck(); } Stop(); }