WouoUI/Third-party-design/platform-test/User/iic.c

182 lines
3.5 KiB
C
Raw Permalink Normal View History

#include "iic.h"
//*****************************************************************/
//<2F><>ʼ<EFBFBD>ź<EFBFBD>
void IIC_Start(void)
{
SDA_OUT();
IIC_SDA(HIGH);
IIC_SCL(HIGH);
IIC_Delay();
IIC_SDA(LOW);
IIC_Delay();
IIC_SCL(LOW);
}
/*****************************************************************/
//ֹͣ<CDA3>ź<EFBFBD>
void IIC_Stop(void)
{
SDA_OUT();
IIC_SCL(LOW);
IIC_SDA(LOW);
IIC_Delay();
IIC_SCL(HIGH);
IIC_SDA(HIGH);
IIC_Delay();
}
/*****************************************************************/
//<2F>ȴ<EFBFBD><C8B4><EFBFBD>Ӧ<EFBFBD>ź<EFBFBD>
//<2F><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6>ʧ<EFBFBD><CAA7>
// 0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><D3A6><EFBFBD>ɹ<EFBFBD>
uint8_t IIC_Wait_Ack(void)
{
uint8_t Time = 0;
SDA_IN();
IIC_SDA(HIGH);
IIC_Delay();
IIC_SCL(HIGH);
IIC_Delay();
while(READ_SDA)
{
Time++;
IIC_Delay();
if(Time>250)
{
SDA_OUT();
IIC_Stop();
return 1;
}
}
SDA_OUT();
IIC_SCL(LOW);
return 0;
}
/***************************************************************/
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ź<EFBFBD>
void IIC_Ack(void)
{
IIC_SCL(LOW);
SDA_OUT();
IIC_SDA(LOW);
IIC_Delay();
IIC_SCL(HIGH);
IIC_Delay();
IIC_SCL(LOW);
}
/**************************************************************/
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ź<EFBFBD>
void IIC_NAck(void)
{
IIC_SCL(LOW);
SDA_OUT();
IIC_SDA(HIGH);
IIC_Delay();
IIC_SCL(HIGH);
IIC_Delay();
IIC_SCL(LOW);
}
/*************************************************************/
//<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֽ<EFBFBD>
void IIC_Send_Byte(uint8_t Txd)
{
uint8_t t;
SDA_OUT();
IIC_SCL(LOW);
for(t = 0;t<8;t++)
{
IIC_SDA((Txd&0x80)>>7);
Txd<<=1;
IIC_SCL(HIGH);
IIC_Delay();
IIC_SCL(LOW);
IIC_Delay();
}
}
/**************************************************************/
//<2F><>ȡһ<C8A1><D2BB><EFBFBD>ֽڣ<D6BD>ack=1ʱ<31><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ACK<43><4B>ack=0<><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD>nACK
uint8_t IIC_Read_Byte(uint8_t Ack)
{
uint8_t i,receive = 0;
SDA_IN();
for(i = 0;i<8;i++)
{
IIC_SCL(LOW);
IIC_Delay();
IIC_SCL(HIGH);
IIC_Delay();
IIC_Delay();
receive<<=1;
if(READ_SDA)
{
receive++;
}
IIC_Delay();
}
SDA_OUT();
if(!Ack)
IIC_NAck();
else
IIC_Ack();
return receive;
}
/**************************************************************/
//IIC<49><43>ʱ
void IIC_Delay(void)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><D6B8> 1/72M=0.01389us
// co_delay_10us(1);
// __NOP();__NOP();__NOP();__NOP();__NOP();
// __NOP();__NOP();__NOP();__NOP();__NOP();
// __NOP();__NOP();__NOP();__NOP();__NOP();
// __NOP();__NOP();__NOP();__NOP();__NOP();
// __NOP();__NOP();__NOP();__NOP();__NOP();
// __NOP();__NOP();__NOP();__NOP();__NOP();
// __NOP();__NOP();__NOP();__NOP();__NOP();
// __NOP();__NOP();__NOP();__NOP();__NOP();
// __NOP();__NOP();__NOP();__NOP();__NOP();
LL_mDelay(1);
}
void SDA_OUT(void)
{
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = LCD_SDA_Pin;
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
LL_GPIO_Init(LCD_SCL_GPIO_Port, &GPIO_InitStruct);
}
void SDA_IN(void)
{
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = LCD_SDA_Pin;
GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
// GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
LL_GPIO_Init(LCD_SCL_GPIO_Port, &GPIO_InitStruct);
}
//<2F><>ʼ<EFBFBD><CABC>IIC
void IIC_Init(void)
{
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = LCD_SCL_Pin|LCD_SDA_Pin;
GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_OPENDRAIN;
LL_GPIO_Init(LCD_SCL_GPIO_Port, &GPIO_InitStruct);
}