Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f4xx.h"
- #include "stm32f4_discovery.h"
- unsigned int ADC_Result;
- double wynik;
- double w;
- void portDInit();
- void adcInit();
- void tim3Init();
- void dacInit();
- void tim3Run();
- void adcRun();
- void dacRun();
- int main(void) {
- //portDInit();
- adcInit();
- dacInit();
- //tim3Init();
- //tim3Run();
- adcRun();
- dacRun();
- DAC_SetChannel1Data(DAC_Align_12b_R, 0x682);
- for (;;) {
- ADC_SoftwareStartConv(ADC1);
- while (ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET)
- ;
- ADC_Result = ADC_GetConversionValue(ADC1);
- wynik = (double) ADC_Result;
- wynik = wynik * 2.95;
- wynik = wynik / 4095;
- w = wynik;
- }
- }
- void portDInit() {
- /* GPIOD Periph clock enable */
- 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);
- }
- void adcInit() {
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // zegar dla portu GPIO z którego wykorzystany zostanie pin jako wejście ADC (PA1)
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); // zegar dla modułu ADC1
- GPIO_InitTypeDef GPIO_InitStructure;
- //inicjalizacja wejścia ADC
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- ADC_CommonInitTypeDef ADC_CommonInitStructure;
- // niezależny tryb pracy przetworników
- ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
- // zegar główny podzielony przez 2
- ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
- // opcja istotna tylko dla trybu multi ADC
- ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
- // czas przerwy pomiędzy kolejnymi konwersjami
- ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
- ADC_CommonInit(&ADC_CommonInitStructure);
- 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;
- //wartość binarna wyniku będzie podawana z wyrównaniem do prawej
- //funkcja do odczytu stanu przetwornika ADC zwraca wartość 16-bitową
- //dla przykładu, wartość 0xFF wyrównana w prawo to 0x00FF, w lewo 0x0FF0
- 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);
- }
- void dacInit() {
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); // zegar dla portu GPIO z którego wykorzystany zostanie pin jako wyjście DAC (PA4)
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); // zegar dla modułu DAC
- GPIO_InitTypeDef GPIO_InitStructure;
- //inicjalizacja wyjścia DAC
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- DAC_InitTypeDef DAC_InitStructure;
- //wyłączenie zewnętrznego wyzwalania
- //konwersja może być wyzwalana timerem, stanem wejścia itd. (szczegóły w dokumentacji)
- DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
- //nast. 2 linie - wyłączamy generator predefiniowanych przebiegów //wyjściowych (wartości zadajemy sami, za pomocą odpowiedniej funkcji)
- DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
- DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
- //włączamy buforowanie sygnału wyjściowego
- DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
- DAC_Init(DAC_Channel_1, &DAC_InitStructure);
- }
- void tim3Init() {
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- /* Time base configuration */
- TIM_TimeBaseStructure.TIM_Period = 6279;
- TIM_TimeBaseStructure.TIM_Prescaler = 26751;
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
- }
- void tim3Run() {
- TIM_Cmd(TIM3, ENABLE);
- }
- void adcRun() {
- ADC_Cmd(ADC1, ENABLE);
- }
- void dacRun() {
- DAC_Cmd(DAC_Channel_1, ENABLE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement