Facebook
From PussyHounter69, 6 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 210
  1. /**
  2.   ******************************************************************************
  3.   * @file    main.c
  4.   * @author  Ac6
  5.   * @version V1.0
  6.   * @date    01-December-2013
  7.   * @brief   Default main function.
  8.   ******************************************************************************
  9. */
  10.  
  11.  
  12. #include "stm32f4xx.h"
  13. #include "stm32f4_discovery.h"
  14.  
  15.  
  16. int ADC_Result=0;
  17.  
  18. int main(void)
  19. {
  20. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA , ENABLE);
  21. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  22.  
  23.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); // zegar dla modułu ADC1
  24.         GPIO_InitTypeDef  GPIO_InitStructure1;
  25.             /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
  26.             GPIO_InitStructure1.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
  27.             GPIO_InitStructure1.GPIO_Mode = GPIO_Mode_OUT;
  28.             GPIO_InitStructure1.GPIO_OType = GPIO_OType_PP;
  29.             GPIO_InitStructure1.GPIO_Speed = GPIO_Speed_100MHz;
  30.             GPIO_InitStructure1.GPIO_PuPd = GPIO_PuPd_NOPULL;
  31.             GPIO_Init(GPIOD, &GPIO_InitStructure1);
  32.  
  33.  
  34.  
  35.  
  36.  
  37.         GPIO_InitTypeDef GPIO_InitStructure;
  38.  
  39.         //inicjalizacja wejścia ADC
  40.  
  41.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  42.  
  43.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  44.  
  45.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  46.  
  47.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  48.  
  49.  
  50.         ADC_CommonInitTypeDef ADC_CommonInitStructure;
  51.  
  52.         // niezależny tryb pracy przetworników
  53.  
  54.         ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  55.  
  56.         // zegar główny podzielony przez 2
  57.  
  58.         ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  59.  
  60.         // opcja istotna tylko dla trybu multi ADC
  61.  
  62.         ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
  63.  
  64.         // czas przerwy pomiędzy kolejnymi konwersjami
  65.  
  66.         ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  67.  
  68.         ADC_CommonInit(&ADC_CommonInitStructure);
  69.  
  70.  
  71.         ADC_InitTypeDef ADC_InitStructure;
  72.  
  73.         //ustawienie rozdzielczości przetwornika na maksymalną (12 bitów)
  74.  
  75.         ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  76.  
  77.         //wyłączenie trybu skanowania (odczytywać będziemy jedno wejście ADC
  78.  
  79.         //w trybie skanowania automatycznie wykonywana jest konwersja na wielu //wejściach/kanałach)
  80.  
  81.         ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  82.  
  83.         //włączenie ciągłego trybu pracy
  84.  
  85.         ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  86.  
  87.         //wyłączenie zewnętrznego wyzwalania
  88.  
  89.         //konwersja może być wyzwalana timerem, stanem wejścia itd. (szczegóły w //dokumentacji)
  90.  
  91.         ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  92.  
  93.         ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  94.  
  95.         //wartość binarna wyniku będzie podawana z wyrównaniem do prawej
  96.  
  97.         //funkcja do odczytu stanu przetwornika ADC zwraca wartość 16-bitową
  98.  
  99.         //dla przykładu, wartość 0xFF wyrównana w prawo to 0x00FF, w lewo 0x0FF0
  100.  
  101.         ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  102.  
  103.         //liczba konwersji równa 1, bo 1 kanał
  104.  
  105.         ADC_InitStructure.ADC_NbrOfConversion = 1;
  106.  
  107.         // zapisz wypełnioną strukturę do rejestrów przetwornika numer 1
  108.  
  109.         ADC_Init(ADC1, &ADC_InitStructure);
  110.  
  111.  
  112.         ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_84Cycles);
  113.  
  114.  
  115.  
  116.  
  117.         ADC_Cmd(ADC1, ENABLE);
  118.  
  119.  
  120.  
  121.         for(;;)
  122.         {
  123.  
  124.                 ADC_SoftwareStartConv(ADC1);
  125.  
  126.                 while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
  127.                 ADC_Result = ADC_GetConversionValue(ADC1);
  128.  
  129.                 if(ADC_Result < 1000 && ADC_Result >500  )
  130.                 {
  131.                         GPIO_ResetBits(GPIOD,  GPIO_Pin_12 |  GPIO_Pin_13 |  GPIO_Pin_14|  GPIO_Pin_15);
  132.                         GPIO_SetBits(GPIOD, GPIO_Pin_13);
  133.  
  134.                 }
  135.                 else if(ADC_Result < 500 )
  136.                                 {
  137.                                         GPIO_ResetBits(GPIOD,  GPIO_Pin_12 |  GPIO_Pin_13 |  GPIO_Pin_14|  GPIO_Pin_15);
  138.  
  139.  
  140.                                 }
  141.                 else if(ADC_Result < 2000 && ADC_Result >1000  )
  142.                 {
  143.                         GPIO_ResetBits(GPIOD,  GPIO_Pin_12 |  GPIO_Pin_13 |  GPIO_Pin_14|  GPIO_Pin_15);
  144.                         GPIO_SetBits(GPIOD,  GPIO_Pin_12 |  GPIO_Pin_13);
  145.  
  146.                 }
  147.                 else if(ADC_Result < 3000 && ADC_Result >2000  )
  148.                                 {
  149.                         GPIO_ResetBits(GPIOD,  GPIO_Pin_12 |  GPIO_Pin_13 |  GPIO_Pin_14|  GPIO_Pin_15);
  150.                                         GPIO_SetBits(GPIOD,  GPIO_Pin_12 |  GPIO_Pin_13 |  GPIO_Pin_14);
  151.  
  152.                                 }
  153.                 else if(ADC_Result < 4000 && ADC_Result >3000  )
  154.                                                 {
  155.                         GPIO_ResetBits(GPIOD,  GPIO_Pin_12 |  GPIO_Pin_13 |  GPIO_Pin_14|  GPIO_Pin_15);
  156.                                                         GPIO_SetBits(GPIOD,  GPIO_Pin_12 |  GPIO_Pin_13 |  GPIO_Pin_14|  GPIO_Pin_15);
  157.  
  158.                                                 }
  159.         }
  160. }
  161.