中级会员
 
- 积分
- 218
- 金钱
- 218
- 注册时间
- 2015-1-19
- 在线时间
- 28 小时
|
简介:用串口1打印“hello STM32!”;
[mw_shl_code=c,true]#ifndef __UART_H
#define __UART_H
#include "stm32f10x.h"
void Uart_Config(u32 BaudRate);
void UART_SendData(char byte);
void UART_SendStr(char * str);
#endif
[/mw_shl_code]
[mw_shl_code=c,true]/*****************************
* @file:uart.h
* @brief:config uart & Send Byte
* @author:zx
* @date: 2016/5/28
* @version: V0.1
* @attention: UART1 only
*****************************/
#include "uart.h"
/**
* 功能:初始化串口1波特率 8位数据 一位停止位 无奇偶校验
* 参数:波特率预设值
* 返回值:None
*/
void Uart_Config(u32 BaudRate)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
/*********************GPIO Config***************************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //发送管脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //接收管脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOA,&GPIO_InitStructure);
/********************UART Config*****************************/
USART_InitStructure.USART_BaudRate = 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_Rx | USART_Mode_Tx; //发送接收均使能
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE); //使能串口外设
}
/**
* 功能:串口发送单个字节
* 参数:字节内容
* 返回值:None
*/
void UART_SendData(char byte)
{
USART_SendData(USART1, byte);
while(!USART_GetFlagStatus(USART1,USART_FLAG_TC));
}
/**
* 功能:串口发送字符串
* 参数:字符串指针
* 返回值:None
*/
void UART_SendStr(char * str)
{
while(*str)
{
UART_SendData(*str++);
}
}
[/mw_shl_code]
[mw_shl_code=c,true]#include "Delay.h"
#include "uart.h"
int main(void)
{
s8 times = -6;
SysTick_Init();
Uart_Config(9600);
for(;;)
{
UART_SendStr("hello STM32!\r\n");
Delay_ms(800);
}
}
[/mw_shl_code]
|
|