⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 STM8示例程序
💻 C
字号:
/**
  ******************************************************************************
  * @file UART2_Printf\main.c
  * @brief This file contains the main function for: retarget the C library printf function to the UART2 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 UART2_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:
  * - UART2_DeInit()
  * - UART2_Init()
	* - UART2_Cmd()
	* - CLK_HSIPrescalerConfig()
  */
void main(void)
{
    /*High speed internal clock prescaler: 1*/
    CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
    
    UART2_DeInit();
    /* UART2 configuration ------------------------------------------------------*/
    /* UART2 configured as follow:
          - BaudRate = 115200 baud  
          - Word Length = 8 Bits
          - One Stop Bit
          - No parity
          - Receive and transmit enabled
          - UART2 Clock disabled
    */
    /* Configure UART2 */
     UART2_Init((u32)9600, UART2_WORDLENGTH_8D, UART2_STOPBITS_1, UART2_PARITY_ODD, UART2_SYNCMODE_CLOCK_DISABLE, UART2_MODE_TXRX_ENABLE);

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

    while (1);
}

/**
  * @brief Retargets the C library printf function to the UART2.
  * @param[in] c Character to send
  * @retval char Character sent
  * @par Required preconditions:
  * - None
  * @par Library functions called:
  * - UART2_GetFlagStatus()
	* - UART2_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 (!UART2_GetFlagStatus(UART2_FLAG_TC));
        UART2_SendData8('\r');
        /* Wait transmission is completed */
        while (!UART2_GetFlagStatus(UART2_FLAG_TC));
    }

    /* put c to hardware here */
    /* Wait transmission is completed : otherwise the first data is not sent */
    while (!UART2_GetFlagStatus(UART2_FLAG_TC));
    UART2_SendData8(c);
    /* Wait transmission is completed */
    while (!UART2_GetFlagStatus(UART2_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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -