关于stm32驱动ADXL345加速度传感器读写的疑问

电气K哥 2011-04-12 10:29:28
大家好,有个问题求助用过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里都没有数据,请问这是怎么回事啊
...全文
1006 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
__壹零贰肆__ 2012-07-21
  • 打赏
  • 举报
回复
楼上的可以留个qq吗,这段时间刚接触adxl345,不太了解怎么使用呢,还望指点指点啊
__壹零贰肆__ 2012-07-21
  • 打赏
  • 举报
回复
楼上的可以留个qq吗,这段时间刚接触adxl345,不太了解怎么使用呢,还望指点指点啊
Cojumos 2011-07-29
  • 打赏
  • 举报
回复
+1,ADXL345不是一般的简单
电气K哥 2011-05-18
  • 打赏
  • 举报
回复

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 | RCC_APB2Periph_GPIOA |
RCC_APB2Periph_GPIOC, ENABLE);

/* Enable UART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

/* Enable SPI1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1,ENABLE);



/* UART1 pins configurations */

GPIO_StructInit(&GPIO_InitStructure);
/*Configure USART1 Rx (PA.10) as input floating*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_StructInit(&GPIO_InitStructure);
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_StructInit(&GPIO_InitStructure);



/*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);

/*UART1 Peripheral Configuration*/
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);


/* Configure one bit for preemption priority
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

NVIC_StructInit(&NVIC_InitStructure);

/* Enable the TIM1 BRK Interrupt */
/*
NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = BRK_PRE_EMPTION_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = BRK_SUB_PRIORITY;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void delay_m(void)
{

int i;
for(i=0;i<8000;i++);

}
u8 read_byte(u8 add)

{
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
SPI_I2S_SendData(SPI1,(add|0x80)<<8|0x00);

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(u8 add,u8 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;

}


这里有我的qq 可以跟我交流843538946
电气K哥 2011-05-18
  • 打赏
  • 举报
回复
我读写成功了 ,有问题加我qq 843538946
电气K哥 2011-04-19
  • 打赏
  • 举报
回复
什么叫模拟的spi,我不太清楚啊,你能给我qq吗,我问问您,谢谢
lbing7 2011-04-14
  • 打赏
  • 举报
回复
自己用模拟的SPI试试看呢?

反正我是对STM32的内部硬件接口是没有信心了

IIC和ADC都让我郁闷过好久

27,511

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 单片机/工控
社区管理员
  • 单片机/工控社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧