void myUSART_Init() { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB,&GPIO_InitStructure); USART_InitTypeDef USART_InitStuctyre; USART_InitStuctyre.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStuctyre.USART_Parity = USART_Parity_No; USART_InitStuctyre.USART_StopBits = USART_StopBits_1; USART_InitStuctyre.USART_BaudRate = 9600; USART_InitStuctyre.USART_WordLength = USART_WordLength_8b; USART_InitStuctyre.USART_HardwareFlowControl =USART_HardwareFlowControl_None; USART_Init(USART1,&USART_InitStuctyre); USART_Cmd(USART1,ENABLE); USART_ITConfig(USART1,USART_IT_TXE,ENABLE); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_Init(&NVIC_InitStructure); } void myUSARTsend_Byte(uint16_t Byte) { USART_SendData(USART1,Byte); while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET); } void myUSARTsend_Array(uint8_t *Array,uint16_t Length) { uint16_t i; for(i=0;i<=Length;i++) { myUSARTsend_Byte(Array[i]); } }
时间: 2023-09-03 12:10:12 浏览: 137
16-RCC—使用HSE或者HSI配置系统时钟_rcc配置_
这是一个使用 STM32F4 的 USART1 进行串口通信的初始化和发送函数的示例代码。
```c
void myUSART_Init()
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3;
NVIC_Init(&NVIC_InitStructure);
}
void myUSARTsend_Byte(uint16_t Byte)
{
USART_SendData(USART1, Byte);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
void myUSARTsend_Array(uint8_t *Array, uint16_t Length)
{
uint16_t i;
for (i = 0; i < Length; i++)
{
myUSARTsend_Byte(Array[i]);
}
}
```
`myUSART_Init()` 函数用于初始化 USART1,配置相关的 GPIO 和 USART 参数。`myUSARTsend_Byte()` 函数用于发送一个字节的数据,`myUSARTsend_Array()` 函数用于发送一个字节数组。
请注意,这只是一个简单的示例,你可能需要根据你的具体需求进行适当的修改。另外,还需要在程序中定义 USART1 的中断处理函数 `USART1_IRQHandler()` 来处理接收中断和其他相关操作。
阅读全文