STM 32--3つのシリアルポートを同時に適用するアプリケーションコード

17945 ワード

uart.c
#include "uart.h"    

int fputc(int ch,FILE *p)       //   printf              
{    
    USART_SendData(USART2,(u8)ch);      
    while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);    
    return ch;    

}    
/*******************************************************************************  
*               : uart_init  
*              : IO     1,           A9,A10    
*                :    
*                :    
*******************************************************************************/    
void uart1_init(u32 bt)    
{    
    GPIO_InitTypeDef GPIO_InitStructure;    //         ,     GPIO    
    NVIC_InitTypeDef NVIC_InitStructure;     //           
    USART_InitTypeDef  USART_InitStructure;   //           

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1|RCC_APB2Periph_AFIO,ENABLE);    

    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX    
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;    
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;    
    GPIO_Init(GPIOA,&GPIO_InitStructure);    
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX    
    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;    
    GPIO_Init(GPIOA,&GPIO_InitStructure);    


    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);     
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;     
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;     
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;     
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;     
    NVIC_Init(&NVIC_InitStructure);    


    USART_InitStructure.USART_BaudRate=bt;   //      bt    
    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;    
    USART_Init(USART1,&USART_InitStructure);    
    USART_Cmd(USART1, ENABLE);    
    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//         USART           
    USART_ClearFlag(USART1,USART_FLAG_TC);//  USARTx            
}    

/*******************************************************************************  
*               : uart2_init  
*              : IO     2,            A2,A3   
*                :    
*                :    
*******************************************************************************/    
void uart2_init(u32 bt)    
{    
     USART_InitTypeDef USART_InitStructure;    
  NVIC_InitTypeDef NVIC_InitStructure;     
    GPIO_InitTypeDef GPIO_InitStructure;    //         ,     GPIO    
   //     RCC      
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //  UART3  GPIOB       
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);    

   //     GPIO       
   // Configure USART2 Rx (PB.11) as input floating      
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;    
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;    
   GPIO_Init(GPIOA, &GPIO_InitStructure);    

   // Configure USART2 Tx (PB.10) as alternate function push-pull    
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;    
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;    
   GPIO_Init(GPIOA, &GPIO_InitStructure);    

   //        
   USART_InitStructure.USART_BaudRate = bt;    
   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;    


   // Configure USART3     
   USART_Init(USART2, &USART_InitStructure);//    3    

  // Enable USART1 Receive interrupts             
   USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);    
   //                  
   //USART_ITConfig(USART2, USART_IT_TXE, ENABLE);    

   // Enable the USART3     
   USART_Cmd(USART2, ENABLE);//    3    

   //          
   //Configure the NVIC Preemption Priority Bits       
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);    

   // Enable the USART3 Interrupt     
   NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;    
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;    
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;    
   NVIC_Init(&NVIC_InitStructure);    
}    


/*******************************************************************************  
*               : uart3_init  
*              : IO     3,          B10,B11     
*                :    
*                :    
*******************************************************************************/    
void uart3_init(u32 bt)    
{    
    USART_InitTypeDef USART_InitStructure;    
  NVIC_InitTypeDef NVIC_InitStructure;     
    GPIO_InitTypeDef GPIO_InitStructure;    //         ,     GPIO    
   //     RCC      
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); //  UART3  GPIOB       
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);    

   //     GPIO       
   // Configure USART2 Rx (PB.11) as input floating      
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;    
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;    
   GPIO_Init(GPIOB, &GPIO_InitStructure);    

   // Configure USART2 Tx (PB.10) as alternate function push-pull    
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;    
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;    
   GPIO_Init(GPIOB, &GPIO_InitStructure);    

   //        
   USART_InitStructure.USART_BaudRate = bt;    
   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;    


   // Configure USART3     
   USART_Init(USART3, &USART_InitStructure);//    3    

  // Enable USART1 Receive interrupts             
   USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);    
   //                  
   //USART_ITConfig(USART2, USART_IT_TXE, ENABLE);    

   // Enable the USART3     
   USART_Cmd(USART3, ENABLE);//    3    

   //          
   //Configure the NVIC Preemption Priority Bits       
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);    

   // Enable the USART3 Interrupt     
   NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;    
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;    
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;    
   NVIC_Init(&NVIC_InitStructure);    


}    

/*******************************************************************************  
*               : uart1_send_char  
*              :   1             
*                :    
*                :    
*******************************************************************************/    
void uart1_send_char(u8 temp)      
{     
    USART_SendData(USART1,(u8)temp);        
    while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);          
}    

/*******************************************************************************  
*               : uart1_send_buff  
*              :   1             
*                :    
*                :    
*******************************************************************************/    
void uart1_send_buff(u8 buf[],u32 len)         
{    
    u32 i;    
    for(i=0;i/*******************************************************************************  
*               : uart2_send_char  
*              :   2             
*                :    
*                :    
*******************************************************************************/    
void uart2_send_char(u8 temp)      
{        
    USART_SendData(USART2,(u8)temp);        
    while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);         
}    



/*******************************************************************************  
*               : uart2_send_buff  
*              :   2             
*                :    
*                :    
*******************************************************************************/    
void uart2_send_buff(u8 buf[],u32 len)     
{    
    u32 i;    
    for(i=0;i//--------------------------------------------------------------------------------------------------------    


/*******************************************************************************  
*               : uart3_send_char  
*              :   3             
*                :    
*                :    
*******************************************************************************/    
void uart3_send_char(u8 temp)         
{    
    USART_SendData(USART3,(u8)temp);        
    while(USART_GetFlagStatus(USART3,USART_FLAG_TXE)==RESET);         
}    



/*******************************************************************************  
*               : uart3_send_buff  
*              :   3             
*                :    
*                :    
*******************************************************************************/    
void uart3_send_buff(u8 buf[],u32 len)    
{    
    u32 i;    
    for(i=0;i
uart.h
#ifndef _UART_H_  
#define _UART_H_  

#include "stm32f10x.h"  
#include "stdio.h"  


/******************************************************************************* 
*               : fputc 
*              : printf    
*                :   
*                :   
*******************************************************************************/  
int fputc(int ch,FILE *p);  

/******************************************************************************* 
*               : uart1_init 
*              : IO     1,              
*                :   
*                :   
*******************************************************************************/  
void uart1_init(u32 bt);  


/******************************************************************************* 
*               : uart1_send_char 
*              :   1            
*                :   
*                :   
*******************************************************************************/  
void uart1_send_char(u8 temp);  


/******************************************************************************* 
*               : uart1_send_buff 
*              :   1            
*                :   
*                :   
*******************************************************************************/  
void uart1_send_buff(u8 buf[],u32 len);  

//-----------------------------------------------------------------------------------------------------  

/******************************************************************************* 
*               : uart2_init 
*              : IO     2,              
*                :   
*                :   
*******************************************************************************/  
void uart2_init(u32 bt);  



/******************************************************************************* 
*               : uart2_send_char 
*              :   2            
*                :   
*                :   
*******************************************************************************/  
void uart2_send_char(u8 temp);  



/******************************************************************************* 
*               : uart2_send_buff 
*              :   2            
*                :   
*                :   
*******************************************************************************/  
void uart2_send_buff(u8 buf[],u32 len);  


//--------------------------------------------------------------------------------------------------------  

/******************************************************************************* 
*               : uart3_init 
*              : IO     3,              
*                :   
*                :   
*******************************************************************************/  
void uart3_init(u32 bt);  



/******************************************************************************* 
*               : uart3_send_char 
*              :   3            
*                :   
*                :   
*******************************************************************************/  
void uart3_send_char(u8 temp);  



/******************************************************************************* 
*               : uart3_send_buff 
*              :   3            
*                :   
*                :   
*******************************************************************************/  
void uart3_send_buff(u8 buf[],u32 len);  
タンデム割込み処理関数
void USART1_IRQHandler(void)  
{  
    USART_ClearFlag(USART1,USART_FLAG_TC);  
    if(USART_GetITStatus(USART1,USART_IT_RXNE)!=Bit_RESET)//     USART        
    {    
        u8  k=USART_ReceiveData(USART1);  
        uart1_send_char(k+1);  
    }  
}  

void USART2_IRQHandler(void)  
{  
    USART_ClearFlag(USART2,USART_FLAG_TC);  
    if(USART_GetITStatus(USART2,USART_IT_RXNE)!=Bit_RESET)//     USART        
    {    
        u8  k=USART_ReceiveData(USART2);      
        uart1_send_char(k+1);  
    }    
}  

void USART3_IRQHandler(void)  
{  
    USART_ClearFlag(USART3,USART_FLAG_TC);  
    if(USART_GetITStatus(USART3,USART_IT_RXNE)!=Bit_RESET)//     USART        
    {    
        u8  k=USART_ReceiveData(USART3);  
        uart1_send_char(k+1);    
    }    
}