【CW32L052测评】 i2c之eeprom读写测试
[复制链接]
本帖最后由 TL-LED 于 2023-7-29 07:19 编辑
这篇来学习下i2c协议读写EEPROM芯片CW24C02测试。
一、硬件电路
CW24C02芯片是开发板自带的芯片,电路图部分如下
i2c使用的端口是PB08、PB09和PC13,PC13是写保护控制,这里不使用。使用时需要将J2接插件跳线。
二、程序部分
i2c驱动EEPROM芯片的API函数,芯片库中厂家已经做好,直接可以应用挺方便。
2.1、fun_i2c.c
#include "main.h"
#define WRITEADDRESS 0
#define WriteReadCycle 22
uint8_t u8Senddata[] = "cw32l052 board write and read i2c eeprom test";
#define BufferSize1 (ARRAY_SZ(u8Senddata) - 1)
uint8_t u8Recdata[BufferSize1]={0x00};
uint8_t u8SendLen=0;
uint8_t u8RecvLen=0;
uint8_t SendFlg = 0,Comm_flg = 0;
uint8_t u8recvflg=0;
uint8_t u8State = 0;
TestStatus TStatus = FAILED;
void init_i2c(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
I2C_InitTypeDef I2C_InitStruct;
TEST_I2C_AFSCL;
TEST_I2C_AFSDA;
GPIO_InitStruct.Pins = TEST_I2C_SCL_GPIO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.IT = GPIO_IT_NONE;
GPIO_Init(TEST_I2C_SCL_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pins = TEST_I2C_SDA_GPIO_PIN;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.IT = GPIO_IT_NONE;
GPIO_Init(TEST_I2C_SDA_GPIO_PORT, &GPIO_InitStruct);
TEST_I2C_ClkENx;
//I2C3õê¼»ˉ
I2C_InitStruct.I2C_BaudEn = ENABLE;
I2C_InitStruct.I2C_Baud = 0x05;//1MHz=(24000000/(8*(2+1))
I2C_InitStruct.I2C_FLT = DISABLE;
I2C_InitStruct.I2C_AA = DISABLE;
TEST_I2C_DeInit;
I2C_Master_Init(TEST_I2C,&I2C_InitStruct);
I2C_Cmd(TEST_I2C,ENABLE);
}
void I2C_Test(void)
{
printf("\r\n i2c eeprom test Start\r\n");
uint8_t writecnt;
if(BufferSize1%8)
{
writecnt = BufferSize1/8 +1;
}
else
{
writecnt = BufferSize1/8;
}
for(uint8_t j=0;j<writecnt;j++)
{
I2C_MasterWriteEepromData(TEST_I2C,(WRITEADDRESS+j*8),&u8Senddata[j*8],8);
SysTickDelay(10);
}
printf("D′èëμÄêy¾YÎa£o%s\r\n", u8Senddata);
I2C_MasterReadEepomData(TEST_I2C,WRITEADDRESS,u8Recdata,BufferSize1);
printf("¶á3öμÄêy¾YÎa£o%s\r\n", u8Recdata);
TStatus = Buffercmp(u8Senddata, u8Recdata, BufferSize1);
if(TStatus == PASSED)
{
printf("I2C_EEPROM Test Success\r\n");
}
else
{
printf("I2C_EEPROM Test Error:Read and Write Data is not same\r\n");
while(1);
}
}
2.2、fun_i2c.h
#ifndef __FUN_I2C_H
#define __FUN_I2C_H
#define TEST_I2C CW_I2C1
#define TEST_I2C_ClkENx __RCC_I2C1_CLK_ENABLE()
#define TEST_I2C_DeInit I2C1_DeInit()
#define TEST_I2C_SCL_GPIO_PORT CW_GPIOB
#define TEST_I2C_SCL_GPIO_PIN GPIO_PIN_8
#define TEST_I2C_SDA_GPIO_PORT CW_GPIOB
#define TEST_I2C_SDA_GPIO_PIN GPIO_PIN_9
#define TEST_I2C_AFSCL PB08_AFx_I2C1SCL()
#define TEST_I2C_AFSDA PB09_AFx_I2C1SDA()
void init_i2c(void);
void I2C_Test(void);
#endif
2.3、main.c
#include "main.h"
void rcc_config(void)
{
RCC_HSI_Enable(RCC_HSIOSC_DIV2);
RCC_HCLKPRS_Config(RCC_HCLK_DIV1);
RCC_PCLKPRS_Config(RCC_PCLK_DIV1);
__RCC_FLASH_CLK_ENABLE();
RCC_HSE_Enable(RCC_HSE_MODE_CLK,24000000,RCC_HSE_DRIVER_LEAST,RCC_HSE_FLT_OPEN);
FLASH_SetLatency(FLASH_Latency_2);
RCC_HSI_Enable( RCC_HSIOSC_DIV1 );
RCC_SysClk_Switch(RCC_SYSCLKSRC_HSI);
RCC_SystemCoreClockUpdate(48000000);
__RCC_GPIOF_CLK_ENABLE();
__RCC_GPIOE_CLK_ENABLE();
__RCC_GPIOD_CLK_ENABLE();
__RCC_GPIOC_CLK_ENABLE();
__RCC_GPIOB_CLK_ENABLE();
__RCC_GPIOA_CLK_ENABLE();
__RCC_LCD_CLK_ENABLE();
CW_SYSCTRL->AHBEN = 0xFFFFFFFF;
CW_SYSCTRL->APBEN1 = 0xFFFFFFFF;
CW_SYSCTRL->APBEN2 = 0xFFFFFFFF;
}
int32_t main(void)
{
uint8_t dp[8];
uint8_t i=0;
uint16_t tp=0;
uint32_t dp1=0;
uint32_t dp_temp=0;
GPIO_InitTypeDef GPIO_InitStruct = {0};
rcc_config();
init_uart();
init_led();
init_adc();
init_spi_flash();
init_i2c();
InitTick( 48000000 ); //初始化SysTick
LCD_Configuration();
SPI_FLASH_Test();
dp[0]=disp[0];
dp[1]=disp[0];
dp[2]=disp[0];
dp[3]=disp[0];
dp[4]=disp[0];
dp[5]=disp[0];
dp[6]=disp[0];
dp[7]=disp[0];
I2C_Test();
while (1)
{
// float adv=0.0;
// tp=(adc_readval(1)*33)/4096;
// dp[4]=disp[tp/1000];
// dp[3]=disp[((tp%1000)/100)];
// dp[2]=disp_p[((tp%1000)%100)/10];
// dp[1]=disp[((tp%1000)%100)%10];
// dp[0]=0x4c;
// led2_tog();
// SysTickDelay(100);
// lcd_disp(dp);
// adv=tp*0.1;
// printf("ADC value = %1.1f \r\n",adv );
}
}
三、测试结果
程序运行后、串口输出内容
|