twi.c
/*******************************************************************************
* examples/twi.c *
* ============== *
* *
* Version: 1.0.6 *
* Date : 21.11.17 *
* Author : Peter Weissig *
* *
* If you are changing this file, you may also consider to update *
* demos/diagnosticTool/diagnosticTool.c *
*******************************************************************************/
//*********************************<Included files>*****************************
#include <tucbot/tucbot.h>
//*********************************<Methods>************************************
void testTwi_lcdhex(uint8_t data);
void testTwi(void);
int main(void);
//*********************************[testTwi_lcdhex]*****************************
void testTwi_lcdhex(uint8_t data) {
uint8_t a;
a = data >> 4;
if (a < 10) {
lcdout(a + 48); // 48 == '0'
} else {
lcdout(a + 55); // 55 == 'A' - 10
}
a = data & 0x0F;
if (a < 10) {
lcdout(a + 48); // 48 == '0'
} else {
lcdout(a + 55); // 55 == 'A' - 10
}
}
//*********************************[testTwi]************************************
void testTwi(void) {
uint8_t value = 0x05;
lcdclr();
lcdstr_p(PSTR("TWI err=##"));
while (1) {
mdelay(100);
lcdxy(4,0);
lcdstr_p(PSTR( "write" ));
lcdxy(0,1);
lcdstr_p(PSTR("S60A2>##<03P "));
// This is just a short info what we are going to send:
// S ... Start-Bit
// 60 ... TWI device address (driving module - write mode)
// A2 ... packet descriptor (kTwiPdMD_SensLineEnable)
// >##< ... the data we will be sending (first byte of package)
// this will set the threshold value for the line sensors
// 03 ... second byte of this package
// this will enable the all line sensors
// P ... Stop-Bit
if (!twi_lock()) {
lcdxy(14,0);
lcdout('l');
lcdout('!');
continue;
}
lcdxy(6,1);
testTwi_lcdhex(value);
lcdxy(14,0);
testTwi_lcdhex(twi_errorGet());
value+= 0x10;
mdelay(400);
} else if (buttons_get(kButtonRight)) {
mdelay(100);
lcdxy(4,0);
lcdstr_p(PSTR( "read " ));
lcdxy(0,1);
lcdstr_p(PSTR("S60A2PS61>##<03P"));
// This is just a short info what we are going to send and
// what we are receiving:
// S ... Start-Bit
// 60 ... TWI device address (driving module - write mode)
// A2 ... packet descriptor (kTwiPdMD_SensLineEnable)
// P ... Stop-Bit (by only sending the packet descriptor
// we force the driving module to upload the package with
// the next read command)
// S ... Start-Bit
// 61 ... TWI device address (driving module - read mode)
// A2 ... packet descriptor (kTwiPdMD_SensLineEnable)
// >##< ... the data we want to read (first byte of package)
// this is the current threshold value of the line sensors
// 03 ... second byte of this package
// P ... Stop-Bit
if (!twi_lock()) {
lcdxy(14,0);
lcdout('l');
lcdout('!');
continue;
}
if (twistat()) {
lcdxy(10,1);
testTwi_lcdhex(twiinp());
}
lcdxy(14,0);
testTwi_lcdhex(twi_errorGet());
mdelay(400);
}
}
}
//*********************************[main]***************************************
int main (void) {
init_tucbot(0xFF);
testTwi();
return (0);
}
leds_set
void leds_set(eLedMask bitmask, uint8_t bool)
Sets the selected leds on or off.
buttons_get
eButtonMask buttons_get(eButtonMask bitmask)
Returns the last stored value of the selected buttons.
kButtonMiddle
@ kButtonMiddle
bit mask (0x08): middle button
Definition: buttons.h:35
twi_reset
void twi_reset(void)
Resets the TWI software.
kLedOn
@ kLedOn
constant (0xFF): turn leds on
Definition: leds.h:63
kLedRight
@ kLedRight
bit mask (0x04): right led
Definition: leds.h:46
twi_wait
void twi_wait(void)
Waits until the TWI is not busy anymore.
kButtonRight
@ kButtonRight
bit mask (0x10): right button
Definition: buttons.h:37
twi_lock
uint8_t twi_lock(void)
Locks the TWI for the user.
buttons_request
void buttons_request(eRequestType request)
Requests new data from the buttons.
kRequestContinuous
@ kRequestContinuous
constant (3): request continuous updates
Definition: update.h:37
twiinp
uint8_t twiinp(void)
Gets one byte from the receive buffer of the twi.
kLedMiddle
@ kLedMiddle
bit mask (0x08): middle led
Definition: leds.h:44
kLedOff
@ kLedOff
constant (0x00): turn leds off
Definition: leds.h:61
lcdclr
void lcdclr(void)
Clears the display.
twi_read
void twi_read(uint8_t count)
Starts the reception of data over the TWI.
lcdxy
void lcdxy(uint8_t x, uint8_t y)
Moves the cursor to a postion on the display.
kTwiPdMD_SensLineEnable
@ kTwiPdMD_SensLineEnable
packet descriptor (0xA2): sMD_SensLineEnable
Definition: twi.h:856
twi_unlock
void twi_unlock(void)
Unlocks the TWI by the user.
twiout
void twiout(uint8_t send_data)
Puts one byte in the transmit buffer of the twi.
lcdstr_p
void lcdstr_p(const char *send_data)
Writes a string located in the flash to the display.
lcdout
void lcdout(uint8_t data)
Writes one byte to the display.
init_tucbot
void init_tucbot(uint8_t enable_interrupts)
Initiates the TUC-Bot.
mdelay
void mdelay(uint16_t mseconds)
Waits for the given time in milliseconds.
twi_errorGet
uint8_t twi_errorGet(void)
Returns the error value of the TWI.
twistat
uint8_t twistat(void)
Gets the number of bytes in the receive buffer of the twi.
twi_write
void twi_write(void)
Starts the transmission of data over the TWI.