stm 32は3つのシリアルポートを初期化する

11948 ワード

serial.c
 #include "serial.h"  
  
int fputc(int ch,FILE *p)       //   printf            
{  
    USART_SendData(USART2,(u8)ch);    
    while(USART_GetFlagStatus(USART2,USART_FLAG_TXE)==RESET);  
    return ch;  
      
}  
/******************************************************************************* 
*               : serial_init 
*              : IO     1,           A9,A10   
*                :   
*                :   
*******************************************************************************/  
void serial1_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          
}  
  
/******************************************************************************* 
*               : serial2_init 
*              : IO     2,            A2,A3  
*                :   
*                :   
*******************************************************************************/  
void serial2_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);  
}  

  
/******************************************************************************* 
*               : serial3_init 
*              : IO     3,          B10,B11    
*                :   
*                :   
*******************************************************************************/  
void serial3_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);  
      
      
}  
  
   
  
  
/******************************************************************************* 
*               : serial1_sned_char 
*              :   1            
*                :   
*                :   
*******************************************************************************/  
void serial1_sned_char(u8 temp)  
      
{  
      
      
    USART_SendData(USART1,(u8)temp);      
    while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);  
      
}  
  
/******************************************************************************* 
*               : serial1_sned_buff 
*              :   1            
*                :   
*                :   
*******************************************************************************/  
void serial1_sned_buff(u8 buf[],u32 len)  
      
{  
    u32 i;  
    for(i=0;i

serial.h
**************************************************/

#ifndef	_serial_
#define _serial_

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


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

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

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


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


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

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

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



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



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


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

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



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



/*******************************************************************************
*               : serial3_sned_buff
*     		   :   3      	   
*                :  
*                :  
*******************************************************************************/
void serial3_sned_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);
	serial1_sned_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);
		
	serial1_sned_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);
	serial1_sned_char(k+1);
	
	}
	
}