在stm32f4和arduino之间通信时SPI中的问题

时间:2018-10-01 10:57:18

标签: stm32 spi stm32f4discovery

在没有HAL库的spi代码中,如果我要发送的任何数据默认为0xFF,则无法发送数据而不是0xFF,这是我的代码:

#include <stm32f4xx.h>

uint8_t i = 100;
int j = 0;
uint8_t data = 82;

void afio_config(void);
void spi_init(void);

int main(void)
{
    afio_config();
    spi_init();
    NVIC_EnableIRQ(SPI2_IRQn);
    while(1)
    {
    }
}

void afio_config()
{
    RCC -> AHB1ENR |= (1<<1)|(1<<2);
    RCC -> APB1ENR |= (1<<14);
    GPIOB -> MODER |= (1<<21)|(1<<24);
    GPIOC -> MODER |= (1<<7)|(1<<5);
    GPIOB ->OTYPER &= ~((1<<12)&(1<<10));
    GPIOC ->OTYPER &= ~((1<<2)&(1<<3));
    GPIOB -> OSPEEDR |= (1<<24)|(1<<20);
    GPIOC -> OSPEEDR |= (1<<4)|(1<<6);
    GPIOB -> PUPDR |= (1<<25)|(1<<21);
    GPIOC -> PUPDR |= (1<<4)|(1<<6);
    GPIOC -> AFR[0] |= (1<<14)|(1<<12)|(1<<10)|(1<<8);
    GPIOB -> AFR[1] |= (1<<10)|(1<<8);
}


void spi_init()
{
    SPI2 ->CR1 |= (1<<4)|(1<<5);
    SPI2 ->CR1 |= (1<<15)|(1<<14);
    SPI2 ->CR1 |= (1<<9)|(1<<8);
    SPI2 ->CR2 |= (1<<7);
    SPI2 ->CR1 |= (1<<2);
    SPI2 ->CR1 |= (1<<6);
    GPIOB ->ODR &= ~(1<<12);
}

void SPI2_IRQHandler()
{
    SPI2 ->DR = data;
}

0 个答案:

没有答案