#include "uart1.h"
#include "stm8l15x.h"
void Uart1_Init(void)
{
CLK_PeripheralClockConfig(CLK_Peripheral_USART1,ENABLE);
SYSCFG_REMAPDeInit();
SYSCFG_REMAPPinConfig(REMAP_Pin_USART1TxRxPortA,ENABLE);
GPIO_Init(GPIOA, GPIO_Pin_2, GPIO_Mode_Out_PP_High_Fast);//TXD
GPIO_Init(GPIOA, GPIO_Pin_3, GPIO_Mode_In_PU_No_IT);//RXD
USART_DeInit(USART1); // Duplicate UART1
/*
* Configure UART1 as:
* Baud rate = 115200
* Data bits = 8
* 1 stop bit
* No check digit
* Enable receiving and sending
*/
USART_Init(USART1,(u32)115200, USART_WordLength_8b, USART_StopBits_1,
USART_Parity_No, USART_Mode_Tx|USART_Mode_Rx);
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //Enable receive interrupt
//USART_ITConfig(USART1, USART_IT_TC, ENABLE); //Enable receive interrupt
USART_Cmd(USART1, ENABLE); //Enable UART2
}
/*******************************************************************************
* Name: UART1_SendByte
* Function: UART1 sends a byte
* Parameters: data -> bytes to send
* Return: None
* Description: None
******************************************************************************/
void UART1_SendByte(u8 data)
{
USART_SendData8(USART1, data);
/* Wait for the transfer to complete */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
/*******************************************************************************
* Name: UART1_SendStr
* Function: UART1 sends len characters
* Parameter: data -> points to the string to be sent
* len -> number of bytes to send
* Return: None
* Description: None
******************************************************************************/
void UART1_SendStr(u8 *str)
{
while(*str != '')
{
UART1_SendByte(*str++); /* Loop call to send a character function*/
}
}
/*******************************************************************************
* Name: UART2_ReceiveByte
* Function: UART2 receives a character
* Parameters: None
* Return: received characters
* Description: None
******************************************************************************/
u8 UART1_ReceiveByte(void)
{
u8 UART1_RX_BUF;
/* Wait for receiving to complete */
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
UART1_RX_BUF = USART_ReceiveData8(USART1);
return UART1_RX_BUF;
}
/*******************************************************************************
* Name: fputc
* Function: Redirect C library function printf to UART1
* Parameters: None
* Return: the character to be printed
* Description: Called by printf
******************************************************************************/
#ifdef _IAR_
int fputc(int ch, FILE *f)
{
/* Send Printf content to the serial port*/
UART1_SendByte(ch);
return (ch);
}
#else
PUTCHAR_PROTOTYPE
{
/* Write a character to the UART1 */
UART1_SendByte(c);
return (c);
}
#endif
GETCHAR_PROTOTYPE
{
#ifdef _COSMIC_
char c = 0;
#else
int c = 0;
#endif
/* Loop until the Read data register flag is SET */
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
c = USART_ReceiveData8(USART1);
return (c);
}
Previous article:Tutorial on using STM8L's own bootloader
Next article:STM8 ROP programming
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- What do you think of the official open source of Huawei's Ark Compiler?
- [New version of Zhongke Bluexun AB32VG1 RISC-V development board] - 5: Enhanced version of Blink
- Current Status and Future Development of Satellite Mobile Communications
- Intelligently Connected World—Application of Internet of Vehicles and Future Development of Digitalization
- Motor drive circuit design 1
- Free Review - Topmicro Intelligent Display Module (5) Touch Screen
- How to deal with the acidic wastewater from acid salt spray testing of metal electronic products and devices?
- Summary: annysky2012's practical journey of motor development based on "STM32F746ZG+IHM07M1"
- Smart home management based on ESP8266 WIFI network control and Gizwits Cloud
- Engineer, if you don't develop from the perspective of engineering or product, how can you be worthy of the word "engineering"...