您的位置:首页 > 其它

STM32F103程序设计-8-USB转TTL串口(printf)

2017-02-28 11:13 357 查看
USB转TTL串口(printf)

单片机的串口可以转为TTL电平,可以转232,可以转485。本篇讲的是通过CH340G转TTL电平与PC的USB通信。单片机串口发送数据到电脑的USB,printf作用:做项目时,单片机通过串口往电脑发数据。程序的修改分为四步,具体如下。

Step1:初始化串口

void USART1_Init(void)

{

  /* USARTx configured as follow:

        - BaudRate = 115200 baud  

        - Word Length = 8 Bits

        - One Stop Bit

        - No parity

        - Hardware flow control disabled (RTS and CTS signals)

        - Receive and transmit enabled

  */

  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;

/* Enable GPIO clock */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

/* Enable UART clock */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

/* Configure USART Tx as alternate function push-pull */

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

 

  /* Configure USART Rx as input floating */

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

 

  /* USART configuration */

  USART_Init(USART1, &USART_InitStructure);

    

  /* Enable USART */

  USART_Cmd(USART1, ENABLE);

注意两点:1.一些宏定义的替换2.增加stm32f10x_usart.c文件

Step2:引用头文件stdio.h

Step3:定义PUTCHAR_PROTOTYPE

Step4:勾选MicroLIB

By:霜月孤鸟

2017.2.28

CSDN博客地址:http://blog.csdn.net/ourrtems

欢迎访问、关注单片机百宝箱!

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  stm32f103 程序设计