12.07.2015 Views

ATmega32A Datasheet - Sunrom Technologies

ATmega32A Datasheet - Sunrom Technologies

ATmega32A Datasheet - Sunrom Technologies

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.

<strong>ATmega32A</strong>20.6 Using the TWIThe AVR TWI is byte-oriented and interrupt based. Interrupts are issued after all bus events, likereception of a byte or transmission of a START condition. Because the TWI is interrupt-based,the application software 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 of 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 of the TWI bus. The application software can then decide how the TWI should behave inthe next TWI bus cycle by manipulating the TWCR and TWDR Registers.Figure 20-10 is a simple example of 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 behaviour is also presented.Figure 20-10. Interfacing the Application to the TWI in a Typical TransmissionApplicationAction1. Applicationwrites to TWCRto initiatetransmission ofSTART3. Check TWSR to see if START wassendt. Application loads SLA+W intoTWDR, and loads appropriate controlsignals into TWCR, making sure thatTWINT is written to one, and TWSTAis written to zero5. 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 theTWINT bit is set in the value written. Writing a one to TWINT clears the Flag. The TWIwill not start any operation as long as the TWINT bit in TWCR is set. Immediately afterthe application has cleared TWINT, the TWI will initiate transmission of the STARTcondition.2. When the START condition has been transmitted, the TWINT Flag in TWCR is set, andTWSR is updated with a status code indicating that the START condition has successfullybeen sent.3. The application software should now examine the value of TWSR, to make sure that theSTART condition was successfully transmitted. If TWSR indicates otherwise, the applicationsoftware might take some special action, like calling an error routine. Assumingthat the status code is as expected, the application must load SLA+W into TWDR.Remember that TWDR is used both for address and data. After TWDR has been8155C–AVR–02/11185

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

Saved successfully!

Ooh no, something went wrong!