软件iic测试成功
This commit is contained in:
+115
-88
@@ -11,121 +11,148 @@
|
||||
#define iic_scl(x) HAL_GPIO_WritePin(iic_scl_GPIO_Port, iic_scl_Pin, x)
|
||||
#define read_iic_sda HAL_GPIO_ReadPin(iic_sda_GPIO_Port, iic_sda_Pin)
|
||||
|
||||
#define IIC_SDA_SET iic_sda(1)
|
||||
#define IIC_SCL_SET iic_scl(1)
|
||||
#define IIC_SDA_CLR iic_sda(0)
|
||||
#define IIC_SCL_CLR iic_scl(0)
|
||||
#define SDA_OUT() change_io_function(iic_sda_GPIO_Port, iic_sda_Pin,0)
|
||||
#define SDA_IN() change_io_function(iic_sda_GPIO_Port, iic_sda_Pin,1)
|
||||
#define READ_SDA read_iic_sda
|
||||
|
||||
void iic_start()
|
||||
//产生IIC起始信号
|
||||
void IIC_Start(void)
|
||||
{
|
||||
iic_sda(1);
|
||||
iic_scl(1);
|
||||
iic_sda(0);
|
||||
iic_scl(0);
|
||||
SDA_OUT(); //sda线输出
|
||||
IIC_SDA_SET;
|
||||
IIC_SCL_SET;
|
||||
IIC_SDA_CLR;//START:when CLK is high,DATA change form high to low
|
||||
IIC_SCL_CLR;//钳住I2C总线,准备发送或接收数据
|
||||
}
|
||||
|
||||
void iic_stop()
|
||||
//产生IIC停止信号
|
||||
void IIC_Stop(void)
|
||||
{
|
||||
iic_sda(0);
|
||||
iic_scl(1);
|
||||
iic_sda(1);
|
||||
SDA_OUT();//sda线输出
|
||||
IIC_SCL_CLR;
|
||||
IIC_SDA_CLR;//STOP:when CLK is high DATA change form low to high
|
||||
IIC_SCL_SET;
|
||||
IIC_SDA_SET;//发送I2C总线结束信号
|
||||
}
|
||||
|
||||
void iic_ack()
|
||||
//等待应答信号到来
|
||||
//返回值:1,接收应答失败
|
||||
// 0,接收应答成功
|
||||
uint8_t IIC_Wait_Ack(void)
|
||||
{
|
||||
iic_scl(0);
|
||||
iic_sda(0);
|
||||
iic_scl(1);
|
||||
iic_scl(0);
|
||||
}
|
||||
char iic_wait_ack()
|
||||
{
|
||||
int a=3000;
|
||||
iic_scl(1);
|
||||
iic_sda(1);
|
||||
change_io_function(iic_sda_GPIO_Port, iic_sda_Pin,1);
|
||||
// while(read_iic_sda)
|
||||
// {
|
||||
// a--;
|
||||
// if(a==0)
|
||||
// {
|
||||
// iic_stop();
|
||||
// return 1;
|
||||
// }
|
||||
// }
|
||||
HAL_Delay(1);
|
||||
change_io_function(iic_sda_GPIO_Port, iic_sda_Pin,0);
|
||||
uint8_t ucErrTime=0;
|
||||
SDA_IN(); //SDA设置为输入
|
||||
IIC_SDA_SET;
|
||||
IIC_SCL_SET;
|
||||
while(READ_SDA)
|
||||
{
|
||||
ucErrTime++;
|
||||
if(ucErrTime>250)
|
||||
{
|
||||
IIC_Stop();
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
IIC_SCL_CLR;//时钟输出0
|
||||
return 0;
|
||||
}
|
||||
|
||||
void IIC_Write_Byte(unsigned char IIC_Byte)
|
||||
//产生ACK应答
|
||||
void IIC_Ack(void)
|
||||
{
|
||||
iic_scl(0);
|
||||
for(unsigned char i=0;i<8;i++)
|
||||
{
|
||||
if(IIC_Byte & 0x80)
|
||||
{
|
||||
iic_sda(1);
|
||||
}else
|
||||
{
|
||||
iic_sda(0);
|
||||
}
|
||||
IIC_Byte<<=1;
|
||||
iic_scl(1);
|
||||
iic_scl(0);
|
||||
}
|
||||
IIC_SCL_CLR;
|
||||
SDA_OUT();
|
||||
IIC_SDA_CLR;
|
||||
IIC_SCL_SET;
|
||||
IIC_SCL_CLR;
|
||||
}
|
||||
|
||||
unsigned char IIC_Read_Byte()
|
||||
//不产生ACK应答
|
||||
void IIC_NAck(void)
|
||||
{
|
||||
unsigned char k=0;
|
||||
iic_scl(0);
|
||||
iic_sda(1);
|
||||
change_io_function(iic_sda_GPIO_Port, iic_sda_Pin,1);
|
||||
for(unsigned char i=0; i<8; i++)
|
||||
{
|
||||
iic_scl(1);
|
||||
k<<=1;
|
||||
if(read_iic_sda==1)
|
||||
{
|
||||
k|=0x01;
|
||||
}
|
||||
|
||||
iic_scl(0);
|
||||
}
|
||||
change_io_function(iic_sda_GPIO_Port, iic_sda_Pin,0);
|
||||
return(k);
|
||||
IIC_SCL_CLR;
|
||||
SDA_OUT();
|
||||
IIC_SDA_SET;
|
||||
IIC_SCL_SET;
|
||||
IIC_SCL_CLR;
|
||||
}
|
||||
//IIC发送一个字节
|
||||
//返回从机有无应答
|
||||
//1,有应答
|
||||
//0,无应答
|
||||
void IIC_Send_Byte(uint8_t txd)
|
||||
{
|
||||
uint8_t t;
|
||||
SDA_OUT();
|
||||
IIC_SCL_CLR;//拉低时钟开始数据传输
|
||||
for(t=0;t<8;t++)
|
||||
{
|
||||
//IIC_SDA=(txd&0x80)>>7;
|
||||
if((txd&0x80)>>7)
|
||||
IIC_SDA_SET;
|
||||
else
|
||||
IIC_SDA_CLR;
|
||||
txd<<=1;
|
||||
|
||||
IIC_SCL_SET;
|
||||
|
||||
IIC_SCL_CLR;
|
||||
|
||||
}
|
||||
}
|
||||
//读1个字节,ack=1时,发送ACK,ack=0,发送nACK
|
||||
uint8_t IIC_Read_Byte(unsigned char ack)
|
||||
{
|
||||
unsigned char i,receive=0;
|
||||
SDA_IN();//SDA设置为输入
|
||||
for(i=0;i<8;i++ )
|
||||
{
|
||||
IIC_SCL_CLR;
|
||||
|
||||
IIC_SCL_SET;
|
||||
receive<<=1;
|
||||
if(READ_SDA)receive++;
|
||||
|
||||
}
|
||||
if (!ack)
|
||||
IIC_NAck();//发送nACK
|
||||
else
|
||||
IIC_Ack(); //发送ACK
|
||||
return receive;
|
||||
}
|
||||
|
||||
void IIC_SAND_DATE(unsigned char DEVICE_ADD,unsigned char IN_DEVICE_ADD,char *DATAS,uint16_t LONG)
|
||||
{
|
||||
iic_start();
|
||||
IIC_Write_Byte(DEVICE_ADD);
|
||||
if(iic_wait_ack()){return;}
|
||||
IIC_Write_Byte(IN_DEVICE_ADD);
|
||||
if(iic_wait_ack()){return;}
|
||||
IIC_Start();
|
||||
IIC_Send_Byte(DEVICE_ADD);
|
||||
if(IIC_Wait_Ack()){return;}
|
||||
IIC_Send_Byte(IN_DEVICE_ADD);
|
||||
if(IIC_Wait_Ack()){return;}
|
||||
for(int a=0;a<LONG;a++)
|
||||
{
|
||||
IIC_Write_Byte(*DATAS);
|
||||
IIC_Send_Byte(*DATAS);
|
||||
DATAS++;
|
||||
|
||||
}
|
||||
if(iic_wait_ack()){return;}
|
||||
iic_stop();
|
||||
if(IIC_Wait_Ack()){return;}
|
||||
IIC_Stop();
|
||||
}
|
||||
|
||||
void IIC_READ_DATE(unsigned char DEVICE_ADD,unsigned char IN_DEVICE_ADD,char *DATAS,uint16_t LONG)
|
||||
{
|
||||
iic_start();
|
||||
IIC_Write_Byte(DEVICE_ADD);
|
||||
if(iic_wait_ack()){return;}
|
||||
IIC_Write_Byte(IN_DEVICE_ADD);
|
||||
if(iic_wait_ack()){return;}
|
||||
iic_start();
|
||||
IIC_Write_Byte(DEVICE_ADD+1);
|
||||
if(iic_wait_ack()){return;}
|
||||
IIC_Start();
|
||||
IIC_Send_Byte(DEVICE_ADD);
|
||||
if(IIC_Wait_Ack()){return;}
|
||||
IIC_Send_Byte(IN_DEVICE_ADD);
|
||||
if(IIC_Wait_Ack()){return;}
|
||||
IIC_Start();
|
||||
IIC_Send_Byte(DEVICE_ADD+1);
|
||||
if(IIC_Wait_Ack()){return;}
|
||||
for(int a=0;a<LONG;a++)
|
||||
{
|
||||
*DATAS=IIC_Read_Byte();
|
||||
*DATAS=IIC_Read_Byte(0);
|
||||
DATAS++;
|
||||
}
|
||||
if(iic_wait_ack()){return;}
|
||||
iic_stop();
|
||||
if(IIC_Wait_Ack()){return;}
|
||||
IIC_Stop();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user