#include "usart.h"
#include "stm32f4xx.h"
#define RS485tX GPIO_SetBits(GPIOE,GPIO_Pin_1);//1 拉高
#define RS485rX GPIO_ResetBits(GPIOE, GPIO_Pin_1);//0 拉低
void uart_init(void)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5); //UART5_RX
GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5); //UART5_TX
GPIO_PinAFConfig(GPIOE, GPIO_PinSource1, GPIO_AF_UART5); //uart5_DE
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_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_Init(GPIOE, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
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);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_Cmd(UART5, ENABLE);
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
}
static void delay(unsigned int dl)
{
unsigned int i,y;
for(i = 0; i < 500; i++)
{
for(y = 0; y < dl; y++);
}
}
void UART5_IRQHandler(void)
{
uint8_t x,y,z;
uint16_t checksum1,checksum2;
if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)
{
x=USART_ReceiveData(UART5);
USART_SendData(UART5,x);
}
}
void uartsend(int ch)
{
RS_485tX;
delay(100);
while(RESET == USART_GetFlagStatus(UART5,USART_FLAG_TXE));
USART_SendData(UART5, (uint8_t) ch);
RS485rX;
delay(100);
}
用的是STM32F407的片子,现在接受和发送数据都没有,这是为什么呢?