#include "stm32f10x.h"
#include "stm32f10x_adc.h"
#include "stm32f10x_dma.h"
#define ADC1_DR_Address ((uint32_t)0x4001244C)
double x=0, y=0;
GPIO_InitTypeDef GP_C;
ADC_InitTypeDef AD_C; //ADC
DMA_InitTypeDef DM_K; //DMA
__IO uint32_t ADC_DualConvertedValueTab[2]; //전역변수
void GPIO_Configuration(void);
void ADC_Configuration(void);
int main(void)
{
GPIO_Configuration();
ADC_Configuration();
while(1)
{
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);//ADC!을 사용하겠다고 알리고 ADC 내부 회로를 초기화 하는 캘리브레이션
x=ADC_DualConvertedValueTab[0]*3.3/4095;
if(x>2)
{
GPIOC->BSRR=GPIO_Pin_8;
}
else
{
GPIOC->BRR=GPIO_Pin_8;
}
y=ADC_DualConvertedValueTab[1]*3.3/4095;
if(y>2)
{
GPIOC->BSRR=GPIO_Pin_9;
}
else
{
GPIOC->BRR=GPIO_Pin_9;
}
}
}
void ADC_Configuration(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//DMA 사용
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //clock for ADC (max 14MHz, 72/6=12MHz)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //enable ADC clock
DMA_DeInit(DMA1_Channel1);//DMA 설정시작
DM_K.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;//변수를 통해 ADC의 정보를 받을지 설정
DM_K.DMA_MemoryBaseAddr = (uint32_t)ADC_DualConvertedValueTab; //저장할 주소 (버퍼)
DM_K.DMA_DIR = DMA_DIR_PeripheralSRC;
DM_K.DMA_BufferSize = 2;
DM_K.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DM_K.DMA_MemoryInc = DMA_MemoryInc_Enable;//여기를 Enable해야 메모리 주소르 증가시키면서 다음메모리에 정보 씀
DM_K.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; // 32bit
DM_K.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; // 32bit
DM_K.DMA_Mode = DMA_Mode_Circular; //정해진 크기의 메모리에 데이터를 쓰면 처음으로 돌아와서 씀
DM_K.DMA_Priority = DMA_Priority_High;
DM_K.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DM_K);//설정 완료
// Enable DMA1 Channel1
DMA_Cmd(DMA1_Channel1, ENABLE);//DMA 명령전달
AD_C.ADC_Mode=ADC_Mode_Independent;
AD_C.ADC_ScanConvMode=ENABLE;
AD_C.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
AD_C.ADC_DataAlign=ADC_DataAlign_Right;
AD_C.ADC_NbrOfChannel=2;
ADC_Init(ADC1,&AD_C);
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_55Cycles5); //PC1 as Input
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 2, ADC_SampleTime_55Cycles5); //PC2 as Input
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!을 사용하겠다고 알리고 ADC 내부 회로를 초기화 하는 캘리브레이션
};
void GPIO_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_StructInit(&GP_C);
GP_C.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9;
GP_C.GPIO_Mode=GPIO_Mode_Out_PP; //GPIO를 아웃풋으로 한다.
GP_C.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_Init(GPIOC,&GP_C);
GP_C.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2;//데이터 시트안에 있는 ADC 핀에설정해야함.
GP_C.GPIO_Mode=GPIO_Mode_AIN; // GPIO아날로그 인풋으로 설정
GPIO_Init(GPIOC,&GP_C);
};
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
댓글 없음:
댓글 쓰기