本帖最后由 tiger84 于 2010-5-31 10:10 编辑
串口处理
简单说明 串口1用来显示,无中断,查询发送
串口2中断发送,中断接收
// usart.h
#ifndef __USART__H__
#define __USART__H__
#define UART2_RX_QUEUE_SIZE 1024
#define UART2_TX_QUEUE_SIZE 1024
void usart_init(int usart_num);
int USART2_SendDataLen( unsigned char *pData,int len);
#endif
// usart.c
#include "global.h"
// 串口队列
struct UART_STRUCT
{
struct QueueBuffer *psSend; //发送队列指针
struct QueueBuffer *psReci; //接收队列指针
};
struct UART_STRUCT pUart2; //定义串口的结构体
//串口接收
#define Uart2_Rx_Push(x) Queue_Push(pUart2.psReci,x)
#define Uart2_Rx_Pop() Queue_Pop(pUart2.psReci)
#define Uart2_Rx_Num() Queue_Num(pUart2.psReci)
#define Uart2_Rx_Full() Queue_is_full(pUart2.psReci)
#define Uart2_Rx_Empty() Queue_is_empty(pUart2.psReci)
#define Uart2_Rx_Clr() do{Queue_Clear(pUart2.pReci);}while(0)
// 串口发送
#define Uart2_Tx_Push(x) Queue_Push(pUart2.psSend,x)
#define Uart2_Tx_Pop() Queue_Pop(pUart2.psSend)
#define Uart2_Tx_Num() Queue_Num(pUart2.psSend)
#define Uart2_Tx_Full() Queue_is_full(pUart2.psSend)
#define Uart2_Tx_Empty() Queue_is_empty(pUart2.psSend)
#define Uart2_Tx_Clr() do{Queue_Clear(pUart2.psend);}while(0)
/*******************************************************************************
* Function Name : usart_init()
* Description : This routine is called to set the configuration value
* Then each class should configure device themself.
* Input : None.
* Output : None.
* Return : Return USB_SUCCESS, if the request is performed.
* Return USB_UNSUPPORT, if the request is invalid.
*******************************************************************************/
void usart_init(int usart_num)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
if(USE_USART1 == usart_num)
{
// 配置时钟
/* Enable GPIOx clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* Enable USARTx clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
// 配置IO
/* Configure USARTx_Tx as alternate function push-pull */
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);
/* Configure USARTx_Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &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;
/* Configure the USARTx */
USART_Init(USART1, &USART_InitStructure);
/* Enable USART2 Receive interrupts */
// USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Enable the USARTx */
USART_Cmd(USART1, ENABLE);
}
else if(USE_USART2 == usart_num)
{
/* Enable GPIOx clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* Enable USARTx clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* Configure USARTx_Tx as alternate function push-pull */
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);
/* Configure USARTx_Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &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;
/* Configure the USARTx */
USART_Init(USART2, &USART_InitStructure);
pUart2.psSend=Queue_Register(UART2_TX_QUEUE_SIZE);
pUart2.psReci=Queue_Register(UART2_RX_QUEUE_SIZE);
/* Enable USART2 Receive interrupts */
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
/* Enable the USARTx */
USART_Cmd(USART2, ENABLE);
}
}
// return 返回可发送字节数
int USART2_SendDataLen( unsigned char *pData,int len)
{
int i;
unsigned char *p;
p = pData;
for(i = 0; i < len; i++)
{
if(!Uart2_Tx_Full())
{
Uart2_Tx_Push(*p++);
}
else
{
break;
}
}
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);//只要发送寄存器为空,就会一直有中断,因此,要是不发送数据时,把发送中断关闭,只在开始发送时,才打开
return (i + 1);
}
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
/* Read one byte from the receive data register */
Uart2_Rx_Push(USART_ReceiveData(USART2));
}
//发送中断
if( USART_GetITStatus(USART2, USART_IT_TXE) == SET )
{
if(!Uart2_Tx_Empty())
{
USART_SendData(USART2, Uart2_Tx_Pop());
}
else
{
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
}
}
} |