今晚搞好了硬件I2C读写24C04,开发环境KEIL。用例程,程序一点没改,立马OK!
代码:
#include "i2c.h"
#include "gpio.h"
/******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
/******************************************************************************
* Global variable definitions (declared in header file with 'extern')
******************************************************************************/
/******************************************************************************
* Local type definitions ('typedef')
******************************************************************************/
/******************************************************************************
* Local function prototypes ('static')
******************************************************************************/
/******************************************************************************
* Local variable definitions ('static') *
******************************************************************************/
/******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
/*****************************************************************************
* Function implementation - global ('extern') and local ('static')
******************************************************************************/
/**
******************************************************************************
** \brief Main function of project
**
** \return uint32_t return value, if needed
**
** This sample
**
******************************************************************************/
#define I2C_SLAVEADDR 0xA0
#define READLEN 10
uint8_t u8Senddata[10] = {0x12,0x34,0x77,0x66,0x55,0x44,0x33,0x22,0x11,0x99};
uint8_t u8Recdata[10]={0x00};
uint8_t u8SendLen=0;
uint8_t u8RecvLen=0;
uint8_t Send** = 0,Comm_** = 0;
uint8_t u8recv**=0;
uint8_t u8State = 0;
//主发送函数
en_result_t I2C_MasterWriteData(en_i2c_channel_t enCh,uint8_t u8Addr,uint8_t *pu8Data,uint32_t u32Len);
//主接收函数
en_result_t I2C_MasterReadData(en_i2c_channel_t enCh,uint8_t u8Addr,uint8_t *pu8Data,uint32_t u32Len);
/**
******************************************************************************
** \brief 主机接收函数
**
** \param u8Addr从机内存地址,pu8Data读数据存放缓存,u32Len读数据长度
**
** \retval 读数据是否成功
**
******************************************************************************/
en_result_t I2C_MasterReadData(en_i2c_channel_t enCh,uint8_t u8Addr,uint8_t *pu8Data,uint32_t u32Len)
{
en_result_t enRet = Error;
uint8_t u8i=0,u8State;
I2C_SetFunc(enCh,I2cStart_En);
while(1)
{
while(0 == I2C_GetIrq(enCh))
{}
u8State = I2C_GetState(enCh);
switch(u8State)
{
case 0x08:
I2C_ClearFunc(enCh,I2cStart_En);
I2C_WriteByte(enCh,I2C_SLAVEADDR);
break;
case 0x18:
I2C_WriteByte(enCh,u8Addr);
break;
case 0x28:
I2C_SetFunc(enCh,I2cStart_En);
break;
case 0x10:
I2C_ClearFunc(enCh,I2cStart_En);
I2C_WriteByte(enCh,I2C_SLAVEADDR|0x01);//从机地址发送OK
break;
case 0x40:
if(u32Len>1)
{
I2C_SetFunc(enCh,I2cAck_En);
}
break;
case 0x50:
pu8Data[u8i++] = I2C_ReadByte(enCh);
if(u8i==u32Len-1)
{
I2C_ClearFunc(enCh,I2cAck_En);
}
break;
case 0x58:
pu8Data[u8i++] = I2C_ReadByte(enCh);
I2C_SetFunc(enCh,I2cStop_En);
break;
case 0x38:
I2C_SetFunc(enCh,I2cStart_En);
break;
case 0x48:
I2C_SetFunc(enCh,I2cStop_En);
I2C_SetFunc(enCh,I2cStart_En);
break;
default:
I2C_SetFunc(enCh,I2cStart_En);//其他错误状态,重新发送起始条件
break;
}
I2C_ClearIrq(enCh);
if(u8i==u32Len)
{
break;
}
}
enRet = Ok;
return enRet;
}
/**
******************************************************************************
** \brief 主机发送函数
**
** \param u8Addr从机内存地址,pu8Data写数据,u32Len写数据长度
**
** \retval 写数据是否成功
**
******************************************************************************/
en_result_t I2C_MasterWriteData(en_i2c_channel_t enCh,uint8_t u8Addr,uint8_t *pu8Data,uint32_t u32Len)
{
en_result_t enRet = Error;
uint8_t u8i=0,u8State;
I2C_SetFunc(enCh,I2cStart_En);
while(1)
{
while(0 == I2C_GetIrq(enCh))
{;}
u8State = I2C_GetState(enCh);
switch(u8State)
{
case 0x08:
I2C_ClearFunc(enCh,I2cStart_En);
I2C_WriteByte(enCh,I2C_SLAVEADDR);//从设备地址发送
break;
case 0x18:
I2C_WriteByte(enCh,u8Addr);//从设备内存地址发送
break;
case 0x28:
I2C_WriteByte(enCh,pu8Data[u8i++]);
break;
case 0x20:
case 0x38:
I2C_SetFunc(enCh,I2cStart_En);
break;
case 0x30:
I2C_SetFunc(enCh,I2cStop_En);
break;
default:
break;
}
if(u8i>u32Len)
{
I2C_SetFunc(enCh,I2cStop_En);//此顺序不能调换,出停止条件
I2C_ClearIrq(enCh);
break;
}
I2C_ClearIrq(enCh);
}
enRet = Ok;
return enRet;
}
int32_t main(void)
{
stc_gpio_config_t stcGpioCfg;
stc_i2c_config_t stcI2cCfg;
stc_sysctrl_clk_config_t stcCfg;
DDL_ZERO_STRUCT(stcCfg);
DDL_ZERO_STRUCT(stcI2cCfg);
DDL_ZERO_STRUCT(stcGpioCfg);
Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE);
Sysctrl_SetPeripheralGate(SysctrlPeripheralI2c1,TRUE);
stcGpioCfg.enDir = GpioDirOut;
stcGpioCfg.enOD = GpioOdEnable;
stcGpioCfg.enPuPd = GpioPu;
Gpio_Init(GpioPortA, GpioPin11,&stcGpioCfg);
Gpio_Init(GpioPortA, GpioPin12,&stcGpioCfg);
Gpio_SetAfMode(GpioPortA, GpioPin11,GpioAf3);//SCL
Gpio_SetAfMode(GpioPortA, GpioPin12,GpioAf3);//SDA
stcI2cCfg.enFunc = I2cBaud_En;
stcI2cCfg.u8Tm = 0x04;//100K=(4000000/(8*(4+1))
stcI2cCfg.pfnI2c1Cb = NULL;
stcI2cCfg.bTouchNvic = FALSE;
if(TRUE == stcI2cCfg.bTouchNvic)
{
EnableNvic(I2C1_IRQn,IrqLevel3,TRUE);
}
I2C_DeInit(I2C1);
I2C_Init(I2C1,&stcI2cCfg);//初始化模块
I2C_SetFunc(I2C1,I2cMode_En);//模块使能
I2C_SetFunc(I2C1,I2cStart_En);//开始信号
I2C_MasterWriteData(I2C1,0x00,u8Senddata,10);
delay1ms(100);
I2C_MasterReadData(I2C1,0x00,u8Recdata,10);
while(1)
{
}
}
效果图:
没想到模拟时序不行,明天搞出来。
|
共1人点赞
|