#include "I2C_Communication.h" #include "C8051F020.h" #include "Std_Types.h" uint8 SendData(uint8 ucDeviceAddress, uint8 ucRegisterAddress, uint8 ucNumberOfBytes, uint8 ucStartByte, uint8 ucValuesT[8]){ uint8 ucSmBusy; uint8 test = 0; static uint8 ucCnt = 0; uint8 CheckSend = 0; SMB0CN = 0x40; ucSmBusy = 1; STO = 0; STA = 1; while(ucSmBusy){ while(SI == 1){ switch(SMB0STA){ case START: SI = 0; STA = 0; SMB0DAT = ucDeviceAddress | WRITE; break; case T_ADDACK: SI = 0; SMB0DAT = ucRegisterAddress; //send register address break; case T_ADDNACK: SI = 0; STO = 1; STA = 1; SMB0CN = 0x00; CheckSend = 0; break; case T_DACK: SI = 0; if(ucCnt < ucNumberOfBytes){ test = 1; SMB0DAT = ucValuesT[ucStartByte + ucCnt]; ucCnt++; } else{ test = 2; ucCnt = 0; STA = 0; STO = 1; ucSmBusy = 0; SMB0CN = 0x00; CheckSend = 1; } break; case T_DNACK: SI = 0; ucCnt = 0; STA = 0; STO = 1; SMB0CN = 0x00; CheckSend = 0; break; default: //SI = 1; break; } } } return CheckSend; } uint8 ReadData(uint8 ucDeviceAddress, uint8 ucNumberOfBytes, uint8 ucStartByte, uint8 ucValuesR[8]){ uint8 ucSmBusy; static uint8 ucCnt = 0; uint8 CheckRecieve = 0; ucSmBusy = 1; SMB0CN = 0x44; STO = 0; STA = 1; while(ucSmBusy){ while(SI == 1){ switch(SMB0STA){ case START: SI = 0; STA = 0; SMB0DAT = ucDeviceAddress | READ; break; case R_ADDACK: SI = 0; //SMB0DAT = ucRegisterAddress; //send register address break; case R_ADDNACK: SI = 0; STO = 1; STA = 1; SMB0CN = 0x00; CheckRecieve = 0; break; case R_DACK: if(ucCnt < (ucNumberOfBytes - 1)){ SI = 0; ucValuesR[ucStartByte + ucCnt] = SMB0DAT; ucCnt++; } else{ ucCnt = 0; STA = 0; STO = 1; ucSmBusy = 0; SMB0CN = 0x00; CheckRecieve = 1; } break; case R_DNACK: ucCnt = 0; STA = 0; STO = 1; SMB0CN = 0x00; CheckRecieve = 0; break; default: break; } } } return CheckRecieve; }