编码器角度读取流程:
一、初始化SPI。
注意这里需要初始化SPI读写位16位,平时的例程,正点原子,或者野火均是8位,需要修改下位数
void SPI1_Init(void)
{ GPIO_InitTypeDef GPIO_InitStructure;SPI_InitTypeDef SPI_InitStructure;RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//ʹÄÜGPIOBʱÖÓRCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);//ʹÄÜSPI1ʱÖÓ//GPIOFB3,4,5³õʼ»¯ÉèÖÃGPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;//PB3~5¸´Óù¦ÄÜÊä³ö GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸´Óù¦ÄÜGPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//ÍÆÍìÊä³öGPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHzGPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//ÉÏÀGPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIO_PinAFConfig(GPIOA,GPIO_PinSource5,GPIO_AF_SPI1); //PB3¸´ÓÃΪ SPI1GPIO_PinAFConfig(GPIOA,GPIO_PinSource6,GPIO_AF_SPI1); //PB4¸´ÓÃΪ SPI1GPIO_PinAFConfig(GPIOA,GPIO_PinSource7,GPIO_AF_SPI1); //PB5¸´ÓÃΪ SPI1//ÕâÀïÖ»Õë¶ÔSPI¿Ú³õʼ»¯RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,ENABLE);//¸´Î»SPI1RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1,DISABLE);//Í£Ö¹¸´Î»SPI1SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //ÉèÖÃSPIµ¥Ïò»òÕßË«ÏòµÄÊý¾Ýģʽ:SPIÉèÖÃΪ˫ÏßË«ÏòÈ«Ë«¹¤SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //ÉèÖÃSPI¹¤×÷ģʽ:ÉèÖÃΪÖ÷SPISPI_InitStructure.SPI_DataSize = SPI_DataSize_16b; //ÉèÖÃSPIµÄÊý¾Ý´óС:SPI·¢ËͽÓÊÕ8λ֡½á¹¹SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //´®ÐÐͬ²½Ê±ÖӵĿÕÏÐ״̬Ϊ¸ßµçƽSPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //´®ÐÐͬ²½Ê±Öӵĵڶþ¸öÌø±äÑØ£¨ÉÏÉý»òϽµ£©Êý¾Ý±»²ÉÑùSPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSSÐźÅÓÉÓ²¼þ£¨NSS¹Ü½Å£©»¹ÊÇÈí¼þ£¨Ê¹ÓÃSSI룩¹ÜÀí:ÄÚ²¿NSSÐźÅÓÐSSIλ¿ØÖÆSPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; //¶¨Ò岨ÌØÂÊÔ¤·ÖƵµÄÖµ:²¨ÌØÂÊÔ¤·ÖƵֵΪ256SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //Ö¸¶¨Êý¾Ý´«Êä´ÓMSBλ»¹ÊÇLSBλ¿ªÊ¼:Êý¾Ý´«Êä´ÓMSBλ¿ªÊ¼SPI_InitStructure.SPI_CRCPolynomial = 15; //CRCÖµ¼ÆËãµÄ¶àÏîʽSPI_Init(SPI1, &SPI_InitStructure); //¸ù¾ÝSPI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèSPIx¼Ä´æÆ÷SPI_Cmd(SPI1, ENABLE); //ʹÄÜSPIÍâÉèSPI1_ReadWriteByte(0xffff);//Æô¶¯´«Êä
}
//SPI1ËÙ¶ÈÉèÖú¯Êý
//SPIËÙ¶È=fAPB2/·ÖƵϵÊý
//@ref SPI_BaudRate_Prescaler:SPI_BaudRatePrescaler_2~SPI_BaudRatePrescaler_256
//fAPB2ʱÖÓÒ»°ãΪ84Mhz£º
void SPI1_SetSpeed(u8 SPI_BaudRatePrescaler)
{assert_param(IS_SPI_BAUDRATE_PRESCALER(SPI_BaudRatePrescaler));//ÅжÏÓÐЧÐÔSPI1->CR1&=0XFFC7;//λ3-5ÇåÁ㣬ÓÃÀ´ÉèÖò¨ÌØÂÊSPI1->CR1|=SPI_BaudRatePrescaler; //ÉèÖÃSPI1ËÙ¶È SPI_Cmd(SPI1,ENABLE); //ʹÄÜSPI1
}
//SPI1 ¶Áдһ¸ö×Ö½Ú
//TxData:ҪдÈëµÄ×Ö½Ú
//·µ»ØÖµ:¶ÁÈ¡µ½µÄ×Ö½Ú
u16 SPI1_ReadWriteByte(u16 TxData)
{ while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET){}//µÈ´ý·¢ËÍÇø¿Õ SPI_I2S_SendData(SPI1, TxData); //ͨ¹ýÍâÉèSPIx·¢ËÍÒ»¸öbyte Êý¾Ýwhile (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET){} //µÈ´ý½ÓÊÕÍêÒ»¸öbyte return SPI_I2S_ReceiveData(SPI1); //·µ»Øͨ¹ýSPIx×î½ü½ÓÊÕµÄÊý¾Ý }
这是SPI的初始化
u16 SPI_AS5048A_Read(u16 TxData)
{u16 data;GPIO_ResetBits(GPIOA,GPIO_Pin_4);SPI1_ReadWriteByte(TxData);GPIO_SetBits(GPIOA,GPIO_Pin_4);CPU_TS_Tmr_Delay_US(10);GPIO_ResetBits(GPIOA,GPIO_Pin_4);data = SPI1_ReadWriteByte(0xc000);GPIO_SetBits(GPIOA,GPIO_Pin_4);data = data & 0x3fff;return data;
}
这是读写,直接调用SPI_AS5048A_Read(0xffff)就是读出来角度数据,根据寄存器应该是从0x3fff这个地址读取,这个地址进行校验等处理后就变成了0xffff,当然,这个函数是读写函数,也可以简化一点,因为我们需要从AS5048获取数据,所以从ANGLE寄存器读取数据就可以了所以可以写成这样:
u16 SPI_AS5048A_Read(void)
{u16 data;GPIO_ResetBits(GPIOA,GPIO_Pin_4);data = SPI1_ReadWriteByte(0xffff);GPIO_SetBits(GPIOA,GPIO_Pin_4);data = data & 0x3fff;return data;
}as5600_adc = SPI_AS5048A_Read(); angle = (float)(as5600_adc) / (float)16384 * 360.0f;
这样就可以都到角度了,为什么&0x3fff,因为是14位的,SPI历程直接参考整点原子就行