STM32 软件I2C方式读取MT6701磁编码器获取角度例程

发布于:2024-04-23 ⋅ 阅读:(20) ⋅ 点赞:(0)

STM32 软件I2C方式读取MT6701磁编码器获取角度例程


📙MT6701 IIC接口电路

在这里插入图片描述
在这里插入图片描述

  • 🔖 第八引脚注意是直接接到VCC,而不是使用电阻上拉。(其内部是有上拉电阻到VCC的)

⛳MT6701 I2C 读取角度操作

MT6701做为I2C从机的地址是b’0000110(这一地址可以通过编程改为b’1000110 )。14位绝对角度数据(2的14次方,16384)保存在0x03和0x04寄存器中,请按照如图-20所示的读取0x03和0x04的角度数据。
注意:要先读0x03,再读0x04。
在这里插入图片描述

⛳注意事项

  • ✨在MT6701芯片和径向磁铁一定要保持稳定的空间距离,一旦空间距离有较大的变化,在读取MT6701芯片寄存器数据就可能出现最大值情况。在检测时,芯片和径向磁铁轴向和径向都需要相对稳定。
    在这里插入图片描述

📗读取代码实现部分

//函数:u8 MT6701_ReadOneByte(u8 ReadAddr)
//功能:从MT6701模块读取一个字节的数据
//参数:ReadAddr    要读取的地址
//返回:读取到的数据
static u8 MT6701_ReadOneByte(u8 ReadAddr)
{                  
    u8 temp=0;                                                                                   
  IIC_Start();  
    IIC_Send_Byte((0x06<<1)|0x00);       //
    IIC_Wait_Ack(); 
  IIC_Send_Byte(ReadAddr);   //
    IIC_Wait_Ack();        
    IIC_Start();              
    IIC_Send_Byte((0x06<<1)|0x01);           //           
    IIC_Wait_Ack();     
  temp=IIC_Read_Byte(0);           
  IIC_Stop();//       
    return temp;
}
//读2个字节数据,获取原始角度
u16 MT6701_ReadTwoByte(u8 higher,u8 lower)
{
	u16 TwoByte_Data = 0;
	u16 hi_Data = 0,lo_Data = 0;
	//Read the first byte (higher address)
	hi_Data = MT6701_ReadOneByte(higher);
	//Read the second byte (lower address)
	lo_Data = MT6701_ReadOneByte(lower);
	//Combine the two bytes into a single 16-bit value
		TwoByte_Data  = (uint16_t)(lo_Data>> 2);
		TwoByte_Data |= ((uint16_t)hi_Data << 6);
	//Return the 16-bit value
	return TwoByte_Data;
}

在这里插入图片描述

  • 📝main函数代码
int main(void)
{

    u16 i = 0;
    u16 raw_num = 0;
    float Angle = 0.0f;
    u8 addr = 0;
    u8 ack;
    u8 read = 0;
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
    delay_init(); //延时函数初始化
    uart_init(115200);	//串口初始化为115200
    printf("MT6701 test\r\n");
    printf("Scanning I2C bus:\r\n");
    LED_Init();	//初始化与LED连接的硬件接口
    IIC_Init();
    for(addr = 0 ; addr < 255 ; addr++) {
        IIC_Start();
        IIC_Send_Byte(addr);	//
        ack = IIC_Wait_Ack();
        if(ack == 0 && (read % 2 == 0)) {
            printf("write addr = 0x%x\r\n", addr);
            read++;
        } else if(ack == 0 && read % 2 == 1) {
            printf("read addr = 0x%x\r\n", addr);
            read++;
        }
        IIC_Stop();
    }

    //for(i=0x00;i<0xFF;i++)
    // {
    //	IIC_Start();
    //	IIC_Send_Byte(i);
    //	if(IIC_ACK_Read()==1)
    //	{
    //	printf("%x\r\n",i);
    //	count=1;
    //	}
    //	IIC_Stop();
    //	delay_ms(1);
    // }
    // if(count==0) printf("No IIC device found!\r\n");
    // count=0;
    while(1) {

        delay_ms(10);
        if(++i > 100) {
            raw_num = MT6701_ReadTwoByte(0x03, 0x04); //读取两个寄存器的值
            Angle = (float)(raw_num / 16384.0f) * 360.0f ; //对寄存器值进行处理得到角度值
            printf("MT6701 Raw_num:%d,Angle:%.1f \r\n", raw_num, Angle);
            LED0 = !LED0; //提示系统正在运行
            i = 0;
        }

    }
}

📚驱动测试代码

链接:https://pan.baidu.com/s/1YRTLFoUJvrdWfcs6dL-qdg?pwd=fsif 
提取码:fsif