Facebook
From Pussy hounter 69, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 240
  1. #include "stm32f4xx.h"
  2. #include "stm32f4_discovery.h"
  3.  
  4. float ADC_Result = 0;
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11. int main(void)
  12. {
  13.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA , ENABLE); // zegar dla portu GPIO z którego wykorzystany zostanie pin jako wyjście DAC (PA4)
  14.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); // zegar dla modułu DAC
  15.  
  16.  
  17.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA , ENABLE); // zegar dla portu GPIO z którego wykorzystany zostanie pin jako wejście ADC (PA1)
  18.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); // zegar dla modułu ADC1
  19.  
  20.  
  21.         GPIO_InitTypeDef GPIO_InitStructure;
  22.         //inicjalizacja wyjścia DAC
  23.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_1 ;
  24.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  25.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  26.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  27.         GPIO_Init(GPIOA, &GPIO_InitStructure);
  28.  
  29.  
  30.         ADC_CommonInitTypeDef ADC_CommonInitStructure;
  31.         ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  32.         ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  33.         ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
  34.         ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  35.         ADC_CommonInit(&ADC_CommonInitStructure);
  36.  
  37.  
  38.  
  39.         ADC_InitTypeDef ADC_InitStructure;
  40.         ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  41.         ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  42.         ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  43.         ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  44.         ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  45.         ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  46.         ADC_InitStructure.ADC_NbrOfConversion = 1;
  47.         ADC_Init(ADC1, &ADC_InitStructure);
  48.  
  49.  
  50.         ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_84Cycles);
  51.         ADC_Cmd(ADC1, ENABLE);
  52.  
  53.  
  54.  
  55.  
  56.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  57.         TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  58.  
  59.         /* Time base configuration */
  60.         TIM_TimeBaseStructure.TIM_Period = 28111;
  61.         TIM_TimeBaseStructure.TIM_Prescaler = 2988;
  62.         TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  63.         TIM_TimeBaseStructure.TIM_CounterMode =  TIM_CounterMode_Up;
  64.         TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
  65.  
  66.  
  67.  
  68.         unsigned int counter = TIM4->CNT;
  69.  
  70.         DAC_InitTypeDef DAC_InitStructure;
  71.         //wyłączenie zewnętrznego wyzwalania
  72.  
  73.         //konwersja może być wyzwalana timerem, stanem wejścia itd. (szczegóły w dokumentacji)
  74.  
  75.         DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
  76.         //nast. 2 linie - wyłączamy generator predefiniowanych przebiegów //wyjściowych (wartości zadajemy sami, za pomocą odpowiedniej funkcji)
  77.  
  78.         DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
  79.         DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
  80.  
  81.         //włączamy buforowanie sygnału wyjściowego
  82.  
  83.         DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
  84.         DAC_Init(DAC_Channel_1, &DAC_InitStructure);
  85.  
  86.         DAC_Cmd(DAC_Channel_1, ENABLE);
  87.  
  88.         DAC_SetChannel1Data(DAC_Align_12b_R, 0xFFF);
  89.  
  90.         float x=4095;
  91.  
  92.  
  93.         ADC_SoftwareStartConv(ADC1);
  94.  
  95.         int flag=0; //0-rosnie, 1-stala, 2-maleje
  96.  
  97.         for(;;)
  98.         {
  99.  
  100.                 while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
  101.                 if(flag==0)
  102.                 {
  103.                         x=4095-counter;
  104.                         ADC_Result = ADC_GetConversionValue(ADC1)/x-1;
  105.                 }
  106.                 if(flag==1)
  107.                 {
  108.                         x=1106;
  109.                         ADC_Result = ADC_GetConversionValue(ADC1)/x-1;
  110.                 }
  111.                 if(flag==2)
  112.                 {
  113.                         x=1106+counter;
  114.                         ADC_Result = ADC_GetConversionValue(ADC1)/x-1;
  115.                 }
  116.                 if(TIM_GetFlagStatus(TIM4, TIM_FLAG_Update)) {
  117.                                         flag++;
  118.                                         if(flag==3)
  119.                                         {
  120.                                                 flag=0;
  121.                                         }
  122.                                             TIM_ClearFlag(TIM4, TIM_FLAG_Update);
  123.  
  124.                 }
  125.  
  126.  
  127.  
  128.  
  129.  
  130.         }
  131.  
  132. }
  133.  
  134.  
  135.