Main: #include "C8051F020.h" #include "I2C_Communication.h" #include "Std_Types.h" //defines #define TRUE 1 #define FALSE 0 #define ONE 0x06 #define TWO 0x5B #define THREE 0x4F #define FOUR 0x66 #define FIVE 0x6D #define SIX 0x7D #define SEVEN 0x07 #define EIGHT 0x7F #define NINE 0x5F #define ZERO 0x3F #define DADD 0xD0 #define RADD 0x00 #define NUMOFBYTES 7 #define STARTBYTE 0 //declaration of global variables ----------------------------------- //Functions --------------------------------------------------------- void main(void) { uint8 ucDataArrayT[8] = {13, 13, 11, 7, 12, 12, 12, 0x80}; uint8 ucDataArrayR[8]; uint8 CheckSend = 0; uint8 CheckRecieve = 0; WDTCN = 0xDE; //Watchdog disable (enables debugging) WDTCN = 0xAD; P2MDOUT = 0xFF; //Port2 defined as output P3MDOUT = 0x00; //Port3 defined as input P74OUT = 0xC0; //Port 7 enable XBR2 = 0x40; //Crossbar enable XBR0 = 0x05; //SMBus(P0.2 & P0.3) and Uart(P0.0 & P0.1) enable OSCICN |= 0x03; //set internal oscillator to 16MHz SMB0CR = -80; //SMBus clock set to 62.5kHz EIE1 |= 2; //SMBus interrupt enable EA = 1; //Global interrupt enable while(1){ while(CheckSend == 0){ CheckSend = SendData(DADD, RADD, NUMOFBYTES, STARTBYTE, ucDataArrayT); } while(CheckRecieve == 0){ CheckRecieve = ReadData(DADD, NUMOFBYTES, STARTBYTE, ucDataArrayR); } } } --------------------------------------------------------------------------------------------------------------------------------------- I2C communication: #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; } ------------------------------------------------------------------------------------------------------------------------------------