新手上路
- 积分
- 48
- 金钱
- 48
- 注册时间
- 2014-4-12
- 在线时间
- 0 小时
|
楼主 |
发表于 2014-4-14 15:42:58
|
显示全部楼层
#include "config.h"
/*******************************************************************************
* Function Name: 重定义系统putchar函数int fputc(int ch, FILE *f)
* Description : 串口发一个字节
* Input : int ch, FILE *f
* Output :
* Return : int ch
*******************************************************************************/
int fputc(int ch, FILE *f)
{
//USART_SendData(USART1, (u8) ch);
USART1->DR = (u8) ch;
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET)
{
}
return ch;
}
/*******************************************************************************
* Function Name: USART1_Configuration
* Description : NUSART1设置
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART1_Configuration(u32 BaudRate)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//USART1_TX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART1_RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//启动USART1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
USART_InitStructure.USART_BaudRate = BaudRate;
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_Tx | USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
/*******************************************************************************
* Function Name: USART2_Configuration
* Description : USART2设置
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART2_Configuration(u32 BaudRate)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//USART2_TX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART2_RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//启动USART3
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/*
USART3 configured as follow:
- BaudRate = 4800 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = BaudRate;
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(USART2, &USART_InitStructure);
// Enable the USART Receive interrupt: this interrupt is generated when the
// USART1 receive data register is not empty
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
/*******************************************************************************
* Function Name: USART3_Configuration
* Description : NUSART1设置
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void USART3_Configuration(u32 BaudRate)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
//USART3_TX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//USART3_RX
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//启动USART3
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
USART_InitStructure.USART_BaudRate = BaudRate;
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(USART3, &USART_InitStructure);
// Enable the USART Receive interrupt: this interrupt is generated when the
// USART1 receive data register is not empty
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_Cmd(USART3, ENABLE);
}
//=============================================================================
//函 数 名: USART1_IRQHandler()
//功 能:中断服务程序
//入口参数: 无
//出口参数: 无
//返 回 值: 无
//=============================================================================
void USART1_IRQHandler(void)
{
u8 buffer;
buffer =(u8) (USART_ReceiveData(USART1)); //获取数据
//GPS_usart(buffer);
USART2->DR = buffer; //转给usart3
USART_ClearITPendingBit(USART1, USART_IT_RXNE);//清楚中断标志
}
/*==============================================================================
//函 数 名: USART3_IRQHandler()
//功 能:中断服务程序
//入口参数: 无
//出口参数: 无
//返 回 值: 无
==============================================================================*/
#ifdef EN_USART3_RX //如果使能了接收
u8 USART3_RX_BUF[64]; //接收缓冲,最大64个字节.
u8 USART3_RX_STA=0; //接收状态标记
void USART3_IRQHandler(void)
{
u8 res3;
if(USART3->SR&(1<<5))//接收到数据
{
res3=USART3->DR;
USART3_RX_BUF[USART3_RX_STA]=res3;
USART3_RX_STA++;
}
}
#endif
//==============================================================================
//----------------------------------------------
void SendASC(u8 d)//发送一个字节
{
USART3->DR=d;
while((USART3->SR&0X40)==0);//等待发送结束
}
//----------------------------------------------
void SendString(u8 *str) //发送字符串
{
while(*str)
{
SendASC(*str) ;
str++;
}
}
/*=============================================================================*/
void USART_OUT(USART_TypeDef* USARTx, u8 *Data)
{
while(1)
{ if( (*Data)=='\0' ) break;
USART_SendData(USARTx, *Data++);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);
}
}
void USART1_OUT(u8 *Data)
{
while(1)
{ if( (*Data)=='\0' ) break;
USART_SendData(USART1, *Data++);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
}
}
void USART2_OUT(u8 *Data)
{
while(1)
{ if( (*Data)=='\0' ) break;
USART_SendData(USART2, *Data++);
while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET);
}
}
void USART3_OUT(u8 *Data)
{
while(1)
{ if( (*Data)=='\0' ) break;
USART_SendData(USART3, *Data++);
while(USART_GetFlagStatus(USART3, USART_FLAG_TC)==RESET);
}
} |
|