在线时间16 小时
UID303348
ST金币0
蝴蝶豆0
注册时间2011-7-5
金牌会员
- 最后登录
- 1970-1-1
|
发表于 2012-8-29 16:49:22
|
显示全部楼层
a0a.1 0b0c
回复:iic 中断处理 求助【悬赏问答】
此程序使用中。。。。。
void I2C_Configuration(void)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure I2C1 pins: SCL PB6 and SDA PB7 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure PB8as Output push-pull */
/*PB8--I2C1_AT24C02_WP;*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* I2C1 configuration ------------------------------------------------------*/
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0XA4;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000;
I2C_Cmd(I2C1, ENABLE);
I2C_Init(I2C1, &I2C_InitStructure);
I2C_AcknowledgeConfig(I2C1, ENABLE);/*允许1字节1应答模式*/
}
/*******************************************************************************
* Function Name : 写一个字节
* Description :
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void I2C_WriteByte(uint8_t Byte, uint16_t WriteAddr, uint8_t EE_Block )
{
/* 起始位 */
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/*发送器件地址(写)*/
I2C_Send7bitAddress(I2C1, EE_Block, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/*发送地址*/
I2C_SendData(I2C1, (uint8_t)((WriteAddr & 0xFF00) >> 8)); //地址高字节
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, (uint8_t)(WriteAddr & 0x00FF)); //地址低字节
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BTF) == RESET);
/*写一个字节*/
I2C_SendData(I2C1, Byte);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/*停止位*/
I2C_GenerateSTOP(I2C1, ENABLE);
Standby(EE_Block);
}
/*******************************************************************************
* Function Name : 读一个字节
* Description :
* Input : None
* Output : None
* Return : None
*******************************************************************************/
uint8_t I2C_ReadByte(uint16_t ReadAddr, uint8_t EE_Block )
{
uint8_t Byte;
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
/*允许1字节1应答模式*/
I2C_AcknowledgeConfig(I2C1, ENABLE);
/* 发送起始位 */
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));/*EV5,主模式*/
/*发送器件地址(写)*/
I2C_Send7bitAddress(I2C1, EE_Block , I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/*发送地址*/
I2C_SendData(I2C1, (uint8_t)((ReadAddr & 0xFF00) >> 8)); //地址高字节
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, (uint8_t)(ReadAddr & 0x00FF)); //地址低字节
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BTF) == RESET);
/*起始位*/
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/*器件读*/
I2C_Send7bitAddress(I2C1, EE_Block, I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
I2C_AcknowledgeConfig(I2C1, DISABLE); //最后一位后要关闭应答的
I2C_GenerateSTOP(I2C1, ENABLE); //发送停止位
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)); /* EV7 */
Byte = I2C_ReceiveData(I2C1);
//再次允许应答模式
I2C_AcknowledgeConfig(I2C1, ENABLE);
return Byte ;
}
/*******************************************************************************
* Function Name : 写铁电
* Description :
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void I2C_FM_Write(uint8_t *pBuffer, uint8_t no, uint16_t WriteAddr, uint8_t EE_Block )
{
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
/*起始位*/
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/*器件地址(写)*/
I2C_Send7bitAddress(I2C1, EE_Block, I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/*发送地址*/
I2C_SendData(I2C1, (uint8_t)((WriteAddr & 0xFF00) >> 8)); //地址高字节
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, (uint8_t)(WriteAddr & 0x00FF)); //地址低字节
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BTF) == RESET);
while(no--)
{
I2C_SendData(I2C1, *pBuffer);
pBuffer++;
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
/*停止位*/
I2C_GenerateSTOP(I2C1, ENABLE);
}
/*******************************************************************************
* Function Name : 读铁电
* Description :
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void I2C_FM_Read(uint8_t *pBuffer, uint8_t no, uint16_t ReadAddr, uint8_t EE_Block )
{
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
/* 发送起始位 */
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));/*EV5,主模式*/
/*发送器件地址(写)*/
I2C_Send7bitAddress(I2C1, EE_Block , I2C_Direction_Transmitter);
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
/* Clear EV6 by setting again the PE bit */
I2C_Cmd(I2C1, ENABLE);
/*发送地址*/
I2C_SendData(I2C1, (uint8_t)((ReadAddr & 0xFF00) >> 8)); //地址高字节
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, (uint8_t)(ReadAddr & 0x00FF)); //地址低字节
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BTF) == RESET);
/*起始位*/
I2C_GenerateSTART(I2C1, ENABLE);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));
/*器件读*/
I2C_Send7bitAddress(I2C1, EE_Block, I2C_Direction_Receiver);
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
while (no)
{
if(no==1)
{
I2C_AcknowledgeConfig(I2C1, DISABLE); //最后一位后要关闭应答的
I2C_GenerateSTOP(I2C1, ENABLE); //发送停止位
}
if(I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_RECEIVED)) /* EV7 */
{
*pBuffer = I2C_ReceiveData(I2C1);
pBuffer++;
no--;
}
}
//再次允许应答模式
I2C_AcknowledgeConfig(I2C1, ENABLE);
}/*******************************************************************************
* Function Name : 判忙
* Description :
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void Standby(uint8_t EE_Block)
{
vu16 Tmp = 0;
do
{
/*起始位*/
I2C_GenerateSTART(I2C1, ENABLE);
/*读SR1*/
Tmp = I2C_ReadRegister(I2C1, I2C_Register_SR1);
/*器件地址(写)*/
I2C_Send7bitAddress(I2C1, EE_Block, I2C_Direction_Transmitter);
}while(!(I2C_ReadRegister(I2C1, I2C_Register_SR1) & 0x0002));
I2C_ClearFlag(I2C1, I2C_FLAG_AF);
/*停止位*/
I2C_GenerateSTOP(I2C1, ENABLE);
} |
|