@carlitoco:
Ich habe 'mal ein Auslesen eines 16-Bit-Werts als Beispiel angehängt. Die M32 liest den ADC4 der Base über I2C aus. Du müßtest da für die Batteriespannung nur ADC7 nehmen.
Die RP6Base (Slave) hat als einfachstes Programm dies hier:
Code:
/*****************************************************************************/
// Includes:
#include "RP6RobotBaseLib.h"
#include "RP6I2CslaveTWI.h" // Include the I²C-Bus Slave Library
/*****************************************************************************/
// Defines:
#define RP6BASE_I2C_SLAVE_ADR 10
#define ADC_INT1 4
/*****************************************************************************/
// Variables:
uint16_t adc4;
/*****************************************************************************/
// Main - The program starts here:
int16_t main(void)
{
initRobotBase();
setLEDs(0b111111);
mSleep(500);
setLEDs(0b000000);
I2CTWI_initSlave(RP6BASE_I2C_SLAVE_ADR);
powerOFF();
while(true) {
mSleep(500);
if (!I2CTWI_readBusy) {
adc4 = readADC(ADC_INT1);
I2CTWI_readRegisters[0] = (uint8_t)(adc4);
I2CTWI_readRegisters[1] = (uint8_t)(adc4 >> 8);
}
}
/*****************************************************************************/
// End of main:
return 0;
}
Auf der M32 (Master) läuft die Abfrage z.B. so (Programm-Ausschnitt):
Code:
/*****************************************************************************/
// Includes:
#include "RP6ControlLib.h" // The RP6 Control Library.
// Always needs to be included!
#include "RP6I2CmasterTWI.h"
/*****************************************************************************/
// Defines:
#define RP6BASE_I2C_SLAVE_ADR 10 // Slave I2C address
/*****************************************************************************/
// I2C Error handler
/**
* This function gets called automatically if there was an I2C Error like
* the slave sent a "not acknowledge" (NACK, error codes e.g. 0x20 or 0x30).
* The most common mistakes are:
* - using the wrong address for the slave
* - slave not active or not connected to the I2C-Bus
* - too fast requests for a slower slave
* Be sure to check this if you get I2C errors!
*/
void I2C_transmissionError(uint8_t errorState)
{
writeString_P("\nI2C ERROR - TWI STATE: 0x");
writeInteger(errorState, HEX);
writeChar('\n');
}
/*****************************************************************************/
// Main function - The program starts here:
int main(void)
{
initRP6Control(); // Always call this first! The Processor will not
// work correctly otherwise.
// IMPORTANT:
I2CTWI_initMaster(100); // Initialize the TWI Module for Master operation
// with 100kHz SCL Frequency
// Register the event handlers:
I2CTWI_setTransmissionErrorHandler(I2C_transmissionError);
// Lesen der Daten (hier 2 Bytes) mit ...
I2CTWI_transmitByte(RP6BASE_I2C_SLAVE_ADR, 0);
I2CTWI_readBytes(RP6BASE_I2C_SLAVE_ADR, results, 2);
Gruß Dirk
Lesezeichen