IIC Topic 2 - STM32 driving AT24C02

Publisher:幸福家园Latest update time:2019-01-09 Source: eefocusKeywords:IIC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1 Overview


The EEPROM chip model on the MiniSTM32 development board is 24C02. The total capacity of the chip is 256 bytes, and the chip is connected to the outside through the IIC bus. Here we directly use the AT24C02 on the Atom board, mainly for learning software programming.


2. Hardware Connection


Write the picture description here


The three pins A2, A1, and A0 are directly grounded. Power supply: (VCC = 2.7V to 5.5V)


Device address setting:


Write the picture description here


For AT24C02:Addr—>0xA0(write)/0xA1(read).


Single byte write:


Write the picture description here


Write by page:


Write the picture description here


Read data from the current address;


Write the picture description here


Random Read:


Write the picture description here


Sequential read:


Write the picture description here


3. Routine Analysis


(I) IIC partial implementation code


Including IIC initialization (IO port), IIC start, IIC end, ACK, IIC read and write and other functions. In other functions, you only need to call the relevant IIC function to communicate with the external IIC device. This is not limited to 24C02. This code can be used on any IIC device.


IIC_SCL and IIC_SDA are connected to PC12 and PC11 of STM32 respectively.


1.IIC initialization

//IO operation function     

#define IIC_SCL PCout(12) //SCL

#define IIC_SDA PCout(11) //SDA   

#define READ_SDA PCin(11) // Input SDA


//Initialize IIC

void IIC_Init(void)

{

    GPIO_InitTypeDef GPIO_InitStructure;


    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE );

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_11;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //Push-pull output

    GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;

    GPIO_Init(GPIOC, &GPIO_InitStructure);


    IIC_SCL=1;

    IIC_SDA=1;

}


2. Generate IIC start signal

//Generate IIC start signal

void IIC_Start(void)

{

    SDA_OUT(); //sda line output


    IIC_SDA=1;

    IIC_SCL=1;


    delay_us(4);


    IIC_SDA=0;//START:when CLK is high,DATA change form high to low


    delay_us(4);


    IIC_SCL=0; //Clamp the I2C bus and prepare to send or receive data

}


3. Generate a stop IIC signal

//Generate IIC stop signal

void IIC_Stop(void)

{

    SDA_OUT(); //sda line output


    IIC_SCL=0;

    IIC_SDA=0;//STOP:when CLK is high, DATA change form low to high

    delay_us(4);


    IIC_SCL=1;


    IIC_SDA=1; //Send I2C bus end signal

    delay_us(4);

}


4. Wait for the response signal

//Wait for the response signal to arrive

//Return value: 1, failed to receive response

// 0, receiving the response successfully

u8 IIC_Wait_Ack(void)

{

    u8 ucErrTime=0;


    SDA_IN(); //Set SDA to input


    IIC_SDA=1;

    delay_us(1);

    IIC_SCL=1;

    delay_us(1);


    while(READ_SDA)

    {

    ucErrTime++;

    if(ucErrTime>250)

    {

    IIC_Stop();

    return 1;

    }

    }

    IIC_SCL=0; //Clock output 0

    return 0;

}



5. Response signal

//Generate ACK response

void IIC_Ack(void)

{

    IIC_SCL=0;


    SDA_OUT();


    IIC_SDA=0;

    delay_us(2);


    IIC_SCL=1;


    delay_us(2);

    IIC_SCL=0;

}


6. No response is generated

//No ACK response is generated

void IIC_NAck(void)

{

    IIC_SCL=0;

    SDA_OUT();


    IIC_SDA=1;


    delay_us(2);

    IIC_SCL=1;

    delay_us(2);

    IIC_SCL=0;

}


7.IIC sends a byte

//IIC sends a byte

//Return whether the slave has a response

//1, there is a response

//0, no response

void IIC_Send_Byte(u8 txd)

{

    u8 t;


    SDA_OUT();


    IIC_SCL=0; //Pull the clock low to start data transmission


    for(t=0;t<8;t++)

    {

        IIC_SDA=(txd&0x80)>>7; //Start from high bit, transmit data one bit at a time

        txd<<=1;

        delay_us(2); //These three delays are required for TEA5767

        IIC_SCL=1; //Keep data stable

        delay_us(2);

        IIC_SCL=0; //Next bit of data transmission starts

        delay_us(2);

    }

}


8.IIC reads one byte

//Read 1 byte, when ack=1, send ACK, when ack=0, send nACK

u8 IIC_Read_Byte(unsigned char ack)

{

    unsigned char i,receive=0;


    SDA_IN(); //SDA is set as input


    for(i=0;i<8;i++ )

    {

        IIC_SCL=0;

        delay_us(2);

        IIC_SCL=1;

        receive<<=1;

        if(READ_SDA)

            receive++;

        delay_us(1);

    }

    if (!ack)

    IIC_NAck(); //Send nACK

    else

    IIC_Ack(); //Send ACK

    return receive;

}



2. AT24C02 Operation

Directly set the IO port mode to input or output through register operation. The code is as follows:


#define SDA_IN() {GPIOC->CRH&=0XFFFF0FFF;GPIOC->CRH|=8<<12;}


#define SDA_OUT() {GPIOC->CRH&=0XFFFF0FFF;GPIOC->CRH|=3<<12;}  


1. Initialize the IIC interface

//Initialize the IIC interface

void AT24CXX_Init(void)

{

    IIC_Init();

}


2. Read a data at the specified address of AT24CXX

//Read a data at the specified address of AT24CXX

//ReadAddr: the address to start reading, 24C02 size is 2Kbit; ADDR: 0x00---0x7FF

//Return value: the data read


u8 AT24CXX_ReadOneByte(u16 ReadAddr)

{

    u8 temp=0;


    IIC_Start();

    if(EE_TYPE>AT24C16)

    /*The macro definition has a chip type selection. If the definition is greater than AT24C16, two 8-bit addresses are sent, otherwise one 8-bit address is sent*/

    {

    IIC_Send_Byte(0XA0); //Send write command, 0XA0: device address, 7-bit address + write (0)

    IIC_Wait_Ack();

    IIC_Send_Byte(ReadAddr>>8); //Send high address

    }else IIC_Send_Byte(0XA0+((ReadAddr/256)<<1)); //Send device address 0XA0, write data

    IIC_Wait_Ack();

    IIC_Send_Byte(ReadAddr%256); //Send low address

    IIC_Wait_Ack();

    IIC_Start();

    IIC_Send_Byte(0XA1); //Enter receiving mode, 7-bit address + read (1)

    IIC_Wait_Ack();

    temp=IIC_Read_Byte(0);

    IIC_Stop(); //Generate a stop condition

    return temp;

}


3. Write a data to the specified address of AT24CXX

//Write a data at the specified address of AT24CXX

//WriteAddr: Destination address for writing data

//DataToWrite: data to be written


void AT24CXX_WriteOneByte(u16 WriteAddr,u8 DataToWrite)

{

    IIC_Start();

    if(EE_TYPE>AT24C16)

    {

    IIC_Send_Byte(0XA0); //Send write command

    IIC_Wait_Ack();

    IIC_Send_Byte(WriteAddr>>8); //Send high address

    }else IIC_Send_Byte(0XA0+((WriteAddr/256)<<1)); //Send device address 0XA0, write data

    IIC_Wait_Ack();

    IIC_Send_Byte(WriteAddr%256); //Send low address

    IIC_Wait_Ack();

    IIC_Send_Byte(DataToWrite); //Send byte

    IIC_Wait_Ack();

    IIC_Stop(); //Generate a stop condition

    delay_ms(10); //For EEPROM devices, you need to wait for a while each time you write, otherwise the write will fail!

}


4. Write data of length Len starting from the specified address in AT24CXX

//Start writing data of length Len at the specified address in AT24CXX

//This function is used to write 16-bit or 32-bit data.

//WriteAddr: start writing address

//DataToWrite: the first address of the data array

//Len: length of data to be written 2,4


void AT24CXX_WriteLenByte(u16 WriteAddr,u32 DataToWrite,u8 Len)

{

    u8 t;

    for(t=0;t

        AT24CXX_WriteOneByte(WriteAddr+t,(DataToWrite>>(8*t))&0xff);

}


5. Start reading data of length Len from the specified address in AT24CXX

//Start reading data of length Len from the specified address in AT24CXX

//This function is used to read 16-bit or 32-bit data.

//ReadAddr: start reading address

//Return value: data

//Len: length of the data to be read 2,4


u32 AT24CXX_ReadLenByte(u16 ReadAddr,u8 Len)

{

    u8 t; u32 temp=0;

    for(t=0;t

    {

        temp<<=8;

        temp+=AT24CXX_ReadOneByte(ReadAddr+Len-t-1);

    }

    return temp;

}


6. Check whether AT24CXX is normal

// Check if AT24CXX is normal

//Here the last address (255) of 24XX is used to store the flag word.

//If you use other 24C series, this address needs to be modified

//Return 1: Detection failed

//Return 0: Detection successful

u8 AT24CXX_Check(void)

{

    u8 temp;

    temp=AT24CXX_ReadOneByte(255); //Avoid writing AT24CXX every time you start the computer

    if(temp==0X55)

        return 0;

    else //Exclude the first initialization

    {

        AT24CXX_WriteOneByte(255,0X55);

        temp=AT24CXX_ReadOneByte(255);

        if(temp==0X55)

            return 0;

    }

    return 1;

}


7. Start reading the specified number of data from the specified address in AT24CXX

/Start reading the specified number of data from the specified address in AT24CXX

//ReadAddr: The address to start reading is 0~255 for 24c02

//pBuffer: the first address of the data array

//NumToRead: the number of data to be read


void AT24CXX_Read(u16 ReadAddr,u8 *pBuffer,u16 NumToRead)

{

    while(NumToRead)

    {

        *pBuffer++=AT24CXX_ReadOneByte(ReadAddr++);

        NumToRead--;

    }

}


8. Write the specified number of data starting from the specified address in AT24CXX

//Start writing the specified number of data at the specified address in AT24CXX

//WriteAddr: The address to start writing is 0~255 for 24c02

//pBuffer: the first address of the data array

//NumToWrite: the number of data to be written


void AT24CXX_Write(u16 WriteAddr,u8 *pBuffer,u16 NumToWrite)

{

    while(NumToWrite--)

    {

        AT24CXX_WriteOneByte(WriteAddr,*pBuffer);

        WriteAddr++;

        pBuffer++;

    }

}


refer to:

1. Atomic STM32 development library function version


Keywords:IIC Reference address:IIC Topic 2 - STM32 driving AT24C02

Previous article:SPI Topic (II)——STM32 Driver FLASH (W25Q64)
Next article:STM32 study notes ADC

Latest Microcontroller Articles
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号