gpt4 book ai didi

c - 为什么 SPI 不适用于我的 STM32f4DISCOVERY?

转载 作者:太空宇宙 更新时间:2023-11-04 03:39:28 27 4
gpt4 key购买 nike

我已经被这个问题困扰了一段时间。我在网上关注了几个例子,但没有成功。我无法让它与我的 STM32F4Discovery 一起使用。我有一个外部芯片(更具体地说是来自 Semtech 的 SX1272),我试图与它进行 SPI 通信。通过 Arduino 进行所有 child 游戏,但 ST 产品不走运。我用过示波器,它显示发送了 MOSI 命令,但没有 MISO 发出。 (使用 Arduino 很好)。这是我的代码:

#include "main.h"

void init_SPI1(void){


GPIO_InitTypeDef GPIO_InitStruct;
SPI_InitTypeDef SPI_InitStruct;

// enable clock for used IO pins
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

/* configure pins used by SPI1
* PA5 = SCK
* PA6 = MISO
* PA7 = MOSI
*/
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStruct);

// connect SPI1 pins to SPI alternate function
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);

// enable clock for used IO pins
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);

/* Configure the chip select pin
in this case we will use PE7 */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOE, &GPIO_InitStruct);

GPIO_SetBits(GPIOE, GPIO_Pin_7); // set PE7 high

// enable peripheral clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);

/* configure SPI1 in Mode 0
* CPOL = 0 --> clock is low when idle
* CPHA = 0 --> data is sampled at the first edge
*/
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines
SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // transmit in master mode, NSS pin has to be always high
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide
SPI_InitStruct.SPI_CPOL = SPI_CPOL_High; // clock is low when idle
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; // data sampled at first edge
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft | SPI_NSSInternalSoft_Set; // set the NSS management to internal and pull internal NSS high
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; // SPI frequency is APB2 frequency / 4
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB; // data is transmitted MSB first
SPI_Init(SPI1, &SPI_InitStruct);

SPI_Cmd(SPI1, ENABLE); // enable SPI1
}


/* This funtion is used to transmit and receive data
* with SPI1
* data --> data to be transmitted
* returns received value
*/
uint8_t SPI1_read(uint8_t data){
GPIO_ResetBits(GPIOE, GPIO_Pin_7); // set PE7 (CS) low

SPI1->DR = data; // write data to be transmitted to the SPI data register
while( !(SPI1->SR & SPI_I2S_FLAG_TXE) ); // wait until transmit complete
while( SPI1->SR & SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore

GPIO_SetBits(GPIOE, GPIO_Pin_7); // set PE7 (CS) high
uint8_t ret = SPI1->DR;
return ret; // return received data from SPI data register
}


void SPI1_write(uint8_t address, uint8_t data){

uint16_t comb = (address << 8) | data;
GPIO_ResetBits(GPIOE, GPIO_Pin_7); // set PE7 (CS) low

SPI1->DR = address; // write data to be transmitted to the SPI data register
while( !(SPI1->SR & SPI_I2S_FLAG_TXE) ); // wait until transmit complete
//while( SPI1->SR & SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore

SPI1->DR = data; // write data to be transmitted to the SPI data register
while( !(SPI1->SR & SPI_I2S_FLAG_TXE) ); // wait until transmit complete
while( SPI1->SR & SPI_I2S_FLAG_BSY ); // wait until SPI is not busy anymore

GPIO_SetBits(GPIOE, GPIO_Pin_7); // set PE7 (CS) high
}

int main(void){

init_SPI1();

while(1){
SPI1_read(0x01);
}
}

我从 MOSI 得到的只是一个随机的 ~1.5V 峰值,这是不同步的。

最佳答案

/* configure pins used by SPI1 
* PA5 = SCK
* PA6 = MISO
* PA7 = MOSI
*/

这表明你的 MISO(master in,slave out)信号在 PA6 上

  GPIO_InitStruct.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5; 
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStruct);

但这会将 PA6 配置为输出

将PA6配置为输入,再试一次。如果它仍然不起作用,请尝试通过一个 1K 左右的电阻器向那里注入(inject)一个电平,看看是否可以让范围内的电压变化和接收到的值完全反射(reflect)出来。

关于c - 为什么 SPI 不适用于我的 STM32f4DISCOVERY?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29827659/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com