moerjielovecookie

Sawen_Blog

一个普通工科牲的博客网站
x
github
follow
email

STM32F407 Learning

1.1 GPIO#

STM 32 F 4 Each group of general-purpose GPIO ports is controlled by 7 32-bit registers, including:

  • 4 32-bit configuration registers (MODER, OTYPER, OSPEEDR, PUPDR)
  • 2 32-bit data registers (IDR, ODR)
  • 1 32-bit set/reset register (BSRR)
  • 1 32-bit lock register (LCKR)
  • 2 32-bit alternate function selection registers (AFRH, AFRL)

1.1.1 Registers#

1.1.1.1 GPIO Port Mode Register (GPIOx_MODER) (x=A.. I)#

Used to control the working mode of GPIO
image
Each group of GPIO has 16 IO ports, with two register bits controlling 1 IO. The reset value of PortA is 0 xA 800 0000, corresponding to the binary 1010 1000 0000 0000 0000 0000 0000 0000, indicating that PA 15/14/13 are in alternate function mode, while the other ports are in input mode.

1.1.1.2 GPIO Port Output Type Register (GPIOx_OTYPER)#

Used to control the output type of GPIO.
image
Does not take effect in input mode. The lower 16 bits are valid. In default output mode, the IO port is push-pull output.

1.1.1.3 GPIO Port Output Speed Register (GPIOx_OSPEEDR)#

image
Also only used in output mode.

1.1.1.4 GPIO Port Pull-Up/Pull-Down Register (GPIOx_PUPDR)#

image

  • 00: No pull-up or pull-down
  • 01: Pull-up
  • 10: Pull-down
  • 11: Reserved

1.1.1.5 GPIO Input Data Register (GPIOx_IDR)#

image

1.1.1.6 GPIO Output Data Register (GPIOx_ODR)#

image

1.1.1.7 GPIO Port Set/Reset Register (GPIOx_BSRR)#

image

1.2 ADC#

STM 32 F 4 has 3 independently usable ADCs, among which ADC 1 and ADC 2 can be combined into dual mode to increase the sampling rate. The ADC of STM 32 is a 12-bit successive approximation ADC. It includes 19 channels, capable of measuring 16 external and 2 internal signal sources as well as the Vbat channel signal. The A/D conversion of these channels can be performed in single, continuous, scan, and discontinuous sampling modes. The converted results are stored in a 16-bit data register of LSB or MSB.

1.2.1 Main Features#

  1. Configurable 12-bit, 10-bit, 8-bit, 6-bit resolution
  2. Generates interrupts at the end of conversion, end of injected conversion, analog watchdog occurrence, or overflow
  3. Single and continuous conversion modes
  4. Data alignment to ensure consistency of built-in data
  5. Independently set sampling time for each channel
  6. Discontinuous sampling mode
  7. ADC power supply: 2.4 V to 3.6 V during full-speed operation, 1.8 V during slow operation
  8. ADC input range: $V_{REF-} \leq V_{IN}\leq V_{REF+}$
  9. Can generate DMA requests during regular channel conversion

1.2.2 Conversion Sequence#

When any ADCx multi-channel performs a series of conversions in any order, grouped conversions are born, with two types of grouped conversions: regular group and injected group. The regular group allows up to 16 input channels for conversion, while the injected group allows up to 4 input channels for conversion.

1.2.2.1 Regular Group#

Grouped conversions in a specific order, commonly used.

1.2.2.2 Injected Group#

"Injection" breaks the original state, equivalent to an interrupt. If the injected group starts during the regular group conversion, the regular group continues conversion only after the injected group conversion is completed.
image

1.2.2.3 Conversion Rate#

The ADC conversion time calculation formula is $$
T_{CONV}=Sample_Time+TSAR\times ADC_CLK

The sampling time is controlled by the ADC_SMPR register. ADC_CLK is generated by APB 2, and the division factor is set by PPRE 2 in the RCC_CFGR register, with division options of 2/4/6/8/16. ### Registers ## Timer The basic characteristics table of the timer is as follows: ![|455](ipfs://QmezCyADZUZwpuUCQftwB6pmgZyupStk8UCHjKUKRRo2jY) ## SPI SPI stands for Serial Peripheral Interface. The block diagram of SPI is as follows: ![|535](ipfs://QmaKpmxfqkbGY5jPn96xt46ahN8ocjwPtPSUWRt6EDrhdb) The pin information of SPI is: 1. MISO (Master In / Slave Out): Master device data input, slave device data output. 2. MOSI (Master Out / Slave In): Master device data output, slave device data input. 3. SCLK (Serial Clock): Clock signal, output from the master device. 4. CS (Chip Select): Slave device chip select signal, output from the master device. ==Working Principle==: In the SPI communication, both the slave and master have a Shift Register, and the master initiates a transmission by writing a Byte of data to its own shift register. The shift register transmits the byte to the slave via MOSI, while the slave transmits the contents of its byte shift register back to the master via MISO, thus achieving data exchange between the two shift registers. Therefore, if only write operations are performed, the master can ignore the received data. If the master wants to read data from the slave, it sends an empty byte to trigger the slave's transmission. SPI supports full-duplex, half-duplex, and simplex transmission modes. ### SPI Working Modes The working mode of SPI is determined by CPOL and CPHA, both of which have 0 and 1 states, thus SPI has four working modes. | Working Mode | CPOL | CPHA | SCL Idle State | Sample Edge | Sample Time | | ---- | ---- | ---- | -------- | ---- | ---- | | 0 | 0 | 0 | Low Level | Rising Edge | Odd Edge | | 1 | 0 | 1 | Low Level | Falling Edge | Even Edge | | 2 | 1 | 0 | High Level | Falling Edge | Odd Edge | | 3 | 1 | 1 | High Level | Rising Edge | Even Edge | From the table, it can be seen that CPOL determines whether the SCL idle state is high or low, and CPHA determines whether sampling occurs on the odd or even edge of the clock. ### SPI Registers #### SPI_CR 1 (SPI Control Register 1) ![](ipfs://QmbXeQQ2Uqcecx3ohHDHJFdGwBPyJ2BNnjyCa1ijdwbEed) - Bit 11 DFF: Data frame format, 0: 8-bit data frame, 1: 16-bit - Bit 10 RXONLY: Receive only. 0: full-duplex, 1: receive only - Bit 7: Frame format. 0: send MSB first, 1: send LSB first. **This bit should not be modified during communication** - Bit 6: SPI enable. 0: disable peripheral, 1: enable peripheral. - Bit 5-3: BR[2:0]: Control baud rate. - ![](ipfs://QmYJ3Q21c8Sxrwopy11pESKP15g9boiAbiwsy6EU3i7hoq) - Bit 2: Master mode selection. 0: slave mode, 1: master mode - Bit 1: CPOL - Bit 0: CPHA #### SPI_SR (SPI Status Register) ![](ipfs://QmV5Ggb2fgjvNuSuy9mzMg7NqvkC4SC7cHKz4HGR8XLA2Q) - Bits 15-9 reserved, forced to 0 - Bit 8: Frame format error. 0: no frame format error, 1: frame format error - Bit 7: Busy flag. 0: not busy, 1: SPI is in communication state or Tx buffer not empty - Bit 1: Send buffer empty. 0: not empty, 1: empty - Bit 0: Receive buffer not empty. 0: empty, 1: not empty #### SPI_DR (SPI Data Register) ![](ipfs://QmYDcgSDE4dmfbJANB4hjZxjrnpB6r4hx4zR4Uzpor68Nk) When the data frame is 8 bits, only the lower eight bits are used; when it is 16 bits, the entire register is used.
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.