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

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
MT6701是一种微控制器,通常用于物联网设备中,而IIC(Inter-Integrated Circuit)是一种简单的双向串行通信总线标准。为了通过I2CMT6701读取角度数据,你需要编写一段C语言程序,这里提供一个基本的示例框架: ```c #include <stdio.h> #include <stdlib.h> #include "mraa.h" // MRAA库用于I2C操作 #define I2C_ADDR 0x48 // MT6701I2C地址,假设为7位地址 #define REG_ANGLE 0x00 // 角度传感器的寄存器地址 int main(void) { mraa_i2c_context i2c; // 创建一个I2C上下文 int angle_data; // 存储读取角度值 // 初始化MRAA if (mraa_init() != MRAA_SUCCESS) { printf("Failed to initialize MRAA\n"); return -1; } // 初始化I2C连接 i2c = mraa_i2c_init(0); // 如果有多路I2C,传入对应的I2C端口 if (mraa_i2c_bus.frequency(400000) != MRAA_SUCCESS) { printf("Failed to set I2C frequency\n"); goto exit; } // 开始从地址I2C_ADDR读取角度数据 mraa_result_t result = mraa_i2c_read(i2c, I2C_ADDR << 1 | 0x0, &angle_data, sizeof(angle_data)); // 左移一位加上读写的从机地址偏移 if (result != MRAA_SUCCESS) { printf("Failed to read angle data from sensor\n"); goto exit; } // 输出角度值 printf("Read angle: %d degrees\n", angle_data); exit: // 关闭资源并退出 mraa_i2c_close(i2c); mraa_cleanup(); return 0; } ``` 注意:这个示例假设角度数据存储在一个字节中,并且传感器返回的是直接的数字表示角度。实际应用中,你可能需要处理转换和错误检查。此外,你需要根据MT6701的数据手册修改寄存器地址、数据长度等细节。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值