11.07.2015 Views

ATMega chip full datasheet - UCSD Department of Physics

ATMega chip full datasheet - UCSD Department of Physics

ATMega chip full datasheet - UCSD Department of Physics

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

ATmega48A/PA/88A/PA/168A/PA/328/P• After the TWI has transmitted SLA+R/W.• After the TWI has transmitted an address byte.• After the TWI has lost arbitration.• After the TWI has been addressed by own slave address or general call.• After the TWI has received a data byte.• After a STOP or REPEATED START has been received while still addressed as a Slave.• When a bus error has occurred due to an illegal START or STOP condition.22.6 Using the TWIThe AVR TWI is byte-oriented and interrupt based. Interrupts are issued after all bus events, likereception <strong>of</strong> a byte or transmission <strong>of</strong> a START condition. Because the TWI is interrupt-based,the application s<strong>of</strong>tware is free to carry on other operations during a TWI byte transfer. Note thatthe TWI Interrupt Enable (TWIE) bit in TWCR together with the Global Interrupt Enable bit inSREG allow the application to decide whether or not assertion <strong>of</strong> the TWINT Flag should generatean interrupt request. If the TWIE bit is cleared, the application must poll the TWINT Flag inorder to detect actions on the TWI bus.When the TWINT Flag is asserted, the TWI has finished an operation and awaits applicationresponse. In this case, the TWI Status Register (TWSR) contains a value indicating the currentstate <strong>of</strong> the TWI bus. The application s<strong>of</strong>tware can then decide how the TWI should behave inthe next TWI bus cycle by manipulating the TWCR and TWDR Registers.Figure 22-10 is a simple example <strong>of</strong> how the application can interface to the TWI hardware. Inthis example, a Master wishes to transmit a single data byte to a Slave. This description is quiteabstract, a more detailed explanation follows later in this section. A simple code example implementingthe desired behavior is also presented.Figure 22-10. Interfacing the Application to the TWI in a Typical TransmissionApplicationAction1. Applicationwrites to TWCR toinitiatetransmission <strong>of</strong>START3. Check TWSR to see if START wassent. Application loads SLA+W intoTWDR, and loads appropriate controlsignals into TWCR, makin sure thatTWINT is written to one,and TWSTA is written to zero.5. Check TWSR to see if SLA+W wassent and ACK received.Application loads data into TWDR, andloads appropriate control signals intoTWCR, making sure that TWINT iswritten to one7. Check TWSR to see if data was sentand ACK received.Application loads appropriate controlsignals to send STOP into TWCR,making sure that TWINT is written to oneTWI busSTART SLA+W A Data A STOPTWIHardwareAction2. TWINT set.Status code indicatesSTART condition sent4. TWINT set.Status code indicatesSLA+W sent, ACKreceived6. TWINT set.Status code indicatesdata sent, ACK receivedIndicatesTWINT set1. The first step in a TWI transmission is to transmit a START condition. This is done bywriting a specific value into TWCR, instructing the TWI hardware to transmit a STARTcondition. Which value to write is described later on. However, it is important that the8271D–AVR–05/11225

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!