stm32项目开发实例 STM32实例代码.doc
文本预览下载声明
stm32项目开发实例 STM32实例代码
导读:就爱阅读网友为您分享以下“STM32实例代码”资讯,希望对您有所帮助,感谢您对92的支持!
STM32F103
STM32----1----USART
一,串口USART全功能配置函数
随着程序的代码的增加,调试程序变的越来越困难,通过STM32的串口调试程序不失为一种解决问题的方法。其实串口和PC的通信中,串口主动的向PC发送数据使用的数据较多,PC主动向STM32发送的数据较少。本串口的USART函数借鉴ZN51的串口的配置思路,利用MDK2.0库在STM32中进行了实现。
1、Usart.h
#ifndef __UART_H__
#define __UART_H__
#define uchar unsigned char
#define uint unsigned short
#define ulong unsigned long
//UART 外部相关函数------------------------
void USART_Configuration(void);
void USART_Send_Byte(uchar mydata);
void USART_Send_Str(char *s);
void USART_Send_Enter(void);
void USART_Output_Num(ulong dat);
void USART_Output_Information(char *inf,ulong dat);
//UART-------------------------------------
#endif
这里是USART的头文件,包含了USART操作的所有函数,这些函数包括USART的配置初始化、发送字节、发送字符串、发送回车、发送位长数据、发送调试信息等
2、usart.c文件
#include “usart.h”
#include “stm32f10x_lib.h”
#include “string.h “
#include “myfun.h”
/***************************************************************************
- 功能描述:STM32f103串口的初始化
- 隶属模块:STM32串口操作
- 函数属性:外部,使用户使用
- 参数说明:无
- 返回说明:无
- 串口配置步骤:
(1)首先建立结构体变量,再配置的参数(波特率、字长、停止位、奇偶校验位、接收和发送)
用参数初始化串口1,最后使能该串口。
(2)配置串口操作所需的IO口,其中GPIO_TX为发送端配置为复用推挽输出
其中GPIO_RX配置为输入浮空
(3)对于USART1,PA9为GPIO_TX发送端,PA10为GPIO_RX接收端
*************************************************************************/
void USART_Configuration()
{
//定义串口和GPIO的结构体变量
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitStructure.USART_BaudRate =38400;
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(USART1, USART_InitStructure);
USART_Cmd(USART1 , ENABLE);
GPIO_StructInit(GPIO_InitStructure);
//U
显示全部