第四个实验 串口 查询实验

来源:互联网 发布:平板软件下载 编辑:程序博客网 时间:2024/05/16 14:30
#include "stm32f10x.h"
#include <stdio.h>
int main(void)
{
    GPIO_InitTypeDef GPIO_InitStructure; 
    USART_InitTypeDef USART_InitStructure;  //定义串口初始化结构体
    RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA,ENABLE);
    /*USART1_TX ->PA9*/   
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;        //选中串口默认输出管脚  
    //复用推挽输出模式 
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //定义输出最大速率
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//定义管脚9的模式 
    GPIO_Init(GPIOA , &GPIO_InitStructure);           //调用函数,把结构体参数输入进行初始化    
    /*USART1_RX ->PA10*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;     
  //浮空输入模式
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA , &GPIO_InitStructure);
    /*串口通讯参数设置*/
    USART_InitStructure.USART_BaudRate = 9600; //波特率
    USART_InitStructure.USART_WordLength = USART_WordLength_8b; //数据位8位
    USART_InitStructure.USART_StopBits = USART_StopBits_1; //停止位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_ClearFlag(USART1,USART_FLAG_TC);
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  USART_ITConfig(USART1, USART_IT_TXE, ENABLE);  
    USART_Cmd(USART1, ENABLE);
  
  printf("*************************************************************\r\n");
    printf("*                                                           *\r\n");
    printf("* Thank you for using The Development Board Of YuanDi ! ^_^ *\r\n");
    printf("*                                                           *\r\n");
    printf("*************************************************************\r\n");
    while (1)
    {
   //检查指定的串口标志设置与否
      while (USART_GetFlagStatus(USART1,USART_FLAG_RXNE) == RESET);
   //通过外设US ARTx发送单个数据
      USART_SendData(USART1,USART_ReceiveData(USART1));
    }
}
 
int  fputc(int ch, FILE *f)
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
  USART_SendData(USART1, (uint8_t) ch);
  /* Loop until the end of transmission */
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
  {}
  return ch;
}