uart5.c 3.05 KB
#include "uart5.h"
#include "dev.h"
#include "delay.h"
#include "string.h"


#define	UART5_RX_MAX		40
#define UART5_TX_MAX		40
uint8_t UART5_RX_BUF[UART5_RX_MAX] = {0};					//接收缓存区
uint8_t UART5_TX_BUF[UART5_TX_MAX] = {0};					//发送缓存区

Uart_Type u5;
void uart5_init(void)
{
	GPIO_InitTypeDef 	GPIO_InitStructure;
	USART_InitTypeDef 	USART_InitStructure;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | RCC_AHB1Periph_GPIOF,ENABLE); 
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);	

	GPIO_PinAFConfig(U485_RX_Port,GPIO_PinSource2,GPIO_AF_UART5); 
	GPIO_PinAFConfig(U485_TX_Port,GPIO_PinSource12,GPIO_AF_UART5); 
	
	GPIO_InitStructure.GPIO_Pin 	= U485_RX_Pin; 
	GPIO_InitStructure.GPIO_Mode 	= GPIO_Mode_AF;			
	GPIO_InitStructure.GPIO_Speed 	= GPIO_Speed_50MHz;		
	GPIO_InitStructure.GPIO_OType 	= GPIO_OType_PP; 		
	GPIO_InitStructure.GPIO_PuPd 	= GPIO_PuPd_UP; 		
	GPIO_Init(U485_RX_Port,&GPIO_InitStructure); 					
	
	GPIO_InitStructure.GPIO_Pin 	= U485_TX_Pin; 
	GPIO_InitStructure.GPIO_Mode 	= GPIO_Mode_AF;			
	GPIO_InitStructure.GPIO_Speed 	= GPIO_Speed_50MHz;		
	GPIO_InitStructure.GPIO_OType 	= GPIO_OType_PP; 		
	GPIO_InitStructure.GPIO_PuPd 	= GPIO_PuPd_UP; 		
	GPIO_Init(U485_TX_Port,&GPIO_InitStructure); 
	
	GPIO_InitStructure.GPIO_Pin 	= EN_485_Pin_1; 
	GPIO_InitStructure.GPIO_Mode 	= GPIO_Mode_OUT;			
	GPIO_InitStructure.GPIO_Speed 	= GPIO_Speed_50MHz;		
	GPIO_InitStructure.GPIO_OType 	= GPIO_OType_PP; 		
	GPIO_InitStructure.GPIO_PuPd 	= GPIO_PuPd_UP; 		
	GPIO_Init(EN_485_Port_1,&GPIO_InitStructure); 
	
	GPIO_InitStructure.GPIO_Pin 	= EN_485_Pin_2; 
	GPIO_InitStructure.GPIO_Mode 	= GPIO_Mode_OUT;			
	GPIO_InitStructure.GPIO_Speed 	= GPIO_Speed_50MHz;		
	GPIO_InitStructure.GPIO_OType 	= GPIO_OType_PP; 		
	GPIO_InitStructure.GPIO_PuPd 	= GPIO_PuPd_UP; 		
	GPIO_Init(EN_485_Port_2,&GPIO_InitStructure); 	
	
	USART_InitStructure.USART_BaudRate 				= U5_BAUDRETE;				
	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(UART5, &USART_InitStructure); 
	
	USART_Cmd(UART5, ENABLE);  
}
void uart5_rx_mode(void)
{
	GPIO_ResetBits(EN_485_Port_1,EN_485_Pin_1);
	GPIO_ResetBits(EN_485_Port_2,EN_485_Pin_2);
}
void uart5_tx_mode(void)
{
	GPIO_SetBits(EN_485_Port_1,EN_485_Pin_1);
	GPIO_SetBits(EN_485_Port_2,EN_485_Pin_2);
	delay_ms(1);
}
void uart5_send_byte(uint8_t one_byte)
{
	uart5_tx_mode();
	while(USART_GetFlagStatus(UART5,USART_FLAG_TXE)!=SET);//等待发送结束
	USART_SendData(UART5,one_byte);                      //向串口3发送数据
	uart5_rx_mode();
}
void uart5_send_bytes(uint8_t* buf,uint16_t length)
{
	for(uint16_t i=0;i<length;i++) 
	{
		uart5_send_byte(buf[i]);
	}
}

uint8_t uart5_read_byte(uint8_t* c)
{
  if ( USART_GetFlagStatus(UART5, USART_FLAG_RXNE) != RESET)
  {
    *c = USART_ReceiveData(UART5);
    return 1;
  }
  else
  {
    return 0;
  }		
}