金牌会员
 
- 积分
- 1105
- 金钱
- 1105
- 注册时间
- 2015-10-13
- 在线时间
- 134 小时
|

楼主 |
发表于 2016-1-11 16:43:28
|
显示全部楼层
那接收数据也是中断接受吗?是否要配置SR或CR3寄存器吗?
void uart2_init(u32 bound){
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能USART2,GPIOA时钟
USART_DeInit(USART2); //复位串口1
//USART1_TX PA.2 RTS PA.1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2; //PA.2
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA2
//USART1_RX PA.3 CTS PA.0
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化PA3
//Usart1 NVIC 配置
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 初始化设置
USART_InitStructure.USART_BaudRate = bound;//一般设置为38400;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
USART_InitStructure.USART_StopBits = USART_StopBits_2;//一个停止位
USART_InitStructure.USART_Parity = USART_Parity_Even;//无奇偶校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;//无硬件数据流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收发模式
USART_Init(USART2, &USART_InitStructure); //初始化串口
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启中断
USART_Cmd(USART2, ENABLE); //使能串口
}
#if EN_USART2_RX //如果使能了接收
void USART2_IRQHandler(void) //串口2中断服务程序
{
u8 Res;
#ifdef OS_TICKS_PER_SEC //如果时钟节拍数定义了,说明要使用ucosII了.
OSIntEnter();
#endif
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //接收中断(接收到的数据必须是0x0d 0x0a结尾) it
{
Res =USART_ReceiveData(USART2);//(USART2->DR); //读取接收到的数据
if((USART_RX_STA2&0x8000)==0)//接收未完成
{
if(USART_RX_STA2&0x4000)//接收到了0x0d
{
if(Res!=0x0a)USART_RX_STA2=0;//接收错误,重新开始
else USART_RX_STA2|=0x8000; //接收完成了
}
else //还没收到0X0D
{
if(Res==0x0d)USART_RX_STA2|=0x4000;
else
{
USART_RX_BUF2[USART_RX_STA2&0X3FFF]=Res ;
USART_RX_STA2++;
if(USART_RX_STA2>(USART_REC_LEN2-1))USART_RX_STA2=0;//接收数据错误,重新开始接收
}
}
}
}
#ifdef OS_TICKS_PER_SEC //如果时钟节拍数定义了,说明要使用ucosII了.
OSIntExit();
#endif
}
#endif
int main(void)
{
delay_init(); //延时函数初始化
NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart2_init(38400); //
LCD1602_Init();
KEY_Init(); //初始化与按键连接的硬件接口
while(1)
{
printf("ABCDEFG")
if(USART_RX_STA2&0x8000)
{
len=USART_RX_STA2&0x3fff;//得到此次接收到的数据长度
printf("\r\n您发送的消息为:\r\n");
for(t=0;t<len;t++)
{
USART2->DR=USART_RX_BUF2[t];
while((USART2->SR&0X40)==0);//等待发送结束
}
printf("\r\n\r\n");//插入换行
USART_RX_STA2=0;
}else
{
printf("1234567");
delay_ms(100);
}
}
}
可以发出去数据,但是就是无法接收数据,求指导~~ |
|