STM32F4 SPI2 initializing and sending and receiving data using library functions

Source: Internet
Author: User

My STM32F4 discovery has an accelerometer lis302dl on top. In the demonstration project, ST's engineers used the sensor to do a very admirable thing: to solve the development Board posture. When the development Board is tilted, the top led is lit and the other LEDs are not lit. At the same time, the Development Board will be virtual into a mouse when the board is connected to the computer with a microUSB cable. When you tilt the Development Board, the mouse pointer moves in a tilted direction. In the final analysis, the St engineer of the Cow B has completed the attitude calculation with the acceleration sensor.

On the Development Board, the accelerometer uses the SPI method to communicate with the STM32F4 chip. STM32F4 SPI1 acts as a host, communicates with LIS302DL, reads or writes data. Since I have not used the SPI port of STM32, I found the SPI2 interface in the spare resources of the Board to do the experiment. The experiment is this: connect the SPI's miso and mosi feet. So what data is sent to the SPI and what data is received. Can be tested without the need for additional devices.

Pin: The PB13, PB14, PB15 three pin multiplexing function, respectively, corresponding to the spi2sck, Spi2miso, Spi2mosi.

Pin initialization:

void Spi_gpioconfig (void)
{
Gpio_inittypedef gpio_initstructure;
Rcc_ahb1periphclockcmd (rcc_ahb1periph_gpiob,enable); Turn on the clock

Gpio_initstructure.gpio_mode = GPIO_MODE_AF; Pin initialization
Gpio_initstructure.gpio_otype = gpio_otype_pp;
Gpio_initstructure.gpio_pin = gpio_pin_13 | gpio_pin_14 | Gpio_pin_15;
GPIO_INITSTRUCTURE.GPIO_PUPD = gpio_pupd_up;
Gpio_initstructure.gpio_speed = Gpio_speed_50mhz;
Gpio_init (gpiob,&gpio_initstructure);

Gpio_pinafconfig (GPIOB,GPIO_PINSOURCE13,GPIO_AF_SPI2); The multiplexing function of the open pin
Gpio_pinafconfig (GPIOB,GPIO_PINSOURCE14,GPIO_AF_SPI2);
Gpio_pinafconfig (GPIOB,GPIO_PINSOURCE15,GPIO_AF_SPI2);
}

SPI2 function Initialization:

void Spi_config (void)
{
Spi_gpioconfig ();
Rcc_apb1periphclockcmd (rcc_apb1periph_spi2,enable); Clock

Spi_initstructure.spi_direction = Spi_direction_2lines_fullduplex; Full duplex mode
Spi_initstructure.spi_mode = Spi_mode_master; Use as a host
Spi_initstructure.spi_datasize = spi_datasize_8b; Data Length 8
Spi_initstructure.spi_cpol = Spi_cpol_high;
Spi_initstructure.spi_cpha = Spi_cpha_1edge;
Spi_initstructure.spi_nss = Spi_nss_soft; Software set NSS function
Spi_initstructure.spi_baudrateprescaler = spi_baudrateprescaler_256;
Spi_initstructure.spi_firstbit = SPI_FIRSTBIT_MSB;
Spi_initstructure.spi_crcpolynomial = 7;
Spi_init (spi2,&spi_initstructure);
Spi_cmd (spi2,enable);
}

You can then send and receive the data:

void Myspi_senddata (char da)
{
while (Spi_i2s_getflagstatus (SPI2,SPI_I2S_FLAG_TXE) ==reset);
Spi_senddata (SPI2,DA);
}

uint8_t myspi_receivedata (void)
{
while (Spi_i2s_getflagstatus (Spi2,spi_i2s_flag_rxne) ==reset);
Return Spi_receivedata (SPI2);
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.