Facebook
From Oskar, 6 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 258
  1. #include "stm32f4xx.h"
  2. #include "stm32f4_discovery.h"
  3. int sek=0;
  4. void TIM3_IRQHandler(void)
  5. {
  6.                 if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET)
  7.                 {
  8.                         switch(sek)
  9.                         {
  10.                         case 0:
  11.                         {
  12.                                 GPIO_ResetBits(GPIOD,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  13.                                 GPIO_SetBits(GPIOD,GPIO_Pin_12);
  14.                         }
  15.                         break;
  16.                         case 1:
  17.                                         {
  18.                                                 GPIO_ResetBits(GPIOD,GPIO_Pin_12);
  19.                                                 GPIO_SetBits(GPIOD,GPIO_Pin_14);
  20.                                         }
  21.                         break;
  22.                         case 2:
  23.                         {
  24.                                 GPIO_ResetBits(GPIOD,GPIO_Pin_14);
  25.                                 GPIO_SetBits(GPIOD,GPIO_Pin_15);
  26.                         }
  27.                                 break;
  28.                         case 3:
  29.                         {
  30.                                 GPIO_ResetBits(GPIOD,GPIO_Pin_15);
  31.                                 GPIO_SetBits(GPIOD,GPIO_Pin_13);
  32.                         }
  33.                         break;
  34.                         case 4:
  35.                         {
  36.                                 GPIO_ResetBits(GPIOD,GPIO_Pin_13);
  37.                         }
  38.                         break;
  39.                         case 5:
  40.                         {
  41.                                 GPIO_SetBits(GPIOD,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15);
  42.                         }
  43.                         break;
  44.                         case 6:
  45.                         {
  46.                                 GPIO_ResetBits(GPIOD,GPIO_Pin_12|GPIO_Pin_13);
  47.                         }
  48.                         break;
  49.  
  50.                         }
  51.  
  52.                         TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  53.                 }
  54.                 sek++;
  55.                 if(sek==7)
  56.                         sek=0;
  57. }
  58.  
  59. int main(void)
  60. {
  61.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  62.  
  63. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  64.                                 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  65.                                 TIM_TimeBaseStructure.TIM_Period =4199;
  66.                                 TIM_TimeBaseStructure.TIM_Prescaler = 9999;
  67.                                 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  68.                                 TIM_TimeBaseStructure.TIM_CounterMode =TIM_CounterMode_Up;
  69.                                 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  70.                                 TIM_Cmd(TIM3, ENABLE);
  71.         NVIC_InitTypeDef NVIC_InitStructure;
  72.         // numer przerwania
  73.         NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  74.         // priorytet główny
  75.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
  76.         // subpriorytet
  77.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
  78.         // uruchom dany kanał
  79.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  80.         // zapisz wypełnioną strukturę do rejestrów
  81.         NVIC_Init(&NVIC_InitStructure);
  82.         TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
  83.         // zezwolenie na przerwania od przepełnienia dla timera 3
  84.         TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
  85.  
  86.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA , ENABLE);
  87.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
  88.         GPIO_InitTypeDef GPIO_InitStructure2;
  89.         //inicjalizacja wejścia ADC
  90.         GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_1;
  91.         GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AN;
  92.         GPIO_InitStructure2.GPIO_PuPd = GPIO_PuPd_NOPULL;
  93.         GPIO_Init(GPIOA, &GPIO_InitStructure2);
  94.  
  95.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  96.                 GPIO_InitTypeDef  GPIO_InitStructure;
  97.                 /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */
  98.                 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
  99.                 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  100.                 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  101.                 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  102.                 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  103.                 GPIO_Init(GPIOD, &GPIO_InitStructure);
  104.  
  105.                 ADC_InitTypeDef ADC_InitStructure;
  106.                 //ustawienie rozdzielczości przetwornika na maksymalną (12 bitów)
  107.                 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
  108.                 //wyłączenie trybu skanowania (odczytywać będziemy jedno wejście ADC
  109.                 //w trybie skanowania automatycznie wykonywana jest konwersja na wielu //wejściach/kanałach)
  110.                 ADC_InitStructure.ADC_ScanConvMode = DISABLE;
  111.                 //włączenie ciągłego trybu pracy
  112.                 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
  113.                 //wyłączenie zewnętrznego wyzwalania
  114.                 //konwersja może być wyzwalana timerem, stanem wejścia itd. (szczegóły w //dokumentacji)
  115.                 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
  116.                 ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
  117.                 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
  118.                 //liczba konwersji równa 1, bo 1 kanał
  119.                 ADC_InitStructure.ADC_NbrOfConversion = 1;
  120.                 // zapisz wypełnioną strukturę do rejestrów przetwornika numer 1
  121.                 ADC_Init(ADC1, &ADC_InitStructure);
  122.                 ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_84Cycles);
  123.                 ADC_Cmd(ADC1, ENABLE);
  124.  
  125.  
  126.  
  127.  
  128.  
  129.                 double ADC_Result;
  130.                 for(;;)
  131.                 {
  132.  
  133.                         ADC_SoftwareStartConv(ADC1);
  134.                         while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
  135.                         ADC_Result = ADC_GetConversionValue(ADC1);
  136.                         ADC_Result=ADC_Result/1365;
  137.                 }
  138. }//VCC-3V,ADC1-PA1,GND