关于stm32驱动ADXL345加速度传感器读写的疑问
大家好,有个问题求助用过ADXL345的高手们?
我用下面的程序驱动
u8 buffer;
int main(void)
{
NVIC_Configuration();
RCC_Configuration();
GPIO_Configuration();
spi_init();
write_byte(0x001f,0x0001);
buffer=read_byte(0x001f);
delay();
}
void spi_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* GPIO, TIM1,UART1 clocks enabling */
/* Enable GPIOA, GPIOC, GPIOE, AFIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA |
RCC_APB2Periph_GPIOC, ENABLE);
/* Enable SPI1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);
/*configure SPI NSS,SCK,MISO,MOSI*/
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_WriteBit(GPIOA,GPIO_Pin_4,1);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*SPI1 Peripheral Configuration*/
SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode=SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_CPOL=SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA=SPI_CPHA_2Edge;
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;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1,ENABLE);
}
u8 read_byte(u16 add)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
SPI_I2S_SendData(SPI1,(add|0x80)<<8|0xff);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)==RESET);
GPIO_SetBits(GPIOA,GPIO_Pin_4);
return SPI_I2S_ReceiveData(SPI1)&0xff;
}
void write_byte(u16 add,u16 val)
{
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
SPI_I2S_SendData(SPI1,add<<8|val);
while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE)==RESET);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE)==RESET);
GPIO_SetBits(GPIOA,GPIO_Pin_4);
SPI_I2S_ReceiveData(SPI1)&0xff;
}
可是驱动不成功,spi的DR里都没有数据,请问这是怎么回事啊
[解决办法]
自己用模拟的SPI试试看呢?
反正我是对STM32的内部硬件接口是没有信心了
IIC和ADC都让我郁闷过好久
[解决办法]
什么叫模拟的spi,我不太清楚啊,你能给我qq吗,我问问您,谢谢
[解决办法]
我读写成功了 ,有问题加我qq 843538946
[解决办法]
int main(void)
{
NVIC_Configuration();
RCC_Configuration();
GPIO_Configuration();
spi_init();
delay();
//初始化
write_byte(0x1E,0xFF);
write_byte(0x1F,0x55);
write_byte(0x20,0xFF);
write_byte(0x2C,0x0A);
write_byte(0x2D,0x00);
write_byte(0x2E,0x80);
write_byte(0x2F,0x00);
write_byte(0x31,1);
//这段初始化程序是仿照AD公司的自己出的单片机的官方例程
while(1){
delay();
write_byte(0x1d,0x08);
delay();
a=read_byte(0x1d) ;
}
/*
while(1){
j=0;
for(i=0x00;i<0x2b;i++)
{
write_byte(i,++j);
USART_SendData(USART1,read_byte(i));
delay();
}
delay();
}
*/
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the TIM1 Pins.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOC clock */
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE);
GPIO_DeInit(GPIOC);
GPIO_StructInit(&GPIO_InitStructure);
/* Configure PC.06, PC.07, PC.08 and PC.09 as Output push-pull for debugging
purposes */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6
[解决办法]
GPIO_Pin_7
[解决办法]
GPIO_Pin_8
------解决方案--------------------
GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div4);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures the Vector Table base address.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
//printf("Wrong parameters value: file %s on line %d\r\n", file, line);
}
}
#endif
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
void delay(void)
{
int i,j;
for(j=0;j<100;j++)
for(i=0;i<3000;i++);
}
void spi_init(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* GPIO, TIM1,UART1 clocks enabling */
/* Enable GPIOA, GPIOC, GPIOE, AFIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO