void IIC_SDA_MODE(uint8_t SDA_Mode)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = IIC_SDA_PING;
if(SDA_Mode){
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
}
else
{
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
}
GPIO_Init(IIC_PORT, &GPIO_InitStructure);
}
void I2C_Start(void)
{
SCL_H;
delay_us(DELAYX_TIME);
SDA_H;
delay_us(DELAYX_TIME);
SDA_L;
delay_us(DELAYX_TIME);
SCL_L;
delay_us(DELAYX_TIME);
}
void I2C_Stop(void)
{
SDA_L;
delay_us(DELAYX_TIME);
SCL_H;
delay_us(DELAYX_TIME);
SDA_H;
delay_us(DELAYX_TIME);
}
void I2C_Ack(void)
{
IIC_SDA_MODE(SDA_OUT);
SCL_L;
delay_us(DELAYX_TIME);
SDA_L;
delay_us(DELAYX_TIME);
SCL_H;
delay_us(DELAYX_TIME);
SCL_L;
delay_us(DELAYX_TIME);
}
void I2C_NoAck(void)
{
IIC_SDA_MODE(SDA_OUT);
SCL_L;
delay_us(DELAYX_TIME);
SDA_H;
delay_us(DELAYX_TIME);
SCL_H;
delay_us(DELAYX_TIME);
SCL_L;
delay_us(DELAYX_TIME);
}
uint8_t I2C_WaitAck(uint16_t waitime)
{
SCL_L;
delay_us(DELAYX_TIME);
SDA_H;
IIC_SDA_MODE(SDA_IN);
delay_us(DELAYX_TIME);
SCL_H;
while(waitime){
if(SDA==0)break;
delay_us(DELAYX_TIME);
waitime--;
}
IIC_SDA_MODE(SDA_OUT);
return waitime;
}
void I2C_SendByte(uint8_t data)
{
uint8_t i;
SCL_L;
delay_us(DELAYX_TIME);
for(i = 0;i < 8;i++)
{
if(data & (0x01 << (7 - i))) SDA_H;
else SDA_L;
delay_us(DELAYX_TIME);
SCL_H;
delay_us(DELAYX_TIME);
SCL_L;
}
}
uint8_t I2C_ReadByte(void)
{
uint8_t i,data = 0;
SCL_L;
SDA_H;
IIC_SDA_MODE(SDA_IN);
for(i=0;i<8;i++)
{
SCL_H;
delay_us(DELAYX_TIME);
data <<= 1;
if(SDA)
{
data |= 0x01;
}
// SCL_H;
// delay_us(DELAYX_TIME);
SCL_L;
delay_us(DELAYX_TIME);
}
IIC_SDA_MODE(SDA_OUT);
printf("data = %#x\n", data);
return data;
}
void write_reg(uint8_t reg, uint8_t data)
{
I2C_Start();
I2C_SendByte(LIS2DH12_I2C_ADD_H << 1);
I2C_WaitAck(250);
I2C_SendByte(reg);
I2C_WaitAck(250);
I2C_SendByte(data);
I2C_WaitAck(250);
I2C_Stop();
}
uint8_t IIC_read(uint8_t slave_addr, uint8_t registerAddr,uint8_t *bufp,uint16_t len)
{
I2C_Start();
I2C_SendByte(slave_addr<<1);
printf("slave_addr = %#x\n", slave_addr<<1);
I2C_WaitAck(250);
// if (len !=1)
// {
// I2C_SendByte(registerAddr + 0x80);
// I2C_WaitAck(250);
// }
// else
I2C_SendByte(registerAddr);
// printf("registerAddr = %#x\n", registerAddr);
I2C_WaitAck(250);
I2C_Start();
I2C_SendByte((slave_addr<<1)+1);
// printf("slave_addr = %#x\n", (slave_addr<<1)+1);
I2C_WaitAck(2);
for(int i=0;i<len;i++){
*bufp = I2C_ReadByte();
printf("bufp = %d\n", *bufp);
if(i==len-1){
I2C_NoAck();
}else{
I2C_Ack();
}
}
I2C_Stop();
return bufp[0];
}
void GPIO_I2C_init(void){
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = IIC_SCL_PING | IIC_SDA_PING;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(IIC_PORT,&GPIO_InitStructure);
GPIO_SetBits(IIC_PORT,IIC_SCL_PING);
GPIO_SetBits(IIC_PORT,IIC_SDA_PING);
}
uint8_t i = 0;
/* Main Example --------------------------------------------------------------*/
void lis2dh12_read_data_polling(void)
{
/* Initialize mems driver interface */
uint8_t i = 0;
uint8_t data[1];
uint8_t ID;
while (1) {
// for (i=0; i<6; i++){
// lis2dh12_iic_read_byte(0x28 +i, data + i); //获取X、y、z轴的数据
// }
// sample.x = LIS2DH12_FROM_FS_2g_HR_TO_mg(*(int16_t*)data);
// sample.y = LIS2DH12_FROM_FS_2g_HR_TO_mg(*(int16_t*)(data+2));
// sample.z = LIS2DH12_FROM_FS_2g_HR_TO_mg(*(int16_t*)(data+4));
// sample.new_angle_x = atan((short)sample.x/(short)sqrt(pow(sample.y, 2)+pow(sample.z, 2))) * DEGREE_CAL; // 计算X轴的倾角
// sample.new_angle_y = atan((short)sample.y/(short)sqrt(pow(sample.x, 2)+pow(sample.z, 2))) * DEGREE_CAL; // 计算Y轴的倾角
// sample.new_angle_z = atan((short)sample.z/(short)sqrt(pow(sample.x, 2)+pow(sample.y, 2))) * DEGREE_CAL; // 计算Z轴的倾角
// if (sample.new_angle_z < 0)
// {
// sample.new_angle_x = 180-sample.new_angle_x;
// sample.new_angle_y = 180-sample.new_angle_y;
// }
// printf("X轴倾角: %hd 度\n", sample.new_angle_x);
// printf("Y轴倾角: %hd 度\n", sample.new_angle_y);
// printf("Z轴倾角: %hd 度\n", sample.new_angle_z);
for (i=0; i<1; i++){
ID = lis2dh12_iic_read_byte(0x0F, data); //获取ID
}
printf("id = %d\n", ID);
delay_ms(60); //6ms
}
}
static int32_t lis2dh12_iic_write_byte( uint8_t reg,uint8_t data)//写一个字节
{
write_reg( reg, data);
return 1;
}
static int32_t lis2dh12_iic_read_byte(uint8_t reg, uint8_t *data)//读一个字节
{
IIC_read(LIS2DH12_I2C_ADD_H, reg, data,1);
return data[i];
}
|
所以问题是?
搞个逻辑分析仪看看时序波形。特别注意下发送的设备的7bit地址对不对