main.c

来自「STM8示例程序」· C语言 代码 · 共 132 行

C
132
字号
/**
  ******************************************************************************
  * @file UART3_Printf\main.c
  * @brief This file contains the main function for: retarget the C library printf function to the UART3 example.
  * @author STMicroelectronics - MCD Application Team
  * @version V1.0.1
  * @date 09/22/2008
  ******************************************************************************
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2008 STMicroelectronics</center></h2>
  * @image html logo.bmp
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm8s_lib.h"
#include "stdio.h"
/**
  * @addtogroup UART3_Printf
  * @{
  */
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
char putchar (char c);
/* Private functions ---------------------------------------------------------*/
/**
  * @brief Example firmware main entry point.
  * @par Parameters:
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Library functions called:
  * - UART3_DeInit()
  * - UART3_Init()
	* - UART3_Cmd()
  */
void main(void)
{
		UART3_DeInit();
    /* UART3 configuration ------------------------------------------------------*/
    /* UART3 configured as follow:
          - BaudRate = 115200 baud  
          - Word Length = 8 Bits
          - One Stop Bit
          - No parity
          - Receive and transmit enabled
    */
    /* Configure UART3 */
		UART3_Init((u32)115200, UART3_WORDLENGTH_8D, UART3_STOPBITS_1, UART3_PARITY_NO, UART3_MODE_TXRX_ENABLE);

    /* Output a message on Hyperterminal using printf function */
    printf("\n\rUART3 printf :retarget the C library printf function to the UART3 \n\r");

    while (1);
}

/**
  * @brief Retargets the C library printf function to the UART3.
  * @par Parameters:
  * - None
  * @retval char None
  * @par Required preconditions:
  * - None
  * @par Library functions called:
  * - UART3_GetFlagStatus()
	* - UART3_SendData8()
  */

char putchar (char c)
{
    if (c == '\n')
    {
        /* put '\r' to hardware here */
        /* Wait transmission is completed : otherwise the first data is not sent */
        while (!UART3_GetFlagStatus(UART3_FLAG_TC));
        UART3_SendData8('\r');
        /* Wait transmission is completed */
        while (!UART3_GetFlagStatus(UART3_FLAG_TC));
    }

    /* put c to hardware here */
    /* Wait transmission is completed : otherwise the first data is not sent */
    while (!UART3_GetFlagStatus(UART3_FLAG_TC));
    UART3_SendData8(c);
    /* Wait transmission is completed */
    while (!UART3_GetFlagStatus(UART3_FLAG_TC));


    return (c);
}

/**
  * @brief Reports the name of the source file and the source line number where
  * the assert error has occurred.
  * User can add his own implementation to report the file name and line number.
  * ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line)
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  */
#ifdef FULL_ASSERT
void assert_failed(u8 *file, u16 line)
#else
void assert_failed(void)
#endif
{
  /* Add your own code to manage an assert error */
  /* Infinite loop */
  while (1)
  {
  }
}

/**
  * @}
  */

/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?