STM32f103 dual serial port configuration and interrupt

Publisher:Meiren520Latest update time:2017-10-02 Source: eefocusKeywords:STM32f103 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

//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);
    //}
}


Keywords:STM32f103 Reference address:STM32f103 dual serial port configuration and interrupt

Previous article:STM32f103 watchdog usage
Next article:STM32f103 timer configuration and interrupt processing function

Recommended ReadingLatest update time:2024-11-16 13:28

STM32F103 test SD card serial port write test
1. Hardware pin introduction: This test uses SPI mode to read and write SD cards, and the relevant pin configurations are as follows: Chip select: SD_CS- PB13, corresponding to pin 1 of SD card, low level is effective Clock: SPI1_SCK- PA5, corresponding to pin 5 of the SD card  Master input slave output: MISO- PA6,
[Microcontroller]
STM32F103 PWM configuration
After working, I found that PWM is used in many places. Different voltage outputs are often required in industrial control, which corresponds to changing the hardware circuit and using software to reduce its workload to achieve the same purpose. A typical example of a breathing light. Using PWM output to adjust the vo
[Microcontroller]
stm32f103 RTC periodic standby wake-up (I)
I have been working on a low-power device for several days, but the program has been stuck in one place (see the picture below). Today I finally found out where the problem lies. I will summarize the problems of standby wakeup (only for the problems I encountered, the other parts are available on the Internet, based o
[Microcontroller]
stm32f103 RTC periodic standby wake-up (I)
Measuring battery voltage based on STM32F103 internal AD
STM32 ADC introduction:        STM32 has 1~3 ADCs (STM32F101/102 series has only 1 ADC), which can be used independently or in dual mode (increasing the sampling rate). The ADC of STM32 is a 12-bit successive approximation analog-to-digital converter. It has 18 channels and can measure 16 external and 2 internal signa
[Microcontroller]
Measuring battery voltage based on STM32F103 internal AD
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号