初级会员

- 积分
- 91
- 金钱
- 91
- 注册时间
- 2011-12-9
- 在线时间
- 4 小时
|

楼主 |
发表于 2014-3-5 16:17:54
|
显示全部楼层
回复【楼主位】kingbox568:
---------------------------------
源码压缩包传不上去,代码如下:
#include "usart2.h"
#include "led.h"
#include <stdarg.h>
static u8 Uart2ReceiveState; // 表示接受中数据的状态
static u8 Uart2Rbuffer[30]; // 接受buffer
/***************************************************************************************************
* 函数名:USART2_Config
* 描述 :USART2 GPIO 配置,工作模式配置。9600 8-N-1
* 输入 :无
* 输出: 无
* 调用 :外部调用
***************************************************************************************************/
void USART2_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* config USART2 clock */ //在使用外设时,不仅要使能其时钟,还要调用此函数使能外设才可以正常使用
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // APB2Periph
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE); // APB1Periph 注意:USART2 是 APB1Periph
/* USART2 GPIO config */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //PA2 TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //PA3 RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART2 mode config */
USART_InitStructure.USART_BaudRate = 9600; // 波特率设置
USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 串口传输的字长:8位字长,也可以设置为9位
USART_InitStructure.USART_StopBits = USART_StopBits_1; // 停止位设置为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; // 配置双线全双工通讯,需要把Rx和Tx模式都开启
USART_Init(USART2, &USART_InitStructure); //填充完结构体,调用库函数USART_Init()向寄存器写入配置参数
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ; //抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //开启中断
USART_Cmd(USART2, ENABLE); //调用USART_Cmd() 使能USART外设
Uart2ReceiveState = ReceiveHead1;
}
/***************************************************************************************************
* 函数名:USART2_IRQHandler
* 描述 :USART2中断函数处理
* 输入 :无
* 输出 :无
***************************************************************************************************/
void USART2_IRQHandler(void)
{
u8 DataNum; // 记录几个数据被接受
u8 Data;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE); //清除接收中断标志
Data=USART_ReceiveData(USART2);
switch(Uart2ReceiveState)
{
case ReceiveHead1: if(Data == 0x55)
{
Uart2Rbuffer[0] = Data;
Uart2ReceiveState = ReceiveHead2;
printf("\r\n Uart2Rbuffer[0] = %d \r\n", Data);
printf("\r\n Uart2ReceiveState = %d \r\n", Uart2ReceiveState);
}
break;
case ReceiveHead2: if(Data == 0xAA)
{
Uart2Rbuffer[1] = Data;
Uart2ReceiveState = ReceiveAddr;
printf("\r\n Uart2Rbuffer[1] = %d \r\n", Data);
printf("\r\n Uart2ReceiveState = %d \r\n", Uart2ReceiveState);
}
else
{
Uart2ReceiveState = ReceiveHead1;
printf("\r\n Uart2ReceiveState = %d \r\n", Uart2ReceiveState);
}
break;
case ReceiveAddr: if(Data == 0x10)
{
Uart2Rbuffer[2] = Data;
Uart2ReceiveState = ReceiveDataLong;
printf("\r\n Uart2Rbuffer[2] = %d \r\n", Data);
printf("\r\n Uart2ReceiveState = %d \r\n", Uart2ReceiveState);
}
else
{
Uart2ReceiveState = ReceiveHead1;
printf("\r\n Uart2Rbuffer[2] = %d \r\n", Data);
printf("\r\n Uart2ReceiveState = %d \r\n", Uart2ReceiveState);
}
break;
case ReceiveDataLong: if(Data < 0x20)
{
Uart2Rbuffer[3] = Data;
Uart2ReceiveState = ReceiveOrder;
printf("\r\n Uart2Rbuffer[3] = %d \r\n", Data);
}
else
{
Uart2ReceiveState = ReceiveHead1;
printf("\r\n Uart2Rbuffer[3] = %d \r\n", 456 );
}
break;
case ReceiveOrder: Uart2Rbuffer[4] = Data;
if(Uart2Rbuffer[3] == 0)
{
Uart2ReceiveState = ReceiveTail;
}
else
{
Uart2ReceiveState = ReceiveData;
DataNum = 0;
}
break;
case ReceiveData: Uart2Rbuffer[5+DataNum] = Data;
DataNum++;
if(Uart2Rbuffer[3] == DataNum)
{
Uart2ReceiveState = ReceiveTail;
}
break;
case ReceiveTail: Uart2Rbuffer[5+Uart2Rbuffer[3]] = Data;
Uart2ReceiveState = DisposeOrder;
break;
default:
Uart2ReceiveState = ReceiveHead1;
DataNum = 0;
break;
}
if(Uart2ReceiveState == DisposeOrder)
{
USART_SendData(USART2, Uart2Rbuffer[20]); //将数据回送至超级终端
while(USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET); //等待数据发送完毕
}
}
}
/***************************************************************************************************
* 函数名:fputc
* 描述 :重定向c库函数printf到USART2
* 输入 :无
* 输出 :无
* 调用 :由printf调用
***************************************************************************************************/
int fputc(int ch, FILE *f)
{
USART_SendData(USART2, (unsigned char) ch);
while( USART_GetFlagStatus(USART2,USART_FLAG_TC)!= SET);
return (ch);
} |
|