Advertisement
andrzejd-pl

zad4DAC

Jan 16th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.28 KB | None | 0 0
  1. #include "stm32f4xx.h"
  2. #include "stm32f4_discovery.h"
  3.  
  4. unsigned int ADC_Result;
  5. double wynik;
  6. double w;
  7.  
  8. void portDInit();
  9. void adcInit();
  10. void tim3Init();
  11. void dacInit();
  12.  
  13. void tim3Run();
  14. void adcRun();
  15. void dacRun();
  16.  
  17. int main(void) {
  18.  
  19.     //portDInit();
  20.     adcInit();
  21.     dacInit();
  22.     //tim3Init();
  23.  
  24.     //tim3Run();
  25.     adcRun();
  26.     dacRun();
  27.  
  28.     DAC_SetChannel1Data(DAC_Align_12b_R, 0x682);
  29.  
  30.     for (;;) {
  31.         ADC_SoftwareStartConv(ADC1);
  32.         while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET)
  33.             ;
  34.         ADC_Result = ADC_GetConversionValue(ADC1);
  35.         wynik = (double) ADC_Result;
  36.         wynik = wynik * 2.95;
  37.         wynik = wynik / 4095;
  38.         w = wynik;
  39.     }
  40.  
  41. }
  42.  
  43. void portDInit() {
  44.     /* GPIOD Periph clock enable */
  45.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  46.  
  47.     GPIO_InitTypeDef GPIO_InitStructure;
  48.     /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
  49.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14
  50.             | GPIO_Pin_15;
  51.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  52.     GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  53.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  54.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  55.     GPIO_Init(GPIOD, &GPIO_InitStructure);
  56. }
  57.  
  58. void adcInit() {
  59.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // zegar dla portu GPIO z którego wykorzystany zostanie pin jako wejście ADC (PA1)
  60.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); // zegar dla modułu ADC1
  61.  
  62.     GPIO_InitTypeDef GPIO_InitStructure;
  63.     //inicjalizacja wejścia ADC
  64.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  65.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  66.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  67.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  68.  
  69.     ADC_CommonInitTypeDef ADC_CommonInitStructure;
  70.     // niezależny tryb pracy przetworników
  71.     ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
  72.     // zegar główny podzielony przez 2
  73.     ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
  74.     // opcja istotna tylko dla trybu multi ADC
  75.     ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
  76.     // czas przerwy pomiędzy kolejnymi konwersjami
  77.     ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
  78.     ADC_CommonInit(&ADC_CommonInitStructure);
  79.  
  80.     ADC_InitTypeDef ADC_InitStructure;
  81.     //ustawienie rozdzielczości przetwornika na maksymalną (12 bitów)
  82.     ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  83.     //wyłączenie trybu skanowania (odczytywać będziemy jedno wejście ADC
  84.     //w trybie skanowania automatycznie wykonywana jest konwersja na wielu //wejściach/kanałach)
  85.     ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  86.     //włączenie ciągłego trybu pracy
  87.     ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  88.     //wyłączenie zewnętrznego wyzwalania
  89.     //konwersja może być wyzwalana timerem, stanem wejścia itd. (szczegóły w //dokumentacji)
  90.     ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  91.     ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  92.     //wartość binarna wyniku będzie podawana z wyrównaniem do prawej
  93.     //funkcja do odczytu stanu przetwornika ADC zwraca wartość 16-bitową
  94.     //dla przykładu, wartość 0xFF wyrównana w prawo to 0x00FF, w lewo 0x0FF0
  95.     ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  96.     //liczba konwersji równa 1, bo 1 kanał
  97.     ADC_InitStructure.ADC_NbrOfConversion = 1;
  98.     // zapisz wypełnioną strukturę do rejestrów przetwornika numer 1
  99.     ADC_Init(ADC1, &ADC_InitStructure);
  100.  
  101.     ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_84Cycles);
  102.  
  103. }
  104.  
  105. void dacInit() {
  106.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // zegar dla portu GPIO z którego wykorzystany zostanie pin jako wyjście DAC (PA4)
  107.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); // zegar dla modułu DAC
  108.  
  109.     GPIO_InitTypeDef GPIO_InitStructure;
  110.     //inicjalizacja wyjścia DAC
  111.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  112.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
  113.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  114.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  115.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  116.  
  117.     DAC_InitTypeDef DAC_InitStructure;
  118.     //wyłączenie zewnętrznego wyzwalania
  119.     //konwersja może być wyzwalana timerem, stanem wejścia itd. (szczegóły w dokumentacji)
  120.     DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
  121.     //nast. 2 linie - wyłączamy generator predefiniowanych przebiegów //wyjściowych (wartości zadajemy sami, za pomocą odpowiedniej funkcji)
  122.     DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
  123.     DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
  124.     //włączamy buforowanie sygnału wyjściowego
  125.     DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
  126.     DAC_Init(DAC_Channel_1, &DAC_InitStructure);
  127.  
  128. }
  129.  
  130. void tim3Init() {
  131.  
  132.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  133.  
  134.     TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  135.  
  136.     /* Time base configuration */
  137.     TIM_TimeBaseStructure.TIM_Period = 6279;
  138.     TIM_TimeBaseStructure.TIM_Prescaler = 26751;
  139.     TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  140.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  141.     TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  142.  
  143. }
  144.  
  145. void tim3Run() {
  146.     TIM_Cmd(TIM3, ENABLE);
  147. }
  148.  
  149. void adcRun() {
  150.     ADC_Cmd(ADC1, ENABLE);
  151. }
  152.  
  153. void dacRun() {
  154.     DAC_Cmd(DAC_Channel_1, ENABLE);
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement