CooCox ile Çok Kanal ADC

Başlatan muhittin_kaplan, 11 Kasım 2012, 11:25:56

muhittin_kaplan

#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_flash.h"

void SetSysClockTo24(void)
{
	int i;
  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------*/
  /* RCC system reset(for debug purpose) */
	RCC_DeInit();
	RCC_HSEConfig(RCC_HSE_ON);

  for(i=0;i<0x000FFFF;i++);
  	FLASH_SetLatency(FLASH_Latency_0);
    RCC_HCLKConfig(RCC_SYSCLK_Div1);
    RCC_PCLK2Config(RCC_HCLK_Div1);
    RCC_PCLK1Config(RCC_HCLK_Div1);

    RCC_PREDIV1Config(RCC_PREDIV1_Source_HSE,RCC_PREDIV1_Div1);
    RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_3);
    RCC_PLLCmd(ENABLE);

    while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

    while (RCC_GetSYSCLKSource() != 0x08)
    {
    }
}

void ADC_Configuration(void)
{
  ADC_InitTypeDef  ADC_InitStructure; //Katılım Sağlanıyor
  RCC_ADCCLKConfig(RCC_PCLK2_Div2); //ADC max 12 mhz olabilir
  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); //ADC ye Clock Uygulanıyor
  ADC_DeInit(ADC1);//Tüm Degerler Varsayılan Olarak Ayarlanıyor reset durumu sonrası

  /* ADC1 Config ------------------------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
  ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  ADC_InitStructure.ADC_NbrOfChannel = 1;

  ADC_Init(ADC1, &ADC_InitStructure); //Yukardaki tüm ayarlamalar ADC1 için Uygula
  ADC_Cmd(ADC1, ENABLE); //AÇ

  ADC_ResetCalibration(ADC1);
  while(ADC_GetResetCalibrationStatus(ADC1));

  ADC_StartCalibration(ADC1);
  while(ADC_GetCalibrationStatus(ADC1));
}


int readADC1(char channel)
{
  ADC_RegularChannelConfig(ADC1, channel, 1, ADC_SampleTime_1Cycles5);
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);   // Çevrime Başla
  while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); //Çevrim Bitene Kadar Bekle
  return ADC_GetConversionValue(ADC1); // ADC değerini Gönder
}



int main(void)
{
	int Deger_1,Deger_2,Deger_3;
	SetSysClockTo24();

		GPIO_InitTypeDef GPIO_InitStructure;
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;//Pin0,1,2 seçilecek
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//Alternatif IN olarak düzenlenecek
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//Port 50Mhz de çalışacak
		GPIO_Init(GPIOC, &GPIO_InitStructure);// Yukardaki tümişlemler GPIOC için yapılacak.

		ADC_Configuration();
    while(1)
    {
    	Deger_1=readADC1(10);
    	Deger_2=readADC1(11);
    	Deger_3=readADC1(12);
    }
}