有个设计时需要STM32F10X作为主机与STM320XX作为从机通过I2C通讯,之前没弄个STM32F0XX,还以为STM32F10X与STM32F0XX的I2C程序很接近。花了三天时间总于将STM32F10X的I2C双机通讯搞定。昨天开始把程序移植到STM32F0XX上。悲剧发现了,STM32F0XX的跟STM3210X的不一样。好吧,我认了,我参照库函数重新写。
结果更悲剧就发生了。STM32F0XX的I2C不管是硬件中断发送,还是接受。都没反应。
下面是小弟写的STM32F0XX的I2C初始化函数,请各位大神帮忙看看。
//初始化
I2C_InitTypeDef I2C_InitStructure;
//48M,从机,用的是8M外部晶振
I2C_InitStructure.I2C_Timing=0x30e30000; //??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;//地址为0
I2C_InitStructure.I2C_Ack=I2C_Ack_Enable; //ack??
I2C_InitStructure.I2C_AcknowledgedAddress=I2C_AcknowledgedAddress_7bit;
I2C_Init(I2C1, &I2C_InitStructure);
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 PB8,9 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¹Ü½Å³õʼ»¯
/* 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);
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_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);
//中断
void I2C1_IRQHandler(void)
{
|