夏普红外测距传感器规格书引脚如下
一共七个引脚,如果要改地址的话还需要用到第八个引脚(当时改地址试了半天,仔细一看数据手册还有第八个引脚),第八个引脚Vpp如下图所示
和GP2Y0E03通讯是用IIC协议,数据手册上具体协议格式如下
笔者使用STM32F103系列单片机IIC通讯代码如下
void GP2_SDA_Mode(GPIOMode_TypeDef sdagpio_mode)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = sdagpio_mode;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void delay_us_us(u16 time)
{
u16 i=0;
while(time--)
{
i=10;
while(i--) ;
}
}
void GP2_IIC_Start(void)
{
GP2_IIC_SCL=0;
GP2_SDA_Mode(GPIO_Mode_Out_OD);
GP2_IIC_SDA=1;
GP2_IIC_SCL=1;
delay_us_us(4);
GP2_IIC_SDA=0;
delay_us_us(4);
GP2_IIC_SCL=0;
}
void GP2_IIC_Stop(void)
{
GP2_IIC_SCL=0;
GP2_SDA_Mode(GPIO_Mode_Out_OD);
GP2_IIC_SCL=0;
GP2_IIC_SDA=0;
GP2_IIC_SCL=1;
delay_us_us(4);
GP2_IIC_SDA=1;
delay_us_us(4);
GP2_IIC_SCL=0;
}
uint8_t GP2_IIC_Wait_Ack(void)
{
uint8_t ucErrTime=0;
GP2_SDA_Mode(GPIO_Mode_IN_FLOATING);
delay_us_us(4);
GP2_IIC_SCL=1;
delay_us_us(4);
while(GP2_READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
GP2_IIC_Stop();
return 1;
}
}
GP2_IIC_SCL=0;//ʱÖÓÊä³ö0
return 0;
}
void GP2_IIC_Ack(void)
{
GP2_IIC_SCL=0;
GP2_SDA_Mode(GPIO_Mode_Out_OD);
GP2_IIC_SDA=0;
delay_us_us(4);
GP2_IIC_SCL=1;
delay_us_us(4);
GP2_IIC_SCL=0;
}
void GP2_IIC_NAck(void)
{
GP2_IIC_SCL=0;
GP2_SDA_Mode(GPIO_Mode_Out_OD);
GP2_IIC_SDA=1;
delay_us_us(4);
GP2_IIC_SCL=1;
delay_us_us(4);
GP2_IIC_SCL=0;
}
void GP2_IIC_Send_Byte(uint8_t txd)
{
uint8_t t;
GP2_SDA_Mode(GPIO_Mode_Out_OD);
GP2_IIC_SCL=0;
for(t=0;t<8;t++)
{
if(txd&0x80)GP2_IIC_SDA=1;
else GP2_IIC_SDA=0;
txd<<=1;
delay_us_us(4);
GP2_IIC_SCL=1;
delay_us_us(4);
GP2_IIC_SCL=0;
delay_us_us(4);
}
}
uint8_t GP2_IIC_Read_Byte(void)
{
unsigned char i,receive=0;
GP2_SDA_Mode(GPIO_Mode_IN_FLOATING);
delay_us_us(4);
for(i=0;i<8;i++ )
{
receive<<=1;
GP2_IIC_SCL=1;
delay_us_us(4);
if(GP2_READ_SDA)receive |= 0x01;
delay_us_us(4); //1
GP2_IIC_SCL=0;
delay_us_us(4); //1
}
return receive;
}
uint8_t GP2_IIC_Write_1Byte(uint8_t SlaveAddress,uint8_t REG_Address,uint8_t REG_data)
{
GP2_IIC_Start();
GP2_IIC_Send_Byte(SlaveAddress);
if(GP2_IIC_Wait_Ack())
{
GP2_IIC_Stop();//ÊÍ·Å×ÜÏß
return 1;//ûӦ´ðÔòÍ˳ö
}
GP2_IIC_Send_Byte(REG_Address);
GP2_IIC_Wait_Ack();
delay_us_us(5);
GP2_IIC_Send_Byte(REG_data);
GP2_IIC_Wait_Ack();
GP2_IIC_Stop();
return 0;
}
uint8_t GP2_IIC_Read_1Byte(uint8_t SlaveAddress, uint8_t REG_Address)
{
uint8_t REG_data;
GP2_IIC_Start();
GP2_IIC_Send_Byte(SlaveAddress);
if(GP2_IIC_Wait_Ack())
{
GP2_IIC_Stop();//ÊÍ·Å×ÜÏß
return 1;//ûӦ´ðÔòÍ˳ö
}
GP2_IIC_Send_Byte(REG_Address);
GP2_IIC_Wait_Ack();
GP2_IIC_Stop();//ÊÍ·Å×ÜÏß
delay_us_us(5);
GP2_IIC_Start();
GP2_IIC_Send_Byte(SlaveAddress|0x01);
GP2_IIC_Wait_Ack();
REG_data = GP2_IIC_Read_Byte();
GP2_IIC_Stop();
return REG_data;
}
uint8_t GP2_I2C_Read_nByte(uint8_t SlaveAddress, uint8_t REG_Address, uint8_t *buf, uint16_t len)
{
GP2_IIC_Start();
GP2_IIC_Send_Byte(SlaveAddress);
if(GP2_IIC_Wait_Ack())
{
GP2_IIC_Stop();//ÊÍ·Å×ÜÏß
return 1;//ûӦ´ðÔòÍ˳ö
}
GP2_IIC_Send_Byte(REG_Address);
GP2_IIC_Wait_Ack();
GP2_IIC_Stop();//ÊÍ·Å×ÜÏß
delay_us_us(5);
GP2_IIC_Start();
GP2_IIC_Send_Byte(SlaveAddress|0x01);
GP2_IIC_Wait_Ack();
while(len)
{
*buf = GP2_IIC_Read_Byte();
if(1 == len)
{
GP2_IIC_NAck();
}
else
{
GP2_IIC_Ack();
}
buf++;
len--;
}
GP2_IIC_Stop();
return STATUS_OK;
}
uint8_t GP2_I2C_Write_nByte(uint8_t SlaveAddress, uint8_t REG_Address, uint8_t *buf, uint16_t len)
{
GP2_IIC_Start();
GP2_IIC_Send_Byte(SlaveAddress);
if(GP2_IIC_Wait_Ack())
{
GP2_IIC_Stop();
return 1;
}
GP2_IIC_Send_Byte(REG_Address);
GP2_IIC_Wait_Ack();
while(len--)
{
GP2_IIC_Send_Byte(*buf++);
GP2_IIC_Wait_Ack();
}
GP2_IIC_Stop();
return STATUS_OK;
}
PS:使用时请注意镜面反射的影响,如果不改地址使用多个传感器模块,需控制使能脚依次读取数据
版权声明:本文为CSDN博主「宋君千里」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_42388209/article/details/119150572
夏普红外测距传感器规格书引脚如下
一共七个引脚,如果要改地址的话还需要用到第八个引脚(当时改地址试了半天,仔细一看数据手册还有第八个引脚),第八个引脚Vpp如下图所示
和GP2Y0E03通讯是用IIC协议,数据手册上具体协议格式如下
笔者使用STM32F103系列单片机IIC通讯代码如下
void GP2_SDA_Mode(GPIOMode_TypeDef sdagpio_mode)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = sdagpio_mode;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void delay_us_us(u16 time)
{
u16 i=0;
while(time--)
{
i=10;
while(i--) ;
}
}
void GP2_IIC_Start(void)
{
GP2_IIC_SCL=0;
GP2_SDA_Mode(GPIO_Mode_Out_OD);
GP2_IIC_SDA=1;
GP2_IIC_SCL=1;
delay_us_us(4);
GP2_IIC_SDA=0;
delay_us_us(4);
GP2_IIC_SCL=0;
}
void GP2_IIC_Stop(void)
{
GP2_IIC_SCL=0;
GP2_SDA_Mode(GPIO_Mode_Out_OD);
GP2_IIC_SCL=0;
GP2_IIC_SDA=0;
GP2_IIC_SCL=1;
delay_us_us(4);
GP2_IIC_SDA=1;
delay_us_us(4);
GP2_IIC_SCL=0;
}
uint8_t GP2_IIC_Wait_Ack(void)
{
uint8_t ucErrTime=0;
GP2_SDA_Mode(GPIO_Mode_IN_FLOATING);
delay_us_us(4);
GP2_IIC_SCL=1;
delay_us_us(4);
while(GP2_READ_SDA)
{
ucErrTime++;
if(ucErrTime>250)
{
GP2_IIC_Stop();
return 1;
}
}
GP2_IIC_SCL=0;//ʱÖÓÊä³ö0
return 0;
}
void GP2_IIC_Ack(void)
{
GP2_IIC_SCL=0;
GP2_SDA_Mode(GPIO_Mode_Out_OD);
GP2_IIC_SDA=0;
delay_us_us(4);
GP2_IIC_SCL=1;
delay_us_us(4);
GP2_IIC_SCL=0;
}
void GP2_IIC_NAck(void)
{
GP2_IIC_SCL=0;
GP2_SDA_Mode(GPIO_Mode_Out_OD);
GP2_IIC_SDA=1;
delay_us_us(4);
GP2_IIC_SCL=1;
delay_us_us(4);
GP2_IIC_SCL=0;
}
void GP2_IIC_Send_Byte(uint8_t txd)
{
uint8_t t;
GP2_SDA_Mode(GPIO_Mode_Out_OD);
GP2_IIC_SCL=0;
for(t=0;t<8;t++)
{
if(txd&0x80)GP2_IIC_SDA=1;
else GP2_IIC_SDA=0;
txd<<=1;
delay_us_us(4);
GP2_IIC_SCL=1;
delay_us_us(4);
GP2_IIC_SCL=0;
delay_us_us(4);
}
}
uint8_t GP2_IIC_Read_Byte(void)
{
unsigned char i,receive=0;
GP2_SDA_Mode(GPIO_Mode_IN_FLOATING);
delay_us_us(4);
for(i=0;i<8;i++ )
{
receive<<=1;
GP2_IIC_SCL=1;
delay_us_us(4);
if(GP2_READ_SDA)receive |= 0x01;
delay_us_us(4); //1
GP2_IIC_SCL=0;
delay_us_us(4); //1
}
return receive;
}
uint8_t GP2_IIC_Write_1Byte(uint8_t SlaveAddress,uint8_t REG_Address,uint8_t REG_data)
{
GP2_IIC_Start();
GP2_IIC_Send_Byte(SlaveAddress);
if(GP2_IIC_Wait_Ack())
{
GP2_IIC_Stop();//ÊÍ·Å×ÜÏß
return 1;//ûӦ´ðÔòÍ˳ö
}
GP2_IIC_Send_Byte(REG_Address);
GP2_IIC_Wait_Ack();
delay_us_us(5);
GP2_IIC_Send_Byte(REG_data);
GP2_IIC_Wait_Ack();
GP2_IIC_Stop();
return 0;
}
uint8_t GP2_IIC_Read_1Byte(uint8_t SlaveAddress, uint8_t REG_Address)
{
uint8_t REG_data;
GP2_IIC_Start();
GP2_IIC_Send_Byte(SlaveAddress);
if(GP2_IIC_Wait_Ack())
{
GP2_IIC_Stop();//ÊÍ·Å×ÜÏß
return 1;//ûӦ´ðÔòÍ˳ö
}
GP2_IIC_Send_Byte(REG_Address);
GP2_IIC_Wait_Ack();
GP2_IIC_Stop();//ÊÍ·Å×ÜÏß
delay_us_us(5);
GP2_IIC_Start();
GP2_IIC_Send_Byte(SlaveAddress|0x01);
GP2_IIC_Wait_Ack();
REG_data = GP2_IIC_Read_Byte();
GP2_IIC_Stop();
return REG_data;
}
uint8_t GP2_I2C_Read_nByte(uint8_t SlaveAddress, uint8_t REG_Address, uint8_t *buf, uint16_t len)
{
GP2_IIC_Start();
GP2_IIC_Send_Byte(SlaveAddress);
if(GP2_IIC_Wait_Ack())
{
GP2_IIC_Stop();//ÊÍ·Å×ÜÏß
return 1;//ûӦ´ðÔòÍ˳ö
}
GP2_IIC_Send_Byte(REG_Address);
GP2_IIC_Wait_Ack();
GP2_IIC_Stop();//ÊÍ·Å×ÜÏß
delay_us_us(5);
GP2_IIC_Start();
GP2_IIC_Send_Byte(SlaveAddress|0x01);
GP2_IIC_Wait_Ack();
while(len)
{
*buf = GP2_IIC_Read_Byte();
if(1 == len)
{
GP2_IIC_NAck();
}
else
{
GP2_IIC_Ack();
}
buf++;
len--;
}
GP2_IIC_Stop();
return STATUS_OK;
}
uint8_t GP2_I2C_Write_nByte(uint8_t SlaveAddress, uint8_t REG_Address, uint8_t *buf, uint16_t len)
{
GP2_IIC_Start();
GP2_IIC_Send_Byte(SlaveAddress);
if(GP2_IIC_Wait_Ack())
{
GP2_IIC_Stop();
return 1;
}
GP2_IIC_Send_Byte(REG_Address);
GP2_IIC_Wait_Ack();
while(len--)
{
GP2_IIC_Send_Byte(*buf++);
GP2_IIC_Wait_Ack();
}
GP2_IIC_Stop();
return STATUS_OK;
}
PS:使用时请注意镜面反射的影响,如果不改地址使用多个传感器模块,需控制使能脚依次读取数据
版权声明:本文为CSDN博主「宋君千里」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_42388209/article/details/119150572
暂无评论