#include "stm32f4xx.h" #include "stm32f4_discovery.h" int sek=0; void TIM3_IRQHandler(void) { if(TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) { switch(sek) { case 0: { GPIO_ResetBits(GPIOD,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15); GPIO_SetBits(GPIOD,GPIO_Pin_12); } break; case 1: { GPIO_ResetBits(GPIOD,GPIO_Pin_12); GPIO_SetBits(GPIOD,GPIO_Pin_14); } break; case 2: { GPIO_ResetBits(GPIOD,GPIO_Pin_14); GPIO_SetBits(GPIOD,GPIO_Pin_15); } break; case 3: { GPIO_ResetBits(GPIOD,GPIO_Pin_15); GPIO_SetBits(GPIOD,GPIO_Pin_13); } break; case 4: { GPIO_ResetBits(GPIOD,GPIO_Pin_13); } break; case 5: { GPIO_SetBits(GPIOD,GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15); } break; case 6: { GPIO_ResetBits(GPIOD,GPIO_Pin_12|GPIO_Pin_13); } break; } TIM_ClearITPendingBit(TIM3, TIM_IT_Update); } sek++; if(sek==7) sek=0; } int main(void) { NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_TimeBaseStructure.TIM_Period =4199; TIM_TimeBaseStructure.TIM_Prescaler = 9999; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode =TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); TIM_Cmd(TIM3, ENABLE); NVIC_InitTypeDef NVIC_InitStructure; // numer przerwania NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; // priorytet główny NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00; // subpriorytet NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; // uruchom dany kanał NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // zapisz wypełnioną strukturę do rejestrów NVIC_Init(&NVIC_InitStructure); TIM_ClearITPendingBit(TIM3, TIM_IT_Update); // zezwolenie na przerwania od przepełnienia dla timera 3 TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA , ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); GPIO_InitTypeDef GPIO_InitStructure2; //inicjalizacja wejścia ADC GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure2.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure2); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; /* Configure PD12, PD13, PD14 and PD15 in output pushpull mode */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOD, &GPIO_InitStructure); ADC_InitTypeDef ADC_InitStructure; //ustawienie rozdzielczości przetwornika na maksymalną (12 bitów) ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //wyłączenie trybu skanowania (odczytywać będziemy jedno wejście ADC //w trybie skanowania automatycznie wykonywana jest konwersja na wielu //wejściach/kanałach) ADC_InitStructure.ADC_ScanConvMode = DISABLE; //włączenie ciągłego trybu pracy ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //wyłączenie zewnętrznego wyzwalania //konwersja może być wyzwalana timerem, stanem wejścia itd. (szczegóły w //dokumentacji) ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //liczba konwersji równa 1, bo 1 kanał ADC_InitStructure.ADC_NbrOfConversion = 1; // zapisz wypełnioną strukturę do rejestrów przetwornika numer 1 ADC_Init(ADC1, &ADC_InitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_84Cycles); ADC_Cmd(ADC1, ENABLE); double ADC_Result; for(;;) { ADC_SoftwareStartConv(ADC1); while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); ADC_Result = ADC_GetConversionValue(ADC1); ADC_Result=ADC_Result/1365; } }//VCC-3V,ADC1-PA1,GND