这个程序的功能是求u32型数组中十个数的算术平均值,平均值定义为float型,但是求出的结果总是错误,下面是我用Printf语句通过串口输出到串口调试助手的计算结果。数组中的值为2700
数组中的值为2300
数组中的值为2700
数组中的值为2300
数组中的值为2700
数组中的值为2300
数组中的值为2700
数组中的值为2300
数组中的值为2700
数组中的值为2300
Average=15607.000000
C:\Users\Administrator\Desktop- #include "stm32f10x.h" //新的库中整合了头文件
- #include "delay.h"
- #include "math.h"
- #include "misc.h"
- #include "stdio.h"
- u32 ADC_RegularConvertedValueTab1[10]={2700,2300,2700,2300,2700,2300,2700,2300,2700,2300};//存放采样值
- float Average;
- void RCC_Configuration(void);
- void GPIO_Configuration(void);
- void NVIC_Configuration(void);
- void USART1_Configuration(void);
- float ADC_Average()
- {
- u8 i;
- u32 Add;
- for(i=0;i<10;i++)
- {
- Add=Add+ADC_RegularConvertedValueTab1[i];
- printf("数组中的值为%d\r\n",ADC_RegularConvertedValueTab1[i]);
- }
- Average=Add*1.0/10;
- printf("Average=%f\r\n",Average);
- return Average;
- }
- /*******主函数***********/
- int main()
- {
- RCC_Configuration(); //系统时钟配置
- GPIO_Configuration(); //引脚配置
- USART1_Configuration();
- ADC_Average();
- while(1)
- {
- }
- }
- /*******系统时钟设置*********/
- void RCC_Configuration(void)
- {
- ErrorStatus HSEStartUpStatus; //定义外部高速晶体启动状态枚举变量
- RCC_DeInit(); //复位RCC外部设备寄存器到默认值
- RCC_HSEConfig(RCC_HSE_ON); //打开外部高速晶振
- HSEStartUpStatus=RCC_WaitForHSEStartUp(); //等待外部高速时钟准备好
- if(HSEStartUpStatus==SUCCESS)
- {
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //开启FLASH预读缓冲功能,加速FLASH的读取。所有程序中必须的用法.位置:RCC初始化子函数里面,时钟起振之后
- FLASH_SetLatency(FLASH_Latency_2); //flash操作的延时
- RCC_HCLKConfig(RCC_SYSCLK_Div1); //配置AHB(HCLK)时钟等于==SYSCLK
- RCC_PCLK2Config(RCC_HCLK_Div1); //配置APB2(PCLK2)钟==AHB时钟
- RCC_PCLK1Config(RCC_HCLK_Div2); //配置APB1(PCLK1)钟==AHB1/2时钟
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); //配置PLL时钟 == 外部高速晶体时钟 * 9 = 72MHz
- RCC_PLLCmd(ENABLE); //使能PLL
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //等待PLL时钟就绪
- {
- }
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //选PLL作系统时钟
- while(RCC_GetSYSCLKSource() != 0x08) //检查PLL时钟是否作为系统时钟
- {
- }
- RCC_ADCCLKConfig(RCC_PCLK2_Div6); //ADC时钟=PCLK/6 12MHZ
- /*开启端口A、B、ADC1、TIM3、TIM2、DMA1的时钟*/
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);//开启GPIO口时钟
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);//打开串口时钟
- }
- }
- /*GPIO配置,务必注意打开GPIO时钟时,一定打开AFIO时钟时钟*/
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- 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
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //推挽输出
- GPIO_Init(GPIOC,&GPIO_InitStructure);
- /*USART1 TX 配置为复用推挽输出*/
- 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 RX 引脚配置为浮空输入*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
- void NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //分组2先占优先级2位,从优先级2位
- /***********USART1中断配置函数*************/
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //通道设置为串口1中断
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //中断响应优先级0
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断
- NVIC_Init(&NVIC_InitStructure); //初始化
- }
- /****************串口1**********************/
- void USART1_Configuration(void)
- {
- USART_InitTypeDef USART_InitStructure;
- USART_ClockInitTypeDef USART_ClockInitStructure;
- USART_InitStructure.USART_BaudRate = 115200;
- 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_ClockInitStructure.USART_Clock = USART_Clock_Disable;
- USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
- USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
- USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
- USART_ClockInit(USART1, &USART_ClockInitStructure);
- USART_Init(USART1, &USART_InitStructure);
- USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
- USART_Cmd(USART1, ENABLE);
- USART_ClearFlag(USART1, USART_FLAG_TC);
- }
- /*********Printf函数**************/
- #ifdef __GNUC__
- #define PUTCHAR_PROTOTYPE int _io_putchar(int ch)
- #else
- #define PUTCHAR_PROTOTYPE int fputc(int ch,FILE *f)
- #endif
- /*__GNUC__*/
- PUTCHAR_PROTOTYPE
- {
- USART_SendData(USART1,(unsigned char)ch);
- while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
- return ch;
- }
- int GetKey(void)
- {
- while(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET);
- return((int)(USART_ReceiveData(USART1)&0x1ff));
- }
|