我正在使用STM32f030c8在24c04开发用于I2C eeprom的库。我的读写功能有效,但是当我在写入eeprom后立即尝试读取时,eeprom doest会返回任何数据。但是我可以马上连续写一下。只有在写入语句后才能读取问题。我尝试启用和禁用I2C使能位,但问题仍然存在。有人可以告诉我这是什么问题吗
- void main()
- {
- Configure_GPIO_I2C2();
- Configure_I2C2_Master(0xA0,1);
- I2C_WriteByte(5,'k');
- charr= I2C_ReadByte(5);//the program get stuck here because no byte is
- //received from eeprom
- UART_Send_String(1,&charr);
- }
- void I2C_WriteByte(uint8_t addr,uint8_t bytetowrite)
- {
- I2C2->ISR=0x01;
- I2C2_StartWrite(2);//start
- I2C2->TXDR = addr;//write addr
- while(!(I2C2->ISR & I2C_ISR_TXE));
- //I2C2_StartWrite(1);
- I2C2->TXDR = bytetowrite;
- while(!(I2C2->ISR & I2C_ISR_TXE));
- I2C2->CR2 |= I2C_CR2_STOP;//stop
- while(I2C2->CR2 & I2C_CR2_STOP);
- }
- uint8_t I2C_ReadByte(uint8_t byteToRead)
- {
- I2C2->ISR=0x01;
- I2C2_StartWrite(1);
- I2C2->TXDR = byteToRead;
- while(!(I2C2->ISR & I2C_ISR_TXE));
- I2C2_StartRead(1);
- while(!(I2C2->ISR & I2C_ISR_RXNE));
- UART_Send_String(1,"r strt");
- uint8_t recv_data=I2C2->RXDR;
- I2C2->CR2 |= I2C_CR2_STOP;
- while(I2C2->CR2 & I2C_CR2_STOP);
- return recv_data;
- }
- void Configure_GPIO_I2C2(void)
- {
- RCC->AHBENR |= RCC_AHBENR_GPIOFEN;
- GPIOF->MODER |= (2<<12) | (2<<14);
- GPIOF->OTYPER |= GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_7;
- GPIOF->OSPEEDR &= ~(1<<12);
- GPIOF->OSPEEDR &= ~(1<<14);
- GPIOF->PUPDR &= ~(1<<12);
- GPIOF->PUPDR &= ~(1<<12);
- }
- void Configure_I2C2_Master(uint8_t slave_addr,uint8_t no_of_bytes)
- {
- RCC->APB1ENR |= RCC_APB1ENR_I2C2EN;
- /* (1) timing register value is computed with the AN4235 xls file,
- fast Mode @400kHz with I2CCLK = 48MHz, rise time = 140ns, fall time = 40ns */
- I2C2->CR1 &= ~I2C_CR1_PE;
- I2C2->TIMINGR |= (uint32_t)0x00B01A4B; /* (1) */
- I2C2->CR2 |= (uint8_t)slave_addr;
- I2C2->CR2 |= no_of_bytes<<16;
- I2C2->CR1 |= I2C_CR1_PE;
- //NVIC_SetPriority(I2C2_IRQn, 0); /* (7) */
- //NVIC_EnableIRQ(I2C2_IRQn); /* (8) */
- }
- void I2C2_StartWrite(int bytesToWrite)
- {
- I2C2->CR2 &= ~I2C_CR2_RD_WRN;
- I2C2->CR2 |= bytesToWrite<<16;
- I2C2->CR2 |= I2C_CR2_START;
- while(I2C2->CR2 & I2C_CR2_START);
- }
- void I2C2_StartRead(int bytesToRead)
- {
- I2C2->CR2 |= I2C_CR2_RD_WRN;
- I2C2->CR2 |= bytesToRead<<16;
- I2C2->CR2 |= I2C_CR2_START;
- while(I2C2->CR2 & I2C_CR2_START);
- }
复制代码
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
大多数EEPROM都有类似状态寄存器的地方,你可以在其中进行轮询以查看它是否正忙。无论是否繁忙,都可以执行该读取操作。
最通用的软件架构总是在尝试执行任何操作之前轮询忙碌位。如果EEPROM忙,它会继续轮询,直到它没有,然后继续执行所请求的任何操作。更复杂的实现在写入和擦除时设置标志,表明EEPROM可能正忙。只有在设置此标志时才会进行繁忙检查。天真的实现总是在擦除或写入后等待。
仔细查看你的代码,特别是当写入后立即读取时,确切地发送到EEPROM。我的猜测是,在这种情况下,繁忙的检查会以某种方式被忽略。
一周热门 更多>