ATmega128 (961723), страница 43
Текст из файла (страница 43)
The different status codes are described later in this section. Note that the value read from TWSRcontains both the 5-bit status value and the 2-bit prescaler value. The applicationdesigner should mask the prescaler bits to zero when checking the Status bits. Thismakes status checking independent of prescaler setting. This approach is used in thisdatasheet, unless otherwise noted.• Bit 2 – Res: Reserved BitThis bit is reserved and will always read as zero.• Bits 1..0 – TWPS: TWI Prescaler Bits2072467M–AVR–11/04These bits can be read and written, and control the bit rate prescaler.Table 87.
TWI Bit Rate PrescalerTWPS1TWPS0Prescaler Value00101410161164To calculate bit rates, see “Bit Rate Generator Unit” on page 204. The value ofTWPS1..0 is used in the equation.TWI Data Register – TWDRBit76543210TWD7TWD6TWD5TWD4TWD3TWD2TWD1TWD0Read/WriteR/WR/WR/WR/WR/WR/WR/WR/WInitial Value11111111TWDRIn Transmit mode, TWDR contains the next byte to be transmitted. In receive mode, theTWDR contains the last byte received.
It is writable while the TWI is not in the process ofshifting a byte. This occurs when the TWI interrupt flag (TWINT) is set by hardware.Note that the Data Register cannot be initialized by the user before the first interruptoccurs. The data in TWDR remains stable as long as TWINT is set. While data is shiftedout, data on the bus is simultaneously shifted in.
TWDR always contains the last bytepresent on the bus, except after a wake up from a sleep mode by the TWI interrupt. Inthis case, the contents of TWDR is undefined. In the case of a lost bus arbitration, nodata is lost in the transition from Master to Slave. Handling of the ACK bit is controlledautomatically by the TWI logic, the CPU cannot access the ACK bit directly.• Bits 7..0 – TWD: TWI Data RegisterThese eight bits constitute the next data byte to be transmitted, or the latest data bytereceived on the Two-wire Serial Bus.TWI (Slave) Address Register– TWARBit76543210TWA6TWA5TWA4TWA3TWA2TWA1TWA0TWGCERead/WriteR/WR/WR/WR/WR/WR/WR/WR/WInitial Value11111110TWARThe TWAR should be loaded with the 7-bit slave address (in the seven most significantbits of TWAR) to which the TWI will respond when programmed as a slave transmitter orreceiver, and not needed in the master modes.
In multimaster systems, TWAR must beset in masters which can be addressed as slaves by other masters.The LSB of TWAR is used to enable recognition of the general call address ($00). Thereis an associated address comparator that looks for the slave address (or general calladdress if enabled) in the received serial address. If a match is found, an interruptrequest is generated.• Bits 7..1 – TWA: TWI (Slave) Address RegisterThese seven bits constitute the slave address of the TWI unit.• Bit 0 – TWGCE: TWI General Call Recognition Enable Bit208ATmega1282467M–AVR–11/04ATmega128If set, this bit enables the recognition of a General Call given over the Two-wire SerialBus.Using the TWIThe AVR TWI is byte-oriented and interrupt based.
Interrupts are issued after all busevents, like reception of a byte or transmission of a START condition. Because the TWIis interrupt-based, the application software is free to carry on other operations during aTWI byte transfer. Note that the TWI Interrupt Enable (TWIE) bit in TWCR together withthe Global Interrupt Enable bit in SREG allow the application to decide whether or notassertion of the TWINT flag should generate an interrupt request. If the TWIE bit iscleared, the application must poll the TWINT flag in order to detect actions on the TWIbus.When the TWINT flag is asserted, the TWI has finished an operation and awaits application response. In this case, the TWI Status Register (TWSR) contains a value indicatingthe current state of the TWI bus. The application software can then decide how the TWIshould behave in the next TWI bus cycle by manipulating the TWCR and TWDRRegisters.Figure 95 is a simple example of how the application can interface to the TWI hardware.In this example, a master wishes to transmit a single data byte to a slave.
This description is quite abstract, a more detailed explanation follows later in this section. A simplecode example implementing the desired behavior is also presented.ApplicationActionFigure 95. Interfacing the Application to the TWI in a Typical TransmissionTWI bus1. Applicationwrites to TWCRto initiatetransmission ofSTART3. Check TWSR to see if STARTwas sent. Application loadsSLA+W into TWDR, and loadsappropriate control signals intoTWCR, making sure that TWINTis written to one, and TWSTA iswritten to zero.STARTTWIHardwareAction2.
TWINT set.Status code indicatesSTART condition sentSLA+W5. Check TWSR to see if SLA+Wwas sent and ACK received.Application loads data into TWDR,and loads appropriate control signalsinto TWCR, making sure that TWINTis written to one.A4. TWINT set.Status code indicatesSLA+W sendt, ACKreceivedData7. Check TWSR to see if datawas sent and ACK received.Application loads appropriatecontrol signals to send STOPinto TWCR, making sure thatTWINT is written to oneA6.
TWINT set.Status code indicatesdata sent, ACK receivedSTOPIndicatesTWINT set1. The first step in a TWI transmission is to transmit a START condition. This isdone by writing a specific value into TWCR, instructing the TWI hardware totransmit a START condition. Which value to write is described later on. However,it is important that the TWINT bit is set in the value written. Writing a one toTWINT clears the flag. The TWI will not start any operation as long as theTWINT bit in TWCR is set. Immediately after the application has cleared TWINT,the TWI will initiate transmission of the START condition.2092467M–AVR–11/042.
When the START condition has been transmitted, the TWINT flag in TWCR isset, and TWSR is updated with a status code indicating that the START conditionhas successfully been sent.3. The application software should now examine the value of TWSR, to make surethat the START condition was successfully transmitted.
If TWSR indicates otherwise, the application software might take some special action, like calling anerror routine. Assuming that the status code is as expected, the application mustload SLA+W into TWDR. Remember that TWDR is used both for address anddata. After TWDR has been loaded with the desired SLA+W, a specific valuemust be written to TWCR, instructing the TWI hardware to transmit the SLA+Wpresent in TWDR. Which value to write is described later on. However, it isimportant that the TWINT bit is set in the value written.
Writing a one to TWINTclears the flag. The TWI will not start any operation as long as the TWINT bit inTWCR is set. Immediately after the application has cleared TWINT, the TWI willinitiate transmission of the address packet.4. When the address packet has been transmitted, the TWINT flag in TWCR is set,and TWSR is updated with a status code indicating that the address packet hassuccessfully been sent. The status code will also reflect whether a slaveacknowledged the packet or not.5.
The application software should now examine the value of TWSR, to make surethat the address packet was successfully transmitted, and that the value of theACK bit was as expected. If TWSR indicates otherwise, the application softwaremight take some special action, like calling an error routine. Assuming that thestatus code is as expected, the application must load a data packet into TWDR.Subsequently, a specific value must be written to TWCR, instructing the TWIhardware to transmit the data packet present in TWDR. Which value to write isdescribed later on. However, it is important that the TWINT bit is set in the valuewritten. Writing a one to TWINT clears the flag.
The TWI will not start any operation as long as the TWINT bit in TWCR is set. Immediately after the applicationhas cleared TWINT, the TWI will initiate transmission of the data packet.6. When the data packet has been transmitted, the TWINT flag in TWCR is set, andTWSR is updated with a status code indicating that the data packet has successfully been sent. The status code will also reflect whether a slave acknowledgedthe packet or not.7. The application software should now examine the value of TWSR, to make surethat the data packet was successfully transmitted, and that the value of the ACKbit was as expected.
If TWSR indicates otherwise, the application software mighttake some special action, like calling an error routine. Assuming that the statuscode is as expected, the application must write a specific value to TWCR,instructing the TWI hardware to transmit a STOP condition. Which value to writeis described later on. However, it is important that the TWINT bit is set in thevalue written. Writing a one to TWINT clears the flag.
The TWI will not start anyoperation as long as the TWINT bit in TWCR is set. Immediately after the application has cleared TWINT, the TWI will initiate transmission of the STOPcondition. Note that TWINT is NOT set after a STOP condition has been sent.210ATmega1282467M–AVR–11/04ATmega128Even though this example is simple, it shows the principles involved in all TWI transmissions.