PT32L007 与 MT6701 iic通讯源代码 分享
/**
******************************************************************************
* @file MT6701.c
* @author Yangzhiqiang@qst
* @version V1.0
* @date 2020-5-27
* @brief MT6701
******************************************************************************
* @attention
*
*
******************************************************************************
*/
#include "MT6701.h"
#include "PT32x007x.h"
#include "string.h"
#define MT6701_ADDR (0x0C)
#define fabs(x) (x < 0 ? -x : x)
void delay_us(uint32_t delay)
{
uint32_t i,j,k;
for(i=0;i<delay;i++)
{
for(j=0;j<100;j++)
{
k++;
}
}
}
//---------------------------------------
void Acce_Write_Byte(unsigned int WriteAddr, u8 data)
{
// int i;
/******************等待从机ready***************/
I2C_GenerateEvent(I2C,I2C_Event_Start,DISABLE);
I2C->CCR |= I2C_CCR_SI | I2C_CCR_ACK;
I2C_Cmd(I2C,DISABLE);
I2C_Cmd(I2C,ENABLE);
I2C_GenerateEvent(I2C,I2C_Event_Start,ENABLE);
while(I2C_GetFlagStatus(I2C,I2C_FLAG_StartOk)!= SET);
// printf("---%s %d---\n", __func__, __LINE__) ;
I2C_SendAddr(I2C, MT6701_ADDR);//器件地址,写
while(I2C_GetFlagStatus(I2C,I2C_FLAG_MASGetAckW)!=SET);
printf("%s %d\r\n", __func__, __LINE__) ;
I2C_SendData(I2C,WriteAddr);//发送要写的字地址
while(I2C_GetFlagStatus(I2C,I2C_FLAG_MDSGetAck)!=SET);
// printf("---%s %d---\n", __func__, __LINE__) ;
I2C_SendData(I2C, data);
while(I2C_GetFlagStatus(I2C,I2C_FLAG_MDSGetAck) != SET);
/******************发送停止位***************/
I2C_GenerateEvent(I2C,I2C_Event_Stop,ENABLE);
}
//uint8_t I2C_EE_Read(u8* pBuffer,u16 ReadAddr, u16 DeviceAddr, u16 data_size)
uint8_t Acce_Read_Byte(u16 ReadAddr)
{
// int i;
uint8_t date = 0 ;
/******************等待从机ready***************/
//printf("---%s %d---\n", __func__, __LINE__) ;
I2C_GenerateEvent(I2C,I2C_Event_Start,ENABLE);
while(I2C_GetFlagStatus(I2C,I2C_FLAG_StartOk)!= SET);
// printf("---%s %d---\n", __func__, __LINE__) ;
I2C_SendAddr(I2C, MT6701_ADDR);//器件地址,写
while(I2C_GetFlagStatus(I2C,I2C_FLAG_MASGetAckW)!=SET);
I2C_SendData(I2C,ReadAddr);//发送要读的页地址
while(I2C_GetFlagStatus(I2C,I2C_FLAG_MDSGetAck)!=SET);
I2C_GenerateEvent(I2C,I2C_Event_Stop,ENABLE);
/******************接收数据***************/
I2C_GenerateEvent(I2C,I2C_Event_Start,ENABLE);
while(I2C_GetFlagStatus(I2C,I2C_FLAG_StartOk)!= SET);
I2C_SendAddr(I2C, MT6701_ADDR|0x01);//器件地址,读
while(I2C_GetFlagStatus(I2C,I2C_FLAG_MASGetAckR)!=SET);
I2C->CCR=I2C_CCR_ACK|I2C_CCR_SI;//主机发送NACK
while(I2C_GetFlagStatus(I2C,I2C_FLAG_MDGSendNack) != SET);
date = I2C_ReceiveData(I2C);
/******************发送停止位***************/
I2C_GenerateEvent(I2C,I2C_Event_Stop,ENABLE);
return date ;
}
//---------------------------------------
uint8_t MT6813_read_reg(uint8_t Reg)
{
u8 RegVal = 0;
RegVal = Acce_Read_Byte(Reg);
return RegVal;
}
void MT6813_write_reg(uint8_t Reg,uint8_t Val)
{
Acce_Write_Byte(Reg, Val);
}
void MT6701_INIT(void)
{
uint8_t Buff[2];
// Buff[0] = MT6813_read_reg(0x22);
// if(Buff[0] == 0x60)
// {
// printf("RG 0x22=%x\r\n",Buff[0]);
// return;
// }
MT6813_write_reg(0x22, 0x60); //
delay_us(10) ;
MT6813_write_reg(0x24, 0x80); //
delay_us(10) ;
MT6813_write_reg(0x27, 0xB8);
delay_us(10) ;
Buff[0] = MT6813_read_reg(0x22);
printf("RG 0x22 C=%x\r\n",Buff[0]);
Buff[0] = MT6813_read_reg(0x24);
printf("RG 0x24 D=%x\r\n",Buff[0]);
//save eeprom
MT6813_write_reg(0x09, 0xB3);
MT6813_write_reg(0x0A, 0x05);
}
short angle_now;
uint8_t MT6701_GetData(uint8_t *Magnet)
{
uint8_t Buff[6];
int data;
float angle;
Buff[0] = MT6813_read_reg(0x03);
Buff[1] = MT6813_read_reg(0x04);
Magnet[0] = Buff[0];
Magnet[1] = Buff[1];
data = (short)((Magnet[0]<<8)|(Magnet[1]));
data = data>>2;
angle = (float)(data*0.022);
if(angle<0)
{
angle = 360+angle;
}
// if((short)angle != angle_now)
// {
// angle_now = (short)angle;
// printf("angle=%d\r\n",angle_now);
// }
printf("angle=%.2f\r\n",angle);
return angle_now;
}
uint8_t Zero_point_programming(void)
{
uint8_t Buff[2],ZH,ZL;
int data ;
Buff[0] = MT6813_read_reg(0x32);
ZH = Buff[0] & 0xf0;
MT6813_write_reg(0x32, ZH);
MT6813_write_reg(0x33, 0x00);
Buff[0] = MT6813_read_reg(0x03);
data = (short)((Buff[0]<<8)|(Buff[1]));
data = data>>2; //14bit
data = data>>2; //12bit
ZH = ZH | (uint8_t)(data >> 8) ;
ZL = (uint8_t)(data & 0x0f) ;
MT6813_write_reg(0x32, ZH);
MT6813_write_reg(0x33, ZL);
//save eeprom
MT6813_write_reg(0x09, 0xB3);
MT6813_write_reg(0x0A, 0x05);
Buff[0] = MT6813_read_reg(0x32);
if((Buff[0] == ZH) && (Buff[1] == ZL))
{#ifndef __QMA6100P_H
#define __QMA6100P_H
#include "PT32x007x.h"
#include <stdbool.h>
#include <string.h>
void MT6701_INIT(void);
uint8_t MT6701_GetData(uint8_t *Magnet) ;
//调用零点编程之前必须把机械零点固定好,不要晃动;
uint8_t Zero_point_programming(void);
#endif /*MT6701*/
return 1;
}
return 0;
}
|