SPI initialization of stm32

Publisher:算法之手Latest update time:2016-12-26 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1 void SPIx_Init(void)

 2 {     

 3 RCC->APB2ENR|=1<<2; //PORTA clock enable      

 4 RCC->APB2ENR|=1<<12; //SPI1 clock enable 

 5            

 6 //Here is only for SPI port initialization

 7 GPIOA->CRL&=0X000FFFFF; 

 8 GPIOA->CRL|=0XBBB00000; //PA5.6.7 multiplexed push-pull output         

 9 //GPIOA->ODR|=0X7<<5; //PA5.6.7 pull-up should not work because this mode

10 SPI1->CR1&=~(1<<6); //Turn off SPI to configure some bits

11 

12 SPI1->CR1&=~(1<<15); //1: two-line bidirectional mode 0: unidirectional bidirectional mode

13 SPI1->CR1&=~(1<<13);//1: Enable CRC check 0: Disable CRC check Can only be rewritten when SPE=0, otherwise an error will occur. Can only be used in full-duplex mode

14 SPI1->CR1|=0<<11; //1: 16-bit data format 0: 8-bit data format It can only be rewritten when SPE=0, otherwise an error will occur.

15 SPI1->CR1|=0<<10; //1: full-duplex mode 0: receive only mode    

16 SPI1->CR1|=1<<9; //1: Enable software slave device management 0: Disable software slave device management When it is 1, the NSS level is determined by the SSI bit

17 SPI1->CR1|=1<<8;//

18 SPI1->CR1|=0<<7; //1: send MSB first 0: send LSB first

19 SPI1->CR1|=0<<3 //5~3bit: frequency division coefficient 0~7 The frequency division coefficient is Fpclk/2, 4, 8, 16, 32, 64, 128, 256

20 SPI1->CR1|=1<<2; //1: master device 0: slave device 

21 SPI1->CR1|=1<<1; //CPOL 1: SCK is 1 when idle 0: SCK=0 when idle

22 SPI1->CR1|=1<<0; //CPHA 1: Data sampling starts from the second clock edge 0: Data sampling starts from the first clock edge      

23 SPI1->CR1|=1<<6; //1: turn on SPI 0: turn off SPI

twenty four }   


Keywords:stm32 Reference address:SPI initialization of stm32

Previous article:DMA configuration of STM32
Next article:NVIC interrupt control function implementation

Recommended ReadingLatest update time:2024-11-16 14:50

The stm32 instruction set is not case sensitive
The instructions received by the microcontroller from the serial port are generally strings. If we use strncmp to compare whether they are instructions in our instruction set, it is not case-sensitive. Here we can use the strcasecmp() function and the strncasecmp() function to receive and compare the received string i
[Microcontroller]
STM32 I2C configuration
First configure I2C 1. Reset I2C through APB and enable I2C clock 2. Enable GPIO clock and  configure GPIO in AF OD mode 3. Enable I2C ( PE="1") 4. Set the speed parameters FREQ CCR F/S DUTY TRISE A: CR2.FREQR = desired I2C clock, usually set to the maximum clock 36 MHz Determined Tck = 1 / FREQ -- similar t
[Microcontroller]
When writing STM32 program with keil, "literally treated as "longlong" appears
In Keil MDKARM unsigned int value2=0x80000000; unsigned int value4=2147483648; value2 does not generate a warning when compiled, but value4 generates a warning main.c(17): warning:  #1134-D: literal treated as "long long"   Solution:   The key is that 2147483648 is a literal constant, and its type is int, not unsigned
[Microcontroller]
STM32 timer interrupt function
#include "timer.h" #include "led.h" //The crystal oscillator is 8MHZ, and the default CPU frequency is 9 times. //General timer 3 interrupt initialization //The clock here is selected to be twice that of APB1, and APB1 is 36M //SYSCLK:72M //AHB:72M //APB1(PCLK1):36M  // APB2(PCLK2):72M //PLL:72M //arr: automatically r
[Microcontroller]
STM32 Digital-to-Analog Converter - DAC_DMA
DAC—Digital-to-Analog Converter DAC is a digital-to-analog converter, which converts digital quantities into analog quantities. Everyone should understand the principle, so let's jump directly into how to use it: STM32's DAC is very simple to use. First, configure the GPIO pin connected to the DAC channel to analog
[Microcontroller]
STM32 study notes EXTI (external interrupt)
1: EXTI (external interrupt) description   ===========================================dividing line==========================================          The external interrupt/event controller consists of 19 edge detectors that generate event/interrupt requests. Each input line can be independently configured Input type
[Microcontroller]
STM32 study notes EXTI (external interrupt)
Many pin functions on STM32 can be remapped
There are many I/O ports and many built-in peripherals. In order to save pins, these built-in peripherals share pins with I/O ports. ST calls it the multiplexing function of I/O pins. I believe everyone is very clear about this because basically all microcontrollers do this. But I don’t know how many people know that t
[Microcontroller]
Many pin functions on STM32 can be remapped
STM32 ADC input channel configuration
There are up to 3 ADC modules in STM32, and the channels corresponding to each module do not completely overlap. The figure below is the lower left corner of the general block diagram in the STM32F103CDE data sheet. It can be seen from the figure that 8 external ADC pins are connected to 3 ADC modules, 8
[Analog Electronics]
STM32 ADC input channel configuration
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号