ITG3200::ITG3200(uint8_t range) { readI2C(GYRADDR, 0x00, 1, gBuffer); // Who am I? #ifdef DEBUG Serial.print("ITG-3200 ID = "); Serial.println((int) gBuffer[0]); #endif // Configure ITG-3200 // Refer to DS Section 8: Register Description. sendI2C(GYRADDR, 0x15, 0x18); // 00011000 -- Sample rate divider is 24(+1), so 40 Hz // Set Range readI2C(GYRADDR, 0x16, 1, gBuffer); gBuffer[1] = range; gBuffer[1] = (gBuffer[1] << 3); // See DS. gBuffer[0] |= gBuffer[1]; sendI2C(GYRADDR, 0x16, gBuffer[0]); #ifdef DEBUG Serial.println("ITG-3200 configured!"); #endif // Zero buffer. for (int i=0; i<READ_SIZE; i++) { gBuffer[i] = 0; } }
int getRegister(int regAddress) { char byte; int c=0; I2C2CONbits.SEN=1; while(I2C2CONbits.SEN); sendI2C(0x52); //write byte=((char)regAddress & 0xFF00) >> 8; //send high byte of 16-bit index of register. sendI2C(byte); byte=((char)regAddress & 0x00FF); //send lower byte of 16-bit index of register. sendI2C(byte); I2C2CONbits.PEN = 1; //wait while(I2C2CONbits.PEN); I2C2CONbits.SEN = 1; //wait while(I2C2CONbits.SEN); sendI2C(0x53); //send 7-bit I2C address and read bit I2C2CONbits.RCEN = 1; while(I2C2STATbits.RBF == 0); c = I2C2RCV; I2C2CONbits.ACKDT = 1; //ACK or NACK I2C2CONbits.ACKEN = 1; //enable acknowledge while(I2C2CONbits.PEN); return c; }
/********************************************************************************************************* ** Function name: doCalibrate ** Descriptions: tell slave to do a calibration, it will take about 8s after the calibration, must reread the R0 values *********************************************************************************************************/ void MutichannelGasSensor::doCalibrate(void) { sendI2C(0x22); delay(8000); if(readR0() >= 0) is_connected = 1; else is_connected = 0; }
/********************************************************************************************************* ** Function name: readData ** Descriptions: read 4 bytes from I2C slave *********************************************************************************************************/ int16_t MutichannelGasSensor::readData(uint8_t cmd) { uint16_t timeout = 0; uint8_t buffer[4]; uint8_t checksum = 0; int16_t rtnData = 0; //send command sendI2C(cmd); //wait for a while delay(2); //get response Wire.requestFrom(i2cAddress, (uint8_t)4); // request 4 bytes from slave device while(Wire.available() == 0) { if(timeout++ > 100) return -2;//time out delay(2); } if(Wire.available() != 4) return -3;//rtnData length wrong buffer[0] = Wire.read(); buffer[1] = Wire.read(); buffer[2] = Wire.read(); buffer[3] = Wire.read(); checksum = (uint8_t)(buffer[0] + buffer[1] + buffer[2]); if(checksum != buffer[3]) return -4;//checksum wrong rtnData = ((buffer[1] << 8) + buffer[2]); return rtnData;//successful }
void setFont(unsigned char fontId) { unsigned char data[3]; data[0] = GLK12232_CMD_BYTE; data[1] = GLK12232_SET_FONT; data[2] = fontId; sendI2C(GLK12232_I2C_ADDR, data, 3); }
// tested OK void setBacklight(unsigned char on, unsigned char duration) { unsigned char data[3]; data[0] = GLK12232_CMD_BYTE; data[1] = on ? GLK12232_BACKLIGHT_ON : GLK12232_BACKLIGHT_OFF; data[2] = on ? duration : NULL; sendI2C(GLK12232_I2C_ADDR, data, (on ? 3 : 2)); }
// tested unsaved OK void setContrast(unsigned char contrast, unsigned char save) { unsigned char data[3]; data[0] = GLK12232_CMD_BYTE; data[1] = save ? GLK12232_SET_CONTRAST_AND_SAVE : GLK12232_SET_CONTRAST; data[2] = contrast; sendI2C(GLK12232_I2C_ADDR, data, 3); }
void initPCF8583 (void) { startI2C(); sendI2C(PCF_ADDR_W); sendI2C(PCF_CONTROL); sendI2C(PCF_SET_CONTROL); stopI2C(); /*Direkt die Uhrzeit aus der RTC beziehen*/ getTime(); /*Das Jahr aus dem EEPROM*/ time.Year = eeprom_read(EEPROM_YEAR); time.Year += 2000; if(time.Year > 2100) { time.Year = 2015; } }
void setTextInsertionPointPixel(unsigned char x, unsigned char y) { unsigned char data[4]; data[0] = GLK12232_CMD_BYTE; data[1] = GLK12232_SET_TEXT_INS_PT_PXL; data[2] = x; data[3] = y; sendI2C(GLK12232_I2C_ADDR, data, 4); }
// tested OK void displaySavedBitmap(unsigned char num, unsigned char x, unsigned char y) { unsigned char data[5]; data[0] = GLK12232_CMD_BYTE; data[1] = GLK12232_DISPLAY_SAVED_BITMAP; data[2] = num; data[3] = x; data[4] = y; sendI2C(GLK12232_I2C_ADDR, data, 5); }
void setRegister(char data, int regAddress) { char byte; I2C2CONbits.SEN=1; //start bit while(I2C2CONbits.SEN); sendI2C(0x52); //send 7-bit I2C address and write byte byte=((char)regAddress & 0xFF00) >> 8; //send high byte of 16-bit index of register. sendI2C(byte); byte=((char)regAddress & 0x00FF); //send lower byte of 16-bit index of register. sendI2C(byte); sendI2C(data); //send 8-bits of data to register I2C2CONbits.PEN=1; //stop while(I2C2CONbits.PEN); return; }
void setTime (tTime localTime) { startI2C(); sendI2C(PCF_ADDR_W); sendI2C(PCF_SECONDS); /*Auto Inkrement..*/ sendI2C(ITOBCD(localTime.Sec)); sendI2C(ITOBCD(localTime.Min)); sendI2C(ITOBCD(localTime.Hour)); /*Jahres ID# ermitteln (0..3 wobei 0: Schaltjahr*/ sendI2C((((localTime.Year-YEAR_ID)%4)<<6) | ITOBCD(localTime.Day)); sendI2C((localTime.wDay <<5) | ITOBCD(localTime.Month)); stopI2C(); }
tTime getTime (void) { uint8_t tmp; tTime localTime; /*Default wird davon ausgegeangen, dass es sich nicht um ein Schaltjahr handelt*/ localTime.sYear = false; /*Setzten des Adresspointers der RTC*/ startI2C(); sendI2C(PCF_ADDR_W); sendI2C(PCF_SECONDS); startI2C(); sendI2C(PCF_ADDR_R); tmp = reciveI2C_ack(); localTime.Sec = BCDTOI(tmp); tmp = reciveI2C_ack(); localTime.Min = BCDTOI(tmp); tmp = reciveI2C_ack(); localTime.Hour = BCDTOI(tmp); tmp = reciveI2C_ack(); localTime.Day = BCDTOI((0x3F & tmp)); /*Jahres ID ermitteln (ist es ein Schaltjahr?)*/ if( (tmp & 0xC0) == 0x00 ) { localTime.sYear = true; } tmp = reciveI2C_nack(); localTime.Month = BCDTOI((0x1F & tmp)); localTime.wDay = BCDTOI(((0xE0 & tmp))>>5); stopI2C(); /*Anpassung an Sommer-/Winterzeit*/ if(time.stime == MESZ) { if( localTime.Month == 10 && localTime.wDay == Sonntag && localTime.Day >= 25 && localTime.Hour == 3 ) { localTime.stime = MEZ; localTime.Hour--; setTime(localTime); } } if(time.stime == MEZ) { if( localTime.Month == 3 && localTime.wDay == Sonntag && localTime.Day >= 25 && localTime.Hour == 2 ) { localTime.stime = MESZ; localTime.Hour++; setTime(localTime); } } /*Jahresinformation wird nicht durch die RTC gespeichert*/ localTime.Year = time.Year; return localTime; }
int readSensor(int i) { sendI2C(sensArray[i][0], sensArray[i][1]); // (DeviceAdress, RegisterToRead) }
void glk12232Print(unsigned char* text) { sendI2C(GLK12232_I2C_ADDR, text, strlen(text)); }
/********************************************************************************************************* ** Function name: powerOff ** Descriptions: power off sensor heater *********************************************************************************************************/ void MutichannelGasSensor::powerOff(void) { sendI2C(0x20); }
/********************************************************************************************************* ** Function name: powerOn ** Descriptions: power on sensor heater *********************************************************************************************************/ void MutichannelGasSensor::powerOn(void) { sendI2C(0x21); }
// tested OK void clearDisplay() { char data[] = { GLK12232_CMD_BYTE, GLK12232_CLEAR_DISPLAY }; sendI2C(GLK12232_I2C_ADDR, data, 2); }