用
STM32f030c8硬件iic 读pcf8563 实时时钟,在初步调试的时候就出现了问题,每次过8s就会week month数据就变了,很有规律,没找出什么原因,求助啊
下面是程序,希望大家给予指正,谢谢。
/*********************************************************************************
Func
tion : InitializeI2cGpio
Description: 初始化i2c的GPIO
Input : 无
Output : 无
Return : 无
Others :
*********************************************************************************/
static bool InitializeI2cGpio ( unsigned char type )
{
GPIO_InitTypeDef GPIO_InitStructure;
if( type >= I2Cn )
return false;
/* Enable SCK and SDA GPIO clocks */
RCC_AHBPeriphClockCmd(ST_I2C_CLK_GPIO_CLK_TABLE[type] | ST_I2C_SDA_GPIO_CLK_TABLE[type] , ENABLE);
GPIO_PinAFConfig(ST_I2C_CLK_GPIO_PORT_TABLE[type], ST_I2C_CLK_SOURCE_TABLE[type], ST_I2C_CLK_AF_TABLE[type]);
GPIO_PinAFConfig(ST_I2C_SDA_GPIO_PORT_TABLE[type], ST_I2C_SDA_SOURCE_TABLE[type], ST_I2C_SDA_AF_TABLE[type]);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;//开漏
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//sw
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/* I2C SCK pin configuration */
GPIO_InitStructure.GPIO_Pin = ST_I2C_CLK_PIN_TABLE[type];
GPIO_Init(ST_I2C_CLK_GPIO_PORT_TABLE[type], &GPIO_InitStructure);
/* I2C SDA pin configuration */
GPIO_InitStructure.GPIO_Pin = ST_I2C_SDA_PIN_TABLE[type];
GPIO_Init(ST_I2C_SDA_GPIO_PORT_TABLE[type], &GPIO_InitStructure);
return true;
}
/*********************************************************************************
Function : InitializeI2cConfig
Description: 初始化硬件I2C配置
Input : clock = true 8M_400k false 8M_100k
Output : 无
Return : 无
Others :
*********************************************************************************/
static bool InitializeI2cConfig ( unsigned char type ,bool clock )
{
I2C_InitTypeDef I2C_InitStructure;
if( type >= I2Cn )
return false;
/* Enable the I2C periph source*/
// RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);
RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);//48MHZSW
RCC_APB1PeriphClockCmd(ST_I2C_CLK_TABLE[type], ENABLE); //Enable I2C1 Clock
//初始I2C时钟 SW
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
I2C_InitStructure.I2C_DigitalFilter = 0x00;
I2C_InitStructure.I2C_OwnAddress1 = 0x00;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
if( clock )
// I2C_InitStructure.I2C_Timing = EE_I2C_8MHZ_400KHZ_TIMING;//晶振修改为8MHZ sw
I2C_InitStructure.I2C_Timing = 0x30E32E44;//0x00A0184D;//48M-400K
else
I2C_InitStructure.I2C_Timing = EE_I2C_8MHZ_100KHZ_TIMING;
I2C_Init(ST_I2C_TABLE[type], &I2C_InitStructure);
I2C_Cmd(ST_I2C_TABLE[type], ENABLE);
return true;
}
*********************************************************************************
Function : InitializeI2c
Description: 初始化i2c
Input : 无
Output : 无
Return : 无
Others :
*********************************************************************************/
void InitializeI2c ( bool clock )
{
for(unsigned char i=0;i<I2Cn;i++)
{
InitializeI2cGpio(i);
InitializeI2cConfig(i , clock );
}
}
上面是初始化硬件IIC,剩下的会在下面跟上
bool ReadPCF8563( uint32_t offset , uint32_t len , unsigned char* p_data )
{
unsigned char reg_add ,loop = 3;
unsigned char result = false;
while( loop-- )
{
//读取失败
//if ( I2CDeviceRecv( &iic_deviece ) )
if (I2C1_Read_NBytes(PCF8563_I2C_ADDR_READ, 0x02, 7, p_data));
{
result = true;
break;
}
}
return result;
}
一周热门 更多>