stm32串口3的蓝牙模块uart3.h和uart3.c怎么写,要完整版
时间: 2024-02-12 22:07:47 浏览: 137
以下是一个简单的UART3驱动程序,包含uart3.h和uart3.c文件。需要注意的是,这里的驱动程序仅供参考,具体实现需要根据实际情况进行修改和优化。
uart3.h:
```c
#ifndef __UART3_H
#define __UART3_H
#include "stm32f4xx.h"
#include <stdio.h>
#define UART3_BAUDRATE 9600
void UART3_Config(void);
void UART3_Putc(char c);
void UART3_Puts(char *str);
#endif /* __UART3_H */
```
uart3.c:
```c
#include "uart3.h"
void UART3_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_USART3); // PB10 -> USART3_TX
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_USART3); // PB11 -> USART3_RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = UART3_BAUDRATE;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
}
void UART3_Putc(char c)
{
while (USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
USART_SendData(USART3, (uint16_t)c);
}
void UART3_Puts(char *str)
{
while (*str) {
UART3_Putc(*str++);
}
}
```
使用时只需调用UART3_Config()函数进行初始化,然后使用UART3_Putc()和UART3_Puts()函数进行数据发送即可。例如:
```c
#include "uart3.h"
int main(void)
{
UART3_Config();
UART3_Puts("Hello, world!\r\n");
while (1);
}
```
阅读全文