你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

STM32 nucleo 之简单串口显示程序

[复制链接]
我是酱油哥 提问时间:2014-12-13 11:54 /
基于mdk的开发平台 今天初步学习了串口模块,于是就写了一简单的接受显示程序。主程序如下
  1. /**
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Date               : 13/12/2014 10:10:53
  5.   * Description        : Main program body
  6.   ******************************************************************************
  7.   *
  8.   * COPYRIGHT(c) 2014 STMicroelectronics
  9.   *
  10.   * Redistribution and use in source and binary forms, with or without modification,
  11.   * are permitted provided that the following conditions are met:
  12.   *   1. Redistributions of source code must retain the above copyright notice,
  13.   *      this list of conditions and the following disclaimer.
  14.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  15.   *      this list of conditions and the following disclaimer in the documentation
  16.   *      and/or other materials provided with the distribution.
  17.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  18.   *      may be used to endorse or promote products derived from this software
  19.   *      without specific prior written permission.
  20.   *
  21.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  24.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  25.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  27.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.   *
  32.   ******************************************************************************
  33.   */

  34. /* Includes ------------------------------------------------------------------*/
  35. #include "stm32f0xx_hal.h"

  36. /* USER CODE BEGIN Includes */

  37. /* USER CODE END Includes */

  38. /* Private variables ---------------------------------------------------------*/
  39. UART_HandleTypeDef huart2;

  40. /* USER CODE BEGIN PV */

  41. /* USER CODE END PV */

  42. /* Private function prototypes -----------------------------------------------*/
  43. void SystemClock_Config(void);
  44. //void putchar(uchar);
  45. static void MX_GPIO_Init(void);
  46. static void MX_USART2_UART_Init(void);
  47. #define uchar unsigned char
  48. #define uint unsigned int
  49. void delay(uint x)
  50. {
  51.         int z,y;
  52.         for(z=0;z<x;z++)
  53.         for(y=0;y<5000;y++);
  54. }

  55. /* USER CODE BEGIN PFP */

  56. /* USER CODE END PFP */

  57. /* USER CODE BEGIN 0 */

  58. /* USER CODE END 0 */

  59. void putchar(uchar data)
  60. {
  61.                 while(!(USART2->ISR&(1<<7)));
  62.                 USART2->TDR=data;
  63. }
  64. void puts(uchar *p)
  65. {
  66.         while(*p!=0)
  67.         {
  68.                 putchar(*p);
  69.                 p++;
  70.         }
  71.         putchar(0X0D);
  72.         putchar(0X0A);
  73. }
  74. uchar getchar(void)
  75. {
  76.         uchar data;
  77.         while(!(USART2->ISR&(1<<5)));
  78.                 data=USART2->RDR;
  79.         return data;
  80. }
  81. int main(void)
  82. {
  83.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  84.   HAL_Init();
  85.   /* Configure the system clock */
  86.   SystemClock_Config();
  87.   /* Initialize all configured peripherals */
  88.   MX_GPIO_Init();
  89.   MX_USART2_UART_Init();
  90.   while (1)
  91.   {
  92.                 switch(getchar())
  93.                 {
  94.                         case 0x00:puts("hello!");
  95.                                 break;
  96.                         case 0x01:puts("This is my first time");
  97.                                 break;
  98.                         case 0x02:puts("Learn stm32f072");
  99.                                 break;
  100.                         default:puts("Thank you !");
  101.                                 break;
  102.                 }
  103.   }
  104. }

  105. /** System Clock Configuration
  106. */
  107. void SystemClock_Config(void)
  108. {

  109.   RCC_OscInitTypeDef RCC_OscInitStruct;
  110.   RCC_ClkInitTypeDef RCC_ClkInitStruct;
  111.   RCC_PeriphCLKInitTypeDef PeriphClkInit;

  112.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  113.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  114.   RCC_OscInitStruct.HSICalibrationValue = 16;
  115.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  116.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  117.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  118.   RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV2;
  119.   HAL_RCC_OscConfig(&RCC_OscInitStruct);

  120.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
  121.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  122.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  123.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  124.   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1);

  125.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
  126.   PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  127.   HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);

  128.   __SYSCFG_CLK_ENABLE();

  129. }

  130. /* USART2 init function */
  131. void MX_USART2_UART_Init(void)
  132. {

  133.   huart2.Instance = USART2;  
  134.   huart2.Init.BaudRate = 9600;//²¨ÌØÂÊÉèÖÃΪ9600
  135.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  136.   huart2.Init.StopBits = UART_STOPBITS_1;
  137.   huart2.Init.Parity = UART_PARITY_NONE;
  138.   huart2.Init.Mode = UART_MODE_TX_RX;
  139.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  140.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  141.   huart2.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED ;
  142.   huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  143.   HAL_UART_Init(&huart2);

  144. }

  145. /** Configure pins as
  146.         * Analog
  147.         * Input
  148.         * Output
  149.         * EVENT_OUT
  150.         * EXTI
  151. */
  152. void MX_GPIO_Init(void)
  153. {

  154.   GPIO_InitTypeDef GPIO_InitStruct;

  155.   /* GPIO Ports Clock Enable */
  156.   __GPIOC_CLK_ENABLE();
  157.   __GPIOF_CLK_ENABLE();
  158.   __GPIOA_CLK_ENABLE();

  159.   /*Configure GPIO pin : PC13 */
  160.   GPIO_InitStruct.Pin = GPIO_PIN_13;
  161.   GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING;
  162.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  163.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

  164.   /*Configure GPIO pin : PA5 */
  165.   GPIO_InitStruct.Pin = GPIO_PIN_5;
  166.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  167.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  168.   GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  169.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  170. }

  171. /* USER CODE BEGIN 4 */

  172. /* USER CODE END 4 */

  173. #ifdef USE_FULL_ASSERT

  174. /**
  175.    * @brief Reports the name of the source file and the source line number
  176.    * where the assert_param error has occurred.
  177.    * @param file: pointer to the source file name
  178.    * @param line: assert_param error line source number
  179.    * @retval None
  180.    */
  181. void assert_failed(uint8_t* file, uint32_t line)
  182. {
  183.   /* USER CODE BEGIN 6 */
  184.   /* User can add his own implementation to report the file name and line number,
  185.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  186.   /* USER CODE END 6 */

  187. }

  188. #endif

  189. /**
  190.   * @}
  191.   */

  192. /**
  193.   * @}
  194. */

  195. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码
显示的结果如下: 4JD89JY`V(J(I%6GXYF}VX0.png
以后在方案开发中准备用串口去调试
收藏 1 评论12 发布时间:2014-12-13 11:54

举报

12个回答
沐紫 回答时间:2014-12-15 09:18:30
谢谢!
大秦正声 回答时间:2015-1-7 18:06:07
不错啊!
Small利 回答时间:2015-1-8 00:10:05
来点注释比较好啊
我是酱油哥 回答时间:2015-1-8 08:45:50

谢谢啊   
我是酱油哥 回答时间:2015-1-8 08:47:15
Small利 发表于 2015-1-8 00:10
来点注释比较好啊

cube建立的工程 后来就没用他的库函数,一直操作的是寄存器。寄存器比较的简单,所以你懂的。。。。
Small利 回答时间:2015-1-8 08:57:23
我是酱油哥 发表于 2015-1-8 08:47
cube建立的工程 后来就没用他的库函数,一直操作的是寄存器。寄存器比较的简单,所以你懂的。。。。 ...

还没用过cube呢,我也喜欢寄存器简单粗暴
我是酱油哥 回答时间:2015-1-8 08:58:11
Small利 发表于 2015-1-8 08:57
还没用过cube呢,我也喜欢寄存器简单粗暴

寄存器模式  简单 明了 而又直观
回答时间:2015-1-10 23:29:00
感谢~~~~~~~~~~~~~~~~
我是酱油哥 回答时间:2015-1-11 08:51:55
xnmc2013 发表于 2015-1-10 23:29
感谢~~~~~~~~~~~~~~~~

感谢评论  
黑溱郎 回答时间:2015-1-22 11:13:59
楼主写个如何用串口调试的教程吧。
wangweili1978 回答时间:2015-1-23 09:53:16
12614.png
木易-357428 回答时间:2015-2-1 22:02:04
谢谢!看看!

所属标签

相似问题

关于意法半导体
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
招聘信息
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
关注我们
st-img 微信公众号
st-img 手机版