电子说
/**
******************************************************************************
* @file qma6100p.c
* @author Yangzhiqiang@qst
* @version V1.0
* @date 2020-5-27
* @brief qma6100p
******************************************************************************
* @attention
*
*
******************************************************************************
*/
#include "qma6100p.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 {
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 %drn", __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=%xrn",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=%xrn",Buff[0]);
Buff[0] = MT6813_read_reg(0x24);
printf("RG 0x24 D=%xrn",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=%drn",angle_now);
// }
printf("angle=%.2frn",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))
{
return 1;
}
return 0;
}
PT32L007是一款Cortex M0 内核的MCU,该系列产品最高主频可达64MHz,1.8~5.5v,-40~105℃,Flash 64Kbyte,SRAM 16Kbyte,深度睡眠电流低至 500nA,支持16位低功耗定时器,12bit ADC,UART x 2,I2C,SPI,Timer x 6,18 GPIO,封装TSSOP20/QFN20,广泛应用于消费电子、手持设备、消防安防、仪器仪表、物联网、工业控制等领域。
审核编辑 黄宇
全部0条评论
快来发表一下你的评论吧 !