int16_t ReadsMasterI2C (uint8_t adresse, uint8_t nb_octet, uint8_t offset) { uint8_t j; int16_t retval = 0; if (StartI2C () != 1) { return ERREUR_I2C_START; } if (WriteI2C ((adresse<<1)+1) != 1) { return ERREUR_I2C_WRITE; } for (j=0; j<nb_octet; j++) { retval = ReadI2C (); //FIXME : WTF?!!! Probleme depuis ajout aversive, strat autom // Carte PLVL 1 ok, test avec backup i2c ok, donc le problème se situe bien au niveau // de cette carte... Delay_Xx10us (70); if (retval < 0) { i2c.buffer_rx[offset+j] = 0; return ERREUR_I2C_READ; } else { i2c.buffer_rx[offset+j] = (unsigned char)retval; } if ((j+1) >= nb_octet) NackI2C (); else AckI2C (); } if (StopI2C () != 1) { return ERREUR_I2C_STOP; } return 1; }
/********************************************************************* * Function: getsI2C() * Input: array pointer, Length. * Overview: read Length number of Bytes into array ********************************************************************/ unsigned int getsI2C(unsigned char *rdptr, unsigned char Length) { while (Length --) { *rdptr++ = getI2C(); //get a single byte if(I2C1STATbits.BCL) //Test for Bus collision { return(-1); } if(Length) { AckI2C(); //Acknowledge until all read } } return(0); }
static void EEPROM_read_seq(BYTE devaddr, BYTE *data, BYTE size) { BYTE i = 0; StartI2C(); IdleI2C(); WriteI2C( (devaddr << 1) | 0x01 ); IdleI2C(); for(; i < size; ++i) { data[i] = ReadI2C(); if(i < size-1) { AckI2C(); } else { NotAckI2C(); } } StopI2C(); }
void ADXL345_multiByteRead(unsigned char startAddress, char* buffer, unsigned char size) { #if I2C unsigned char i; StartI2C(); WriteI2C(ADXL343_ADDR_WRITE); WriteI2C(startAddress); RestartI2C(); WriteI2C(ADXL343_ADDR_READ); for (i = 0; i < size; i++) { buffer[i] = ReadI2C(); //keep the clock pulsing // if not last byte, send ack // if last byte, send nack if(i < size-1) { AckI2C(); } else { NotAckI2C(); } } StopI2C(); #elif SPI unsigned char tx = (ADXL345_SPI_READ | ADXL345_MULTI_BYTE | (startAddress & 0x3F)); // the &0x3F restricts reading from only the XYZ data registers unsigned char i; SPI_CS_PIN = 0; //CS pin low, ie enable chip Delay1TCY(); // delay at least 5 ns WriteSPI(tx); //Send address to start reading from. for (i = 0; i < size; i++) { buffer[i] = ReadSPI(); //keep the clock pulsing } SPI_CS_PIN = 1; //CS pin high, ie disable chip #endif }
static int i2c_read(struct i2c_platform_data *adap, unsigned char *buf, unsigned int len) { int i; u32 data; pr_debug("i2c_read\n"); i = 0; while (i < len) { data = MasterReadI2C(adap); buf[i++] = data; if (i < len) AckI2C(adap); else NotAckI2C(adap); } StopI2C(adap); IdleI2C(adap); return 0; }
void readTempSensor(unsigned char *byte1, unsigned char *byte2, unsigned char *slope, unsigned char *counter) { StartI2C(); IdleI2C(); WriteI2C(0x9E); // slave address + W IdleI2C(); WriteI2C(0xAA); // read two bytes command IdleI2C(); RestartI2C(); IdleI2C(); WriteI2C(0x9F); // slave address + R IdleI2C(); (*byte1) = ReadI2C(); IdleI2C(); AckI2C(); IdleI2C(); (*byte2) = ReadI2C(); IdleI2C(); NotAckI2C(); IdleI2C(); StopI2C(); Delay10KTCYx(1); // read counter StartI2C(); IdleI2C(); WriteI2C(0x9E); // slave address + W IdleI2C(); WriteI2C(0xA8); // read counter command IdleI2C(); RestartI2C(); IdleI2C(); WriteI2C(0x9F); // slave address + R IdleI2C(); (*counter) = ReadI2C(); IdleI2C(); NotAckI2C(); IdleI2C(); StopI2C(); Delay10KTCYx(1); // read slope StartI2C(); IdleI2C(); WriteI2C(0x9E); // slave address + W IdleI2C(); WriteI2C(0xA9); // read slope command IdleI2C(); RestartI2C(); IdleI2C(); WriteI2C(0x9F); // slave address + R IdleI2C(); (*slope) = ReadI2C(); IdleI2C(); NotAckI2C(); IdleI2C(); StopI2C(); Delay10KTCYx(1); }
void getDS1307(void) { int hora=0, minuto=0, segundo=0, diasemana=0, dia=0, mes=0, ano=0, dummy=0; char msg[40]; //#define StartI2C() SSPCON2bits.SEN=1;while(SSPCON2bits.SEN) LED_AMAR=1; IdleI2C(); StartI2C(); //IdleI2C(); __delay_us(16); WriteI2C( 0xD0 ); //IdleI2C(); __delay_us(60); WriteI2C( 0x00 ); IdleI2C(); __delay_us(16); //AckI2C();AckI2C();AckI2C();AckI2C();AckI2C();AckI2C();AckI2C();AckI2C(); StopI2C(); //#define StopI2C() SSPCON2bits.PEN=1;while(SSPCON2bits.PEN) //IdleI2C(); __delay_us(26); RestartI2C(); __delay_us(16); WriteI2C( 0xD1 ); __delay_us(1); IdleI2C(); segundo =ReadI2C(); AckI2C(); IdleI2C(); minuto =ReadI2C(); AckI2C(); IdleI2C(); hora =ReadI2C(); AckI2C(); IdleI2C(); diasemana=ReadI2C(); AckI2C(); IdleI2C(); dia =ReadI2C(); AckI2C(); IdleI2C(); mes =ReadI2C(); AckI2C(); IdleI2C(); ano =ReadI2C(); AckI2C(); IdleI2C(); dummy =ReadI2C(); //AckI2C(); //__delay_us(16); //IdleI2C(); //NotAckI2C(); //IdleI2C(); StopI2C(); //#define StopI2C() SSPCON2bits.PEN=1;while(SSPCON2bits.PEN) LED_VERM = 0; LED_AMAR=0; LED_VERD=1; sprintf(msg,"%xh:%xm:%xs _ dia %x/%x/%x _ ", hora,minuto,segundo,dia,mes,ano); while(BusyUSART()); putsUSART( msg ); LED_VERD=0; }
void getTemperaturaHumidade (void) { unsigned char TEMPL=0, TEMPH=0, HUMIDL=0, HUMIDH=0; unsigned char DUMMY=0, OP=0, BT=0; float humidade, temperatura; char msg[55]; LED_AMAR=1; //#define StartI2C() SSPCON2bits.SEN=1;while(SSPCON2bits.SEN) StartI2C(); // ACORDAR DEVICE __delay_us(16); WriteI2C(0xB8); // endereco Slave do AM2315 __delay_us(135); StopI2C(); //#define StopI2C() SSPCON2bits.PEN=1;while(SSPCON2bits.PEN) // com clock de 4 mhz: // 10K (100) = 1000 ms // 1K (100) = 100 ms // 1K (10) = 10 ms // 1K (2) = 2 ms // Delay100TCYx(); __delay_us(25); RestartI2C(); // REQUISITAR PEDIDO DE BYTES __delay_us(16); WriteI2C(0xB8); // endereco Slave do AM2315 __delay_us(60); // manual do AM2315 recomenda minimo de 30us WriteI2C(0x03); // byte que simboliza a temperatura __delay_us(60); WriteI2C(0x00); // start byte para leitura __delay_us(60); WriteI2C(0x04); // quantidades de bytes a serem lidos; //AckI2C(); __delay_us(16); StopI2C(); //#define StopI2C() SSPCON2bits.PEN=1;while(SSPCON2bits.PEN) __delay_ms(10); // manual do AM2315 recomenda esperar no minimo 10ms RestartI2C(); WriteI2C(0xB9); // endereco Slave do AM2315 //AckI2C(); // retirado por nao necessitar (?) __delay_us(60); // manual do AM2315 recomenda minimo de 30us IdleI2C(); OP = ReadI2C(); // 1o byte AckI2C(); IdleI2C(); BT = ReadI2C(); // 2o byte AckI2C(); IdleI2C(); HUMIDL = ReadI2C(); // 3o byte AckI2C(); IdleI2C(); HUMIDH = ReadI2C(); // 4o byte AckI2C(); IdleI2C(); TEMPL = ReadI2C(); // 5o byte AckI2C(); IdleI2C(); TEMPH = ReadI2C(); // 6o byte AckI2C(); IdleI2C(); DUMMY = ReadI2C(); // 7o byte AckI2C(); IdleI2C(); DUMMY = ReadI2C(); // 8 byte //__delay_us(16); StopI2C(); //#define StopI2C() SSPCON2bits.PEN=1;while(SSPCON2bits.PEN) LED_VERM=0; LED_AMAR=0; LED_VERD=1; // Calculos obtidos do exemplo do Arduino humidade = HUMIDL; humidade *= 256; humidade += HUMIDH; humidade /= 10; temperatura = TEMPL; temperatura *= 256; temperatura += TEMPH; temperatura /= 10; /* ou ainda RH = RHH << 8; RH |= RHL; TEMP = TEMPH << 8; TEMP |= TEMPL; */ sprintf (msg, "Temp= %0.2f, Humid= %0.2f .", temperatura, humidade); while(BusyUSART()); putsUSART(msg); while(BusyUSART()); putrsUSART("\n\r"); LED_VERD=0; }