打印
[STM32F1]

各位帮忙看一下这段串口通信程序!!用的是USART3

[复制链接]
1456|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
ljhya|  楼主 | 2013-12-7 18:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
请问一下这个程序,用USART3的传送数据有没有问题???

volatile u8 USART_Received_Flag=0;
u8 USART_RX_BUF[64];     //接收缓冲,最大64个字节.
//接收状态
//bit7,接收完成标志
//bit6,接收到0x0d
//bit5~0,接收到的有效字节数目
u8 USART_RX_STA=0;       //接收状态标记

void uart_init(u32 bound){
    //GPIO端口设置
    GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); //使能UART3所在GPIOB的时钟
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE); //串口时钟配置
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);

   
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB,   &GPIO_InitStructure);
      // Configure USART3 Rx (PB.11) as input floating
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_11;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;         
    GPIO_Init(GPIOB, &GPIO_InitStructure);

   //Usart1 NVIC 配置

    NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;                //

        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                        //IRQ通道使能
        NVIC_Init(&NVIC_InitStructure);        //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器USART3
  
   //USART 初始化设置
   
        USART_InitStructure.USART_BaudRate = bound;//一般设置为9600;
        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);
   

    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//开启中断
   
    USART_Cmd(USART3, ENABLE);                    //使能串口

}

void USART3_SendBytes(u8 * str,u32 DataLen)         //传送一组字符
{
         
    while(DataLen)
    {
                delay_us(10);
        USART_SendData(USART3, *str++);
        while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
                DataLen--;
    }

}
void USART3_IRQHandler(void)                        //串口3中断服务程序
        {
        u8 Res;
        if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)  //接收中断(接收到的数据必须是0x0d 0x0a结尾)
                {
                Res =USART_ReceiveData(USART3);//(USART3->DR);        //读取接收到的数据

                USART_RX_BUF[USART_RX_STA]=Res ;
                USART_RX_STA++;
                if(USART_RX_STA>63)
                {
                        USART_RX_STA=0;//接收数据错误,重新开始接收          
                      USART_Received_Flag=1;
                 }                  
     }
}
沙发
mmuuss586| | 2014-4-21 09:36 | 只看该作者
啥问题呢?

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

5

主题

13

帖子

1

粉丝