xiaoxiaocheng_plc/users/ADC.C

121 lines
5.1 KiB
C++
Raw Normal View History

2018-11-13 22:25:34 +08:00
/********************************************************/
// CPU<50><55>Ҫ<EFBFBD><D2AA>STM32F103--RAM<41>ڴ治С<E6B2BB><D0A1>64K Flash<73>ڴ治С<E6B2BB><D0A1>128K
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>STM32F103RDT6<54><36>VET6<54><36><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>
// <20><EFBFBD><E0BCAD><EFBFBD>ڣ<EFBFBD> 20150909
// editor by СС<D0A1><D0A1>
// <20><><EFBFBD>꣺ shop182385147.taobao.com
/********************************************************/
#include "stm32f10x.h"
#include "stm32f10x_adc.h"
#include "stm32f10x_dac.h"
#include "stm32f10x_dma.h"
#include <stdio.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADC1_DR_Address ((uint32_t)0x4001244C) //ADC1 DR<44>Ĵ<EFBFBD><C4B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
#define N 100 //ÿ<><C3BF>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
#define M 4 //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8>
// ע<><D7A2>ADCΪ12λģ<CEBB><C4A3>ת<EFBFBD><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֻ<EFBFBD><D6BB>ADCConvertedValue<75>ĵ<EFBFBD>12λ<32><CEBB>Ч
__IO uint16_t ADCConvertedValue[N][M];
extern u16 PLC_16BIT[12100];
extern void DAC_out_init(void);
void RCC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure; //ADC<44><43>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
DMA_InitTypeDef DMA_InitStructure; //DMA<4D><41>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable ADC1 and GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);
/* Enable DMA1 clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
/* Configure PC.00 (ADC Channel10) as analog input -------------------------*/
//PC0 <20><>Ϊģ<CEAA><C4A3>ͨ<EFBFBD><CDA8>10<31><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* DMA1 channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //DMA<4D><41>Ӧ<EFBFBD><D3A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue; //<2F>ڴ<EFBFBD><DAB4><EFBFBD><E6B4A2><EFBFBD><EFBFBD>ַ
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //DMA<4D><41>ת<EFBFBD><D7AA>ģʽΪSRCģʽ<C4A3><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5>ڴ<EFBFBD>
DMA_InitStructure.DMA_BufferSize = N*M; //DMA<4D><41><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>λΪDMA_MemoryDataSize
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD><DDBA><EFBFBD><EFBFBD><EFBFBD><E8B1B8>ַ<EFBFBD><D6B7>ֹ<EFBFBD><D6B9><EFBFBD><EFBFBD>
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD><DDBA><EFBFBD>Ŀ<EFBFBD><C4BF><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD>Ϊ16λ
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //DMA<4D><41><EFBFBD><EFBFBD><EFBFBD>ݳߴ磬HalfWord<72><64><EFBFBD><EFBFBD>Ϊ16λ
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //ת<><D7AA>ģʽ<C4A3><CABD>ѭ<EFBFBD><D1AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģʽ<C4A3><CABD>
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA<4D><41><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD>
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //M2Mģʽ<C4A3><CABD><EFBFBD><EFBFBD>
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>ģʽ
ADC_InitStructure.ADC_ScanConvMode = ENABLE; //<2F><><EFBFBD><EFBFBD>ɨ<EFBFBD><C9A8>ģʽ
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>ģʽ
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //ADC<44>ⲿ<EFBFBD><E2B2BF><EFBFBD>أ<EFBFBD><D8A3>ر<EFBFBD>״̬
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //<2F><><EFBFBD>ʽ,ADCΪ12λ<32>У<EFBFBD><D0A3>Ҷ<EFBFBD><D2B6>ʽ
ADC_InitStructure.ADC_NbrOfChannel = M; //<2F><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4<EFBFBD><34>
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel10 configuration ADCͨ<43><CDA8><EFBFBD><20><>10<31><30>ͨ<EFBFBD><CDA8> <20><><EFBFBD><EFBFBD>˳<EFBFBD><CBB3>1<EFBFBD><31>ת<EFBFBD><D7AA>ʱ<EFBFBD><CAB1> */
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 3, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 4, ADC_SampleTime_239Cycles5);
// ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 5, ADC_SampleTime_239Cycles5);
// ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 6, ADC_SampleTime_239Cycles5);
/* ADC<44><43><EFBFBD>ʹ<EEA3AC><CAB9> */
ADC_DMACmd(ADC1, ENABLE);
/* <20><><EFBFBD><EFBFBD>ADC1 */
ADC_Cmd(ADC1, ENABLE);
/* <20><><EFBFBD><EFBFBD>У׼ */
ADC_ResetCalibration(ADC1);
/* <20>ȴ<EFBFBD><C8B4><EFBFBD><EFBFBD><EFBFBD>У׼<D0A3><D7BC><EFBFBD><EFBFBD> */
while(ADC_GetResetCalibrationStatus(ADC1));
/* <20><>ʼУ׼ */
ADC_StartCalibration(ADC1);
/* <20>ȴ<EFBFBD>У׼<D0A3><D7BC><EFBFBD><EFBFBD> */
while(ADC_GetCalibrationStatus(ADC1));
/* <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>ADCͨ<43><CDA8>DMA<4D><41>ʽ<EFBFBD><CABD><EFBFBD>ϵĸ<CFB5><C4B8><EFBFBD>RAM */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
void ADC_init(void)
{
RCC_Configuration();
DAC_out_init();
}
void filter(void)
{
int sum=0;
u8 count,i;
for(i=0;i<M;i++) //
{
for(count=0;count<N;count++)
{
sum+=ADCConvertedValue[count][i];
}
PLC_16BIT[0X1000+6030+i]=sum/N;
sum=0;
}
}