unsigned char I2Cm_ReadBytes(unsigned char SlaveAdr, unsigned char *RxArray, unsigned char SubAdr0, unsigned char SubAdr1, unsigned char RxByteCount) { unsigned char i=0; SlaveAdr &= (~0x01); //for write // and the address with the Write bit. I2Cm_Start(); if(I2Cm_Put(SlaveAdr)) { I2Cm_Stop(); return 1; //exit with SlaveAddress error bit on } if(I2Cm_Put(SubAdr0)) { I2Cm_Stop(); return 2; //exit with SubAddress0 error bit on } if(I2Cm_Put(SubAdr1)) { I2Cm_Stop(); return 3; //exit with SubAddress1 error bit on } I2Cm_Stop(); udelay(50); // 50us I2Cm_Start(); SlaveAdr |= 0x01; //for read //OR the address with the Read bit. if(I2Cm_Put(SlaveAdr)) ;//return 4; //exit with SlaveAddress error bit on udelay(50); // 50us for(i=0; i<RxByteCount; i++) { RxArray[i] = I2Cm_Get(); if(i!=(RxByteCount-1)) I2Cm_ACKSend(); else I2Cm_NAKSend(); } I2Cm_Stop(); return 0; }
unsigned char I2Cm_WriteBytes(unsigned char SlaveAdr, unsigned char *TxArray, unsigned char SubAdr0, unsigned char SubAdr1, unsigned char TxByteCount) { unsigned char i=0; SlaveAdr &= (~0x01); //for write //and the address with the Write bit. I2Cm_Start(); if(I2Cm_Put(SlaveAdr)) { I2Cm_Stop(); return 1; //exit with SlaveAddress error bit on } if(I2Cm_Put(SubAdr0)) { I2Cm_Stop(); return 2; //exit with SubAddress0 error bit on } if(I2Cm_Put(SubAdr1)) { I2Cm_Stop(); return 3; //exit with SubAddress1 error bit on } udelay(50); // 50us for(i=0; i<TxByteCount; i++) { if(I2Cm_Put(TxArray[i])) { I2Cm_Stop(); return 8; //exit with write data error bit on } } I2Cm_Stop(); return 0; }
void main(void) { //Variables char lcdFirstLine[LCD_LENGTH], lcdSecondLine[LCD_LENGTH]; int displaymode = 1; int temperature[5]; int humidity[5]; /** init **/ // interrupt and SleepTimer init M8C_EnableGInt ; // Turn on interrupts SleepTimer_Start(); SleepTimer_SetInterval(SleepTimer_8_HZ); // Set interrupt to a SleepTimer_EnableInt(); // 8 Hz rate // LCD init LCD_Init(); // print welcome screen to LCD csprintf(lcdFirstLine," Welcome to "); csprintf(lcdSecondLine, " Weatherstation "); LCD_Position(0,0); LCD_PrString(lcdFirstLine); LCD_Position(1,0); LCD_PrString(lcdSecondLine); while(1) { I2Cm_Start(); //Initialize I2C I2Cm_fSendStart( 0x28, 0); //Send Measuring Request measuring(temperature, humidity); //measuring temperature and humidity switch(displaymode) { case 0: // overview(); break; case 1: printtemp(lcdFirstLine, lcdSecondLine, temperature); //write temp in the variable for the lcd break; case 2: printhum(lcdFirstLine, lcdSecondLine, humidity); //wirte humidity in the variable for the lcd break; case 3: // rain(); break; case 4: // wind(); break; default: displaymode = 0; csprintf(lcdFirstLine," Error "); csprintf(lcdSecondLine," "); } // lets see what we've got LCD_Position(0,0); LCD_PrString(lcdFirstLine); LCD_Position(1,0); LCD_PrString(lcdSecondLine); // lets sleep for a while SleepTimer_SyncWait(8, SleepTimer_WAIT_RELOAD); } }