找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 10762|回复: 14
收起左侧

STM32F030C8T6串口例程

  [复制链接]
ID:254507 发表于 2017-11-28 09:44 | 显示全部楼层 |阅读模式
    现在提供STM32F030C8T6串口例程,适合初学上手
单片机源程序如下:
  1. /**
  2.   ******************************************************************************
  3.   * @file    USART/USART_Printf/main.c
  4.   * @author  MCD Application Team
  5.   * @version V1.4.0
  6.   * @date    24-July-2014
  7.   * @brief   Main program body
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2>
  12.   *
  13.   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14.   * You may not use this file except in compliance with the License.
  15.   * You may obtain a copy of the License at:
  16.   *
  17.   *        http://www.st.com/software_license_agreement_liberty_v2
  18.   *
  19.   * Unless required by applicable law or agreed to in writing, software
  20.   * distributed under the License is distributed on an "AS IS" BASIS,
  21.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22.   * See the License for the specific language governing permissions and
  23.   * limitations under the License.
  24.   *
  25.   ******************************************************************************
  26.   */

  27. /* Includes ------------------------------------------------------------------*/
  28. #include "main.h"

  29. /** @addtogroup STM32F0xx_StdPeriph_Examples
  30.   * @{
  31.   */

  32. /** @addtogroup USART_Printf
  33.   * @{
  34.   */

  35. /* Private typedef -----------------------------------------------------------*/
  36. /* Private define ------------------------------------------------------------*/
  37. /* Private macro -------------------------------------------------------------*/
  38. /* Private variables ---------------------------------------------------------*/
  39. /* Private function prototypes -----------------------------------------------*/
  40. void USART_Config(void);


  41.   // 重定义  
  42. int fputc(int ch, FILE *f)
  43. {
  44.         while (!(USART1->ISR & USART_FLAG_TXE));
  45.         USART1->TDR = ch;
  46.   
  47.   return (ch);
  48. }

  49. void USART1_Send_Char(unsigned char c)
  50. {
  51.         while(!((USART1->ISR)&USART_FLAG_TXE));
  52.         USART1->TDR=c;       
  53. }
  54. void USART1_Send_String(char *s )
  55. {
  56.         while (*s)
  57.         USART1_Send_Char(*s++);
  58. }

  59. void SYSTICK_INIT(void)
  60. {
  61.                 SysTick_Config(SystemCoreClock / 1000);        //Set SysTick Timer for 1ms interrupts
  62.                 //SysTick_Config(SystemCoreClock / 200);        //Set SysTick Timer for 5ms interrupts
  63.                 //SysTick_Config(SystemCoreClock / 100);        //Set SysTick Timer for 10ms interrupts
  64.                 //SysTick_Config(SystemCoreClock / 10);        //Set SysTick Timer for 100ms interrupts     
  65. }


  66. /**
  67.   * @brief  Main program
  68.   * @param  None
  69.   * @retval None
  70.   */
  71. int main(void)
  72. {
  73.   /*!< At this stage the microcontroller clock setting is already configured,
  74.        this is done through SystemInit() function which is called from startup
  75.        file (startup_stm32f0xx.s) before to branch to application main.
  76.        To reconfigure the default setting of SystemInit() function, refer to
  77.        system_stm32f0xx.c file
  78.      */     
  79.        
  80.         //        SYSTICK_INIT();
  81.                

  82.                 /* USART configuration */  
  83.                 USART_Config();
  84.                 USART1_Send_String("Fresh Persimmon all right reserved!\r\n");
  85.                 printf("USART Printf Example: retarget the C library printf function to the USART\r\n");

  86.           printf("ASD=%.3f\r\n",9.88987);
  87.                
  88.                 while (1)
  89.                 {
  90.                        
  91.                 }
  92. }

  93. /**
  94.   * @brief Configure the USART Device
  95.   * @param  None
  96.   * @retval None
  97.   */
  98. void USART_Config(void)
  99. {
  100.     GPIO_InitTypeDef GPIO_InitStructure;  
  101.                 USART_InitTypeDef USART_InitStructure;
  102.                 NVIC_InitTypeDef         NVIC_InitStructure;
  103.        
  104.                 NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  105.                 NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
  106.                 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  107.                 NVIC_Init(&NVIC_InitStructure);
  108.        
  109.                 RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
  110.                 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );                                               
  111.                 GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
  112.                 GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);                                                                                              
  113.                 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;                 
  114.                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  115.                 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  116.                 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  117.                 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  118.                 GPIO_Init(GPIOA, &GPIO_InitStructure);   

  119.                 USART_InitStructure.USART_BaudRate = 115200;
  120.                 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  121.                 USART_InitStructure.USART_StopBits = USART_StopBits_1;
  122.                 USART_InitStructure.USART_Parity = USART_Parity_No;
  123.                 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  124.                 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  125.                 USART_Init(USART1, &USART_InitStructure);
  126.                
  127.                 USART_Cmd(USART1, ENABLE);
  128.                 USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
  129.   

  130. }


  131. #ifdef  USE_FULL_ASSERT

  132. /**
  133.   * @brief  Reports the name of the source file and the source line number
  134. ……………………

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

所有资料51hei提供下载:
STM32F030C8T6串口例程.zip (487.8 KB, 下载次数: 541)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:368498 发表于 2018-7-10 10:33 | 显示全部楼层
感谢分享!!!!
回复

使用道具 举报

ID:369278 发表于 2018-7-19 15:48 | 显示全部楼层
下载了,不错,谢谢!!
回复

使用道具 举报

ID:388109 发表于 2018-10-17 18:21 | 显示全部楼层
下载,好东西
回复

使用道具 举报

ID:380361 发表于 2019-9-26 14:54 | 显示全部楼层

感谢分享!!!!
回复

使用道具 举报

ID:616902 发表于 2019-9-26 21:15 | 显示全部楼层
正在找这个呢
回复

使用道具 举报

ID:185372 发表于 2019-9-27 08:23 | 显示全部楼层
这个可以有,不过想屏蔽一下串口呢。
回复

使用道具 举报

ID:622200 发表于 2019-10-11 11:28 | 显示全部楼层
有空下来看看
回复

使用道具 举报

ID:21828 发表于 2019-12-30 10:02 | 显示全部楼层
感谢分享!!!!
回复

使用道具 举报

ID:61493 发表于 2019-12-30 15:33 | 显示全部楼层
谢谢分享,不错的东西
回复

使用道具 举报

ID:469589 发表于 2020-1-19 11:52 | 显示全部楼层
感谢分享,学习了!
回复

使用道具 举报

ID:568247 发表于 2021-6-10 17:38 | 显示全部楼层
感谢楼主的分享,部分疑惑解答了
回复

使用道具 举报

ID:1029891 发表于 2022-5-30 15:47 | 显示全部楼层
好用吗?
回复

使用道具 举报

ID:1031499 发表于 2022-6-1 12:03 | 显示全部楼层
赞,正在找串口程序。
回复

使用道具 举报

ID:1060439 发表于 2023-1-6 13:38 | 显示全部楼层
刚下载了,不错,正在学习中
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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