找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5085|回复: 0
打印 上一主题 下一主题
收起左侧

STM32 串口1打印例程

[复制链接]
跳转到指定楼层
楼主
STM32 串口1打印例程

单片机源程序如下:
  1. /*-------------------------------------------------------------------------------
  2. 文件名称:main.c
  3. 文件描述:通过串口1,使用printf函数打印信息,编译时需勾选Use MicroLIB
  4. 硬件平台:尼莫M3S开发板
  5. 备    注:通过简单修改可以移植到其他开发板,部分资料来源于网络。
  6. ---------------------------------------------------------------------------------*/
  7. #include <stdio.h>
  8. #include "stm32f10x.h"
  9. #include "led.h"
  10. #include "delay.h"
  11. #include "key.h"
  12. #include "timer.h"
  13. #include "beep.h"



  14. #ifdef __GNUC__
  15.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  16.      set to 'Yes') calls __io_putchar() */
  17.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  18. #else
  19.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  20. #endif /* __GNUC__ */

  21. int main(void)
  22. {
  23.         GPIO_InitTypeDef GPIO_InitStructure;
  24.         USART_InitTypeDef USART_InitStructure;

  25.          
  26.           RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);        //使能USART1,GPIOA时钟
  27.            USART_DeInit(USART1);  //复位串口1
  28.           //USART1_TX   PA.9
  29.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
  30.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  31.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;        //复用推挽输出
  32.     GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA9
  33.    
  34.     //USART1_RX          PA.10
  35.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  36.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
  37.     GPIO_Init(GPIOA, &GPIO_InitStructure);  //初始化PA10     
  38.   /* USARTx configured as follow:
  39.         - BaudRate = 9600 baud  波特率
  40.         - Word Length = 8 Bits  数据长度
  41.         - One Stop Bit          停止位
  42.         - No parity             校验方式
  43.         - Hardware flow control disabled (RTS and CTS signals) 硬件控制流
  44.         - Receive and transmit enabled                         使能发送和接收
  45.   */
  46.   USART_InitStructure.USART_BaudRate = 9600;
  47.   USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  48.   USART_InitStructure.USART_StopBits = USART_StopBits_1;
  49.   USART_InitStructure.USART_Parity = USART_Parity_No;
  50.   USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  51.   USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  52.   USART_Init(USART1, &USART_InitStructure);
  53.   USART_Cmd(USART1, ENABLE);                    //使能串口

  54.         LED_Init();//LED初始化
  55.   KEY_Init();//按键初始化
  56.   SysTick_Init();//延时初始化
  57.         BEEP_Init();   //蜂鸣器初始化
  58.   while (1)
  59.   {
  60.         //使用printf函数循环发送固定信息
  61.   printf("\n\rUSART Printf Example: (德飞莱)尼莫M3S开发板串口测试程序\r");
  62.         Delay_ms(500);                  
  63.   LED2_REV;        
  64.   }
  65. }


  66. PUTCHAR_PROTOTYPE
  67. {
  68.   /* Place your implementation of fputc here */
  69. ……………………

  70. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
Keil代码.7z (178.77 KB, 下载次数: 38)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表