uart5.c
3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#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;
}
}