STM 32制御(二)

12122 ワード

Usartインタフェース
RS 232通信
#ifndef __USART_H
#define __USART_H
#include 
#include 
#include 
#include "stdio.h"	
#include "sys.h" 


#define USART_n		USART1  //    printf     ,       USART_printf      

#define USART1_REC_LEN  			200  	//  USART1       
#define USART2_REC_LEN  			200  	//  USART2       
#define USART3_REC_LEN  			200  	//  USART3       

//              ,      
#define EN_USART1 			1		//  (1)/  (0)  1
#define EN_USART2 			0		//  (1)/  (0)  2
#define EN_USART3 			1		//  (1)/  (0)  3
	  	
extern u8  USART1_RX_BUF[USART1_REC_LEN]; //    ,  USART_REC_LEN   .        
extern u8  USART2_RX_BUF[USART2_REC_LEN]; //    ,  USART_REC_LEN   .       
extern u8  USART3_RX_BUF[USART3_REC_LEN]; //    ,  USART_REC_LEN   .       
 
extern u16 USART1_RX_STA;         		//      	
extern u16 USART2_RX_STA;         		//      	
extern u16 USART3_RX_STA;         		//      	

//    
void USART1_Init(u32 bound);//  1      
void USART2_Init(u32 bound);//  2      
void USART3_Init(u32 bound);//  3      
void USART1_printf(char* fmt,...); //  1   printf  
void USART2_printf(char* fmt,...); //  2   printf  
void USART3_printf(char* fmt,...); //  3   printf  

#endif
ヽoo..........................................................USART_H萼デfine_uUSART_H((zhi endif)の3つの言語は、ライブラリファイルが重複して参照を含むことを防止し、デッドサイクルに入ると最終的にエラーが発生します.https://blog.csdn.net/weixin_41656968/articale/detail/80493923https://blog.csdn.net/Tristesse_wjw/articale/detail/78526109
実現する
#include "sys.h"
#include "usart.h"
	  	 
// UASRT    printf    
// usart.h        printf      	  
#if 1
#pragma import(__use_no_semihosting)             
//                           
struct __FILE {
	int handle; 
}; 
FILE __stdout;       
//  _sys_exit()              
_sys_exit(int x){ 
	x = x; 
} 
//   fputc   
int fputc(int ch, FILE *f){      
	while((USART_n->SR&0X40)==0);//    ,         
    USART_n->DR = (u8) ch;      
	return ch;
}
#endif 


/*
USART1      
*/
 
#if EN_USART1   //USART1       
u8 USART1_RX_BUF[USART1_REC_LEN];     //    ,  USART_REC_LEN   .
//    
//bit15,	      
//bit14,	   0x0d
//bit13~0,	          
u16 USART1_RX_STA=0;       //      	  

/*
USART1   printf  
     2      ,printf          ,          printf  
    :USART1_printf("123"); // USART2    123
*/
void USART1_printf (char *fmt, ...){ 
	char buffer[USART1_REC_LEN+1];  //     
	u8 i = 0;	
	va_list arg_ptr;
	va_start(arg_ptr, fmt);  
	vsnprintf(buffer, USART1_REC_LEN+1, fmt, arg_ptr);
	while ((i < USART1_REC_LEN) && (i < strlen(buffer))){
        USART_SendData(USART1, (u8) buffer[i++]);
        while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET); 
	}
	va_end(arg_ptr);
}

void USART1_Init(u32 bound){ //  1      
    //GPIO    
    GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;	 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);	//  USART1,GPIOA  
     //USART1_TX   PA.9
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	//      
    GPIO_Init(GPIOA, &GPIO_InitStructure);  
    //USART1_RX	  PA.10
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//    
    GPIO_Init(GPIOA, &GPIO_InitStructure); 
   //Usart1 NVIC   
    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//     3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		//    3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ    
	NVIC_Init(&NVIC_InitStructure);	//          VIC    
   //USART      
	USART_InitStructure.USART_BaudRate = bound;//     9600;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//   8     
	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_ITConfig(USART1, USART_IT_RXNE, ENABLE);//  ENABLE/  DISABLE  
    USART_Cmd(USART1, ENABLE);                    //     
}

void USART1_IRQHandler(void){ //  1      (          )	
	u8 Res;
	//         USART_RX_BUF[]   ,(USART_RX_STA&0x3FFF)      (     )
	// (USART_RX_STA&0xC000)           ,           。
	//        if(USART_RX_STA&0xC000),   USART_RX_BUF[]  ,  0x0d 0x0a    。
	//              ,  USART_RX_STA 0
	if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){  //    (         0x0d 0x0a  )		
		Res =USART_ReceiveData(USART1);//(USART1->DR);	//        
		printf("%c",Res); //        a          		
		if((USART1_RX_STA&0x8000)==0){//     			
			if(USART1_RX_STA&0x4000){//    0x0d				
				if(Res!=0x0a)USART1_RX_STA=0;//    ,    
				else USART1_RX_STA|=0x8000;	//      
			}else{ //    0X0D					
				if(Res==0x0d)USART1_RX_STA|=0x4000;
				else{
					USART1_RX_BUF[USART1_RX_STA&0X3FFF]=Res ; //          
					USART1_RX_STA++;	//       1
					if(USART1_RX_STA>(USART1_REC_LEN-1))USART1_RX_STA=0;//      ,      	  
				}		 
			}
		}   		 
	} 
} 
#endif	

/*
USART2      
*/
#if EN_USART2   //USART2       
u8 USART2_RX_BUF[USART2_REC_LEN];     //    ,  USART_REC_LEN   .
//    
//bit15,	      
//bit14,	   0x0d
//bit13~0,	          
u16 USART2_RX_STA=0;       //      	  

/*
USART2   printf  
     2      ,printf          ,          printf  
    :USART2_printf("123"); // USART2    123
*/
void USART2_printf (char *fmt, ...){ 
	char buffer[USART2_REC_LEN+1];  //     
	u8 i = 0;	
	va_list arg_ptr;
	va_start(arg_ptr, fmt);  
	vsnprintf(buffer, USART2_REC_LEN+1, fmt, arg_ptr);
	while ((i < USART2_REC_LEN) && (i < strlen(buffer))){
        USART_SendData(USART2, (u8) buffer[i++]);
        while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); 
	}
	va_end(arg_ptr);
}


void USART2_Init(u32 bound){ //  1      
    //GPIO    
    GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	NVIC_InitTypeDef NVIC_InitStructure;
		 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE); //  UART2  GPIOA   
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //     RCC  

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //  USART2 RX   PA3
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//    
    GPIO_Init(GPIOA, &GPIO_InitStructure); 

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //  USART2 TX   PA2
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;	//      
    GPIO_Init(GPIOA, &GPIO_InitStructure);  

   //USART2      
	USART_InitStructure.USART_BaudRate = bound;//     9600;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;//   8     
	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(USART2, &USART_InitStructure); //     
    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//  ENABLE/  DISABLE  
    USART_Cmd(USART2, ENABLE);                    //     
   //Usart2 NVIC   
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
    NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//     3
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;		//   3
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			//IRQ    
	NVIC_Init(&NVIC_InitStructure);	//          VIC    
}

void USART2_IRQHandler(void){ //  2      (          )	

} 
#endif	


#if EN_USART3   //       
u8 USART3_RX_BUF[USART3_REC_LEN];     //    ,  USART_REC_LEN   .
//    
//bit15,	      
//bit14,	   0x0d
//bit13~0,	          
u16 USART3_RX_STA=0;       //      	  

/*
USART3   printf  
     2      ,printf          ,          printf  
    :USART3_printf("123"); // USART3    123
*/
void USART3_printf (char *fmt, ...){ 
	char buffer[USART3_REC_LEN+1];  //     
	u8 i = 0;	
	va_list arg_ptr;
	va_start(arg_ptr, fmt);  
	vsnprintf(buffer, USART3_REC_LEN+1, fmt, arg_ptr);
	while ((i < USART3_REC_LEN) && (i < strlen(buffer))){
        USART_SendData(USART3, (u8) buffer[i++]);
        while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); 
	}
	va_end(arg_ptr);
}

void USART3_Init(u32 BaudRate){ //USART3      
   GPIO_InitTypeDef GPIO_InitStructure;
   USART_InitTypeDef USART_InitStructure;
   NVIC_InitTypeDef NVIC_InitStructure; 

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); //  UART3  GPIOB   
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //     RCC  

   //     GPIO   
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//  USART3 RX   PB11
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//         
   GPIO_Init(GPIOB, &GPIO_InitStructure);

   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//  USART3 TX   PB10
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//    50MHz
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//           
   GPIO_Init(GPIOB, &GPIO_InitStructure);

   //    
   USART_InitStructure.USART_BaudRate = BaudRate;
   USART_InitStructure.USART_WordLength = USART_WordLength_8b;//   8     
   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(USART3, &USART_InitStructure);//    3
   USART_ITConfig(USART3, USART_IT_RXNE, DISABLE);//          
   //USART_ITConfig(USART3, USART_IT_TXE, ENABLE);//              
   USART_Cmd(USART3, ENABLE);//    3

   //      
   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
   NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;//  USART3  
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//    
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);
}

//  3      (          )
void USART3_IRQHandler(void){ 	

}
#endif	
冒头のこの钾if 1の役割http://blog.sina.com.cn/s/blog_6931560 f 0100 kuzd.c 8 t 6は3のusartがあって、どれを使ってどれらをつけますか?
RS 485
インターフェース
#ifndef __RS485_H
#define __RS485_H	 
#include "sys.h"

#define RS485PORT	GPIOA	//  IO  
#define RS485_RE	GPIO_Pin_8	//  IO  



void RS485_Init(void);//   
void RS485_printf (char *fmt, ...); //RS485  
		 				    
#endif
実現する
#include "sys.h"
#include "usart.h"
#include "rs485.h"

void RS485_Init(void){ //RS485     
	GPIO_InitTypeDef  GPIO_InitStructure; 	
    GPIO_InitStructure.GPIO_Pin = RS485_RE; //     (0~15 all)                        
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //  IO             
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //  IO    (2/10/50MHz)    
	GPIO_Init(RS485PORT, &GPIO_InitStructure);
	GPIO_ResetBits(RS485PORT,RS485_RE); //RE     /    ,RE 1   , 0   。
	
}
 
/*
RS485    ,  USART3,  RS485   printf  
    :RS485_printf("123"); // USART3    123
*/
void RS485_printf (char *fmt, ...){ 
	char buffer[USART3_REC_LEN+1];  //     
	u8 i = 0;
	va_list arg_ptr;
	GPIO_SetBits(RS485PORT,RS485_RE); //    (  )//RS485     	
	va_start(arg_ptr, fmt);  
	vsnprintf(buffer, USART3_REC_LEN+1, fmt, arg_ptr);
	while ((i < USART3_REC_LEN) && (i < strlen(buffer))){
        USART_SendData(USART3, (u8) buffer[i++]);
        while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); 
	}
	va_end(arg_ptr);
	GPIO_ResetBits(RS485PORT,RS485_RE); //    (  )//RS485     	
}
実質的にRS 485とRS 232はマクロプロセッサ内部で発行されるものと同じであるが、転送と安定性のために異なるレベルの変換チップを使用するだけで、異なる効果がある.https://blog.csdn.net/u013178472/article/details/81226780