STM32-USART
来源:互联网 发布:网络整合营销理论4i 编辑:程序博客网 时间:2024/05/17 00:05
#include "stm32f10x.h"
#include "stdio.h" //标准IO库函数
#include "Usart.h"
void Usart_Configurtion(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX IO口配置
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;//RX IO口配置
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate=115200;//波特率115200,直接配置多少就是多少,系统会根据时钟自动调整
USART_InitStructure.USART_WordLength=USART_WordLength_8b;//数据位
USART_InitStructure.USART_StopBits=USART_StopBits_1;//停止位
USART_InitStructure.USART_Parity=USART_Parity_No;//奇偶校验位
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//接收中断使能
USART_Cmd(USART1,ENABLE);//使能USART1
}
void Usart_NVIC_Configuration(void) //配置串口中断函数
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USARTz Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Usart_Init(void) //USART配置
{
Usart_NVIC_Configuration();//中断配置
Usart_Configurtion();//USART_IO&工作模式配置
}
{
USART_SendData(USART1, (unsigned char) ch);// USART1 ???? USART2 ?
while (!(USART1->SR & USART_FLAG_TXE));
return (ch);
#include "stdio.h" //标准IO库函数
#include "Usart.h"
void Usart_Configurtion(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
//开启GPIOA & USART1时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX IO口配置
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;//RX IO口配置
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate=115200;//波特率115200,直接配置多少就是多少,系统会根据时钟自动调整
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_ITConfig(USART1,USART_IT_RXNE,ENABLE);//接收中断使能
USART_Cmd(USART1,ENABLE);//使能USART1
}
void Usart_NVIC_Configuration(void) //配置串口中断函数
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USARTz Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Usart_Init(void) //USART配置
{
Usart_NVIC_Configuration();//中断配置
Usart_Configurtion();//USART_IO&工作模式配置
}
//标准库函数重定向发送数据,重定向后即可使用printf();直接发送数据到USART1
//*注:在target中勾选MicroLib
int fputc(int ch, FILE *f){
USART_SendData(USART1, (unsigned char) ch);// USART1 ???? USART2 ?
while (!(USART1->SR & USART_FLAG_TXE));
return (ch);
}
//发送数据函数
void Uart_Senddata(u8 data)
{
while(USART_GetBitState(USART1, USART_FLAG_TBE) == RESET);//确保数据全部发送后再执行下次发送
USART_DataSend(USART1,data);
}
阅读全文
0 0
- STM32 USART
- STM32 USART
- STM32-USART
- STM32 -USART中断方式
- STM32之USART(one)
- STM32之USART
- STM32之USART
- STM32之USART学习
- STM32 DMA USART ADC
- STM32 USART的使用
- STM32 -USART中断方式
- STM32之USART学习
- STM32 USART 一些问题
- STM32 USART DMA
- stm32 USART 学习笔记!
- STM32之USART配置
- stm32之USART
- STM32的USART
- transform
- java--集合-Map
- CentOS 7升级python
- 2017年,我们为什么要学习C++?
- Java并发编程:volatile关键字解析
- STM32-USART
- json与jsonp之间的区别
- Context.LAYOUT_INFLATER_SERVICE的使用
- gcc -m32选项
- 一维数组,二维数组,三维数组名的含义
- 华硕B250主板安装Ubuntu16.04+gtx1050Ti显卡驱动+cuda8.0+cudnn5.1+opencv3.1+caffe+tensorflow
- Array 的copyWithin() 方法
- linux下freetds+unixodbc 连接sql server
- LVS原理与使用(5)