Skip to content
Snippets Groups Projects
Commit 95cd8d3a authored by Peter Gerwinski's avatar Peter Gerwinski
Browse files

Beispiel für I²C: Kompaß-Modul von RP6 aus ansteuern

parent f2ed257c
Branches
No related tags found
No related merge requests found
#include "RP6RobotBaseLib.h"
#include "RP6I2CmasterTWI.h"
void read_compass (uint16_t *x, uint16_t *y)
{
uint8_t result[5];
I2CTWI_transmit2Bytes (0x60, 0x00, 0x02); // set coil
mSleep (1);
I2CTWI_transmit2Bytes (0x60, 0x00, 0x04); // reset coil
mSleep (5);
I2CTWI_transmit2Bytes (0x60, 0x00, 0x01); // Messung starten
mSleep (5); // 5ms warten, bis Sensor fertig gemessen hat
I2CTWI_transmitByte (0x60, 0x01); // Leseindex setzen
I2CTWI_readBytes (0x61, result, 4); // 4 Bytes lesen. In result liegt nun (msb x, lsb x, msb y, lsb y).
result[0] &= 0b00001111; // Unwichtge Bits vom msb abschneiden
result[2] &= 0b00001111;
*x = (result[0] << 8) + result[1]; // Wert berechnen aus msb und lsb
*y = (result[2] << 8) + result[3];
}
int main (void)
{
uint16_t compass_x = 0;
uint16_t compass_y = 0;
initRobotBase ();
I2CTWI_initMaster (100);
powerON ();
changeDirection (RIGHT);
moveAtSpeed (20, 20);
startStopwatch1 ();
while (1)
{
task_RP6System ();
if (getStopwatch1 () > 200)
{
read_compass (&compass_x, &compass_y);
writeInteger (compass_x, DEC);
writeChar (' ');
writeInteger (compass_y, DEC);
writeChar ('\n');
setStopwatch1 (0);
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment