Skip to content

Instantly share code, notes, and snippets.

@ozturkib
Last active December 17, 2019 12:24
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ozturkib/218ca9056cdc7fad57f3d118121d4deb to your computer and use it in GitHub Desktop.
#include "main.h"
/*
Projede Drivers/STM32F4xx_HAL_Driver altinda kullanilacak .c dosyasinin bulunmamasinin adimlari (adc icin):
Oncesinde uygun yerlere asagidakileri indir.
stm32f4xx_hal_adc.c
stm32f4xx_hal_adc_ex.c
stm32f4xx_hal_adc.h
stm32f4xx_hal_adc_ex.h
Projeye ADC icin asagidaki iki kutuphaneyi ekle
stm32f4xx_hal_adc.c
stm32f4xx_hal_adc_ex.c
stm32f4xx_hal_conf.h icerisindeki
#define HAL_ADC_MODULE_ENABLED satirini aktiflestir.
*/
/*
STM32F4Discovery board with the STM32F407VG microcontroller
We will use the PC1 pin connected to channel 11 of ADCs 1-3 according to the STM32F407xx datasheet.
The easiest way to start using the ADC is to enable it in the poll mode. To do that you will need the following:
- Enable the ADC clock
- Enable the GPIO clock for the pin you want to use
- Configure the GPIO pin as an analog input
- Configure the ADC speed (prescaler/sample time)
- Enable continuous measurement mode
*/
/*
UYARI:
Derste kullandigimiz pin olan PC1 pin'ini odev ve projelerde kullanamazsiniz.
*/
/*
Derste bahsedilecekler
Polling Mode
Vin < 0, 0 <= Vin <= Vref , Vin > Vref
Belirlenen aralikta ise formulunu cikar
*/
static uint32_t gADCValue;//gADCdegeri;
static uint32_t gMeasurementNumber;
static ADC_HandleTypeDef gAdcHandle;
void ADC_Polling_App(void)
{
//ADC_DevamliOku
/*
Potansiyometre giris okunma
PC1 pinine bagla
ADC123_IN11
*/
if (HAL_ADC_PollForConversion(&gAdcHandle, 1000000) == HAL_OK)
{
gADCValue = HAL_ADC_GetValue(&gAdcHandle);
gMeasurementNumber++;
}
}
void ADC_Config(void)
{
//ADC_Tanimlama
GPIO_InitTypeDef GPIO_InitStruct;
ADC_ChannelConfTypeDef ADC_ChannelStruct;
__HAL_RCC_GPIOC_CLK_ENABLE(); /* GPIO C portunun clock'unu aktiflestir */
__HAL_RCC_ADC1_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
HAL_NVIC_SetPriority(ADC_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(ADC_IRQn);
gAdcHandle.Instance = ADC1;
gAdcHandle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV2;
gAdcHandle.Init.Resolution = ADC_RESOLUTION_12B;
gAdcHandle.Init.ScanConvMode = DISABLE; // 1 Channel
gAdcHandle.Init.ContinuousConvMode = ENABLE;
gAdcHandle.Init.DiscontinuousConvMode = DISABLE;
gAdcHandle.Init.NbrOfDiscConversion = 0;
gAdcHandle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
gAdcHandle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
gAdcHandle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
gAdcHandle.Init.NbrOfConversion = 1;
gAdcHandle.Init.DMAContinuousRequests = ENABLE;
gAdcHandle.Init.EOCSelection = DISABLE;
HAL_ADC_Init(&gAdcHandle);
ADC_ChannelStruct.Channel = ADC_CHANNEL_11;
ADC_ChannelStruct.Rank = 1;
ADC_ChannelStruct.SamplingTime = ADC_SAMPLETIME_480CYCLES;
ADC_ChannelStruct.Offset = 0;
if (HAL_ADC_ConfigChannel(&gAdcHandle, &ADC_ChannelStruct) != HAL_OK)
{
Error_Handler();
}
}
void ADC_Start_Poll(void)
{
//ADC_Baslat
HAL_ADC_Start(&gAdcHandle);
}
uint32_t getValue(void)
{
return gADCValue;
}
/////////////////////////
void ADC_Start_IT(void)
{
HAL_ADC_Start_IT(&gAdcHandle);
}
/*
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* AdcHandle)
{
gADCValue = HAL_ADC_GetValue(AdcHandle);
}
void ADC_IRQHandler()
{
HAL_ADC_IRQHandler(&gAdcHandle);
}
*/
/////////////////////////
void TIM2_IRQHandler(void)
{
HAL_TIM_IRQHandler(&TIM_InitStruct);
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_15);
}
#include "main.h"
/*
stm32f4xx_hal_conf.h icerisindeki
#define HAL_TIM_MODULE_ENABLED satirini aktiflestir.
stm32f4xx_it.c ve
- extern TIM_HandleTypeDef TIM_InitStruct;
- void TIM2_IRQHandler(void) icerigi
stm32f4xx_it.h
- decleration of void TIM2_IRQHandler(void);
*/
/*
Global Variables
*/
TIM_HandleTypeDef TIM_InitStruct;
void TIMER_Init(void)
{
__HAL_RCC_TIM2_CLK_ENABLE(); //Enable clock to Timer-2
/* Select Timer-2 for further configuration */
TIM_InitStruct.Instance = TIM2;
/*
Ana clock frekansi = 16 MHz
Eger Prescaler = 1000 olursa
Prescaler cikisinda 16MHz / 1000 = 16kHz
*/
/*
Divide the timer-2 input frequency (16Mhz)
by a factor of 1000 (16,000,000/1,000 = 16,000 = 16Khz)
*/
TIM_InitStruct.Init.Prescaler = 1000;
/* Up-Counter mode*/
TIM_InitStruct.Init.CounterMode = TIM_COUNTERMODE_UP;
/*
We want the time count to be 500msec (half a second).
As the input frequency is 16khz so the total
counts required for 500msec delay:
total counts = 500msec * f
= (.5 sec) * 16,000
= 8,000
= 0x1F40
*/
/*
istedigim period = 0.5sn
Period >>> 16kHz * T = 16kHz * 0.5 = 8000
*/
TIM_InitStruct.Init.Period = 8000;
/*
Finally initialize Timer-2
*/
while (HAL_TIM_Base_Init(&TIM_InitStruct)!= HAL_OK);
/*
Enable timer-2 IRQ interrupt
*/
HAL_TIM_Base_Start_IT(&TIM_InitStruct);
HAL_NVIC_SetPriority(TIM2_IRQn, 1, 0);
/* Enable interrupt at IRQ-Level */
HAL_NVIC_EnableIRQ(TIM2_IRQn);
/*
Start the timer
*/
HAL_TIM_Base_Start(&TIM_InitStruct);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment