标题:
STM32 串口1打印例程
[打印本页]
作者:
杨子123456
时间:
2017-6-13 10:56
标题:
STM32 串口1打印例程
STM32 串口1打印例程
单片机源程序如下:
/*-------------------------------------------------------------------------------
文件名称:main.c
文件描述:通过串口1,使用printf函数打印信息,编译时需勾选Use MicroLIB
硬件平台:尼莫M3S开发板
备 注:通过简单修改可以移植到其他开发板,部分资料来源于网络。
---------------------------------------------------------------------------------*/
#include <stdio.h>
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
#include "key.h"
#include "timer.h"
#include "beep.h"
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
USART_DeInit(USART1); //复位串口1
//USART1_TX PA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA9
//USART1_RX PA.10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA10
/* USARTx configured as follow:
- BaudRate = 9600 baud 波特率
- Word Length = 8 Bits 数据长度
- One Stop Bit 停止位
- No parity 校验方式
- Hardware flow control disabled (RTS and CTS signals) 硬件控制流
- Receive and transmit enabled 使能发送和接收
*/
USART_InitStructure.USART_BaudRate = 9600;
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_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE); //使能串口
LED_Init();//LED初始化
KEY_Init();//按键初始化
SysTick_Init();//延时初始化
BEEP_Init(); //蜂鸣器初始化
while (1)
{
//使用printf函数循环发送固定信息
printf("\n\rUSART Printf Example: (德飞莱)尼莫M3S开发板串口测试程序\r");
Delay_ms(500);
LED2_REV;
}
}
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
……………………
…………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
0.png
(43.5 KB, 下载次数: 55)
下载附件
2017-6-13 16:32 上传
所有资料51hei提供下载:
Keil代码.7z
(178.77 KB, 下载次数: 38)
2022-10-17 23:27 上传
点击文件名下载附件
下载积分: 黑币 -5
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1