IAR For AVR Two-wire Serial Interface TWI Application

Publisher:WeaselLatest update time:2016-10-06 Source: eefocusKeywords:IAR  AVR  TWI Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
      ATMEL's TWI and PHILIPS's IIC are basically the same thing, but they are different in name, so no one has to pay the other for usage. Their agreements are the same, so as users, we can simply think of TWI as IIC.

      After all the nonsense, let's get down to business. This time it's about the use of hardware IIC (not used to saying TWI yet) under the ATMega16 platform. In the ATMega16 datasheet we can see very powerful functions, and there are many master-slave settings. This article only talks about the most commonly used method, which is "ATMega16 hardware TWI scan send and scan read".

      First of all, we need to clarify the process of sending and receiving TWI:

send:

1. Set the data transmission baud rate

2. Send START signal and wait for response ==》 <== Response signal

3. Send chip address and wait for response ==》 《==Response signal

4. Send the absolute address of the data and wait for the response ==》 ==Response signal

5. Send the data to be written and wait for the response ==》 《==Response signal

6. Send STOP signal to release the bus ==》 Data is written successfully

take over:

1. Set the data transmission baud rate

2. Send START signal and wait for response ==》 <== Response signal

3. Send chip address and wait for response ==》 《==Response signal

4. Send the absolute address of the data and wait for the response ==》 ==Response signal

5. Send RESTART signal and wait for response ==》 <==Response signal      

6. Send the chip address and indicate the read operation, and wait for the response ==》 == response signal   

7. Read data and wait for response ==》 《==Response signal

8. Send STOP signal to release the bus ==》 Data read operation is successful

Application chip: ATMega 16 Crystal: 7.3728

Code file: Project

                        |___TWI.C

                        | |_____ IAR_DELAY.H

                        |___UART.C

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

IAR_DELAY.H

#ifndef __IAR_DELAY_H
#define __IAR_DELAY_H

#include

#define XTAL 7.3728 // can be defined as the crystal frequency you use (unit: MHz)


#define delay_us(x) __delay_cycles ( (unsigned long)(x * XTAL) ) 
#define delay_ms(x) __delay_cycles ( (unsigned long)(x * XTAL*1000) )
#define delay_s(x) __delay_cycles ( (unsigned long) (x * XTAL*1000000) )

#endif

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

UART.C

#include
#define uchar unsigned char 
#define uint unsigned int

//##############################################################
/*Serial port initialization function*/
void Uart_Init(void)
{                         
   UCSRB = (1< UCSRC = (1<

UBRRH=0x00; //Set the baud rate register low byte
UBRRL=47; //9600 //Set the baud rate register high byte

DDRD_Bit1=1; //Configure TX as output (very important)
}
//##############################################################
/*Send one character data, query mode*/
void Uart_Transmit(uchar data)
{
     while(!(UCSRA&(1<    //while(UCSRA_UDRE==0); /* Wait for the send buffer to be empty*/
UDR = data; /* Send data*/
}
  

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

#include
#include "IAR_DELAY.H"
#define uchar unsigned char
#define uint unsigned int

void Uart_Init(void);
void Uart_Transmit(uchar data);


//Variable declaration
#define EEPROM_BUS_ADDRESS 0xA0 //Device address
                                                                             /*######################################################################################*/
                                                                             /*Slave address bit definition:______________________________________-------------*/
                                                                              /* AT24C02 | 1 | 0 | 1 | 0 | A2 | A1 | A0 | R/~W |------------*/
                                                                             /* ---------------------------------------------------*/
                                                                             /*# ...
//Subsequent actions of each status word in host sending mode
#define TW_START 0x08 //Start signal has been issued 
#define TW_REP_START 0x10 //Repeat start signal has been issued
#define TW_MT_SLA_ACK 0x18 //Write byte has been issued and received ACK signal 
#define TW_MT_SLA_NACK 0x20 //Write byte has been issued and received NACK signal 
#define TW_MT_DATA_ACK 0x28 //Data has been issued and received ACK signal
#define TW_MT_DATA_NACK 0x30 //Data has been issued and received NACK signal
#define TW_MT_ARB_LOST 0x38 //Lost bus control right
//Subsequent actions of each status word in host receiving mode
#define TW_MR_ARB_LOST 0x38 //Lost bus control right, no response signal received 
#define TW_MR_SLA_ACK 0x40 //Read command has been issued and received ACK
#define TW_MR_SLA_NACK 0x48 //Read command sent and received NACK
#define TW_MR_DATA_ACK 0x50 //Data received, ACK sent
#define TW_MR_DATA_NACK 0x58 //Data received, NACK sent

#define IIC_Start() TWCR =(1<                                                                       // The TWSTA bit will cause the hardware to generate a START signal on the bus, declaring that it wants to be the host.
                                                                      // The TWEN bit enables the TWI function and switches the PC0 and PC1 pins to the second function. If cleared, it interrupts the transmission of TWI.
#define IIC_Stop() TWCR =(1<

#define IIC_Wait() while(!(TWCR&(1<

//################################################################################
/*I2C bus single byte write*/
unsigned char twi_write(unsigned char addr, unsigned char dd)
{
        TWBR = 10; //Set baud rate

/*start start*/
IIC_Start(); //Hardware sends START signal, and clears TWINT bit, enables hardware TWI, and makes TWI start working
IIC_Wait(); //Wait for sending START to complete TWINT bit
if ((TWSR & 0xF8) != 0x08) return 0; //Detect TWINT bit is in position, compare the status in TWSR register, if correct, transmit data downward, error returns 0
        
        /*SLA_W Chip address*/
TWDR = EEPROM_BUS_ADDRESS ; //Chip address 0xA0, assign to data register TWDR, wait for sending
TWCR = (1 << TWINT) | (1 << TWEN); //Write 1 to clear the TWINT bit of the control register TWCR, then enable the TWI hardware interface, let TWI work, and send the data in the TWDR register
IIC_Wait(); //Wait for data to be sent and TWINT to be reset
if ((TWSR & 0xF8) != 0x18) return 0; //Detect that the TWINT bit is in position, compare the state in the TWSR register, if correct, transfer data downward, otherwise return 0
        
       /*addr operation address*/
TWDR = addr; //Assign the absolute address of the write data to the data register TWDR, wait for sending
TWCR = (1 << TWINT) | (1 << TWEN); //TWINT of the control register TWCR Write 1 to clear the TWDR bit by software, then enable the TWI hardware interface, let TWI work, and send the data in the TWDR register
IIC_Wait(); //Wait for data to be sent and TWINT to be reset
if ((TWSR & 0xF8) != 0x28) return 0; //Detect the TWINT bit, compare the state in the TWSR register, if correct, transfer data downward, and return 0 if error occurs
        
        /*dd write data*/
TWDR = dd; //Assign the data to be written to the data register TWDR and wait for it to be sent
TWCR = (1 << TWINT) | (1 << TWEN); //Write 1 to clear the TWINT bit of the control register TWCR by software, then enable the TWI hardware interface, let TWI work, and send the data in the TWDR register
IIC_Wait(); //Wait for data to be sent and TWINT to be reset
if ((TWSR & 0xF8) != 0x28) return 0; // Detect the TWINT bit, compare the status in the TWSR register, if it is correct, transfer data downward, otherwise return 0

        /*stop stop*/
IIC_Stop(); //Data transmission is completed, send STOP signal, release control of the busreturn
1; //Write data successfully, return 1 to judge whether the data is written successfully
        
}
//#########################################################################################
/*I2C bus single byte read*/
unsigned char twi_read(unsigned char addr)
{

unsigned char Receive_Byte ;
TWBR = 2; //Set baud rate
       
        /*start*/
IIC_Start(); //Hardware sends START signal and clears TWINT bit to enable hardware TWI and start TWI working
IIC_Wait(); //Wait for sending START to complete TWINT bit
if ((TWSR & 0xF8) != 0x08) return 0; //Detect TWINT bit, compare the state in TWSR register, if correct, transfer data downward, otherwise return 0
       
    /*SLA_W chip address*/
TWDR = EEPROM_BUS_ADDRESS; //Chip address 0xA0, assign value to data register TWDR, wait for sending
TWCR = (1 << TWINT) | (1 << TWEN); //TWINT of control register TWCR Write 1 to clear the bit by software, then enable the TWI hardware interface, let TWI work, and send the data in the TWDR register
IIC_Wait(); //Wait for the data to be sent and TWINT to be reset
if ((TWSR & 0xF8) != 0x18) return 0; //Detect the TWINT bit, compare the state in the TWSR register, if correct, transfer the data downward, and return 0 if wrong
        
       /*addr operation address*/
TWDR = addr; //Assign the absolute address of the write data to the data register TWDR and wait for sending
TWCR = (1 << TWINT) | (1 << TWEN); //Write 1 to clear the TWINT bit of the control register TWCR by software, then enable the TWI hardware interface, let TWI work, and send the data in the TWDR register
IIC_Wait(); //Wait for the data to be sent and TWINT to be reset
if ((TWSR & 0xF8) != 0x28) return 0; //Detect TWINT bit is in position, compare the state in TWSR register, if correct, transfer data downward, return 0 if error
        
        /*restart restart*/
IIC_Start(); //Hardware sends RESTART signal, clears TWINT bit, enables hardware TWI, and starts TWI working
        IIC_Wait(); //Wait for data to be sent and TWINT to be reset
if ((TWSR & 0xF8) != 0x10) return 0; //Detect TWINT bit is in position, compare the state in TWSR register, if correct, transfer data downward, return 0 if error
        
       /*SLA_R chip address*/
TWDR = 0xA1; //Chip address 0xA0 and indicate that it is a read operation (the last bit is 1), assign value to data register TWDR, wait for sending    
TWCR = (1 << TWINT) | (1 << TWEN); //TWINT of control register TWCR The software writes 1 to clear the TWINT bit of the control register TWCR, then enables the TWI hardware interface, allows TWI to work, and sends the data in the TWDR register
IIC_Wait(); //Wait for data to be sent and TWINT to be reset
if ((TWSR & 0xF8) != 0x40) return 0; //Detect that the TWINT bit is in position, compare the state in the TWSR register, If correct, transfer data downward, and return 0 if error
        
        /*Read data*/
TWCR = (1 << TWINT) | (1 << TWEN); //Write 1 to clear the TWINT bit of the control register TWCR, then enable the TWI hardware interface, allow TWI to work, and send the data in the TWDR register
IIC_Wait(); //Wait for data to be sent and TWINT to be reset
if ((TWSR & 0xF8) != 0x58) return 0; //Detect that the TWINT bit is in position, compare the state in the TWSR register, If correct, transfer data downward, and return 0 if error
Receive_Byte = TWDR; //Put the read data into the local variable
        
        /*stop*/
IIC_Stop(); //Data transmission completed, send STOP signal, release control of the bus
        
        return Receive_Byte; //Use the read data as the output of the function


}
//################################################################################
/*Main function*/
void main(void)
{
uchar c,d;
Uart_Init(); //Serial port initialization
delay_us(20);                         
Uart_Transmit(0x55); //Test serial port

c = twi_write(0x51,0xf8); //Write data 0x22 at address 0x51
Uart_Transmit(c); //Send the return value to the serial port to test whether the write is successful

delay_ms(2); 
    
d = twi_read(0x51); //Read the data at address 0x51
Uart_Transmit(d); //Send the read data to the serial port
while(1);
}

Keywords:IAR  AVR  TWI Reference address:IAR For AVR Two-wire Serial Interface TWI Application

Previous article:AVR IO port characteristics and applications
Next article:IAR For AVR Serial Port Interrupt Receive

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号