新手入门
- 积分
- 7
- 金钱
- 7
- 注册时间
- 2020-10-19
- 在线时间
- 4 小时
|
1金钱
这个代码我之前在F407的环境里跑没有问题,输出的数据是正确的,但移植到STMF446ret上后传感器读取的数据是错误的,求大佬解答,下面附上IIC的代码和主函数代码
IIC:
//3õê¼»ˉI2C
void I2C_Soft_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Pin = Pin_SCL | Pin_SDA;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
Pin_SCL_H;
Pin_SDA_H;
}
//I2CÆeê¼DÅoÅ
uint8_t I2C_Start(void)
{
Pin_SCL_H;
Pin_SDA_H;
new_delay_us(1);
if(!Read_SDA_Pin)
return 0;
Pin_SDA_L;
new_delay_us(1);
Pin_SCL_L;//GAI
return 1;
}
//I2Cí£Ö1DÅoÅ
uint8_t I2C_Stop(void)
{
Pin_SCL_H;
Pin_SDA_L;
new_delay_us(1);
if(Read_SDA_Pin)
return 0;
Pin_SDA_H;
new_delay_us(1);
if(!Read_SDA_Pin)
return 0;
Pin_SDA_H;
new_delay_us(1);
return 1;
}
//I2có|′eDÅoÅ
void I2C_Ack(void)
{
Pin_SCL_L;
new_delay_us(1);
Pin_SDA_L;
Pin_SCL_H;
new_delay_us(1);
Pin_SCL_L;
Pin_SDA_H;
new_delay_us(1);
}
//I2CÎTD§ó|′e
void I2C_NAck(void)
{
Pin_SCL_L;
new_delay_us(1);
Pin_SDA_H;
Pin_SCL_H;
new_delay_us(1);
Pin_SCL_L;
new_delay_us(1);
}
//μè′yó|′e
uint8_t I2C_Wait_Ack(void)
{
uint16_t tempcount = 0;
Pin_SCL_L;
new_delay_us(1);
Pin_SDA_H;
new_delay_us(1);
Pin_SCL_H;
new_delay_us(1);
while(Read_SDA_Pin)
{
tempcount++;
if(tempcount > 250)
{
Pin_SCL_L;
new_delay_us(1);
return 0;
}
}
Pin_SCL_L;
new_delay_us(1);
return 1;
}
//·¢Ëíò»¸ö×Ö½ú
void I2C_Send_Byte(uint8_t txd)
{ uint8_t i;
for( i=0; i<8; i++)
{
Pin_SCL_L;
if(txd & 0x80)
Pin_SDA_H;
else
Pin_SDA_L;
txd <<= 1;
new_delay_us(1);
Pin_SCL_H;
new_delay_us(1);
}
}
//¶áè¡ò»¸ö×Ö½ú
uint8_t I2C_Read_Byte(void)
{
uint8_t i,rxd = 0;
for( i=0; i<8; i++)
{
rxd <<= 1;
Pin_SCL_L;
new_delay_us(1);
Pin_SCL_H;
new_delay_us(1);
if(Read_SDA_Pin)
{
rxd |= 0x01;
}
}
return rxd;
}
uint8_t I2C_Read_REG(uint8_t SlaveAddress,uint8_t REG_Address,uint8_t *REG_value)
{
uint8_t rex = 0;
if(!I2C_Start())
return 0;
I2C_Send_Byte(SlaveAddress);
if(!I2C_Wait_Ack())
{
I2C_Stop();
return 0;
}
I2C_Send_Byte(REG_Address);
if(!I2C_Wait_Ack())
{
I2C_Stop();
return 0;
}
if(!I2C_Start())
{
return 0;
}
I2C_Send_Byte(SlaveAddress + 1);
if(!I2C_Wait_Ack())
{
I2C_Stop();
return 0;
}
*REG_value = I2C_Read_Byte();
I2C_NAck();
if(!I2C_Stop())
return 0;
return 1;
}uint8_t I2C_Read_NByte(uint8_t SlaveAddress, uint8_t REG_Address, uint8_t* buf, uint8_t len)
{
uint16_t i;
if(!I2C_Start())
return 0;
I2C_Send_Byte(SlaveAddress);
if(!I2C_Wait_Ack())
{
I2C_Stop();
return 0;
}
I2C_Send_Byte(REG_Address);
if(!I2C_Wait_Ack())
{
I2C_Stop();
return 0;
}
if(!I2C_Start())
{
return 0;
}
I2C_Send_Byte(SlaveAddress + 0x01);
if(!I2C_Wait_Ack())
{
I2C_Stop();
return 0;
}
for( i = 0; i < len; i++)
{
buf[i] = I2C_Read_Byte();
if(i < len-1)
{
I2C_Ack();
}
}
I2C_NAck();
I2C_Stop();
return 1;
}
int main(void)
{I2C_Soft_Init();
while (1)
{
I2C_Read_NByte(0xC0, 0x28,(uint8_t*)p11, 4);
I2C_Read_NByte(0xC0, 0x29,(uint8_t*)q11, 4);
x11|=p11[1];
x11<<=4;
y11 = q11[2];
y11>>=4;
x11|=y11;
if((x11&0x0800)==0x0800)
{
x = x11;
x11|=0xF000;
}
x11 = x11/4;
sprintf(str1,"FIRST %d \r\n ",x11);
HAL_UART_Transmit(&huart4,str1,sizeof(str1),1000);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_9);
HAL_Delay(time);
/* USER CODE BEGIN 3 */
HAL_Delay(time);
}
}
|
|