Code:
// Includes:
#include "RP6ControlLib.h" // The RP6 Control Library.
// Always needs to be included!
#include "RP6I2CslaveTWI.h"
#define I2C_RP6_Control_ADR 0x0A
/*****************************************************************************/
void LCD_data(void)
{
static uint8_t menu = 0;
static uint8_t move = false;
// imaginary start values to see something on the LCD even if the I2C transfer doesn't work
static uint16_t adcLSL = 500;
static uint16_t adcLSR = 500;
static uint16_t adcMotorCurrentLeft = 100;
static uint16_t adcMotorCurrentRight = 100;
static uint16_t adcBat = 750;
static uint16_t temperature = (20*256+128);
static uint16_t distance = 300;
static uint16_t direction = 1800;
if(getStopwatch2()>500)
{
if(getPressedKeyNumber() == 1) // menu selection
{
menu = 1;
sound(250,80,0);
setStopwatch2(0);
}
else if(getPressedKeyNumber() == 2)
{
menu = 2;
sound(250,80,0);
setStopwatch2(0);
}
else if(getPressedKeyNumber() == 3)
{
menu = 3;
sound(250,80,0);
setStopwatch2(0);
}
else if(getPressedKeyNumber() == 4)
{
menu = 4;
sound(250,80,0);
setStopwatch2(0);
}
else if(getPressedKeyNumber() == 5)
{
setStopwatch2(0);
if (move == false)
{
move = true;
sound(180,80,25);
sound(220,80,0);
}
else
{
move = false;
sound(220,80,25);
sound(180,80,0);
}
}
}
if(!I2CTWI_readBusy)
{
I2CTWI_readRegisters[0]=move;
}
if(!I2CTWI_writeBusy && I2CTWI_writeRegisters[0])
{
adcLSL = I2CTWI_writeRegisters[0] + I2CTWI_writeRegisters[1] * 256;
adcLSR = I2CTWI_writeRegisters[2] + I2CTWI_writeRegisters[3] * 256;
adcMotorCurrentLeft = I2CTWI_writeRegisters[4] + I2CTWI_writeRegisters[5] * 256;
adcMotorCurrentRight = I2CTWI_writeRegisters[6] + I2CTWI_writeRegisters[7] * 256;
adcBat = I2CTWI_writeRegisters[8] + I2CTWI_writeRegisters[9] * 256;
temperature = I2CTWI_writeRegisters[10] + I2CTWI_writeRegisters[11] * 256;
distance = I2CTWI_writeRegisters[12] + I2CTWI_writeRegisters[13] * 256;
direction = I2CTWI_writeRegisters[14] + I2CTWI_writeRegisters[15] * 256;
I2CTWI_writeRegisters[0] = 0;
switch (menu)
{
case (1):
showScreenLCD("UBat: . V", "IL: IR: ");
setCursorPosLCD(2,11);
writeIntegerLCD(adcBat/100,DEC);
setCursorPosLCD(2,13);
writeIntegerLengthLCD(adcBat,DEC,2);
setCursorPosLCD(1,4);
writeIntegerLCD(adcMotorCurrentLeft,DEC);
setCursorPosLCD(1,12);
writeIntegerLCD(adcMotorCurrentRight,DEC);
break;
case (2):
showScreenLCD("AmbTemp: . ßC", "LSL: LSR: ");
setCursorPosLCD(2,10);
writeIntegerLCD(temperature>>8,DEC);
setCursorPosLCD(2,13);
writeIntegerLCD(((uint8_t) temperature)/25.6,DEC);
setCursorPosLCD(1,4);
writeIntegerLCD(adcLSL,DEC);
setCursorPosLCD(1,12);
writeIntegerLCD(adcLSR,DEC);
break;
case (3):
showScreenLCD("Distance: cm", "Orient: . ß");
setCursorPosLCD(2,10);
writeIntegerLCD(distance,DEC);
setCursorPosLCD(1,10);
writeIntegerLCD(direction/10,DEC);
setCursorPosLCD(1,14);
writeIntegerLengthLCD(direction,DEC,1);
break;
default:
showScreenLCD("Press Key[1]-[3]", "To see SYS-INFOS");
break;
}
}
}
void runningLight(void)
{
static uint8_t runLight = 1;
static uint8_t dir;
if(getStopwatch1() > 100) // 100ms
{
setLEDs(runLight);
if(dir == 0)
runLight <<= 1;
else
runLight >>= 1;
if(runLight > 7 )
dir = 1;
else if (runLight < 2 )
dir = 0;
setStopwatch1(0);
}
}
/*****************************************************************************/
// Main function - The program starts here:
int main(void)
{
initRP6Control(); // Always call this first! The Processor will not work
// correctly otherwise.
initLCD(); // Initialize the LC-Display (LCD)
// Always call this before using the LCD!
I2CTWI_initSlave(I2C_RP6_Control_ADR); // dem Slave die o.g. Adresse zuweisen
// Write some text messages to the UART - just like on RP6Base:
writeString_P("\n\n _______________________\n");
writeString_P(" \\| RP6 ROBOT SYSTEM |/\n");
writeString_P(" \\_-_-_-_-_-_-_-_-_-_/\n\n");
// Set the four Status LEDs:
setLEDs(0b1111);
mSleep(500);
setLEDs(0b0000);
// Play two sounds with the Piezo Beeper on the RP6Control:
sound(180,80,25);
sound(220,80,0);
showScreenLCD(" Compass ", " Navigation ");
mSleep(1000);
// Start the stopwatches:
startStopwatch1();
startStopwatch2();
while(true)
{
runningLight();
LCD_data();
}
return 0;
}
mfg WarChild
Lesezeichen