最近用了一款 BS8112A-3 16NSOP 12键电容触摸按键芯片IC,调了半天没调通 ,最后发现把设备地址看错了,手册给的是7bit地址,子设备地址应该是0xA0不应该是0x50!
现上代码:
#include "stm32f0xx.h"
void I2C_INT_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);//ÅäÖÃʱÖÓ
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE);//ÅäÖÃʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
EXTI_InitStructure.EXTI_Line=EXTI_Line5;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource5);
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=EXTI4_15_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority=2;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void I2C_SDACL_Init(void) //I2CÒý½ÅÅäÖÃ
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);//ÅäÖÃʱÖÓ
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10, GPIO_AF_4);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_4);
}
void ZLG_24C_Init(void)//I2C³õʼ»¯º¯Êý
{
I2C_InitTypeDef I2C_InitStructure;
I2C_INT_Init();
I2C_SDACL_Init();
RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);//ÅäÖÃʱÖÓRCC_APB1Periph_I2C1
I2C_InitStructure.I2C_Mode=I2C_Mode_I2C;
I2C_InitStructure.I2C_Ack=I2C_Ack_Enable;
//I2C_InitStructure.I2C_Ack=I2C_Ack_Disable;
I2C_InitStructure.I2C_AcknowledgedAddress=I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_DigitalFilter = 0x00;
I2C_InitStructure.I2C_OwnAddress1=0x00;
//I2C_InitStructure.I2C_Timing=0x30E32E44;
I2C_InitStructure.I2C_Timing=0x20D22E37;
I2C_InitStructure.I2C_AnalogFilter=I2C_AnalogFilter_Enable;
I2C_Init(I2C1,&I2C_InitStructure);
I2C_Cmd(I2C1,ENABLE);
// ZLG_24C_Read(0xA0,0x08);
}
uint8_t ZLG_24C_Read(uint8_t addr1, uint8_t addr2)//¶ÁI2Cº¯Êý
{
uint8_t data;
uint16_t i;
i=0;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) != RESET)
{
i++;
if(i>600) return 0;
}
I2C_TransferHandling(I2C1, addr1, 1, I2C_SoftEnd_Mode,I2C_Generate_Start_Write);
i=0;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) == RESET)
{
i++;
if(i>600) return 0;
}
I2C_Senddata(I2C1, addr2);
i=0;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TC) == RESET)
{
i++;
if(i>600) return 0;
}
I2C_TransferHandling(I2C1, addr1, 1, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
i=0;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) == RESET)
{
i++;
if(i>600) return 0;
}
data=I2C_Receivedata(I2C1);
i=0;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF) == RESET)
{
i++;
if(i>600) return 0;
}
i=0;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_RXNE) != RESET)
{
i++;
if(i>600) return 0;
}
return(data);
}
void ZLG_24C_Write(uint8_t addr1,uint8_t addr2,uint8_t data)//дI2Cº¯Êý
{
uint16_t i;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY) != RESET);
I2C_TransferHandling(I2C1, addr1, 2, I2C_AutoEnd_Mode,I2C_Generate_Start_Write);
i=0;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) == RESET)
{
i++;
if(i>600) return;
}
I2C_Senddata(I2C1, addr2);
i=0;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_TXIS) == RESET)
{
i++;
if(i>600) return;
}
I2C_Senddata(I2C1, data);
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_STOPF) == RESET);
}
void EXTI4_15_IRQHandler(void)
{
if(EXTI_GetITStatus(EXTI_Line5)!=RESET)
{
settableflag(Flag_getkey,0);
//riar=1;
}
EXTI_ClearITPendingBit(EXTI_Line5);//Çå³ýÖжϱê־λ
}