TI中文支持网
TI专业的中文技术问题搜集分享网站

TMAG5170: TMAG5170与stm32驱动

Part Number:TMAG5170

使用stm32f103,的spi2外设与TMAG5170,无法通信,如果这些初始化函数没有问题下一步应该怎么配置,有没有配置步骤,万分感谢

void SPI2_Initt_mag5170(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* Enable SPI2 and GPIOB clock */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE );
RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI2, ENABLE ); /* Configure PB12 as L_CS ————————-*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Configure PD8 as R_CS ————————-*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_SetBits(GPIOB,GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low ; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256 ; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; /* SPI2 configuration */
SPI_Init(SPI2, &SPI_InitStructure); /* Enable SPI2 */
SPI_Cmd(SPI2, ENABLE); //SPI2_ReadWriteByte(0xff); MLX_CS_HIGH(Right);
MLX_CS_HIGH(Left);
Delay_ms(100);

}//¶Á¼Ä´æÆ÷

uint16_t normalReadRegister( uint8_t address,unsigned char Location)
{
uint8_t dataTx[4] = { 0 };
uint8_t dataRx[4] = { 0 }; unsigned short output[2] = { 0 };
uint8_t cmd_bits ;

dataTx[0] = (address | 0x80);
dataTx[1] = 0x00;
dataTx[2] = 0x00;
dataTx[3] = cmd_bits << 4;
int i;
int byteLength = 4;
MLX_CS_LOW(Location);
for (i = 0; i < byteLength; i++)
{
dataRx[i] = SPI2_ReadWriteByte(dataTx[i]);
}

output[0] = (dataRx[1] << 8) | dataRx[2];
output[1] = (dataRx[0] << 4) | (dataRx[3] >> 4);
MLX_CS_HIGH(Location);
return output[0];
}
//д¼Ä´æÆ÷
void writeToRegister(uint8_t address, uint16_t data_to_write,unsigned char Location)
{
// Check that the input address is in range
unsigned short output[2] = { 0 };
// Build TX and RX byte arrays
uint8_t dataTx[4] = { 0 };
uint8_t dataRx[4] = { 0 };

// MSB is 0 for WRITE command
dataTx[0] = (address);
dataTx[1] = (data_to_write >> 8);
dataTx[2] = (data_to_write);
dataTx[3] = 0x00;
int i;
int byteLength = 4;
MLX_CS_LOW(Location);
for (i = 0; i < byteLength; i++)
{
dataRx[i] = SPI2_ReadWriteByte(dataTx[i]);
}
output[0] = (dataRx[1] << 8) | dataRx[2];
output[1] = (dataRx[0] << 4) | (dataRx[3] >> 4);
MLX_CS_HIGH(Location);
}

Amy Luo:

您好,

在 TMAG5170 产品首页如下路径处提供了 TMAG5170 C code example,希望对您有帮助。

TMAG5170 产品首页链接:https://www.ti.com.cn/product/cn/TMAG5170

赞(0)
未经允许不得转载:TI中文支持网 » TMAG5170: TMAG5170与stm32驱动
分享到: 更多 (0)