#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);
}
}
Previous article:STM32 SPI slave routine
Next article:STM32 SPI principle
Recommended ReadingLatest update time:2024-11-16 16:47
- Popular Resources
- Popular amplifiers
- usb_host_device_code
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Transplantation of real-time operating system RT-ThreadSmart on STM32MP1
- Design of switching power supply with adjustable output voltage based on STM32
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- This year's Mid-Autumn Festival and National Day will not leave Shanghai
- Three issues about MCU timing
- Large-scale lithium-ion battery management system
- VHDL Arithmetic Logic Device Design
- This low pass filter cutoff frequency is calculated
- DSP C6000 assembly, handling of data byte non-alignment issues
- 【BearPi-HM Micro】VII: Trial Summary
- TPS61040 boost abnormality problem
- [National Technology N32G457 Review] III. ADC and Serial Port Function Test
- Open-source language Toit claims to be 30 times faster than MicroPython on ESP32