void IICInit(void) { /* ### Init_IIC init code */ /* IICF: MULT1=0,MULT0=1,ICR5=0,ICR4=0,ICR3=0,ICR2=0,ICR1=0,ICR0=0 */ IICF = 0x40; /* IICA: ADDR7=0,ADDR6=0,ADDR5=0,ADDR4=0,ADDR3=0,ADDR2=0,ADDR1=0 */ IICA = 0x00; /* IICS: TCF=0,IAAS=0,BUSY=0,ARBL=1,SRW=0,IICIF=1,RXAK=0 */ IICS = 0x12; /* Clear the interrupt flags */ /* IICC: IICEN=1 */ IICC |= (unsigned char)0x80; /* IICC: IICEN=1,IICIE=0,MST=0,TX=1,TXAK=0,RSTA=0 */ IICC = 0x90; }
void IICWriteByte(word adr, byte dt){ word temp; gAdr=adr; temp = (gAdrH & 0x01) << 1; //get A8 of 24c04 address. IICC_TXAK=0; IICC|=0x30; //MST=1 TX=1 and generate START condition IICD =IIC_SLAVE_ADR|temp; while(!IICS_IICIF); //wait untill TX completed IICS_IICIF=1; //clear IICIF while(IICS_RXAK); //check for slave ack IICD = gAdrL; while(!IICS_IICIF); IICS_IICIF=1; while(IICS_RXAK); IICD=dt; while(!IICS_IICIF); IICS_IICIF=1; while(IICS_RXAK); IICS_IICIF=1; IICC_MST=0; }
void IICWriteN(word adr,byte *dt,byte n){
byte i; for(i=0;i<n;++i) { IICWriteByte(adr+i,dt); } }
byte IICReadByte(word adr){
word temp; byte d; gAdr=adr; temp = (gAdrH&0x01)<<1; //get A8 of 24c04 address. IICC_TXAK=0; IICC|=0x30; //MST=1 TX=1 and generate START condition IICD =IIC_SLAVE_ADR|temp; while(!IICS_IICIF); //wait untill TX completed IICS_IICIF=1; //clear IICIF while(IICS_RXAK); //check for slave ack IICD = gAdrL; while(!IICS_IICIF); IICS_IICIF=1; while(IICS_RXAK); IICS_IICIF=1; IICC_RSTA=1; IICD =IIC_SLAVE_ADR|temp; while(!IICS_IICIF); //wait untill TX completed IICS_IICIF=1; //clear IICIF while(IICS_RXAK); //check for slave ack IICC_TX=0; IICC_TXAK=1; d=IICD; //dummy read while(!IICS_IICIF); IICS_IICIF=1; IICC_MST=0; d=IICD; return d; }
byte* IICReadN(word adr,byte n){ word temp; byte i,d; static byte * dt; gAdr=adr; temp = (gAdrH&0x01)<<1; //get A8 of 24c04 address. IICC_TXAK=0; IICC|=0x30; //MST=1 TX=1 and generate START condition IICD =IIC_SLAVE_ADR|temp; while(!IICS_IICIF); //wait untill TX completed IICS_IICIF=1; //clear IICIF while(IICS_RXAK); //check for slave ack IICD = gAdrL; while(!IICS_IICIF); IICS_IICIF=1; while(IICS_RXAK); IICS_IICIF=1; IICC_RSTA=1; IICD =IIC_SLAVE_ADR|temp|1; while(!IICS_IICIF); //wait untill TX completed IICS_IICIF=1; //clear IICIF while(IICS_RXAK); //check for slave ack IICC_TX=0; //IIC_TXAK=1; d=IICD; //dummy read while(!IICS_IICIF); IICS_IICIF=1; for(i=0;i<(n-1);++i){ dt=IICD; while(!IICS_IICIF); IICS_IICIF=1; } IICC_TXAK=1; dt=IICD; while(!IICS_IICIF); IICC_MST=0; return dt; } |