望高手指点迷津:STM32求全局变量数组算术均值问题

[复制链接]
 楼主| stevendaoyun 发表于 2013-6-30 20:07 | 显示全部楼层 |阅读模式
这个程序的功能是求u32型数组中十个数的算术平均值,平均值定义为float型,但是求出的结果总是错误,下面是我用Printf语句通过串口输出到串口调试助手的计算结果。数组中的值为2700
数组中的值为2300
数组中的值为2700
数组中的值为2300
数组中的值为2700
数组中的值为2300
数组中的值为2700
数组中的值为2300
数组中的值为2700
数组中的值为2300
Average=15607.000000
C:\Users\Administrator\Desktop
  1. #include "stm32f10x.h"                //新的库中整合了头文件
  2. #include "delay.h"
  3. #include "math.h"
  4. #include "misc.h"
  5. #include "stdio.h"                        
  6. u32 ADC_RegularConvertedValueTab1[10]={2700,2300,2700,2300,2700,2300,2700,2300,2700,2300};//存放采样值
  7. float Average;
  8. void RCC_Configuration(void);
  9. void GPIO_Configuration(void);
  10. void NVIC_Configuration(void);
  11. void USART1_Configuration(void);
  12. float ADC_Average()
  13. {
  14.    u8 i;
  15.    u32 Add;
  16.    for(i=0;i<10;i++)
  17.            {        
  18.                 Add=Add+ADC_RegularConvertedValueTab1[i];
  19.                 printf("数组中的值为%d\r\n",ADC_RegularConvertedValueTab1[i]);
  20.         }
  21.    Average=Add*1.0/10;       
  22.    printf("Average=%f\r\n",Average);
  23.    return Average;
  24. }
  25. /*******主函数***********/
  26. int main()
  27. {
  28.    RCC_Configuration();         //系统时钟配置
  29.    GPIO_Configuration(); //引脚配置
  30.    USART1_Configuration();
  31.    ADC_Average();       
  32.    while(1)                               
  33.    {
  34.    }         
  35. }
  36. /*******系统时钟设置*********/
  37. void RCC_Configuration(void)
  38. {
  39.         ErrorStatus HSEStartUpStatus;                                //定义外部高速晶体启动状态枚举变量
  40.         RCC_DeInit();                                                                //复位RCC外部设备寄存器到默认值
  41.         RCC_HSEConfig(RCC_HSE_ON);                                        //打开外部高速晶振
  42.         HSEStartUpStatus=RCC_WaitForHSEStartUp();        //等待外部高速时钟准备好
  43.         if(HSEStartUpStatus==SUCCESS)
  44.         {
  45.                 FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //开启FLASH预读缓冲功能,加速FLASH的读取。所有程序中必须的用法.位置:RCC初始化子函数里面,时钟起振之后
  46.             FLASH_SetLatency(FLASH_Latency_2);                    //flash操作的延时

  47.                 RCC_HCLKConfig(RCC_SYSCLK_Div1);               //配置AHB(HCLK)时钟等于==SYSCLK
  48.             RCC_PCLK2Config(RCC_HCLK_Div1);                //配置APB2(PCLK2)钟==AHB时钟
  49.             RCC_PCLK1Config(RCC_HCLK_Div2);                //配置APB1(PCLK1)钟==AHB1/2时钟

  50.                 RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);   //配置PLL时钟 == 外部高速晶体时钟 * 9 = 72MHz
  51.                 RCC_PLLCmd(ENABLE);                                   //使能PLL
  52.                 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)    //等待PLL时钟就绪
  53.             {
  54.             }
  55.                 RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);            //选PLL作系统时钟
  56.                 while(RCC_GetSYSCLKSource() != 0x08)                  //检查PLL时钟是否作为系统时钟
  57.             {
  58.             }
  59.                 RCC_ADCCLKConfig(RCC_PCLK2_Div6);                     //ADC时钟=PCLK/6 12MHZ
  60.                 /*开启端口A、B、ADC1、TIM3、TIM2、DMA1的时钟*/
  61.             RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);//开启GPIO口时钟
  62.             RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//打开串口时钟
  63.         }
  64. }
  65. /*GPIO配置,务必注意打开GPIO时钟时,一定打开AFIO时钟时钟*/
  66. void GPIO_Configuration(void)
  67. {
  68.         GPIO_InitTypeDef GPIO_InitStructure;
  69.         GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;//LED2、3、4、5,TEC通道1、2        
  70.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  71.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;   //推挽输出
  72.         GPIO_Init(GPIOC,&GPIO_InitStructure);
  73.         /*USART1 TX 配置为复用推挽输出*/
  74.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  75.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  76.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  77.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  78.         /*USART1 RX 引脚配置为浮空输入*/
  79.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
  80.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  81.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  82. }
  83. void NVIC_Configuration(void)
  84. {
  85.    NVIC_InitTypeDef NVIC_InitStructure;
  86.    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);      //分组2先占优先级2位,从优先级2位
  87.    /***********USART1中断配置函数*************/
  88.    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  89.    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //通道设置为串口1中断
  90.    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;           //中断响应优先级0
  91.    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                   //打开中断
  92.    NVIC_Init(&NVIC_InitStructure);                                                    //初始化
  93. }
  94. /****************串口1**********************/
  95. void USART1_Configuration(void)
  96. {
  97.         USART_InitTypeDef USART_InitStructure;
  98.         USART_ClockInitTypeDef USART_ClockInitStructure;
  99.         USART_InitStructure.USART_BaudRate = 115200;
  100.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  101.         USART_InitStructure.USART_StopBits = USART_StopBits_1;
  102.         USART_InitStructure.USART_Parity = USART_Parity_No;
  103.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  104.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  105.         USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
  106.         USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
  107.         USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
  108.         USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
  109.         USART_ClockInit(USART1, &USART_ClockInitStructure);
  110.         USART_Init(USART1, &USART_InitStructure);       
  111.         USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  112.         USART_Cmd(USART1, ENABLE);
  113.     USART_ClearFlag(USART1, USART_FLAG_TC);  
  114. }
  115. /*********Printf函数**************/
  116. #ifdef __GNUC__
  117.     #define PUTCHAR_PROTOTYPE int _io_putchar(int ch)
  118. #else
  119.     #define PUTCHAR_PROTOTYPE int fputc(int ch,FILE *f)       
  120. #endif
  121. /*__GNUC__*/
  122. PUTCHAR_PROTOTYPE
  123. {
  124.         USART_SendData(USART1,(unsigned char)ch);
  125.         while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
  126.         return ch;
  127. }
  128. int GetKey(void)
  129. {
  130.         while(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET);
  131.         return((int)(USART_ReceiveData(USART1)&0x1ff));
  132. }


 楼主| stevendaoyun 发表于 2013-6-30 20:09 | 显示全部楼层
新手,没多少分,还望大神们体谅
cjhk 发表于 2013-6-30 20:57 | 显示全部楼层
帮你顶一个   再看看别人的意见吧   楼主   
 楼主| stevendaoyun 发表于 2013-6-30 21:16 | 显示全部楼层
cjhk 发表于 2013-6-30 20:57
帮你顶一个   再看看别人的意见吧   楼主

thank you !
 楼主| stevendaoyun 发表于 2013-6-30 21:41 | 显示全部楼层
正在做的项目,数据采集都对,在数据运算处理这里卡住了,急啊
 楼主| stevendaoyun 发表于 2013-7-1 14:49 | 显示全部楼层
kbybhzd 发表于 2013-7-1 13:55
Average=(float)Add*1.0/10;   改成这样看看。

问题出在类型转换上了,加了强制类型转换结果就正确了,Thank you!:handshake
 楼主| stevendaoyun 发表于 2013-7-1 14:57 | 显示全部楼层
kbybhzd 发表于 2013-7-1 13:58
Add 的初值要置零,随机数据肯定不对了。

恩,是的。想起以前写C语言程序变量默认值为0,也没在乎。
qingqiu647 发表于 2013-7-4 01:21 | 显示全部楼层
计算时,使用浮点数
您需要登录后才可以回帖 登录 | 注册

本版积分规则

3

主题

25

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部

3

主题

25

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部