你可以使用一个状态机和定时器中断来实现非阻塞延时。在这种情况下,你需要将写EEPROM的过程分解为几个步骤,并为每个步骤定义一个状态。然后,你可以在定时器中断服务程序中改变状态。
以下是一个简单的示例:
c
typedef enum {
STATE_IDLE,
STATE_START,
STATE_WRITE_PADDR,
STATE_WRITE_ADDRESS,
STATE_WRITE_DATA,
STATE_STOP,
STATE_DELAY
} State;
volatile State state = STATE_IDLE;
uint8_t address, data, paddr;
void TIMx_IRQHandler(void) // 定时器中断服务程序
{
if (TIM_GetITStatus(TIMx, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIMx, TIM_IT_Update);
switch (state)
{
case STATE_IDLE:
break;
case STATE_START:
IIC_GPIO_Start();
state = STATE_WRITE_PADDR;
break;
case STATE_WRITE_PADDR:
IIC_WriteOneByte(paddr);
state = STATE_WRITE_ADDRESS;
break;
case STATE_WRITE_ADDRESS:
IIC_WriteOneByte(address);
state = STATE_WRITE_DATA;
break;
case STATE_WRITE_DATA:
IIC_WriteOneByte(data);
state = STATE_STOP;
break;
case STATE_STOP:
IIC_GPIO_Stop();
state = STATE_DELAY;
break;
case STATE_DELAY:
// 延时5ms后,返回到空闲状态
static int delayCounter = 0;
if (++delayCounter >= 5)
{
delayCounter = 0;
state = STATE_IDLE;
}
break;
}
}
}
void Write_EEPROM(uint8_t _address, uint8_t _data, uint8_t _paddr)
{
// 如果当前状态为空闲,则开始写入过程
if (state == STATE_IDLE)
{
address = _address;
data = _data;
paddr = _paddr;
state = STATE_START;
}
}
这样,你就可以在主循环中调用Write_EEPROM函数,而不会阻塞其他任务。只有当写入过程完成(即状态返回到STATE_IDLE)时,才能开始新的写入过程。 |