/*
* myiic.c
*
* Created on: 2023年3月21日
* Author: 11705
*/
#include "myiic.h"
void delay_1ms(void)
{
delay_100us();delay_100us();delay_100us();delay_100us();delay_100us();delay_100us();delay_100us();delay_100us();delay_100us();delay_100us();
}
void delay_100us(void)
{
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();__NOP();
}
void delay_5us(void)
{
__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();
}
void delay_2us(void)
{
__NOP();__NOP();__NOP();__NOP();
__NOP();__NOP();__NOP();__NOP();
}
void delay_1us(void)
{
__NOP();__NOP();__NOP();__NOP();
}
uint8_t Read_IIC_SDA(void)
{
bsp_io_level_t SDA_pin_status = BSP_IO_LEVEL_LOW;
R_IOPORT_PinRead(&g_ioport_ctrl,SDA_PIN,&SDA_pin_status);
if(SDA_pin_status == BSP_IO_LEVEL_LOW)
{
return 0;
}
else
{
return 1;
}
}
void IIC_Init(void)
{
IIC_SDA_H;
delay_1ms();
IIC_SCL_H;
delay_1ms();
}
void IIC_Start(void)
{
SET_IIC_SDA_OUT;
IIC_SDA_H;
IIC_SCL_H;
delay_5us();
IIC_SDA_L;
delay_5us();
IIC_SCL_L;
}
void IIC_Stop(void)
{
SET_IIC_SDA_OUT;
IIC_SCL_L;
IIC_SDA_L;
delay_5us();
IIC_SCL_H;
delay_5us();
IIC_SDA_H;
}
uint8_t IIC_Wait_Ack(void)
{
uint8_t ucErrTime=0;
SET_IIC_SDA_IN;
IIC_SDA_H;
delay_2us();
IIC_SCL_H;
delay_2us();
while(Read_IIC_SDA())
{
ucErrTime++;
if(ucErrTime>250)
{
IIC_Stop();
return 1;
}
}
IIC_SCL_L;
return 0;
}
void IIC_Ack(void)
{
IIC_SCL_L;
SET_IIC_SDA_OUT;
IIC_SDA_L;
delay_2us();
IIC_SCL_H;
delay_2us();
IIC_SCL_L;
}
void IIC_NAck(void)
{
IIC_SCL_L;
SET_IIC_SDA_OUT;
IIC_SDA_H;
delay_2us();
IIC_SCL_H;
delay_2us();
IIC_SCL_L;
}
void IIC_Send_Byte(uint8_t txd)
{
uint8_t t;
SET_IIC_SDA_OUT;
IIC_SCL_L;
for(t=0;t<8;t++)
{
if((txd&0x80)>>7)
{
IIC_SDA_H;
}
else
{
IIC_SDA_L;
}
txd<<=1;
delay_1us();
IIC_SCL_H;
delay_2us();
IIC_SCL_L;
delay_2us();
}
}
uint8_t IIC_Read_Byte(unsigned char ack)//发送ack为1发送ACK,反之发送nACK
{
unsigned char i,receive=0;
// SET_IIC_SDA_OUT;
// IIC_SDA_H;delay_2us();//未加上拉电阻时,手动拉高数据线
SET_IIC_SDA_IN;
for(i=0;i<8;i++ )
{
IIC_SCL_L;
delay_2us();
IIC_SCL_H;
receive<<=1;
if(Read_IIC_SDA())
{
receive++;
}
delay_2us();
}
if (!ack)
{
IIC_NAck();
}
else
{
IIC_Ack();
}
return receive;
}