完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
有哪位大神帮忙看看下面代码有啥问题不,看了一个星期了,也不知道问题在哪,SPI通讯一直连不上,简单的读取ADXL362的ID号都读不出来
/*********main函数文件*********/ int main(void) { delay_init(); //ÑÓʱº¯Êý³õʼ»¯ uart_init(115200); //´®¿Ú³õʼ»¯Îª115200 LED_Init(); //³õʼ»¯ÓëLEDÁ¬½ÓµÄÓ²¼þ½Ó¿Ú ADXL362_Init(); while(ADXL362_ReadID()!= ADXL362_ID) //¼ì²â²»µ½ADXL362 { delay_ms(500); LED0=!LED0;//DS0ÉÁ˸ delay_ms(500); USART_SendData(USART1,0x78); } while(1) { delay_ms(500); USART_SendData(USART1,ADXL362_ReadID()); delay_ms(500); LED0 = 0; } } /************SPI文件****************/ void SPI1_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE );//PORTBʱÖÓʹÄÜ RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1, ENABLE );//SPI2ʱÖÓʹÄÜ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PB13/14/15¸´ÓÃÍÆÍìÊä³ö GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOB GPIO_SetBits(GPIOA,GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7); //PB13/14/15ÉÏÀ SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //ÉèÖÃSPIµ¥Ïò»òÕßË«ÏòµÄÊý¾Ýģʽ:SPIÉèÖÃΪ˫ÏßË«ÏòÈ«Ë«¹¤ SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //ÉèÖÃSPI¹¤×÷ģʽ:ÉèÖÃΪÖ÷SPI SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //ÉèÖÃSPIµÄÊý¾Ý´óС:SPI·¢ËͽÓÊÕ8λ֡½á¹¹ SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //´®ÐÐͬ²½Ê±ÖӵĿÕÏÐ״̬Ϊ¸ßµçƽ SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //´®ÐÐͬ²½Ê±Öӵĵڶþ¸öÌø±äÑØ£¨ÉÏÉý»òϽµ£©Êý¾Ý±»²ÉÑù SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSSÐźÅÓÉÓ²¼þ£¨NSS¹Ü½Å£©»¹ÊÇÈí¼þ£¨Ê¹ÓÃSSI룩¹ÜÀí:ÄÚ²¿NSSÐźÅÓÐSSIλ¿ØÖÆ SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //¶¨Ò岨ÌØÂÊÔ¤·ÖƵµÄÖµ:²¨ÌØÂÊÔ¤·ÖƵֵΪ256 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //Ö¸¶¨Êý¾Ý´«Êä´ÓMSBλ»¹ÊÇLSBλ¿ªÊ¼:Êý¾Ý´«Êä´ÓMSBλ¿ªÊ¼ SPI_InitStructure.SPI_CRCPolynomial = 7; //CRCÖµ¼ÆËãµÄ¶àÏîʽ SPI_Init(SPI1, &SPI_InitStructure); //¸ù¾ÝSPI_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèSPIx¼Ä´æÆ÷ SPI_Cmd(SPI1, ENABLE); //ʹÄÜSPIÍâÉè SPI1_ReadWriteByte(0xff);//Æô¶¯´«Êä } u8 SPI1_ReadWriteByte(u8 TxData) { u8 retry=0; while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) //¼ì²éÖ¸¶¨µÄSPI±ê־λÉèÖÃÓë·ñ:·¢ËÍ»º´æ¿Õ±ê־λ { retry++; if(retry>200)return 0; } SPI_I2S_SendData(SPI1, TxData); //ͨ¹ýÍâÉèSPIx·¢ËÍÒ»¸öÊý¾Ý retry=0; while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET) //¼ì²éÖ¸¶¨µÄSPI±ê־λÉèÖÃÓë·ñ:½ÓÊÜ»º´æ·Ç¿Õ±ê־λ { retry++; if(retry>200)return 0; } return SPI_I2S_ReceiveData(SPI1); //·µ»Øͨ¹ýSPIx×î½ü½ÓÊÕµÄÊý¾Ý } /*****************ADXL362文件*****************/ void ADXL362_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );//PORTAʱÖÓʹÄÜ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;//CS GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA,GPIO_Pin_4); SPI1_Init(); ADXL362_SoftwareReset(); } unsigned char ADXL362_ReadID(void) { unsigned char regValue = 0xff; ADXL362_GetRegisterValue(®Value,ADXL362_REG_PARTID,1); return regValue; } void ADXL362_SetRegisterValue(unsigned short registerValue, unsigned char registerAddress, unsigned char bytesNumber) { unsigned char buffer[4] = {0, 0, 0, 0}; unsigned char index = 0; buffer[0] = ADXL362_WRITE_REG; buffer[1] = registerAddress; buffer[2] = (registerValue & 0x00FF); buffer[3] = (registerValue >> 8); ADXL362_CS = 0;//CSƬѡ for(index = 0; index < bytesNumber+2; index++) { SPI1_ReadWriteByte(buffer[index]); } ADXL362_CS = 1;//CSƬѡ } void ADXL362_GetRegisterValue(unsigned char* pReadData, unsigned char registerAddress, unsigned char bytesNumber) { unsigned char buffer[2]; unsigned char index = 0; buffer[0] = ADXL362_READ_REG; buffer[1] = registerAddress; ADXL362_CS = 0;//CSƬѡ SPI1_ReadWriteByte(buffer[0]); SPI1_ReadWriteByte(buffer[1]); for(index = 0; index < bytesNumber; index++) { *pReadData = SPI1_ReadWriteByte(0xFF); pReadData++; } ADXL362_CS = 1;//CSƬѡ } void ADXL362_SoftwareReset(void) { ADXL362_SetRegisterValue(ADXL362_RESET_KEY, ADXL362_REG_SOFT_RESET, 1); } void ADXL362_SetPowerMode(unsigned char pwrMode) { unsigned char oldPowerCtl = 0; unsigned char newPowerCtl = 0; ADXL362_GetRegisterValue(&oldPowerCtl, ADXL362_REG_POWER_CTL, 1); newPowerCtl = oldPowerCtl & ~ADXL362_POWER_CTL_MEASURE(0x3); newPowerCtl = newPowerCtl | (pwrMode * ADXL362_POWER_CTL_MEASURE(ADXL362_MEASURE_ON)); ADXL362_SetRegisterValue(newPowerCtl, ADXL362_REG_POWER_CTL, 1); }
|
|
相关推荐
6个回答
|
|
目前调试问题:STM32无法与ADXL进行通讯,测试程序输出ADXL的器件编号有误(读取ADXL寄存器0x02的值,正确应为0xF2,目前输出为0x00)
|
|
|
|
各位大侠们,本人范了个低级错误,调试时stm32板子和传感模块板子没有用同一个电源,导致一直通讯不得,也就是说以上代码木有问题,可以供新人参考使用,特此说明,谢谢
|
|
|
|
所以意思是传感器的电源和stm32不共地,导致电平不对,所以连不上么?
|
|
|
|
楼主,我现在下载了官网上面ADXL362的generic的代码,并且也在看HAL_SPI的代码,但是不知道怎么移植,可否加一下qq交流一下~qq:1450772061
|
|
|
|
你没有遇到下面描述的问题吗?
"配置IO相关寄存器,SCK IO 要选择GPIO_PULLDOWN 模式,其他IO选择GPIO_PULLUP模式,否则会出现初次配置adxl寄存器的时候失败,接着读取寄存器正常的情况。" 我的初始化程序和你基本一致,我就遇到这个问题了。标准库还没办法用HAL库里的东西。 |
|
|
|
你好,可以请问一下硬件如何连接的吗?
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
2105 浏览 1 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
1941 浏览 3 评论
4537 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
2084 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
2599 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-23 04:13 , Processed in 0.820916 second(s), Total 82, Slave 64 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号