/********************************** (C) COPYRIGHT *******************************
* File Name : main.c
* Author : WCH
* Version : V1.0.0
* Date : 2021/06/06
* Description : Main program body.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/*
*@Note
I2C interface routine to operate EEPROM peripheral.
I2C1_SCL(PB10)\I2C1_SDA(PB11).
This example uses EEPROM for AT24Cxx series.
Steps:
READ EEPROM:Start + 0xA0 + 8bit Data Address + Start + 0xA1 + Read Data + Stop.
WRITE EERPOM:Start + 0xA0 + 8bit Data Address + Write Data + Stop.
*/
#include "debug.h"
/**********************************************************************
*@Note:
AT24Cxx
READ EEPROM:Start + 0xA0 + 8bit Data Address + Start + 0xA1 + Read Data + Stop.
WRITE EERPOM:Start + 0xA0 + 8bit Data Address + Write Data + Stop.
*******************************************************************************/
/* EERPOM DATA ADDRESS Length Definition */
#define Address_8bit 0
#define Address_16bit 1
/* EERPOM DATA ADDRESS Length Selection */
#define Address_Lenth Address_8bit
//#define Address_Lenth Address_16bit
/* Global define */
#define SIZE sizeof(TEXT_Buffer)
/* Global Variable */
const u8 TEXT_Buffer[] = {"CH32F10x I2C TEST"};
/*********************************************************************
* @fn IIC_Init
*
* @brief Initializes the IIC peripheral.
*
* @return none
*/
void IIC_Init(u32 bound, u16 address)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
I2C_InitTypeDef I2C_InitTSturcture = {0};
printf("init pos 58\r\n");
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
printf("init pos 61\r\n");
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
printf("init pos 66\r\n");
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
printf("init pos 71\r\n");
I2C_InitTSturcture.I2C_ClockSpeed = bound;
I2C_InitTSturcture.I2C_Mode = I2C_Mode_I2C;
I2C_InitTSturcture.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitTSturcture.I2C_OwnAddress1 = address;
I2C_InitTSturcture.I2C_Ack = I2C_Ack_Enable;
I2C_InitTSturcture.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C2, &I2C_InitTSturcture);
printf("init pos 79\r\n");
I2C_Cmd(I2C2, ENABLE);
printf("init pos 81\r\n");
I2C_AcknowledgeConfig(I2C2, ENABLE);
printf("init pos 83\r\n");
}
/*********************************************************************
* @fn AT24CXX_Init
*
* @brief Initializes AT24xx EEPROM.
*
* @return none
*/
void AT24CXX_Init(void)
{
IIC_Init(50000, 0xA0);
}
/*********************************************************************
* @fn AT24CXX_ReadOneByte
*
* @brief Read one data from EEPROM.
*
* @param ReadAddr - Read frist address.
*
* @return temp - Read data.
*/
u8 AT24CXX_ReadOneByte(u16 ReadAddr)
{
u8 temp = 0;
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY) != RESET)
;
I2C_GenerateSTART(I2C2, ENABLE);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT))
;
I2C_Send7bitAddress(I2C2, 0XA0, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
;
#if(Address_Lenth == Address_8bit)
I2C_SendData(I2C2, (u8)(ReadAddr & 0x00FF));
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
;
#elif(Address_Lenth == Address_16bit)
I2C_SendData(I2C2, (u8)(ReadAddr >> 8));
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
;
I2C_SendData(I2C2, (u8)(ReadAddr & 0x00FF));
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
;
#endif
I2C_GenerateSTART(I2C2, ENABLE);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT))
;
I2C_Send7bitAddress(I2C2, 0XA0, I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
;
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_RXNE) == RESET)
I2C_AcknowledgeConfig(I2C2, DISABLE);
temp = I2C_ReceiveData(I2C2);
I2C_GenerateSTOP(I2C2, ENABLE);
return temp;
}
/*********************************************************************
* @fn AT24CXX_WriteOneByte
*
* @brief Write one data to EEPROM.
*
* @param WriteAddr - Write frist address.
*
* @return DataToWrite - Write data.
*/
void AT24CXX_WriteOneByte(u16 WriteAddr, u8 DataToWrite)
{
printf("write one byte pos 165");
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY) != RESET)
;
printf("write one byte pos 168");
I2C_GenerateSTART(I2C2, ENABLE);
printf("write one byte pos 170");
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT))
;
printf("write one byte pos 173");
I2C_Send7bitAddress(I2C2, 0XA0, I2C_Direction_Transmitter);
printf("write one byte pos 175");
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
;
printf("write one byte pos 178");
#if(Address_Lenth == Address_8bit)
I2C_SendData(I2C2, (u8)(WriteAddr & 0x00FF));
printf("write one byte pos 182");
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
;
printf("write one byte pos 185");
#elif(Address_Lenth == Address_16bit)
I2C_SendData(I2C2, (u8)(WriteAddr >> 8));
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
;
I2C_SendData(I2C2, (u8)(WriteAddr & 0x00FF));
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
;
#endif
if(I2C_GetFlagStatus(I2C2, I2C_FLAG_TXE) != RESET)
{
printf("write one byte pos 199");
I2C_SendData(I2C2, DataToWrite);
}
printf("write one byte pos 202");
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
;
printf("write one byte pos 206");
I2C_GenerateSTOP(I2C2, ENABLE);
printf("write one byte pos 208");
}
/*********************************************************************
* @fn AT24CXX_Read
*
* @brief Read multiple data from EEPROM.
*
* @param ReadAddr - Read frist address. (AT24c02: 0~255)
* pBuffer - Read data.
* NumToRead - Data number.
*
* @return none
*/
void AT24CXX_Read(u16 ReadAddr, u8 *pBuffer, u16 NumToRead)
{
while(NumToRead)
{
*pBuffer++ = AT24CXX_ReadOneByte(ReadAddr++);
NumToRead--;
}
}
/*********************************************************************
* @fn AT24CXX_Write
*
* @brief Write multiple data to EEPROM.
*
* @param WriteAddr - Write frist address. (AT24c02: 0~255)
* pBuffer - Write data.
* NumToWrite - Data number.
*
* @return none
*/
void AT24CXX_Write(u16 WriteAddr, u8 *pBuffer, u16 NumToWrite)
{
while(NumToWrite--)
{
AT24CXX_WriteOneByte(WriteAddr, *pBuffer);
WriteAddr++;
pBuffer++;
Delay_Ms(2);
}
}
/*********************************************************************
* @fn main
*
* @brief Main program.
*
* @return none
*/
int main(void)
{
u8 data[SIZE];
SystemCoreClockUpdate();
Delay_Init();
USART_Printf_Init(115200);
printf("SystemClk:%d\r\n", SystemCoreClock);
printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
AT24CXX_Init();
u8 temp = 0;
u16 ReadAddr=6;
printf("read pos 277\r\n");
while(I2C_GetFlagStatus(I2C2, I2C_FLAG_BUSY) != RESET)
;
while(1){
printf("read pos 280\r\n");
I2C_GenerateSTART(I2C2, ENABLE);
printf("read pos 282\r\n");
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT))
;
printf("read pos 285\r\n");
I2C_Send7bitAddress(I2C2, 12, I2C_Direction_Transmitter);
printf("read pos 287\r\n");
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
;
printf("read pos 290\r\n");
I2C_SendData(I2C2, (u8)(0x003));
printf("read pos 293\r\n");
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
;
printf("read pos 303\r\n");
I2C_Send7bitAddress(I2C2, 12, I2C_Direction_Receiver);
printf("read pos 305\r\n");
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
;
printf("read pos 311\r\n");
temp = I2C_ReceiveData(I2C2);
printf("read pos 313\r\n");
I2C_GenerateSTOP(I2C2, ENABLE);
printf("read pos 315\r\n");
printf("1 read byte value:%d\r\n",temp);
printf("read pos 280\r\n");
I2C_GenerateSTART(I2C2, ENABLE);
printf("read pos 282\r\n");
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_MODE_SELECT))
;
printf("read pos 285\r\n");
I2C_Send7bitAddress(I2C2, 12, I2C_Direction_Transmitter);
printf("read pos 287\r\n");
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
;
printf("read pos 290\r\n");
I2C_SendData(I2C2, (u8)(0x004));
printf("read pos 293\r\n");
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
;
printf("read pos 303\r\n");
I2C_Send7bitAddress(I2C2, 12, I2C_Direction_Receiver);
printf("read pos 305\r\n");
while(!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
;
printf("read pos 311\r\n");
uint8_t temp1 = I2C_ReceiveData(I2C2);
printf("read pos 313\r\n");
I2C_GenerateSTOP(I2C2, ENABLE);
printf("read pos 315\r\n");
printf("2 read byte value:%d\r\n",temp1);
uint16_t u16=0;
u16=((uint16_t)temp1&0x3f) | ((uint16_t)temp<<6);
printf("3 read byte value:%d\r\n",u16);
Delay_Ms(100);
};
return 0;
}
打印结果:
SystemClk:96000000
ChipID:30700518
init pos 58
init pos 61
init pos 66
init pos 71
init pos 79
init pos 81
init pos 83
read pos 277
read pos 280
read pos 282
read pos 285
read pos 287
read pos 290
read pos 293
read pos 303
read pos 305