Değerli arkadaş selamlar.
STM32F407VG Discovery kartında çalışırken kendi ekipmanımda bulunan adxl335 entegresi ile basit bir adc uygulaması yapmak istedim. İstediğim olay her eksenin belli bir uzaklığına göre STM32F407VG'nin üzerindeki ledlerin yanması. Kodu aşağıda paylaşacağım. Uygulamayı denerken tam olarak verim alamıyorum. Siz değerleri üyelerimiz gördüğünüz hataları feedback olarak belirtirse memnun kalırım. EDİT:STM ile çok yeni tanıştım 1 haftadır üzerinde uğraşıyorum kodlarımı CUBEMX yardımı olmadan keilde kendim kodluyorum.
#include "stm32f4xx.h" // Device header
uint16_t Read_ADC1(void)
{
ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_56Cycles);
ADC_SoftwareStartConv(ADC1);
while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==RESET);
return ADC_GetConversionValue(ADC1);
}
uint16_t Read_ADC2(void)
{
ADC_RegularChannelConfig(ADC2,ADC_Channel_0,1,ADC_SampleTime_56Cycles);
ADC_SoftwareStartConv(ADC2);
while(ADC_GetFlagStatus(ADC2,ADC_FLAG_EOC)==RESET);
return ADC_GetConversionValue(ADC2);
}
uint16_t Read_ADC3(void)
{
ADC_RegularChannelConfig(ADC3,ADC_Channel_0,1,ADC_SampleTime_56Cycles);
ADC_SoftwareStartConv(ADC3);
while(ADC_GetFlagStatus(ADC3,ADC_FLAG_EOC)==RESET);
return ADC_GetConversionValue(ADC3);
}
int main ()
{
uint16_t ADC_Data;
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA , ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2 , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3 , ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOD , &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN ;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP ;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, & GPIO_InitStructure);
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4 ;
ADC_CommonInit(& ADC_CommonInitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b ;
ADC_Init(ADC1 , & ADC_InitStructure);
ADC_Cmd(ADC1,ENABLE);
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4 ;
ADC_CommonInit(& ADC_CommonInitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b ;
ADC_Init(ADC2 , & ADC_InitStructure);
ADC_Cmd(ADC2,ENABLE);
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4 ;
ADC_CommonInit(& ADC_CommonInitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b ;
ADC_Init(ADC3 , & ADC_InitStructure);
ADC_Cmd(ADC3,ENABLE);
while(1)
{
ADC_Data = Read_ADC1 () ;
if(ADC_Data < 50)
{
GPIO_ResetBits(GPIOD , GPIO_Pin_12);
GPIO_ResetBits(GPIOD , GPIO_Pin_13);
GPIO_ResetBits(GPIOD , GPIO_Pin_14);
GPIO_ResetBits(GPIOD , GPIO_Pin_15);
}
else if ( 51 < ADC_Data && ADC_Data < 101)
{
GPIO_SetBits (GPIOD , GPIO_Pin_12);
GPIO_ResetBits(GPIOD , GPIO_Pin_13);
GPIO_ResetBits(GPIOD , GPIO_Pin_14);
GPIO_ResetBits(GPIOD , GPIO_Pin_15);
}
else if ( 100 < ADC_Data && ADC_Data < 151)
{
GPIO_SetBits (GPIOD , GPIO_Pin_12);
GPIO_SetBits (GPIOD , GPIO_Pin_13);
GPIO_ResetBits(GPIOD , GPIO_Pin_14);
GPIO_ResetBits(GPIOD , GPIO_Pin_15);
}
else if ( 150 < ADC_Data && ADC_Data < 201)
{
GPIO_SetBits (GPIOD , GPIO_Pin_12);
GPIO_SetBits (GPIOD , GPIO_Pin_13);
GPIO_SetBits (GPIOD , GPIO_Pin_14);
GPIO_ResetBits(GPIOD , GPIO_Pin_15);
}
else
{
GPIO_SetBits (GPIOD , GPIO_Pin_12);
GPIO_SetBits (GPIOD , GPIO_Pin_13);
GPIO_SetBits (GPIOD , GPIO_Pin_14);
GPIO_SetBits (GPIOD , GPIO_Pin_15);
}
ADC_Data = Read_ADC2 () ;
if(ADC_Data < 50)
{
GPIO_ResetBits(GPIOD , GPIO_Pin_12);
GPIO_ResetBits(GPIOD , GPIO_Pin_13);
GPIO_ResetBits(GPIOD , GPIO_Pin_14);
GPIO_ResetBits(GPIOD , GPIO_Pin_15);
}
else if ( 51 < ADC_Data && ADC_Data < 101)
{
GPIO_SetBits (GPIOD , GPIO_Pin_12);
GPIO_ResetBits(GPIOD , GPIO_Pin_13);
GPIO_ResetBits(GPIOD , GPIO_Pin_14);
GPIO_ResetBits(GPIOD , GPIO_Pin_15);
}
else if ( 100 < ADC_Data && ADC_Data < 151)
{
GPIO_SetBits (GPIOD , GPIO_Pin_12);
GPIO_SetBits (GPIOD , GPIO_Pin_13);
GPIO_ResetBits(GPIOD , GPIO_Pin_14);
GPIO_ResetBits(GPIOD , GPIO_Pin_15);
}
else if ( 150 < ADC_Data && ADC_Data < 201)
{
GPIO_SetBits (GPIOD , GPIO_Pin_12);
GPIO_SetBits (GPIOD , GPIO_Pin_13);
GPIO_SetBits (GPIOD , GPIO_Pin_14);
GPIO_ResetBits(GPIOD , GPIO_Pin_15);
}
else
{
GPIO_SetBits (GPIOD , GPIO_Pin_12);
GPIO_SetBits (GPIOD , GPIO_Pin_13);
GPIO_SetBits (GPIOD , GPIO_Pin_14);
GPIO_SetBits (GPIOD , GPIO_Pin_15);
}
ADC_Data = Read_ADC3 () ;
if(ADC_Data < 50)
{
GPIO_ResetBits(GPIOD , GPIO_Pin_12);
GPIO_ResetBits(GPIOD , GPIO_Pin_13);
GPIO_ResetBits(GPIOD , GPIO_Pin_14);
GPIO_ResetBits(GPIOD , GPIO_Pin_15);
}
else if ( 51 < ADC_Data && ADC_Data < 101)
{
GPIO_SetBits (GPIOD , GPIO_Pin_12);
GPIO_ResetBits(GPIOD , GPIO_Pin_13);
GPIO_ResetBits(GPIOD , GPIO_Pin_14);
GPIO_ResetBits(GPIOD , GPIO_Pin_15);
}
else if ( 100 < ADC_Data && ADC_Data < 151)
{
GPIO_SetBits (GPIOD , GPIO_Pin_12);
GPIO_SetBits (GPIOD , GPIO_Pin_13);
GPIO_ResetBits(GPIOD , GPIO_Pin_14);
GPIO_ResetBits(GPIOD , GPIO_Pin_15);
}
else if ( 150 < ADC_Data && ADC_Data < 201)
{
GPIO_SetBits (GPIOD , GPIO_Pin_12);
GPIO_SetBits (GPIOD , GPIO_Pin_13);
GPIO_SetBits (GPIOD , GPIO_Pin_14);
GPIO_ResetBits(GPIOD , GPIO_Pin_15);
}
else
{
GPIO_SetBits (GPIOD , GPIO_Pin_12);
GPIO_SetBits (GPIOD , GPIO_Pin_13);
GPIO_SetBits (GPIOD , GPIO_Pin_14);
GPIO_SetBits (GPIOD , GPIO_Pin_15);
}
}
}