usart、uart、i2c、spi代码
时间: 2023-06-19 16:09:47 浏览: 160
以下是USART、UART、I2C和SPI的基本代码框架,供您参考:
USART:
```c
#include <stdio.h>
#include <stm32f4xx.h>
USART_InitTypeDef USART_InitStruct;
void USART_Config()
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
USART_InitStruct.USART_BaudRate = 115200;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART2, &USART_InitStruct);
USART_Cmd(USART2, ENABLE);
}
int main(void)
{
USART_Config();
while (1)
{
// 发送数据
USART_SendData(USART2, 'a');
// 接收数据
if (USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET)
{
char data = USART_ReceiveData(USART2);
}
}
}
```
UART:
```c
#include <stdio.h>
#include <stm32f4xx.h>
UART_HandleTypeDef UART_InitStruct;
void UART_Config()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
UART_InitStruct.Instance = USART1;
UART_InitStruct.Init.BaudRate = 115200;
UART_InitStruct.Init.WordLength = UART_WORDLENGTH_8B;
UART_InitStruct.Init.StopBits = UART_STOPBITS_1;
UART_InitStruct.Init.Parity = UART_PARITY_NONE;
UART_InitStruct.Init.Mode = UART_MODE_TX_RX;
UART_InitStruct.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UART_InitStruct.Init.OverSampling = UART_OVERSAMPLING_16;
HAL_UART_Init(&UART_InitStruct);
}
int main(void)
{
UART_Config();
while (1)
{
// 发送数据
uint8_t data = 'a';
HAL_UART_Transmit(&UART_InitStruct, &data, 1, 100);
// 接收数据
uint8_t data_recv;
HAL_UART_Receive(&UART_InitStruct, &data_recv, 1, 100);
}
}
```
I2C:
```c
#include <stdio.h>
#include <stm32f4xx.h>
I2C_HandleTypeDef I2C_InitStruct;
void I2C_Config()
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1);
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C;
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStruct.I2C_OwnAddress1 = 0x00;
I2C_InitStruct.I2C_Ack = I2C_Ack_Enable;
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStruct.I2C_ClockSpeed = 100000;
HAL_I2C_Init(&I2C_InitStruct);
}
int main(void)
{
I2C_Config();
while (1)
{
// 写入数据
uint8_t data = 'a';
HAL_I2C_Master_Transmit(&I2C_InitStruct, 0x50, &data, 1, 100);
// 读取数据
uint8_t data_recv;
HAL_I2C_Master_Receive(&I2C_InitStruct, 0x50, &data_recv, 1, 100);
}
}
```
SPI:
```c
#include <stdio.h>
#include <stm32f4xx.h>
SPI_HandleTypeDef SPI_InitStruct;
void SPI_Config()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStruct.SPI_NSS = SPI_NSS_Soft;
SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStruct.SPI_CRCPolynomial = 7;
HAL_SPI_Init(&SPI_InitStruct);
}
int main(void)
{
SPI_Config();
while (1)
{
// 发送数据
uint8_t data = 'a';
HAL_SPI_Transmit(&SPI_InitStruct, &data, 1, 100);
// 接收数据
uint8_t data_recv;
HAL_SPI_Receive(&SPI_InitStruct, &data_recv, 1, 100);
}
}
```
阅读全文