Code:
/*
* ****************************************************************************
* RP6 ROBOT SYSTEM - RP6 CONTROL M32 Examples
* ****************************************************************************
* Example: RP6Control MultiIO
* Author(s): Dirk
* ****************************************************************************
* Description:
* In this example we show a first test for the MultiIO Project Board.
*
* ############################################################################
* The Robot does NOT move in this example! You can simply put it on a table
* next to your PC and you should connect it to the PC via the USB Interface!
* ############################################################################
* ****************************************************************************
*/
/*****************************************************************************/
// Includes:
#include "RP6ControlLib.h" // The RP6 Control Library.
// Always needs to be included!
#include "RP6I2CmasterTWI.h" // Include the I2C-Bus Master Library
/*****************************************************************************/
/*****************************************************************************/
// Include our new "RP6Control MultiIO library":
// (This is the library for accessing the MultiIO Project Board!)
#include "RP6Control_MultiIOLib.h"
/*****************************************************************************/
/**
* Write a floating point number to the UART.
*
* Example:
*
* // Write a floating point number to the UART (no exponent):
* writeDouble(1234567.890, 11, 3);
*
* The value of prec (precision) defines the number of decimal places.
* For 32 bit floating point variables (float, double ...) 6 is
* the max. value for prec (7 relevant digits).
* The value of width defines the overall number of characters in the
* floating point number including the decimal point. The number of
* pre-decimal positions is: (width - prec - 1).
*/
void writeDouble(double number, uint8_t width, uint8_t prec)
{char buffer[width + 1];
dtostrf(number, width, prec, &buffer[0]);
writeString(&buffer[0]);
}
/*****************************************************************************/
// 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 IS: 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.
initLCD(); // Initialize the LC-Display (LCD)
// Always call this before using the LCD!
setLEDs(0b1111);
mSleep(500);
setLEDs(0b0000);
writeString_P("\n\nRP6Control Multi IO Selftest 1!\n");
// IMPORTANT:
I2CTWI_initMaster(100); // Initialize the TWI Module for Master operation
// with 100kHz SCL Frequency
// Register the event handler:
I2CTWI_setTransmissionErrorHandler(I2C_transmissionError);
setLEDs(0b1111);
// Write a text message to the LCD:
showScreenLCD("################", "################");
mSleep(1500);
showScreenLCD(" RP6Control M32", "Example Program");
mSleep(2500);
showScreenLCD(" RP6 Multi IO", " Selftest 1");
mSleep(2500);
clearLCD();
setLEDs(0b0000);
// ---------------------------------------
uint8_t onoff = 0;
uint16_t servopos = SERVO1_LT;
startStopwatch1();
// IMPORTANT:
multiio_init(); // MultiIO init!!!
//setServoPower(1); // Servo power ON!
// ----------------------------------------------
// Set RTC once (battery empty or not existing:
/* rtc_time.second = 10;
rtc_time.minute = 39;
rtc_time.hour = 15; // 12:00
rtc_date.weekday = R_TH;
rtc_date.day = 04;
rtc_date.month = 04;
rtc_date.year = 2013; // Do, 28.3.2013
DS1307_write();*/
// Remove this, if RTC is set and running!!!
// ----------------------------------------------
// EEPROM test:
writeString_P("\nWriting 128 to EEPROM address 5:\n");
I2C_EEPROM_writeByte(5, 128);
mSleep(500);
writeString_P("Done!\n");
writeString_P("\nReading EEPROM address 5:\n");
uint8_t tmp = I2C_EEPROM_readByte(5);
mSleep(500);
I2C_EEPROM_writeByte(5, 0);
writeString_P("Done!\n");
writeString_P("EEPROM address 5 content: ");
writeInteger(tmp, DEC);
writeString_P("\n");
// Buzzer test:
buzzer(330);
mSleep(200);
buzzer(330);
mSleep(200);
buzzer(330);
mSleep(1000);
buzzer(330);
mSleep(200);
buzzer(330);
mSleep(200);
buzzer(330);
while(true)
{
if(getStopwatch1() > 3000) // 1000 = 1s
{
if (onoff) onoff = 0;
else onoff = 1;
// Buttons ADC test:
clearLCD();
pressedMultiIOButtonNumber = getMultiIOPressedButtonNumber();
setCursorPosLCD(0, 0);
writeStringLCD("Button: ");
writeIntegerLCD(pressedMultiIOButtonNumber, DEC);
setCursorPosLCD(1, 0);
writeStringLCD("ADC: ");
writeIntegerLCD(adcButtons, DEC);
// 3V3 voltage sensor ADC test:
v3v3 = measure3V3();
writeString("\n3V3 Voltage: ");
writeDouble(v3v3, 4, 1);
writeString("V\nADC 3V3: ");
writeInteger(adc3v3, DEC);
// Touch sensor ADC test:
touch = getTouch();
if (touch) writeString("\nTOUCHED!!!");
else writeString("\nNOT touched.");
writeString("\nADC Touch: ");
writeInteger(adcTouch, DEC);
// Temperature sensor test:
temperature = TCN75_measure(); // Measure
writeString("\nTemperature: ");
writeDouble(temperature, 5, 1);
writeString("°\n");
// Servo controller test:
// LEDs:
if (onoff) {
setMultiIOLED1(1);
setMultiIOLED2(0);
setMultiIOLED3(0);
setMultiIOLED4(1);
}
else
setMultiIOLEDs(0b0110);
// Servo 1:
setServo(1, servopos);
servopos += 10;
if (servopos > SERVO1_RT) servopos = SERVO1_LT;
// RTC test:
DS1307_read();
writeString("RTC: ");
writeIntegerLength(rtc_time.hour, DEC, 2);
writeString(":");
writeIntegerLength(rtc_time.minute, DEC, 2);
writeString(":");
writeIntegerLength(rtc_time.second, DEC, 2);
writeString(" ");
writeIntegerLength(rtc_date.day, DEC, 2);
writeString(".");
writeIntegerLength(rtc_date.month, DEC, 2);
writeString(".");
writeIntegerLength(rtc_date.year, DEC, 4);
writeString("\n");
// Voltage & current sensor test:
LTC2990_measure();
writeString("Temperature: ");
writeDouble(tint, 5, 1);
writeString("°\n");
writeString("BAT Current: ");
writeDouble(cbat, 6, 1);
writeString("mA\nBAT Voltage: ");
writeDouble(vbat, 4, 1);
writeString( "V\nSERVO Volt.: ");
writeDouble(vservo, 4, 1);
writeString( "V\nVCC Voltage: ");
writeDouble(vcc, 4, 1);
writeString("V\n");
// MultiIO shutdown:
if (pressedMultiIOButtonNumber == 4) {
writeString("\nPress button 1 for MultiIO SHUTDOWN");
writeString("\nor any other button to continue!!!\n");
do {
mSleep(1);
releasedMultiIOButtonNumber = checkMultiIOReleasedButtonEvent();
task_I2CTWI();
} while (!releasedMultiIOButtonNumber);
if (releasedMultiIOButtonNumber == 1) {
writeString("\nPlease wait for MultiIO SHUTDOWN...\n");
multiio_shutdown();
mSleep(3000);
writeString("\nThe MultiIO now is in SHUTDOWN MODE!!!\n");
mSleep(1000);
writeString("\nRESET the M32 microcontroller now...\n\n");
while(true) {};
}
}
setStopwatch1(0);
}
task_I2CTWI();
}
return 0;
}
Lesezeichen