STM32 SPI master routine

Publisher:SereneMeadow7Latest update time:2019-03-08 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include "stm32f10x.h"



/*RCC clock configuration*/

void RCC_config(void)

ErrorStatus HSEStartUpStatus;



/* RCC registers are set to default configuration */

RCC_DeInit();

/* Turn on external high speed clock */

RCC_HSEConfig(RCC_HSE_ON);

/* Wait for the external high-speed clock to stabilize*/

HSEStartUpStatus = RCC_WaitForHSEStartUp();

if(HSEStartUpStatus == SUCCESS) 

/* Set HCLK = SYSCLK */

RCC_HCLKConfig(RCC_SYSCLK_Div1);

/* Set PCLK2 = HCLK */

RCC_PCLK2Config(RCC_HCLK_Div1);

/* Set PCLK1 = HCLK / 2 */

RCC_PCLK1Config(RCC_HCLK_Div2);

// /* Set FLASH code delay */

// FLASH_SetLatency(FLASH_Latency_2);

// /* Enable prefetch cache */

// FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

/* Set the PLL clock source to HSE multiplier 9 72MHz */

RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

/* Enable PLL */

RCC_PLLCmd(ENABLE);

/* Wait for PLL to stabilize */

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

/* Set PLL as system clock source */

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

/* Wait for the system clock source to switch to PLL */

while(RCC_GetSYSCLKSource() != 0x08);

}

}



/* GPIO configuration */

void GPIO_config(void)

{

GPIO_InitTypeDef GPIO_InitStructure;



/* Clock configuration */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);


/* MISO is configured as a floating input */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &GPIO_InitStructure);



/* SCK and MOSI are configured as multiplexed push-pull outputs */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);



/* NSS is configured as push-pull output */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

}



/* SPI configuration */

void SPI_config(void)

{

SPI_InitTypeDef SPI_InitStructure;


/* Clock configuration */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_SPI1, ENABLE); 


/* Configure SPI mode */

SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;

SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;

SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;

SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_InitStructure.SPI_CRCPolynomial = 7;

SPI_Init(SPI1, &SPI_InitStructure);


/* Open SPI1 */

SPI_Cmd(SPI1, ENABLE);

}



/* Millisecond delay function */

void delay_ms(uint16_t time)

{    

uint16_t i = 0;  



while(time--)

{

i = 12000;

while(i--);    

}

}



/* SPI sends a data */

uint8_t SPI_SendByte(uint8_t data)

{

while(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_TXE) == RESET);

SPI_I2S_SendData(SPI1, data);

while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

return SPI_I2S_ReceiveData(SPI1);

}



/* Main function */

int main(void)

{

volatile uint8_t i = 0, j;


/*RCC clock configuration*/

RCC_config();


/* GPIO configuration */

GPIO_config(); 


/* SPI configuration */

SPI_config(); 



while(1)

{

GPIO_ResetBits(GPIOA, GPIO_Pin_4);

j = SPI_SendByte(i++);

GPIO_SetBits(GPIOA, GPIO_Pin_4);


/* 1 second delay */

delay_ms(500);

}

}

Keywords:STM32 Reference address:STM32 SPI master routine

Previous article:STM32 SPI slave routine
Next article:STM32 SPI principle

Recommended ReadingLatest update time:2024-11-16 16:47

stm32_timer basic timer configuration and light flashing
There are a total of 11 timers in STM32, including 2 advanced control timers, 4 ordinary timers and 2 basic timers, as well as 2 watchdog timers and 1 system tick timer. TIM1 and TIM8 are advanced timers that can generate three pairs of PWM complementary outputs, and are commonly used to drive three-phase motors. The
[Microcontroller]
stm32_timer basic timer configuration and light flashing
stm32.cube (I)——System architecture and directory structure
I. Introduction The application scenarios of Arm are often much more complex than those of 51 MCUs. If the development of an advanced application requires the reconstruction of even the underlying structural code, there will be huge risks in terms of cost and R&D cycle. In order to simplify the coding process, chip ma
[Microcontroller]
Cause of Error 2 when using FATFSW to read and write SD in STM32
Recently, when I was writing data to the SD card using STM32 in my latest project, the data was written normally at first, but an error occurred after a while, error code 2. After consulting the official documentation of FATFS, the following is given: There are 4 possible situations in total. I tried various solutio
[Microcontroller]
Cause of Error 2 when using FATFSW to read and write SD in STM32
51. Comparison of AVR, PIC, MSP430, STM32 microcontrollers
51. Comparison of AVR, PIC, MSP430, STM32: common models, features, advantages and disadvantages, compilation software, download software, and download methods.          Traditional 8051: suitable for beginners, easy to use, average price (in terms of cost performance).   Disadvantages: easy to decrypt (traditional 51
[Microcontroller]
AVR Basics: The ATMEGA's SPI Bus - Part 1
When the AVR communicates with other devices, we need to choose which method to use. You can use classic serial ports such as UART and I2C, or you can choose the serial peripheral interface (SPI). I prefer the SPI bus method. So let's talk about this bus form. relation An important concept in SPI is the master-slave
[Microcontroller]
AVR Basics: The ATMEGA's SPI Bus - Part 1
STM32 IO Operation Notes
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //Output  #define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //Input for example: PAout(0)=1 PA0 outputs high level GPIOx_CRH Port configuration high register IO 15: 8 GPIOG- CRH&=0xFF0FFFFF clears PG13 mode GPIOG- CRH|=0x00300000 PG13 push-pull output GPIOG- CRH&=0xFF0FFFFF clears
[Microcontroller]
STM32 needs to modify the code when using different crystal oscillators
 The first step is to open stm32f10x.h and change   #define HSE_VALUE    ((uint32_t)8000000) /*! Value of the External oscillator in Hz */   change into:   #define HSE_VALUE    ((uint32_t)12000000) /*! Value of the External oscillator in Hz */         The second step is to open system_stm32f10x.c and modify the PLL
[Microcontroller]
STM32 MCU GPIO register
Each GPIO port has two 32-bit configuration registers (GPIOx_CRL, GPIOx_CRH) to control the high and low eight bits of each port respectively. If the IO port is 0-7, write the CRL register, if the IO port is 8-15, write the CRH register, two 32-bit data registers (GPIOx_IDR, GPIOx_ODR), one is read-only as input data r
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号