|
我按照最新STM32F051,库文件提示,编写一个I2C中断发送程序,I2C两脚一直没有电平变化。
程序初始化如下
void I2C1_Init(void)
{
I2C_InitTypeDef I2C_InitStructure;
I2C_InitStructure.I2C_Timing=0x00901D2B; //??SCL?SDA????
I2C_InitStructure.I2C_AnalogFilter=I2C_AnalogFilter_Enable; //??????
I2C_InitStructure.I2C_DigitalFilter=0;
I2C_InitStructure.I2C_Mode=I2C_Mode_I2C; //??IIC??
I2C_InitStructure.I2C_OwnAddress1=0;
I2C_InitStructure.I2C_Ack=I2C_Ack_Enable; //ack??
I2C_InitStructure.I2C_AcknowledgedAddress=I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1, &I2C_InitStructure); //I2C½Ó¿Ú³õʼ»¯
}
/***************************************************
**¹¦ÄÜ:Init I2C.
**×¢ÒâÊÂÏî:
***************************************************/
void I2C_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/*I2C Clock Configuration -----------------------------*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
// SYSCFG_DeInit();
// SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_PB8,ENABLE);
// SYSCFG_I2CFastModePlusConfig(SYSCFG_I2CFastModePlus_PB9,ENABLE);
/*Configuraton I2C NVIC----------------------------------*/
NVIC_InitStructure.NVIC_IRQChannel = I2C1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/*Init I2C Parameters ----------------------------------*/
I2CParameters.I2C1Flag=I2C_Reset;
I2CParameters.I2C1RxLen=0;
I2CParameters.I2C1TxLen=0;
I2CParameters.I2C1RxCount=0;
I2CParameters.I2C1TxCount=0;
GPIO_PinAFConfig(GPIOB,GPIO_Pin_8,GPIO_AF_1);
GPIO_PinAFConfig(GPIOB,GPIO_Pin_9,GPIO_AF_1);
/* I2C1 control io configuration PB6,7 SCL and SDA */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure); //I2C½Ó¿ÚʹÓõÄGPIO¹Ü½Å³õʼ»¯
/*24C128 WP control io configuration----------------*/
/* GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 ; //24C128 WP CONTROL IO
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOB, &GPIO_InitStructure); */ //I2C½Ó¿ÚʹÓõÄGPIO¹Ü½Å³õʼ»¯
/*I2C1 configuration--------------------------------*/
// I2C_SlaveAddressConfig(I2C1,0);
I2C_DeInit(I2C1);
I2C1_Init();
//I2C_StretchClockCmd(I2C1,ENABLE);
//I2C_SlaveByteControlCmd(I2C1,ENABLE);
I2C_AcknowledgeConfig(I2C1, ENABLE); //ʹÄÜI2C½Ó¿ÚÏìÓ¦
I2C_Cmd(I2C1, ENABLE); //ʹÄÜI2C½Ó¿Ú
/*Open I2C IT---------------------------------------*/
//I2C_ITConfig(I2C1,I2C_IT_EVT|I2C_IT_BUF,ENABLE);
I2C_ITConfig(I2C1,I2C_IT_NACKI,ENABLE);
I2C_ITConfig(I2C1,I2C_IT_TXI,ENABLE);
I2C_ITConfig(I2C1,I2C_IT_ERRI,ENABLE);
I2C_ITConfig(I2C1, I2C_IT_TCI, ENABLE);
I2C_ITConfig(I2C1, I2C_IT_STOPI, ENABLE);
I2C_ITConfig(I2C1, I2C_IT_ADDRI, ENABLE);
I2C_ITConfig(I2C1, I2C_IT_RXI, ENABLE);
}
求大神指点
|
|