STM32 introductory study notes EEPROM storage experiment 4

Publisher:xi24Latest update time:2024-03-25 Source: elecfansKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

(2) Create the at24cxx.c file and enter the following code.


/****************************************************** *************************************************** ******

                EEPROM driver

*************************************************** *************************************************** *****/

#include "at24cxx.h"

#include "delay.h"

/****************************************************** **

Name:IIC_Start

Function:IIC start signal

Paramater:None

Return :None

*************************************************** */

void IIC_Start()

{

  GPIOB->CRL &= 0x0FFFFFFF; //PB7 push-pull output

  GPIOB->CRL |= 0x30000000;

  IIC_SDA = 1;

  IIC_SCL = 1;

  delay_us(4);

  IIC_SDA = 0;

  delay_us(4);

  IIC_SCL = 0;

}

/****************************************************** **

Name:IIC_Stop

Function:IIC stop signal

Paramater:None

Return :None

*************************************************** */

void IIC_Stop()

{

  GPIOB->CRL &= 0x0FFFFFFF; //PB7 push-pull output

  GPIOB->CRL |= 0x30000000;

  IIC_SCL = 0;

  IIC_SDA = 0;

   delay_us(4);

  IIC_SCL = 1;

  IIC_SDA = 1;

  delay_us(4);

}

/****************************************************** **

Name:IIC_Wait_Ack

Function: IIC waits for response

Paramater:None

Return:

      0:success

      1:failed

*************************************************** */

void IIC_Wait_Ack()

{

  u8 Time = 0;

  GPIOB->CRL &= 0x0FFFFFFF;

  GPIOB->CRL |= 0x80000000;

  IIC_SDA = 1;

  delay_us( 1 );

  IIC_SCL = 1;

  delay_us( 1 );

  while(IIC_SDA_READ)

  {

    Time++;

    if(Time>250)

    {

      IIC_Stop();

      break ;

    }

  }

  IIC_SCL = 0;

}

/****************************************************** **

Name:IIC_Send_Byte

Function: IIC sends a byte

Paramater:

      ack: response enable

        0: No answer

        1:Response

Return :None

*************************************************** */

void IIC_Send_Byte( u8 Byte )

{

  u8i;

  GPIOB->CRL &= 0x0FFFFFFF; //PB7 push-pull output

  GPIOB->CRL |= 0x30000000;

  IIC_SCL = 0;

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

  {

    if((Byte&0x80)==0x80)

      IIC_SDA = 1;

    else

      IIC_SDA = 0;

    Byte <<= 1;

    delay_us(2);

    IIC_SCL = 1;

    delay_us(2);

    IIC_SCL = 0;

    delay_us(2);

  }

}

/****************************************************** **

Name:IIC_Read_Byte

Function: IIC reads a byte

Paramater:

      ack: response enable

        0: No answer

        1:Response

Return :None

*************************************************** */

u8 IIC_Read_Byte( u8 Ack )

{

  u8 i,Byte=0;

  GPIOB->CRL &= 0x0FFFFFFF;

  GPIOB->CRL |= 0x80000000;

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

  {

    IIC_SCL = 0;

    delay_us(2);

    IIC_SCL = 1;

    Byte <<= 1;

    if(IIC_SDA_READ)

      Byte |= 0x01;

    delay_us( 1 );

  }

  IIC_SCL = 0;

  GPIOB->CRL &= 0x0FFFFFFF; //PB7 push-pull output

  GPIOB->CRL |= 0x30000000;

  IIC_SDA = 1 - Ack;

  delay_us(2);

  IIC_SCL = 1;

  delay_us(2);

  IIC_SCL = 0;

  return Byte;

}

/****************************************************** **

Name:AT24Cxx_Write_Data

Function: write 1 data

Paramater:

      Address: address

      Data: data

Return: the data read

*************************************************** */

void AT24Cxx_Write_Data( u16 Address, u8 Data )

{                                                      

  IIC_Start();

  IIC_Send_Byte( 0xA0|( Address/256 )<<1 ) ; //Send device address and write data

  IIC_Wait_Ack();

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

  IIC_Wait_Ack();

  IIC_Send_Byte( Data ); //Send bytes

  IIC_Wait_Ack();

  IIC_Stop(); //Generate a stop condition

  delay_ms(10); //EEPROM writing speed is relatively slow

}

/****************************************************** **

Name:AT24Cxx_Write_nData

Function: Write n data

Paramater:

      Address: address

      *Buffer: data cache

      Len: data length

Return :None

*************************************************** */

void AT24Cxx_Write_nData( u16 Address, u8 *Buffer, u16 Len ) 

{

  u16i;

  for( i=0; iAPB2ENR |= 1<<3; //Enable peripheral GPIOB clock first

  GPIOB->CRL &= 0x00FFFFFF; //PB6 and PB7 push-pull output

  GPIOB->CRL |= 0x33000000;

  GPIOB->ODR |= 3<<6; //PB6 and PB7 output high

  while(AT24Cxx_Check()==0);

}

(3) Create 1.c file and enter the following code.



#include "sys.h"

#include "delay.h"

#include "usart1.h"

#include "lcd.h"

#include "at24cxx.h"



u8 TEXT_Buffer[] = "STM32F103 IIC Test";

int main()

{

  u8 datatemp[ 17 ];

  STM32_Clock_Init( 9 ); //STM32 clock initialization

  SysTick_Init( 72 ); //SysTick initialization

  USART1_Init( 72, 115200 ); //Initialize serial port 1 baud rate 115200

  LCD_Init(); //LCD initialization

  AT24Cxx_Init(); //AT24C initialization

   POINT_COLOR = RED; //Set the font to red

  AT24Cxx_Write_nData( 0, TEXT_Buffer, 18 ); //Start writing from the 0th address

  AT24Cxx_Read_nData(0, datatemp, 18); //Start reading from the 0th address

  LCD_ShowString(0, 0, datatemp); //Display the read string

  while(1)

  {


  }

}

16.4.2 Hardware IIC control


Note: Since the hardware IIC of STM32 is always prone to jamming (which is why there are almost no examples of hardware IIC communication on the network), the communication mechanism provided internally by ST is used here to ensure the normal use of IIC.


(1) Create at24cxx.h file and enter the following code.


copy

/****************************************************** *************************************************** ******

                EEPROM driver file

*************************************************** *************************************************** *****/

#ifndef _AT24Cxx_H_

#define _AT24Cxx_H_



#include "sys.h"

/****************************************************** *************************************************** ******

                    function list

*************************************************** *************************************************** *****/

void AT24Cxx_Init( void ); //AT24C initialization

void IIC_Write_Data( u8 Address, u8 Data ); //Write 1 data

void AT24Cxx_Write_nData( u16 Address, u8 *Buffer, u16 Len ); //Write n data

void AT24Cxx_Read_Data( u16 Address, u8 *Data ); //Read 1 data

void AT24Cxx_Read_nData( u16 Address, u8 *Buffer, u16 Len ); //Read n data



#endif


Keywords:STM32 Reference address:STM32 introductory study notes EEPROM storage experiment 4

Previous article:Basic knowledge points about C language in STM32 learning
Next article:STM32 introductory study notes: watchdog experiment (Part 2)

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号