/***************************************************************************** * * Atmel Corporation * * File : main.c * Compiler : AVRGCC Toolchain version 3.4.2 * Revision : $Revision: 992 $ * Date : $Date: 2013-11-07 $ * Updated by : $Author: Atmel $ * * Support mail : avr@atmel.com * * Supported devices : All device with USI module can be used. * The example is written for the ATmega169, ATtiny26 & ATtiny2313 * * AppNote : AVR310 - Using the USI module as a TWI Master * * Description : Example of how to use the driver for TWI master * communication with the USI module. * This code reads PORTD and sends the status to the TWI slave. * Then it reads data from the slave and puts the data on PORTB. * To run the example use STK500 and connect PORTB to the LEDS, * and PORTD to the switches. * ****************************************************************************/ #include #include #include "USI_TWI_Master.h" #define SLAVE_ADDR 0x10 #define MESSAGEBUF_SIZE 4 #define TWI_GEN_CALL 0x00 // The General Call address is 0 // Sample TWI transmission commands #define TWI_CMD_MASTER_WRITE 0x10 #define TWI_CMD_MASTER_READ 0x20 // Sample TWI transmission states, used in the main application. #define SEND_DATA 0x01 #define REQUEST_DATA 0x02 #define READ_DATA_FROM_BUFFER 0x03 unsigned char TWI_Act_On_Failure_In_Last_Transmission ( unsigned char TWIerrorMsg ) { // A failure has occurred, use TWIerrorMsg to determine the nature of the failure // and take appropriate actions. // Se header file for a list of possible failures messages. asm("nop"); return TWIerrorMsg; } int main( void ) { unsigned char messageBuf[MESSAGEBUF_SIZE]; unsigned char TWI_targetSlaveAddress, temp, pressedButton, myCounter=0; //LED feedback port - connect port B to the STK500 LEDS DDRB = 0xFF; PORTB = myCounter; //Switch port - connect portD to the STK500 switches DDRD = 0x00; USI_TWI_Master_Initialise(); sei(); TWI_targetSlaveAddress = 0x10; // This example is made to work together with the AVR311 TWI Slave application note and stk500. // In adition to connecting the TWI pins, also connect PORTB to the LEDS and PORTD to the switches. // The code reads the pins to trigger the action you request. There is an example sending a general call, // address call with Master Read and Master Write. The first byte in the transmission is used to send // commands to the TWI slave. // This is a stk500 demo example. The buttons on PORTD are used to control different TWI operations. for(;;) { pressedButton = ~PIND; if (pressedButton) // Check if any button is pressed { do{temp = ~PIND;} // Wait until key released while (temp); temp = TRUE; // I.e. Success. Used to trap failed operations. switch ( pressedButton ) { // Send a Generall Call case (1<