完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
最近用到STM32F04单片机通过SPI功能读取AD7921芯片数据,读取到却始终是0.下面是我的代码,各位路过的大神帮忙看看看看看啊。拜托了,已困扰许久。
#define SPI1_PORT GPIOA #define PIN_SPI1_CSN GPIO_Pin_4 #define PIN_SPI1_SCK GPIO_Pin_5 #define PIN_SPI1_MISO GPIO_Pin_6 #define PIN_SPI1_MOSI GPIO_Pin_7 #define SPI1_CSN_L (SPI1_PORT->BRR = PIN_SPI1_CSN) #define SPI1_CSN_H (SPI1_PORT->BSRR = PIN_SPI1_CSN) // 初始化 void SPI1_INIT(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; //NVIC_InitTypeDef NVIC_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //???? GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //???? //GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //?? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3; GPIO_InitStructure.GPIO_Pin = PIN_SPI1_SCK ; GPIO_Init(SPI1_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //???? GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //???? GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3; GPIO_InitStructure.GPIO_Pin = PIN_SPI1_MOSI ; GPIO_Init(SPI1_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = PIN_SPI1_MISO; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(SPI1_PORT, &GPIO_InitStructure); GPIO_PinAFConfig(SPI1_PORT, GPIO_PinSource5, GPIO_AF_0); GPIO_PinAFConfig(SPI1_PORT, GPIO_PinSource6, GPIO_AF_0); GPIO_PinAFConfig(SPI1_PORT, GPIO_PinSource7, GPIO_AF_0); GPIO_InitStructure.GPIO_Pin = PIN_SPI1_CSN; //?? CSN GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //?? GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //???? GPIO_Init(SPI1_PORT, &GPIO_InitStructure); 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; //8 ???? SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //????? SCK ???? SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //???????? SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //???? NSS SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB; //????? MSB ?? SPI_InitStructure.SPI_CRCPolynomial = 7; //CRC ?? SPI_Init(SPI1, &SPI_InitStructure); SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_HF); SPI_Cmd(SPI1, ENABLE); } uint16_t SPI1_Transfer(uint8_t data) { while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); //SPI_SendData8(SPI1, data); SPI_I2S_SendData16(SPI1, data); while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); //return SPI_ReceiveData8(SPI1); return SPI_I2S_ReceiveData16(SPI1); } uint16_t Spi_Read_Test() { uint16_t val_0=0; uint16_t val_1=0; SPI1_CSN_L; SPI1_Transfer(0xFF); val_0=SPI1_Transfer(0x00); SPI1_CSN_H; } 调用Spi_Read_Test函数 val_0 始终是0,不解啊 |
|
相关推荐
1个回答
|
|
只有小组成员才能发言,加入小组>>
996 浏览 2 评论
给ADUM4223 增加信号驱动15V电压就不正常, 波动很大会被烧是什么情况?
1389 浏览 2 评论
ADP5092 SYS端口为2.09V,但是REG_OUT为0是什么原因?
1940 浏览 1 评论
ad7193差分输入ain1与ain2差是正值时,读到电压与实际值误差小,但为负值值,误差就变的很大
4272 浏览 2 评论
9063 浏览 1 评论
ADC3442采集,分析数据出现有规则毛刺,请问是哪方面的问题啊?
1532浏览 3评论
AD7190状态寄存器一直是0x80,连续转换模式下RDY不拉低
1695浏览 2评论
996浏览 2评论
1674浏览 2评论
给ADUM4223 增加信号驱动15V电压就不正常, 波动很大会被烧是什么情况?
1389浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-24 21:54 , Processed in 0.735706 second(s), Total 80, Slave 63 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号