#include "bsp_uart.h"
#include "stm32f4xx.h"
#include "stdio.h"
#include "bsp_delay.h"
#include "bsp_us100.h"
extern char RxCounter4,RxBuffer4[100];
u8 Timeout2;
//重新指向数据流 printf();---> cmd的界面
// printf(); ---> usart1
//#pragma import(__use_no_semihosting)
struct __FILE
{
int handle;
};
FILE __stdout;
void _sys_exit(int x)
{
x = x;
}
int fputc(int ch, FILE *f) //输出重定向 printf
{
while(USART_GetFlagStatus(USART1 , USART_FLAG_TC) !=SET );
USART1->DR = (uint8_t)ch;
return ch;
}
int fgetc(FILE *f) //输入重定向 scanf
{
while(USART_GetFlagStatus(USART1 , USART_FLAG_RXNE) !=SET );
return (int)USART1->DR;
}
void UART1_Init(uint32_t baud)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 |GPIO_Pin_7;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);
USART_InitStruct.USART_BaudRate = baud; //波特率
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_Init(USART1, &USART_InitStruct);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能接受中断
USART_ClearITPendingBit(USART1, USART_FLAG_TC);
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
NVIC_Init(&NVIC_InitStruct);
USART_Cmd(USART1, ENABLE);
}
uint8_t i=0;
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET) //接受中断,获取相应中断状态,返回值是SET,说明是串口发送完成中断发生
{
USART_ClearITPendingBit(USART1, USART_FLAG_TC);
USART_SendData(USART1, USART_ReceiveData(USART1));
while(USART_GetFlagStatus(USART1 , USART_FLAG_TC) == RESET);
}
}
void UART2_Init(uint32_t baud)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD, ENABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2, ENABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 |GPIO_Pin_6;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART2);
USART_InitStruct.USART_BaudRate = baud; //波特率
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件数据流控制
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//设置收发模式
USART_InitStruct.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStruct.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStruct.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_Init(USART2, &USART_InitStruct);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_ClearITPendingBit(USART2, USART_FLAG_TC);
NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStruct);
USART_Cmd(USART2,ENABLE);
}
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET) //接受中断,获取相应中断状态,返回值是SET,说明是串口发送完成中断发生
{
USART_ClearITPendingBit(USART1, USART_FLAG_TC);
USART_SendData(USART1, USART_ReceiveData(USART1));
while(USART_GetFlagStatus(USART1 , USART_FLAG_TC) == RESET);
}
}
void UART3_Init(uint32_t baud)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD, ENABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3, ENABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 |GPIO_Pin_9;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_USART3);
USART_InitStruct.USART_BaudRate = baud; //波特率
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件数据流控制
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//设置收发模式
USART_InitStruct.USART_Parity = USART_Parity_No;//无奇偶校验位
USART_InitStruct.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStruct.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_Init(USART3, &USART_InitStruct);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_ClearITPendingBit(USART3, USART_FLAG_TC);
NVIC_InitStruct.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStruct);
USART_Cmd(USART3,ENABLE);
}
void USART3_IRQHandler()
{
u8 temp;
if(USART_GetITStatus(USART3, USART_FLAG_RXNE) == SET)
{
temp = ( u8 )USART_ReceiveData(USART3);//(USART1->DR);//读取接受到的数据
USART_ClearFlag(USART3,USART_FLAG_RXNE);
USART_SendData(USART3,temp);
}
}
void UART4_Init(uint32_t baud)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_UART4,ENABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 |GPIO_Pin_11;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4);
USART_InitStruct.USART_BaudRate = baud;//波特率
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //设置收发模式
USART_InitStruct.USART_Parity = USART_Parity_No; //无奇偶校验位
USART_InitStruct.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStruct.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_Init(UART4,&USART_InitStruct);
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
USART_ClearITPendingBit(UART4, USART_FLAG_TC);
NVIC_InitStruct.NVIC_IRQChannel = UART4_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 4;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStruct);
USART_Cmd(UART4,ENABLE);
}
void UART4_IRQHandler()
{
char a[100];
int distance = 0;
if(USART_GetITStatus(UART4, USART_IT_RXNE) == SET)
{
int i;
UART4_send_byte(0x55);
for(i=0;i<100;i++)
{
a[i] = USART_ReceiveData(UART4);
i++;
distance = (a[0] << 8 ) | a[1];
}
}
}
void UART5_Init(uint32_t baud)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD, ENABLE);
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_UART5,ENABLE);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD,&GPIO_InitStruct);
USART_InitStruct.USART_BaudRate = baud;//波特率
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //设置收发模式
USART_InitStruct.USART_Parity = USART_Parity_No; //无奇偶校验位
USART_InitStruct.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStruct.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_Init(UART5,&USART_InitStruct);
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
USART_ClearITPendingBit(UART5, USART_FLAG_TC);
NVIC_InitStruct.NVIC_IRQChannel = UART5_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 4;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;
NVIC_Init(&NVIC_InitStruct);
USART_Cmd(UART5,ENABLE);
}
void UART5_IRQHandler()
{
u8 temp;
if(USART_GetITStatus(UART5, USART_FLAG_RXNE) == SET)
{
temp = ( u8 )USART_ReceiveData(UART4);//(USART1->DR);//读取接受到的数据
USART_ClearFlag(UART5,USART_FLAG_RXNE);
USART_SendData(UART5,temp);
}
}
void UART6_Init(uint32_t baud)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART6, ENABLE);
GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_USART6); //GPIOG复用为USART
GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_USART6);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStruct);
USART_InitStruct.USART_BaudRate = baud;//波特率
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件数据流控制
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //设置收发模式
USART_InitStruct.USART_Parity = USART_Parity_No; //无奇偶校验位
USART_InitStruct.USART_StopBits = USART_StopBits_1;//一个停止位
USART_InitStruct.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_Init(USART6,&USART_InitStruct);
USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);
USART_ClearITPendingBit(USART6, USART_FLAG_TC);
NVIC_InitStruct.NVIC_IRQChannel = USART6_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 4;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 3;
NVIC_Init(&NVIC_InitStruct);
USART_Cmd(USART6,ENABLE);
}
void USART6_IRQHandler()
{
u8 temp;
if(USART_GetITStatus(USART6, USART_FLAG_RXNE) == SET)
{
temp = ( u8 )USART_ReceiveData(USART6);//(USART1->DR);//读取接受到的数据
USART_ClearFlag(USART6,USART_FLAG_RXNE); //清除标志
USART_SendData(USART6,temp); //发送数据
}
}
|