void init_EEPROM_externa(void){ dataEEPROMext[0] = NREG; dataEEPROMext[1] = nr; dataEEPROMext[2] = ie; dataEEPROMext[3] = il; dataEEPROMext[4] = 0; dataEEPROMext[5] = 0; dataEEPROMext[6] = 0; dataEEPROMext[7] = 0; writeEEPROMexterna(0x00,dataEEPROMext); //Cabeçalho NREG, nr, ie, il EEAckPolling(0xA0); }
//--------------------------------------------------------------------- // SONAR_Read() // Reads an integer value from SONAR connected to the adresse_i2c bus at // the specified location MSB[0X02]. with ranging inch ou cm ou us // return an integer distance [MSB,LSB] //--------------------------------------------------------------------- int SONAR_Read( char adresse_i2c, char registre_distance) { union { char b[2]; int i; } data; union { char b[2]; int i; } temp; char error,retry; error = 0; retry = 10; // Poll the SONAR up to 10 times MAX do { error = EEAckPolling(adresse_i2c); retry--; } while(error && retry > 0); retry = 10; // Attempt to read high byte of data do // up to 10 times MAX { temp.i = EERandomRead(adresse_i2c, registre_distance); retry--; } while(temp.b[1] && retry > 0); if(temp.b[1]) data.b[0] = 0xFF; // Make read result 0 if error else data.b[1] = temp.b[0]; // Otherwise get the high byte of data retry = 10; // Attempt to read low byte of data do // up to 10 times MAX { temp.i = EERandomRead(adresse_i2c, registre_distance + 1); retry--; } while(temp.b[1] && retry > 0); if(temp.b[1]) data.b[1] = 0xFF; // Make read result 0 if error else data.b[0] = temp.b[0]; // Otherwise get the low byte of data return data.i; }
unsigned char HDByteWriteI2C( unsigned char ControlByte, unsigned char HighAdd, unsigned char LowAdd, unsigned char data ) { IdleI2C(); // ensure module is idle StartI2C(); // initiate START condition while ( SSPCON2bits.SEN ); // wait until start condition is over WriteI2C( ControlByte ); // write 1 byte - R/W bit should be 0 IdleI2C(); // ensure module is idle WriteI2C( HighAdd ); // write address byte to EEPROM IdleI2C(); // ensure module is idle WriteI2C( LowAdd ); // write address byte to EEPROM IdleI2C(); // ensure module is idle WriteI2C ( data ); // Write data byte to EEPROM IdleI2C(); // ensure module is idle StopI2C(); // send STOP condition while ( SSPCON2bits.PEN ); // wait until stop condition is over while (EEAckPolling(ControlByte)); //Wait for write cycle to complete return ( 0 ); // return with no error }
//--------------------------------------------------------------------- // SONAR_Write() // Writes a char commande value to SONAR connected to the adresse_i2c bus at // the specified location 0x00 with commande ranging inch, us, or cm //--------------------------------------------------------------------- void SONAR_Write( char adresse_i2c, char commande) { char error, retry; error = 0; retry = 10; // Poll the SONAR up to 10 times MAX do { error = EEAckPolling(adresse_i2c); retry--; } while(error && retry > 0); retry = 10; // Attempt to write low byte of data do // up to 10 times MAX { error = EEByteWrite(adresse_i2c, 0x00, commande); retry--; } while(error && retry > 0); }
/********************************************************************* * Function: LDByteWriteI2C() * * Input: Control Byte, 8 - bit address, data. * * Output: None. * * Overview: Write a byte to low density device at address LowAdd * * Note: None ********************************************************************/ unsigned int LDByteWriteI2C(unsigned char ControlByte, unsigned char LowAdd, unsigned char data) { unsigned int ErrorCode; IdleI2C(); //Ensure Module is Idle StartI2C(); //Generate Start COndition WriteI2C(ControlByte); //Write Control byte IdleI2C(); ErrorCode = ACKStatus(); //Return ACK Status WriteI2C(LowAdd); //Write Low Address IdleI2C(); ErrorCode = ACKStatus(); //Return ACK Status WriteI2C(data); //Write Data IdleI2C(); StopI2C(); //Initiate Stop Condition EEAckPolling(ControlByte); //Perform ACK polling return(ErrorCode); }
unsigned int LDByteWriteI2C(unsigned char ControlByte, unsigned char LowAdd, unsigned char data) { unsigned int ErrorCode; IdleI2C(); StartI2C(); WriteI2C(ControlByte); IdleI2C(); ErrorCode = ACKStatus(); WriteI2C(LowAdd); IdleI2C(); ErrorCode = ACKStatus(); WriteI2C(data); IdleI2C(); StopI2C(); EEAckPolling(ControlByte); return(ErrorCode); }
void update_EEPROM_external(char codigoev) { indwrite = endereco*8; dataEEPROMext[0] = hours; dataEEPROMext[1] = minutes; dataEEPROMext[2] = seconds; dataEEPROMext[3] = codigoev; //SetDDRamAddr(0x46); switch(codigoev){ case 1: //while( BusyXLCD() ); //putrsXLCD("I"); dataEEPROMext[4] = (int)temperatura; dataEEPROMext[5] = 0; dataEEPROMext[6] = 0; dataEEPROMext[7] = 0; writeEEPROMexterna(indwrite,dataEEPROMext); EEAckPolling(0xA0); break; case 2: //while( BusyXLCD() ); //putrsXLCD("N"); dataEEPROMext[4] = hours; dataEEPROMext[5] = minutes; dataEEPROMext[6] = seconds; dataEEPROMext[7] = 0; writeEEPROMexterna(indwrite,dataEEPROMext); //Temperatura EEAckPolling(0xA0); break; case 3: //while( BusyXLCD() ); //putrsXLCD("h"); dataEEPROMext[4] = alarme_hours; dataEEPROMext[5] = alarme_minutes; dataEEPROMext[6] = alarme_seconds; dataEEPROMext[7] = 0; writeEEPROMexterna(indwrite,dataEEPROMext); //Temperatura EEAckPolling(0xA0); break; case 4: //while( BusyXLCD() ); //putrsXLCD("t"); dataEEPROMext[4] = alarme_temp; dataEEPROMext[5] = 0; dataEEPROMext[6] = 0; dataEEPROMext[7] = 0; writeEEPROMexterna(indwrite,dataEEPROMext); //Temperatura EEAckPolling(0xA0); break; case 5: //while( BusyXLCD() ); //putrsXLCD("l"); dataEEPROMext[4]=alarme_lum; dataEEPROMext[5]=0; dataEEPROMext[6]=0; dataEEPROMext[7]=0; writeEEPROMexterna(indwrite,dataEEPROMext); //Temperatura EEAckPolling(0xA0); break; case 6: //while( BusyXLCD() ); //putrsXLCD("A"); dataEEPROMext[4] = (int)temperatura; dataEEPROMext[5] = n_lum; dataEEPROMext[6] = 0; dataEEPROMext[7] = 0; writeEEPROMexterna(indwrite,dataEEPROMext); //Temperatura EEAckPolling(0xA0); break; case 7: //while( BusyXLCD() ); //putrsXLCD("H"); dataEEPROMext[4] = (int)temperatura; dataEEPROMext[5] = n_lum; dataEEPROMext[6] = 0; dataEEPROMext[7] = 0; writeEEPROMexterna(indwrite,dataEEPROMext); //Temperatura EEAckPolling(0xA0); break; case 8: //while( BusyXLCD() ); //putrsXLCD("T"); dataEEPROMext[4] = (int)temperatura; dataEEPROMext[5] = n_lum; dataEEPROMext[6] = 0; dataEEPROMext[7] = 0; writeEEPROMexterna(indwrite,dataEEPROMext); //Temperatura EEAckPolling(0xA0); break; case 9: //while( BusyXLCD() ); //putrsXLCD("L"); dataEEPROMext[4] = (int)temperatura; dataEEPROMext[5] = n_lum; dataEEPROMext[6] = 0; dataEEPROMext[7] = 0; writeEEPROMexterna(indwrite,dataEEPROMext); //Temperatura EEAckPolling(0xA0); break; case 10: //while( BusyXLCD() ); //putrsXLCD("L"); dataEEPROMext[4] = old_PMON; dataEEPROMext[5] = PMON; dataEEPROMext[6] = 0; dataEEPROMext[7] = 0; writeEEPROMexterna(indwrite,dataEEPROMext); //Mudança PMON EEAckPolling(0xA0); break; case 11: //while( BusyXLCD() ); //putrsXLCD("L"); dataEEPROMext[4] = NREG; dataEEPROMext[5] = nr; dataEEPROMext[6] = ie; dataEEPROMext[7] = il; writeEEPROMexterna(indwrite,dataEEPROMext); //Memória Cheia EEAckPolling(0xA0); break; default: break; } endereco++; if(nr<NREG){ //nr não pode ser maior que o nº máx de registos nr++; } if(endereco == NREG + 1){ //buffer está cheio endereco = 1; } ie=endereco-1; //índice de escrita é o seguinte ao que foi escrito agora init_EEPROM_externa(); if(nr >= NREG/2 && full == 0){ //memória está meio cheia full = 1; SetDDRamAddr(0x48); while( BusyXLCD() ); putrsXLCD((const far rom char*)"M"); update_EEPROM_external(11); escrever_USART(SOM); escrever_USART(NMCH); escrever_USART((char)NREG+'0'); escrever_USART((char)nr+'0'); escrever_USART((char)ie+'0'); escrever_USART((char)il+'0'); escrever_USART(EOM); } }