stm32用串口接收到数据,怎么把接收到的字符串转换成浮点数?

2019-08-10 16:11发布

STM32接收到的数据是1.12 2.32  1  ,存放到数组A中,当想要去输出A[0]时,应该是A[0]=1.12, A[1]=2.32但是照例程改写出来是A[0]=1,只是个将每个数字当成了字符存到了数组中,怎么把这些存到数组中的字符串转出浮点型数据,实现我想要输出的结果?
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
9条回答
dateher
2019-08-11 09:09
韩宇 发表于 2017-10-30 16:42
我用的STM32F103VET6

能帮我看下串口接收部分吗?  一直收不到
void USART1_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
       
        //Ïàó|ê±Öóê1Äü        USART1oí6ÔúAPB2éÏÆäËû′®¿ú¶¼ÔúAPB1éÏ
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);  //ê1ÄüUSART1ê±Öó  
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB  , ENABLE); //ê1ÄüGPIOBê±Öó
       
        //′®¿ú1¶Ôó|òy½Å¸′óÃó3éä
        GPIO_PinAFConfig(GPIOB,GPIO_PinSource6,GPIO_AF_USART1); //GPIOB9¸′óÃÎaUSART1  
        GPIO_PinAFConfig(GPIOB,GPIO_PinSource7,GPIO_AF_USART1); //GPIOB10¸′óÃÎaUSART1  
       
        //USART1¶Ë¿úÅäÖÃ
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 ; //TX | RX
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//¸′óÃ1|Äü
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   //Ëù¶è50MHz  
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //íÆíì¸′óÃêä3ö
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        GPIO_Init(GPIOB,&GPIO_InitStructure); //3õê¼»ˉPB6
       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 ; //TX | RX
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   //Ëù¶è50MHz  
        GPIO_Init(GPIOB,&GPIO_InitStructure); //3õê¼»ˉPB7
       
       
        //USART1 3õê¼»ˉéèÖÃ
        USART_InitStructure.USART_BaudRate = 115200; //2¨ìØÂêéèÖÃ
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;  //×Ö3¤Îa8λêy¾Y¸ñê½
        USART_InitStructure.USART_StopBits = USART_StopBits_1;  //ò»¸öí£Ö1λ
        USART_InitStructure.USART_Parity = USART_Parity_No ;  //ÎTÆæż¼ìÑéλ
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //ÎTó2¼têy¾Yá÷¿ØÖÆ
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;  //êÕ·¢Ä£ê½
        USART_Init(USART1, &USART_InitStructure);  //3õê¼»ˉ′®¿ú1
       
        USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);  //¿aÆôÏà1ØÖD¶Ï

#if EN_USART1_RX
        NVIC_Configuration();
#endif
       
        USART_Cmd(USART1, ENABLE);  //ê1Äü′®¿ú1
        //learFlag(USART1, USART_FLAG_TC);
}


void NVIC_Configuration(void)
{
        NVIC_InitTypeDef NVIC_InitStructure;
        /* Configure the NVIC Preemption Priority Bits */  
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
       
        /* Enable the USARTy Interrupt */
        NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;          //′®¿ú1ÖD¶Ïí¨μà
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //ÇàÏèóÅÏ輶3
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //×óóÅÏ輶3
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  //IRQí¨μàê1Äü
        NVIC_Init(&NVIC_InitStructure);  //¸ù¾YÖ¸¶¨μÄ2Îêy3õê¼»ˉVIC¼Ä′æÆ÷
}



int fputc(int ch, FILE *f)
{
        /* ½&#171rintfÄúèY·¢íù′®¿ú */
        USART_SendData(USART1, (unsigned char) ch);
        /* μè′y·¢Ëííê±Ï */
  while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);      
       
        return (ch);
}




#if EN_USART1_RX   //???????
void USART1_IRQHandler(void)                        //??1??????
{
        printf("come ");
                                u8 Res;
#ifdef OS_TICKS_PER_SEC                 //??????????,?????ucosII?.
        OSIntEnter();   
#endif
        if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
                {
                Res =USART_ReceiveData(USART1);//(USART1->DR);        //¶á衽óêÕμ½μÄêy¾Y
                printf(Res);
                if((USART_RX_STA&0x8000)==0)//½óêÕÎ′íê3é
                {
                        if(USART_RX_STA&0x4000)//½óêÕμ½áË0x0d
                        {
                                if(Res!=0x0a)USART_RX_STA=0;//½óêÕ′íÎó£¬ÖØD¿aê¼
                                else USART_RX_STA|=0x8000;        //½óêÕíêáË
                                                                                                       
                        }else //»1ûêÕμ½0X0D
                        {        
                                if(Res==0x0d)USART_RX_STA|=0x4000;
                                else
                                                                                                                                {
                                                                                                                                                USART_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
                                                                                                                                                USART_RX_STA++;
                                                                                                                                                if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//
                                                                                                                                }                 
                                                                                                }
                                                                        }                    
     }
#ifdef OS_TICKS_PER_SEC                 //??????????,?????ucosII?.
        OSIntExit();                                                                                          
#endif
}
#endif        


一周热门 更多>