char MOD_IO2::WriteSingle( char data ) { char i; // Send the 8 bits for(i = 0; i<8; i++) { WaitMicrosecond(1000); if(data&RW_MASK) SDA_IN(); else SDA_OUT(); data <<= 1; WaitMicrosecond(1000); SCL_IN(); WaitMicrosecond(1000); SCL_OUT(); } // Read the ACK WaitMicrosecond(1000); SDA_IN(); WaitMicrosecond(1000); SCL_IN(); WaitMicrosecond(1000); i = digitalRead(pinSDA); SCL_OUT(); WaitMicrosecond(1000); return i; }
char MOD_IO2::ReadSingle( char ack ) { char data = 0, i; SDA_IN(); for(i = 0; i < 8; i++) { WaitMicrosecond(1000); SCL_IN(); while(digitalRead(pinSCL)==0); WaitMicrosecond(1000); data |= digitalRead(pinSDA); if( i!=7 ) data<<=1; WaitMicrosecond(1000); SCL_OUT(); WaitMicrosecond(1000); } // send the ACK/NACK WaitMicrosecond(1000); if(ack) SDA_IN(); else SDA_OUT(); WaitMicrosecond(1000); SCL_IN(); WaitMicrosecond(1000); SCL_OUT(); WaitMicrosecond(1000); return data; }
/******************************************************************************* **函 数: IIC_InitPort **功 能: IIC接口初始化 **参 数: void **返 回: void *******************************************************************************/ void IIC_InitPort(void) { printf("\n BL5372 IIC Port Init"); // Init IIC_1_SCL_PIN gpio_init(&gpio_iic_1_scl, IIC_1_SCL_PIN); gpio_dir(&gpio_iic_1_scl, PIN_OUTPUT); // Direction: Output gpio_mode(&gpio_iic_1_scl, PullNone); // No pull gpio_write(&gpio_iic_1_scl, 1); // Init IIC_1_SDA_PIN gpio_init(&gpio_iic_1_sda, IIC_1_SDA_PIN); gpio_dir(&gpio_iic_1_sda, PIN_OUTPUT); // Direction: Output gpio_mode(&gpio_iic_1_sda, PullNone); // No pull gpio_write(&gpio_iic_1_sda, 1); SDA_OUT(); SCL_OUT(); SCL1; SDA1; }
void MOD_IO2::Start() { SDA_OUT(); WaitMicrosecond(1000); SCL_OUT(); WaitMicrosecond(1000); }
//---------------------------------------------------------------------------------- void s_connectionreset(void) //---------------------------------------------------------------------------------- // communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart // _____________________________________________________ ________ // DATA: |_______| // _ _ _ _ _ _ _ _ _ ___ ___ // SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______ { unsigned char i; SDA_OUT(1); SCL_OUT(0); //Initial state for(i=0;i<9;i++) //9 SCK cycles { SCL_OUT(1); SCL_OUT(0); } s_transstart(); //transmission start }
MOD_IO2::MOD_IO2 (unsigned char Address, unsigned char nSDA, unsigned char nSCL) { SL_READ = (Address<<1) | 1; SL_WRITE = (Address<<1) | 0; pinSDA = nSDA; pinSCL = nSCL; SCL_OUT(); SDA_OUT(); }
//---------------------------------------------------------------------------------- char s_read_byte(unsigned char ack) //---------------------------------------------------------------------------------- // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" { unsigned char i,val=0; SDA_OUT(1); //release DATA-line for (i=0x80;i>0;i=i>>1) //shift bit for masking { SCL_OUT(1); //clk for SENSI-BUS if (SDA_READ()) val=(val | i); //read bit SCL_OUT(0); } SDA_OUT(!ack); //in case of "ack==1" pull down DATA-Line delay_us(2); //observe setup time SCL_OUT(1); //clk #9 for ack delay_us(6); //pulswith approx. 5 us SCL_OUT(0); delay_us(2); //observe hold time SDA_OUT(1); //release DATA-line return val; }
//---------------------------------------------------------------------------------- void s_transstart(void) //---------------------------------------------------------------------------------- // generates a transmission start // _____ ________ // DATA: |_______| // ___ ___ // SCK : ___| |___| |______ { SDA_OUT(1); SCL_OUT(0); //Initial state delay_us(2); SCL_OUT(1); delay_us(2); SDA_OUT(0); delay_us(2); SCL_OUT(0); delay_us(6); SCL_OUT(1); delay_us(2); SDA_OUT(1); delay_us(2); SCL_OUT(0); }
//---------------------------------------------------------------------------------- char s_write_byte(unsigned char value) //---------------------------------------------------------------------------------- // writes a byte on the Sensibus and checks the acknowledge { unsigned char i,error=0; for (i=0x80;i>0;i/=2) //shift bit for masking { if (i & value) SDA_OUT(1); //masking value with i , write to SENSI-BUS else SDA_OUT(0); delay_us(2); //observe setup time SCL_OUT(1); //clk for SENSI-BUS delay_us(6); //pulswith approx. 5 us SCL_OUT(0); delay_us(2); //observe hold time } SDA_OUT(1); //release DATA-line delay_us(2); //observe setup time SCL_OUT(1); //clk #9 for ack error=SDA_READ(); //check ack (DATA will be pulled down by SHT11) SCL_OUT(0); return error; //error=1 in case of no acknowledge }
/* * Power up the device. The device can be used after an additional * 11ms waiting time. */ void sht11_init(void) { /* * SCL Output={0,1} * SDA 0: Output=0 * 1: Input and pull-up (Output=0) */ SHT11_PxDIRPWR|= _BV(SHT11_ARCH_PWR); SHT11_PxOUTPWR |= _BV(SHT11_ARCH_PWR); SDA_OUT(); SCL_OUT(); }
/*---------------------------------------------------------------------------*/ static void sstart(void) { SDA_OUT(); SCL_OUT(); SDA_1(); SCL_0(); delay_400ns(); SCL_1(); delay_400ns(); SDA_0(); delay_400ns(); SCL_0(); delay_400ns(); SCL_1(); delay_400ns(); SDA_1(); delay_400ns(); SCL_0(); }
void clear_SCL(void) // Actively drive SCL signal low { SCL_OUT(); SCL_OFF(); }