(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
Previous article:Basic knowledge points about C language in STM32 learning
Next article:STM32 introductory study notes: watchdog experiment (Part 2)
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- Europe's three largest chip giants re-examine their supply chains
- Breaking through the intelligent competition, Changan Automobile opens the "God's perspective"
- The world's first fully digital chassis, looking forward to the debut of the U7 PHEV and EV versions
- Design of automotive LIN communication simulator based on Renesas MCU
- When will solid-state batteries become popular?
- Adding solid-state batteries, CATL wants to continue to be the "King of Ning"
- The agency predicts that my country's public electric vehicle charging piles will reach 3.6 million this year, accounting for nearly 70% of the world
- U.S. senators urge NHTSA to issue new vehicle safety rules
- Giants step up investment, accelerating the application of solid-state batteries
- Guangzhou Auto Show: End-to-end competition accelerates, autonomous driving fully impacts luxury...
- MSP430 ADC conversion + mean filter
- World Cup tickets use RFID technology
- EEWORLD University ---- Linux Kernel Design
- TI eSMO library Fsmopos and Gsmopos parameter analysis
- Today's live broadcast: TE will explain the design trends of smart antennas and sensor application cases in the Internet of Things
- High frequency amplifier circuit knowledge explanation
- msp430 implements a key matrix scan and UART transmission
- MSP430 ADC acquisition filter
- 【GD32E231 DIY】USART0 uses DMA to send and receive, without using CPU
- Soil moisture measurement circuit diagram