32位单片机模拟IIC


void iic_GPIO_Init(void)
{
    __HAL_RCC_GPIOB_CLK_ENABLE();
    
  GPIO_InitTypeDef GPIO_InitStruct;

  GPIO_InitStruct.Pin = SCL_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(SCL_GPIO_Port, &GPIO_InitStruct);

  GPIO_InitStruct.Pin = SDA_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(SCL_GPIO_Port, &GPIO_InitStruct);
    
}

void SDA_OUT()
{
  GPIO_InitTypeDef GPIO_InitStruct;

  GPIO_InitStruct.Pin = SDA_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(SDA_GPIO_Port, &GPIO_InitStruct);    
    
}

void SDA_IN()
{
  GPIO_InitTypeDef GPIO_InitStruct;

  GPIO_InitStruct.Pin = SDA_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(SDA_GPIO_Port, &GPIO_InitStruct);    

}

/*****************************************************************************
** ????:I2CStart
** ????:??I2C??????
** ?    ?:?
** ?    ?:?
** ????:?
** ????:Delay()
** ????:????
******************************************************************************/
void iic_start(void)
{
    SDA_OUT();
    SDA1;
    SCL1;
    delay_us(6);      // ?????
    SDA0;
    delay_us(6);      // ?????
    SCL0;
}
/*****************************************************************************
** ????:I2CStop
** ????:??I2C????????
** ?    ?:?
** ?    ?:?
** ????:?
** ????:Delay()
** ????:????
******************************************************************************/
void iic_stop(void)
{
    SDA_OUT();
    SCL0;
    delay_us(6);      // ?????
    SDA0;
    delay_us(6);      // ?????
    SCL1;
    delay_us(6);      // ?????
    SDA1;
    delay_us(6);
}

uint8_t iic_wait_ack(void)
{
    uint8_t errtime;
    SDA_IN();
    SDA1;
    delay_us(6);
    SCL1;
    delay_us(6);
    while(RDSDA)
    {
        errtime++;
        if(errtime>100)
        {
            iic_stop();
            return 1;
        }
    }
    SCL0;
    return 0;    
}


void iic_ack(void)
{
    SCL0;
    SDA_OUT();
    SDA0;
    delay_us(6);
    SCL1;
    delay_us(6);
    SCL0;
}

void iic_nack(void)
{
    SCL0;
    SDA_OUT();
    SDA1;
    delay_us(6);
    SCL1;
    delay_us(6);
    SCL0;
}

void iic_write_byte(unsigned char txd)
{
    uint8_t t;
    SDA_OUT();
    SCL0;
    for(t = 0; t< 8 ; t++)
    {
        if((txd&0x80)>>7)
            SDA1;
        else
            SDA0;
        txd<<=1;
        delay_us(6);
        SCL1;
        delay_us(6);
        SCL0;
    }
    delay_us(6);
}

uint8_t  iic_read_byte(uint8_t ack)
{
    uint8_t i,receive=0;
    SDA_IN();
    for(i = 0; i< 8; i++)
    {
        SCL0;
        delay_us(6);
        SCL1;
        receive <<=1;
        if(RDSDA)receive++;
        delay_us(6);
    }
    if(!ack)
        iic_nack();
    else
        iic_ack();
    return receive;
}

void sy8801_write_byte(u8 add , u8 data)  //写数据
{
    iic_start();
    iic_write_byte(0x0c);  //写命令
    iic_stop();
    iic_write_byte(add);  //寄存器地址
    iic_wait_ack();
    iic_write_byte(data); //写入的数据
    iic_wait_ack();
    iic_stop();    
}

uint8_t sy8801_read_byte(u8 add)  //读数据
{
    unsigned char i;
    
    iic_start();
    iic_write_byte(0x0c); //写命令
    iic_wait_ack();
    iic_write_byte(add);  //寄存器地址
    iic_wait_ack();
    iic_start();         
    iic_write_byte(0x0d); //读命令
    iic_wait_ack();
    i = iic_read_byte(0); //读数据
    iic_stop();
    
    return i;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值