/** * Call this function to start the Transceiver without specifing new transmission data. Useful for restarting * a transmission, or just starting the transceiver for reception. The driver will reuse the data previously put * in the transceiver buffers. The function will hold execution (loop) until the TWI_ISR has completed with the * previous operation, then initialize the next operation and return. */ void TWI_StartTransceiver(void) { while (TWI_TransceiverBusy() ) {} // Wait until TWI is ready for next transmission. TWI_statusReg.all = 0; TWI_state = TWI_NO_STATE; TWCR = (1 << TWEN) | // TWI Interface enabled. (1 << TWIE) | (1 << TWINT) | // Enable TWI Interupt and clear the flag. (1 << TWEA) | (0 << TWSTA) | (0<<TWSTO) | // Prepare to ACK next time the Slave is addressed. (0 << TWWC); TWI_busy = 0; }
/** * Call this function to read out the received data from the TWI transceiver buffer. I.e. first call * TWI_Start_Transceiver to get the TWI Transceiver to fetch data. Then Run this function to collect the * data when they have arrived. Include a pointer to where to place the data and the number of bytes * to fetch in the function call. The function will hold execution (loop) until the TWI_ISR has completed * with the previous operation, before reading out the data and returning. * If there was an error in the previous transmission the function will return the TWI State code. */ unsigned char TWI_GetDataFromTransceiver(uint8_t *msg, uint8_t msgSize) { uint8_t i; while (TWI_TransceiverBusy()) {} // Wait until TWI is ready for next transmission. if(TWI_statusReg.lastTransOK) { // Last transmission completed successfully. for (i = 0; i < msgSize; i++) { // Copy data from Transceiver buffer. msg[ i ] = TWI_buf[ i ]; } TWI_statusReg.RxDataInBuf = FALSE; // Slave Receive data has been read from buffer. } return(TWI_statusReg.lastTransOK); }
extern uint8_t UpdateTime(dstime_t* current_time) { uint8_t result=0; /*устанавливаем указатель DS1307 на нулевой адрес*/ twiBuf[0] = (DS1307_ADR<<1)|0; //адресный пакет twiBuf[1] = 0; //адрес регистра twiMsgSize = 2; while(TWI_TransceiverBusy()); //ждем, когда TWI модуль освободится twiState = TWI_NO_STATE ; TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWSTA); //разрешаем прерывание и формируем состояние старт while(TWI_TransceiverBusy()); /*считываем время с DS1307*/ twiBuf[0] = (DS1307_ADR<<1)|1; twiMsgSize = 5; while(TWI_TransceiverBusy()); //ждем, когда TWI модуль освободится twiState = TWI_NO_STATE ; TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWSTA); //разрешаем прерывание и формируем состояние старт while(TWI_TransceiverBusy()); if(twiState == TWI_SUCCESS) { //if ((current_time->min != twiBuf[2]) && (current_time->hour != twiBuf[3])) //{ //result = 1; //} // current_time->min = twiBuf[2]; current_time->hour = twiBuf[3]; current_time->day = twiBuf[4]; } return result; }
/** * Call this function to send a prepared message, or start the Transceiver for reception. Include * a pointer to the data to be sent if a SLA+W is received. The data will be copied to the TWI buffer. * Also include how many bytes that should be sent. Note that unlike the similar Master function, the * Address byte is not included in the message buffers. * The function will hold execution (loop) until the TWI_ISR has completed with the previous operation, * then initialize the next operation and return. */ void TWI_StartTransceiverWithData(uint8_t *msg, uint8_t msgSize) { unsigned char temp; while (TWI_TransceiverBusy()) {} // Wait until TWI is ready for next transmission. TWI_msgSize = msgSize; // Number of data to transmit. for (temp = 0; temp < msgSize; temp++) { // Copy data that may be transmitted if the TWI Master requests data. TWI_buf[ temp ] = msg[ temp ]; } TWI_statusReg.all = 0; TWI_state = TWI_NO_STATE; TWCR = (1 << TWEN) | // TWI Interface enabled. (1 << TWIE) | (1 << TWINT) | // Enable TWI Interupt and clear the flag. (1 << TWEA) | (0 << TWSTA) | (0 << TWSTO)| // Prepare to ACK next time the Slave is addressed. (0 << TWWC); TWI_busy = 1; }
/** * Call this function to fetch the state information of the previous operation. The function will hold execution (loop) * until the TWI_ISR has completed with the previous operation. If there was an error, then the function * will return the TWI State code. */ unsigned char TWI_GetStateInfo(void) { while (TWI_TransceiverBusy()) {} // Wait until TWI has completed the transmission. return (TWI_state); // Return error state. }