|
stm32采集12路電壓?jiǎn)纹瑱C(jī)源程序如下:
- #include "system.h"
- #include "SysTick.h"
- #include "systick.h"
- #include "led.h"
- #include "usart.h"
- #include "adc.h"
- #include "stm32f10x_gpio.h"
- #define N 50
- #define M 12
- u16 value1[12];
- float GetVolt(u16 advalue);
- void filter(void);
- u16 After_filter[12]; //?????????????
- u8 i;
- u16 AD_Value[N][M]; //????ADC????,??DMA?????
- void GPIO_Configuration(void);
- void DMA_Configuration(void);
- void RCC_Configuration(void);
- void RCC_Configuration(void)
- {
- ErrorStatus HSEStartUpStatus;
- RCC_DeInit(); //RCC ????
- RCC_HSEConfig(RCC_HSE_ON); //??HSE
- HSEStartUpStatus = RCC_WaitForHSEStartUp(); //??HSE???
- if(HSEStartUpStatus == SUCCESS)
- {
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //Enable Prefetch Buffer
- FLASH_SetLatency(FLASH_Latency_2); //Set 2 Latency cycles
- RCC_HCLKConfig(RCC_SYSCLK_Div1); //AHB clock = SYSCLK
- RCC_PCLK2Config(RCC_HCLK_Div1); //APB2 clock = HCLK
- RCC_PCLK1Config(RCC_HCLK_Div2); //APB1 clock = HCLK/2
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_6); //PLLCLK = 12MHz * 6 = 72 MHz
- RCC_PLLCmd(ENABLE); //Enable PLL
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); //Wait till PLL is ready
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //Select PLL as system clock source
- while(RCC_GetSYSCLKSource() != 0x08); //Wait till PLL is used as system clock source
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB
- | RCC_APB2Periph_GPIOC |RCC_APB2Periph_ADC1 | RCC_APB2Periph_AFIO |RCC_APB2Periph_USART1, ENABLE ); //??ADC1????,??????
- RCC_ADCCLKConfig(RCC_PCLK2_Div6); //72M/6=12,ADC????????14M
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //??DMA??
-
- }
- }
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /*GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //??USART1???????????GPIO???,???????????
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);*/
- //PA0/1/2 ??????????
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0| GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //??????
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //??????
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //PB0/1 ??????????
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //??????
- GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //??????
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- //PC0/1/2/3/4/5 ??????????
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //??????
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- }
- void DMA_Configuration(void)
- {
- DMA_InitTypeDef DMA_InitStructure;
- DMA_DeInit(DMA1_Channel1); //?DMA???1?????????
- DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->DR; //DMA??ADC???
- DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&AD_Value; //DMA?????
- DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //????????????
- DMA_InitStructure.DMA_BufferSize = N*M; //DMA???DMA?????
- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //?????????
- DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //?????????
- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //?????16?
- DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //?????16?
- DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //?????????
- DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA?? x??????
- DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA??x????????????
- DMA_Init(DMA1_Channel1, &DMA_InitStructure); //??DMA_InitStruct?????????DMA???
-
-
-
- }
- /*******************************************************************************
- * 函 數(shù) 名 : main
- * 函數(shù)功能 : 主函數(shù)
- * 輸 入 : 無(wú)
- * 輸 出 : 無(wú)
- *******************************************************************************/
- int main()
- {
- u8 j=0;
- float value[12];
- SysTick_Init(72);
- RCC_Configuration();
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中斷優(yōu)先級(jí)分組 分2組
- LED_Init();
- USART1_Init(9600);
- GPIO_Configuration();
- DMA_Configuration();
- ADC1_Configuration();
-
- ADC_DMACmd(ADC1, ENABLE);
-
-
- ADC_Cmd(ADC1, ENABLE);
-
- ADC_ResetCalibration(ADC1);
-
- while(ADC_GetResetCalibrationStatus(ADC1));
-
- ADC_StartCalibration(ADC1);
-
- while(ADC_GetCalibrationStatus(ADC1));
-
-
-
- ADC_SoftwareStartConvCmd(ADC1, ENABLE);//開啟adc轉(zhuǎn)換
- DMA_Cmd(DMA1_Channel1, ENABLE); //開啟DMA通道
- while(1)
- {
- j++;
-
-
- if(j%50==0)
- {
-
- while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);//?????????????????
- filter();
- for(i=0;i<12;i++)
- {
- value[i]= GetVolt(After_filter[i]);
- value1[i]=value[i];
- printf("value[%d]: %d %.2fv\n",i,value1[i]*100,value[i]) ;
- delay_ms(100);
- }
- }
- }
- }
- float GetVolt(u16 advalue)
- {float temp;
- temp=advalue * 3.3 / 4096;
- return temp;
- }
- void filter(void)
- {
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
21. ADC模數(shù)轉(zhuǎn)換實(shí)驗(yàn).rar
(292.9 KB, 下載次數(shù): 28)
2017-12-5 23:09 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
評(píng)分
-
查看全部評(píng)分
|