Xmega Application Note | |||||
#include "avr_compiler.h"
#include "twi_master_driver.h"
#include "twi_slave_driver.h"
Go to the source code of this file.
Defines | |
#define | BAUDRATE 100000 |
#define | CPU_SPEED 2000000 |
#define | NUM_BYTES 8 |
#define | SLAVE_ADDRESS 0x55 |
#define | TWI_BAUDSETTING TWI_BAUD(CPU_SPEED, BAUDRATE) |
Functions | |
ISR (TWIC_TWIS_vect) | |
ISR (TWIC_TWIM_vect) | |
int | main (void) |
void | TWIC_SlaveProcessData (void) |
Variables | |
uint8_t | sendBuffer [NUM_BYTES] = {0x55, 0xAA, 0xF0, 0x0F, 0xB0, 0x0B, 0xDE, 0xAD} |
TWI_Master_t | twiMaster |
TWI_Slave_t | twiSlave |
This file contains an example application that demonstrates the TWI master and slave driver. It shows how to set up one TWI module as both master and slave, and communicate with itself.
The recommended test setup for this application is to connect 10K pull-up resistors on PC0 (SDA) and PC1 (SCL). Connect a 10-pin cable between the PORTD and SWITCHES, and PORTE and LEDS.
Copyright (c) 2008, Atmel Corporation All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Definition in file twi_example.c.
#define BAUDRATE 100000 |
Definition at line 66 of file twi_example.c.
#define CPU_SPEED 2000000 |
CPU speed 2MHz, BAUDRATE 100kHz and Baudrate Register Settings
Definition at line 65 of file twi_example.c.
#define NUM_BYTES 8 |
Defining number of bytes in buffer.
Definition at line 62 of file twi_example.c.
#define SLAVE_ADDRESS 0x55 |
Defining an example slave address.
Definition at line 59 of file twi_example.c.
Referenced by main().
#define TWI_BAUDSETTING TWI_BAUD(CPU_SPEED, BAUDRATE) |
ISR | ( | TWIC_TWIS_vect | ) |
TWIC Slave Interrupt vector.
Definition at line 173 of file twi_example.c.
References TWI_SlaveInterruptHandler().
00174 { 00175 TWI_SlaveInterruptHandler(&twiSlave); 00176 }
ISR | ( | TWIC_TWIM_vect | ) |
TWIC Master Interrupt vector.
Definition at line 167 of file twi_example.c.
References TWI_MasterInterruptHandler().
00168 { 00169 TWI_MasterInterruptHandler(&twiMaster); 00170 }
int main | ( | void | ) |
/brief Example code
Example code that reads the key pressed and show a value from the buffer, sends the value to the slave and read back the processed value which will be inverted and displayed after key release.
Definition at line 95 of file twi_example.c.
References TWI_Master::readData, sendBuffer, SLAVE_ADDRESS, TWI_Master::status, TWI_BAUDSETTING, TWI_MasterInit(), TWI_MasterWriteRead(), TWI_SlaveInitializeDriver(), TWI_SlaveInitializeModule(), TWIC_SlaveProcessData(), and TWIM_STATUS_READY.
00096 { 00097 /* Initialize PORTE for output and PORTD for inverted input. */ 00098 PORTE.DIRSET = 0xFF; 00099 PORTD.DIRCLR = 0xFF; 00100 PORTCFG.MPCMASK = 0xFF; 00101 PORTD.PIN0CTRL |= PORT_INVEN_bm; 00102 // PORTCFG.MPCMASK = 0xFF; 00103 // PORTD.PIN0CTRL = (PORTD.PIN0CTRL & ~PORT_OPC_gm) | PORT_OPC_PULLUP_gc; 00104 00105 // Enable internal pull-up on PC0, PC1.. Uncomment if you don't have external pullups 00106 // PORTCFG.MPCMASK = 0x03; // Configure several PINxCTRL registers at the same time 00107 // PORTC.PIN0CTRL = (PORTC.PIN0CTRL & ~PORT_OPC_gm) | PORT_OPC_PULLUP_gc; //Enable pull-up to get a defined level on the switches 00108 00109 00110 00111 /* Initialize TWI master. */ 00112 TWI_MasterInit(&twiMaster, 00113 &TWIC, 00114 TWI_MASTER_INTLVL_LO_gc, 00115 TWI_BAUDSETTING); 00116 00117 /* Initialize TWI slave. */ 00118 TWI_SlaveInitializeDriver(&twiSlave, &TWIC, TWIC_SlaveProcessData); 00119 TWI_SlaveInitializeModule(&twiSlave, 00120 SLAVE_ADDRESS, 00121 TWI_SLAVE_INTLVL_LO_gc); 00122 00123 /* Enable LO interrupt level. */ 00124 PMIC.CTRL |= PMIC_LOLVLEN_bm; 00125 sei(); 00126 00127 uint8_t BufPos = 0; 00128 while (1) { 00129 while(!PORTD.IN); /* Wait for user to press button */ 00130 00131 switch(PORTD.IN){ 00132 case (PIN0_bm): BufPos = 0; break; 00133 case (PIN1_bm): BufPos = 1; break; 00134 case (PIN2_bm): BufPos = 2; break; 00135 case (PIN3_bm): BufPos = 3; break; 00136 case (PIN4_bm): BufPos = 4; break; 00137 case (PIN5_bm): BufPos = 5; break; 00138 case (PIN6_bm): BufPos = 6; break; 00139 case (PIN7_bm): BufPos = 7; break; 00140 default: break; 00141 } 00142 00143 /* Show the byte to send while holding down the key. */ 00144 while(PORTD.IN != 0x00){ 00145 PORTE.OUT = sendBuffer[BufPos]; 00146 } 00147 00148 TWI_MasterWriteRead(&twiMaster, 00149 SLAVE_ADDRESS, 00150 &sendBuffer[BufPos], 00151 1, 00152 1); 00153 00154 00155 while (twiMaster.status != TWIM_STATUS_READY) { 00156 /* Wait until transaction is complete. */ 00157 } 00158 00159 /* Show the sent byte received and processed on LEDs. */ 00160 PORTE.OUT = (twiMaster.readData[0]); 00161 00162 while(PORTD.IN); /* Wait for user to release button */ 00163 } 00164 }
void TWIC_SlaveProcessData | ( | void | ) |
Simple function that invert the received value in the sendbuffer. This function is used in the driver and passed on as a pointer to the driver.
Definition at line 82 of file twi_example.c.
References TWI_Slave::bytesReceived, TWI_Slave::receivedData, and TWI_Slave::sendData.
Referenced by main().
00083 { 00084 uint8_t bufIndex = twiSlave.bytesReceived; 00085 twiSlave.sendData[bufIndex] = (~twiSlave.receivedData[bufIndex]); 00086 }
uint8_t sendBuffer[NUM_BYTES] = {0x55, 0xAA, 0xF0, 0x0F, 0xB0, 0x0B, 0xDE, 0xAD} |
TWI master module.
Definition at line 71 of file twi_example.c.
TWI slave module.
Definition at line 72 of file twi_example.c.
Generated on Tue Aug 11 12:42:12 2009 for AVR1308 Using the XMEGA TWI by ![]() |