你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

查看: 1599|回复: 2

请教关于STM32F103USART串口接收的问题,附代码

[复制链接]

5

主题

11

回帖

0

蝴蝶豆

新手上路

最后登录
2018-7-12
发表于 2013-3-20 17:21:36 | 显示全部楼层 |阅读模式
各位我用查询方式接收串口数据,结果接收不到:期待高人指点。
串口配置如下:
/* USART1 config 9600 8-N-1 */
  USART_InitTypeDef USART_InitStructure;
  /* config USART1 clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
  /* USART1 GPIO config */
  /* Configure USART1 Rx PA10 input floating */ 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  /* Configure USART1 Tx (PA9) as alternate push-pull */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART1 mode config */
  USART_InitStructure.USART_BaudRate = 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(USART1, &USART_InitStructure);
  USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
//  USART_ITConfig(USART1,USART_IT_TXE,ENABLE);
  USART_Cmd(USART1, ENABLE); 
 
代码如下:
void Commun_232_Receive(void)
{
    static   uint16_t Data_Rec[4]={0,0,0,0};
    static   uint16_t FrameHead=0;
    static   uint16_t Data_Num=0;
    static   uint16_t i=0,j=0,k=0;
    uint16_t crc=0,crc1=0;
    //===============================
//    Receive_Status= USART_GetITStatus(USART1, USART_IT_RXNE);
    Receive_Status= USART_GetFlagStatus(USART1, USART_FLAG_RXNE);
//    if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) = 1)
    if(Receive_Status==1)//有数据
    {
        Data_receive = USART_ReceiveData(USART1);//接收数据
        Data_receive&=0xFF;
//        USART_ClearFlag(USART1,USART_FLAG_RXNE);//清除接收非空标志位
        if(FrameHead==0&&Data_receive==0x35)//找帧头
        {
            FrameHead=1;   //找到帧头
        }
        else if(FrameHead==1)
        {
            Data_Rec[Data_Num]=Data_receive;
            Data_Num++;
            if(Data_Num>4)
            {
                Data_Num=0;
                FrameHead=0;
                Data_Rec[2] = Data_Rec[2] BSRR = 0x00000008;
                                /* Set PA3 */
                         Switch_Status.bit.TA0=1;
                            }
                            else if(Data_Rec[1]==1)
                            {
                                GPIOA->BRR = 0x00000008;
                                /* Reset PA3 */
                         Switch_Status.bit.TA1=1;
                            }
                            break;
                        }
                        case 2:
                        {
                            if(Data_Rec[1]==0)
                            {
                                GPIOA->BSRR = 0x00000010;
                                /* Set PA4 */
                         Switch_Status.bit.TA2=1;
                            }
                            else if(Data_Rec[1]==1)
                            {
                                GPIOA->BRR = 0x00000010;
                                /* Reset PA4 */
                         Switch_Status.bit.TA3=1;
                            }
                            break;
                        }
                        case 3:
                        {
                            if(Data_Rec[1]==0)
                            {
                                GPIOA->BSRR = 0x00000020;
                                /* Set PA5 */
                         Switch_Status.bit.TA4=1;
                            }
                            else if(Data_Rec[1]==1)
                            {
                                GPIOA->BRR = 0x00000020;
                                /* Reset PA5 */
                         Switch_Status.bit.TA5=1;
                            }
                            break;
                        }
                        case 4:
                        {
                            if(Data_Rec[1]==0)
                            {
                                GPIOA->BSRR = 0x00000040;
                                /* Set PA6 */
                         Switch_Status.bit.TA6=1;
                            }
                            else if(Data_Rec[1]==1)
                            {
                                GPIOA->BRR = 0x00000040;
                                /* Reset PA6 */
                         Switch_Status.bit.TA7=1;
                            }
                            break;
                        }
                        case 5:
                        {
                            if(Data_Rec[1]==0)
                            {
                                GPIOA->BSRR = 0x00000080;
                                /* Set PA7 */
                         Switch_Status.bit.TA8=1;
                            }
                            else if(Data_Rec[1]==1)
                            {
                                GPIOA->BRR = 0x00000080;
                                /* Reset PA7 */
                         Switch_Status.bit.TA9=1;
                            }
                            break;
                        }
                    }         
                }
            }
        }
回复

使用道具 举报

24

主题

591

回帖

0

蝴蝶豆

中级会员

最后登录
2020-12-2
发表于 2013-3-20 21:12:14 | 显示全部楼层

RE:请教关于STM32F103USART串口接收的问题,附代码

已经到中断里面了吧。他就有这个毛病。txe标志。你看看
回复 支持 反对

使用道具 举报

5

主题

11

回帖

0

蝴蝶豆

新手上路

最后登录
2018-7-12
 楼主| 发表于 2013-4-10 11:24:48 | 显示全部楼层

RE:请教关于STM32F103USART串口接收的问题,附代码

问题已解决,将GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
改为下拉输入模式就好了。
回复 支持 反对

使用道具 举报

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32Cube扩展软件包
意法半导体边缘AI套件
ST - 理想汽车豪华SUV案例
ST意法半导体智能家居案例
STM32 ARM Cortex 32位微控制器
关注我们
st-img 微信公众号
st-img 手机版