1./* Configure USART1 Tx (PA.09) as alternate function push-pull */
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);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
2.void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //响应式中断优先级设置为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //使能中断
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
3.RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA,, ENABLE );
4.USART.h
#ifndef __USART_H
#define __USART_H
extern void USART1_SendByte(u16 dat);
extern void USART1Write(u8* data,u16 len);
void USART1_Configuration(void);
#define DIR485_H GPIOC->BSRR=1<<1
#define DIR485_L GPIOC->BRR=1<<1
#endif
USART.c
#include "stm32f10x.h"
#include "USART.h"
void USART1_SendByte(u16 Data)
{
while (!(USART1->SR & USART_FLAG_TXE));
USART1->DR = (Data & (uint16_t)0x01FF);
}
void USART1Write(u8* data,u16 len)
{
u16 i;
// DIR485_H ;
for (i=0; i<len; i++){
USART1_SendByte(data[i]);
}
// Delay10us(1000);
// DIR485_L;
}
void USART1_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 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_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
/* DIR485_L; */
}
void USART1_IRQHandler(void)
{
u8 c;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
c=USART1->DR;
USART1_SendByte(c);
}
}
-------------------------------------------------------------
使用printf的方法:
1.use microLIB
2.#include <stdio.h>
int fputc(int ch, FILE *f)
{
/* Write a character to the USART */
USART_SendData(USART1, (u8) ch);
/* Loop until the end of transmission */
while(!(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == SET))
{
}
return ch;
}
int fgetc(FILE *f)
{
/* Loop until received a char */
while(!(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET))
{
}
/* Read a character from the USART and RETURN */
return (USART_ReceiveData(USART1));
}
3. printf("hello world\n");
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
下面是usart程序终极版:(use microLIB)
#include "stm32f10x.h"
#include <stdio.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__ */
#define buffer_size 20
u8 buffer[buffer_size]="";
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
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);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
//系统中断管理
void NVIC_Configuration(void)
{
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}
//配置系统时钟,使能各外设时钟
void RCC_Configuration(void)
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA
| RCC_APB2Periph_AFIO , ENABLE );
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 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_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStructure);
/* Enable USART1 */
USART_Cmd(USART1, ENABLE);
}
void Delay(vu32 nCount)
{
for(; nCount != 0; nCount--);
}
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (u8) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
return ch;
}
u32 USART_Scanf()
{
u32 index = 0;
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
buffer[index] = (USART_ReceiveData(USART1));
while(buffer[index++]!='#')
{
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
buffer[index] = (USART_ReceiveData(USART1));
}
buffer[--index]='\0';
return ++index;
}
int main(void)
{
RCC_Configuration();
GPIO_Configuration();
NVIC_Configuration();
USART_Configuration();
while(1)
{
printf("Please input a string end with a '#'\n");
Delay(0xFFFFF);
if( USART_Scanf() > buffer_size )
{
printf("The string is too long\n");
}
else
printf("%s\n",buffer);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
stm32_usart.zip_STM32 scanf_stm32 usart
共1个文件
txt:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 55 浏览量
2022-09-22
23:09:12
上传
评论
收藏 2KB ZIP 举报
温馨提示
stm32_usart的源码程序,初始化以及printf和scanf的应用
资源推荐
资源详情
资源评论
收起资源包目录
stm32_usart.zip (1个子文件)
stm32_usart.txt 7KB
共 1 条
- 1
资源评论
APei
- 粉丝: 63
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功