//The code only configures the serial port and the corresponding interrupt processing function. The specific functions need to be defined by yourself
//IAR 7.1
#include "stm32_uart.h"
uint8_t card_ture=0;
uint16_t add_count=0;
uint8_t Key_Data[1]={0};
uint8_t Media_Flag=0;
uint8_t card_code[4]; //[5:1]save the card SN, [0]valid or invalid
uint8_t Temp_Card_Num[4];
uint8_t cardcode_rx_complete = 0;
uint32_t uart_config_record_map[4] = {
0,
UART_CONFIG_UART_MEMORY_ADDR,
FIRMWARE_VERSION_MEMORY_ADDR,
SCHEME_SPECIFIC_MEMORY_ADDR
};
UartConfig_TypeDef the_uart_config;
void stm32_u art_init(uint8_t uart_port, uint32_t BaudRate)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/*Fill the structure of uart init*/
if(uart_port == UART2)
{
/*Fill the structure of uart init*/
USART_InitStructure.USART_BaudRate = BaudRate;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure. USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/*Enable priph clock*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
/*Config gpio as uart pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO _Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*Config gpio as uart pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*Init uart*/
USART_Init(USART2, &USART_InitStructure);
/*Enable uart*/
/*Clear flags*/
USART_ClearFlag(USART2, USART_FLAG_TC);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure .NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
else if(uart_port == UART1)
{
/*Enable priph clock*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
/ *Fill the structure of uart init*/
USART_InitStructure.USART_BaudRate = BaudRate;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
if(the_uart_config.checksum==Parity_No)
{
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //USART_WordLength_8b
USART_InitStructure.USART_Parity = USART_Parity_No; //USART_Parity_No
}
else if(the_uart_config.checksum==Parity_Odd)
{
USART_InitStructure.USART_WordLength = USART_WordLength_9b; //USART_WordLength_9b
USART_InitStructure.USART_Parity = USART_Parity_Odd; //USART_Parity_Odd
}
else if(the_uart_config. checksum==Parity_Even)
{
USART_InitStructure. USART_WordLength = USART_WordLength_9b; //USART_WordLength_9b
USART_InitStructure.USART_Parity = USART_Parity_Even; //USART_Parity_Even
}
else
{
}
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/*Config gpio as uart pin */
GPIO_InitStructure.GPIO_Pin = UART1_TX;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStru cture);
/*Init uart*/
USART_Init(USART1, &USART_InitStructure) ;
/*Enable uart*/
//USART_Cmd(USART1, ENABLE);
/*Clear flags*/
USART_ClearFlag(USART1, USART_FLAG_TC);
#if 0
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //
USART_ITConfig (USART1, USART_IT_RXNE | USART_IT_TXE, ENABLE); #endif USART_Cmd
( USART1 , ENABLE ) ; } }
static void stm32_uart_senddatas(uint8_t uart_port, const uint8_t* data, uint16_t len)
{
uint16_t count;
if(uart_port == UART1)
{
for(count = 0; count < len; count++)
{
USART_SendData(USART1, data[count]);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
}
else if(uart_port == UART2)
{
for(count = 0; count < len; count++)
{
USART_SendData(USART2, data[count]);
while(USART_GetFlagStatus (USART2, USART_FLAG_TC) == RESET);
}
}
}
static uint8_t* uart1_tx_buffer;
static uint8_t uart1_tx_buffer_len;
static uint8_t uart1_tx_buffer_index = 0;
void stm32_uart_pro_poweron(ProComm_TypeDef comm)
{
USART_ClearFlag(USART1,USART_FLAG_TC); //Clear the flag or the first bit of data will be lost
switch(comm)
{
}
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
//USART_Cmd(USART1, ENABLE);
}
void stm32_uart2_senddata(uint8_t uart_port, const uint8_t* data, uint16_t len)
{
USART_ClearFlag(USART2,USART_FLAG_TC); //Clear the flag otherwise The first bit of data is lost
stm32_uart_senddatas(uart_port, data,len);
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
USART_ClearITPendingBit(USART1, USART_IT_TXE);
/* Write one byte to the transmit data register */
USART_SendData(USART1, uart1_tx_buffer[uart1_tx_buffer_index++]);
if(uart1_tx_buffer_index == uart1_tx_buffer_len)
{
/* Disable the USARTz Transmit interrupt */
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
uart1_tx_buffer_index = 0;
}
}
}
/*
void USART2_IRQHandler( void)
{
static uint8_t index = 1;
static uint8_t checksum = 0;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
card_code[index] = USART_ReceiveData(USART2);
if(index < 5)
{
checksum ^= card_code [index++];
}
else
{
if(checksum == card_code[5])
{
}
else
{
}
cardcode_rx_complete = 1;
checksum = 0;
}
}
}
*/
void USART2_IRQHandler(void)
{
uint8_t index=0;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2 , USART_IT_RXNE);
Key_Data[index] = USART_ReceiveData(USART2);
switch(Key_Data[0])
{
}
}
//if(USART_GetITStatus(USART2, USART_IT_TXE) != RESET)
//{
// USART_ClearITPendingBit(USART2, USART_IT_TXE);
//}
}
Previous article:STM32f103 watchdog usage
Next article:STM32f103 timer configuration and interrupt processing function
Recommended ReadingLatest update time:2024-11-16 13:28
- Popular Resources
- Popular amplifiers
- STM8 C language programming (1) - basic program and startup code analysis
- The STM32 MCU drives the BMP280 absolute pressure sensor program and has been debugged
- DigiKey \"Smart Manufacturing, Non-stop Happiness\" Creative Competition - Small Weather Station - STM32F103 Sensor Driver Code
- LwIP application development practical guide: based on STM32
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
- Overvoltage and overcurrent protection circuit of power supply
- Design of high-speed communication between digital signal processor and PC
- msp430f149 communicates with mobile phone via Bluetooth HC-05
- Inventory of brain-burning remedial measures after PCB back drilling leakage
- TI can solve common problems of high-precision ultrasonic flow measurement at low flow rates
- c2000 I2C read register frame format reference routine
- Javascript Smart Watch
- Introduction to Zigbee communication technology transmission distance and wall penetration capability
- HyperLynx High-Speed Circuit Design and Simulation (XI) Quantitative Analysis of Signal Overdrive and Underdrive
- TI seems to have a DCDC switching power supply verification platform, which is controlled by DSP and can achieve voltage boost or voltage reduction by controlling different NMOS.