- /*I2C³õʼ»¯º¯Êý*/
- void i2c_init()
- {
- I2C_InitTypeDef I2C_Struct;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
- I2C_Struct.I2C_ClockSpeed=200000;
- I2C_Struct.I2C_Mode=I2C_Mode_I2C ;
- I2C_Struct.I2C_DutyCycle=I2C_DutyCycle_2;
- I2C_Struct.I2C_Ack=I2C_Ack_Enable;
- I2C_Struct.I2C_AcknowledgedAddress=I2C_AcknowledgedAddress_7bit ;
- I2C_Init ( I2C1, &I2C_Struct);
-
- I2C_Cmd(I2C1,ENABLE);
- }
-
- /*GPIOB³õʼ»¯º¯Êý*/
- void gpio_init()
- {
- GPIO_InitTypeDef GPIO_Struct;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
- GPIO_Struct.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
- GPIO_Struct.GPIO_Mode=GPIO_Mode_AF_OD;
- GPIO_Struct.GPIO_Speed= GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&GPIO_Struct);
- GPIO_Struct.GPIO_Pin=GPIO_Pin_9;
- GPIO_Struct.GPIO_Mode=GPIO_Mode_Out_PP;
- GPIO_Struct.GPIO_Speed= GPIO_Speed_50MHz;
- GPIO_Init(GPIOB,&GPIO_Struct);
- }
- /*µÈ´ýEEPROM¿ÕÏÐ״̬*/
- void I2C_WaitEepromStandbyState(u8 id)
- {
- vu16 SR1_Tmp = 0;
- do
- {
- /* Send START condition */
- I2C_GenerateSTART(I2C1, ENABLE);
- /* Read I2C1 SR1 register */
- SR1_Tmp = I2C_ReadRegister(I2C1, I2C_Register_SR1);
- /* Send EEPROM address for write */
- I2C_Send7bitAddress(I2C1, id, I2C_Direction_Transmitter);
- }while(!(I2C_ReadRegister(I2C1, I2C_Register_SR1) & 0x0002)); //estimate the value of ADDR
-
- /* Clear AF flag */
- I2C_ClearFlag(I2C1, I2C_FLAG_AF);
- }
- /*ÏòÖ¸¶¨µØַдµ¥¸ö×Ö½Ú³ÌÐò*/
- void i2c_write(u8 id,u8 address,u8 byte)
- {
- I2C_WaitEepromStandbyState(id);
- I2C_GenerateSTART ( I2C1, ENABLE);//·¢ËÍÆðʼÐźÅ
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); //ev5
- I2C_Send7bitAddress ( I2C1,id, I2C_Direction_Transmitter); //·¢ËÍÆ÷¼þµØÖ·
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED )); //ev6
- I2C_SendData ( I2C1, address);//·¢ËÍÊý¾ÝÔÚ´æ´¢Æ÷ÖеĵØÖ·
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //ev8
- I2C_SendData ( I2C1, byte);//·¢ËÍÊý¾Ý
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED)); //ev8_2
- I2C_GenerateSTOP(I2C1, ENABLE);//·¢ËÍÍ£Ö¹ÐźÅ
- }
- /*´ÓÖ¸¶¨µØÖ·¶Áµ¥¸ö×Ö½Ú³ÌÐò*/
- u8 i2c_read(u8 id,u8 address)
- {
- u8 temp;
- I2C_WaitEepromStandbyState(id);
- I2C_GenerateSTART(I2C1, ENABLE);//·¢Æô¶¯ÐźÅ
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));//EV5
- I2C_Send7bitAddress ( I2C1,id, I2C_Direction_Transmitter);//·¢ËÍÆ÷¼þµØÖ·
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));//EV6
- I2C_SendData ( I2C1, address);//·¢ËÍÊý¾ÝÔÚ´æ´¢Æ÷ÖеØÖ·
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));//EV8
- I2C_GenerateSTART(I2C1, ENABLE); //ÖØз¢ËÍ¿ªÊ¼ÐźÅ
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT)); //ev5
- I2C_AcknowledgeConfig(I2C1, DISABLE); //¹Ø±ÕÓ¦´ð¹¦ÄÜ
- I2C_Send7bitAddress ( I2C1,id, I2C_Direction_Receiver);//ÖØз¢ËÍÆ÷¼þµØÖ·
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)); //ev6
- temp=I2C_ReceiveData(I2C1);//½ÓÊÕÊý¾Ý
- while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED)); //ev7
- I2C_GenerateSTOP(I2C1, ENABLE);//·¢ËÍÍ£Ö¹ÐźÅ
- return temp;
- }
- int main(void)
- {
- i2c_init();
- gpio_init();
- i2c_write(0xa2,3,0xab);
- data=i2c_read(0xa2,3);
- if(data == 0xab)//¼ì²â¶Á³öдÈëÊÇ·ñÏàͬ
- {
- for(;;)//LEDÉÁ˸
- {
- Delay_MS(1000);
- // GPIO_Write(GPIOB, 0x10);
- GPIOB->ODR = 0x10;
- GPIO_SetBits(GPIOB,GPIO_Pin_9);
- // GPIO_Write(GPIOB, 0x00);
- Delay_MS(1000);
- GPIOB->ODR = 0x00;
- GPIO_ResetBits(GPIOB,GPIO_Pin_9);
- }
- }
- while(1);
- }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
嗯,在网上看到相关的问题了,但是模拟的话好像是比较麻烦的吧
一周热门 更多>