最近在学单片机的串口,首先从串口发送接收开始吧,串口发送比较简单,下面讲讲串口接收字符串的情况。
单片机型号用的是 stm32f103rct6,IDE版本是keil 5。本代码是操作寄存器实现串口接收字符串。代码如下:
usart.h
#ifndef USTART_H
#define USTART_H
#include <stm32f10x.h>
#include <stm32f10x_usart.h>
#include "sys.h"
void USART1_Init();
void USART_SendString(USART_TypeDef* USARTx, char *DataString);
#endif
usart.c
#include "usart.h"
#include <string.h>
#include "stdio.h" //ucos 使用
#if 1
#pragma import(__use_no_semihosting)
//标准库需要的支持函数
struct __FILE
{
int handle;
};
FILE __stdout;
//定义_sys_exit()以避免使用半主机模式
void _sys_exit(int x)
{
x = x;
}
//重定义fputc函数
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
USART1->DR = (u8) ch;
return ch;
}
#endif
void USART1_Init()
{