这个是SPI引脚和SPI的设置。
static void SPI1_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); /* Enable GPIO clocks */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; /*!< Configure SPI1 pins: SCK */
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; /*!< Configure SPI pins: MISO */
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; /*!< Configure SPI pins: MOSI */
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; /*!< Configure SPI_CS_PIN pin: SPI Card CS pin */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_4); /* Deselect the FLASH: Chip Select high */
}
/**
* @brief SPI1 ¹¤×÷ģʽ ÅäÖÃ
* @param ÎÞ
* @retval ÎÞ
* @notice ÓÃÓÚADS1256_µÄͨÐÅ £¬×î´óʱÖÓΪ 2M
*/
static void SPI1_MODE_Config(void)
{
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); /*!<SPI Periph clock enable */
SPI_I2S_DeInit(SPI1);
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_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7; /*²»Ö§³ÖCRCУÑ飬ËùÒÔÕâ¸ö²ÎÊýûÓÐÓ㬿ÉÒÔËæ±ãÅäÖÃ*/
SPI_Init(SPI1, &SPI_InitStructure);
SPI_I2S_ClearFlag(SPI1, SPI_I2S_FLAG_TXE);
SPI_I2S_ClearFlag(SPI1, SPI_I2S_FLAG_RXNE);
SPI_Cmd(SPI1, ENABLE); /* Enable SPI1 */
}
这个是读取数据的流程
void SCA100T_Init(void)
{
uint8_t cmd_MEAS = MEAS;
SPI_CS_Clr();
delay_us(1);
Spi_write(&cmd_MEAS,1);
delay_us(1);
SPI_CS_Set();
}
void SCA100T_ReadReg(uint8_t addr,uint8_t buffer[],uint8_t n)
{
uint8_t i = 0,ret = 0;
SPI_CS_Set();
delay_us(150);
SPI_CS_Clr();
delay_us(1);
Spi_write(&addr,1);
for(i = 0; i < n; i++)
{
Spi_read(&ret,1);
buffer = ret;
}
delay_us(2);
SPI_CS_Set();
}
uint16_t SCA100T_GetX(void)
{
uint8_t bufx[2] = {0,0};
uint16_t DATA_X = 0;
SCA100T_ReadReg(RDAX,bufx,2);
DATA_X = bufx[0] << 8 |bufx[1];
return DATA_X;
}
uint16_t SCA100T_GetY(void)
{
uint8_t bufy[2] = {0,0};
uint16_t DATA_Y = 0;
SCA100T_ReadReg(RDAY,bufy,2);
DATA_Y = bufy[0] << 8 |bufy[1];
return DATA_Y;
}
uint8_t SCA100T_GetTemp(void)
{
uint8_t data,cmd_RWTR;
cmd_RWTR = RWTR;
SPI_CS_Set();
delay_us(150);
SPI_CS_Clr();
delay_us(1);
Spi_write(&cmd_RWTR,1);
delay_us(1);
Spi_read(&data,1);
delay_us(2);
SPI_CS_Set();
return data;
} |