This program was created to test the voltage from a potentiometer on LED diodes Using ADC .
if: v < 1 => PD12_ON
1 < v < 2 => PD12_PD12_ON
2 < v < 3 => PD12_PD13_PD14_ON
3 < v < 5 => PD12_PD13_PD14_PD15_ON .
/**
* STM32F4 Discovery
* @author OUESALTI SABRI
* @email sabrioueslati1@gmail.com
* @website http://electronicsabriart.blogspot.com/
* @ide IAR Workbench
*/
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_tim.h"
void delais (int t){
int i ;
for (i=0;i<t*10000;i++) {}
}
void InitializeLEDs()
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_InitTypeDef gpioStructure;
gpioStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
gpioStructure.GPIO_Mode = GPIO_Mode_OUT;
gpioStructure.GPIO_Speed = GPIO_Speed_50MHz;
gpioStructure.GPIO_OType=GPIO_OType_PP ;
gpioStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOD, &gpioStructure);
}
void adc_config()
{
ADC_DeInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_InitTypeDef ADC_Initstructure ;
ADC_Initstructure.ADC_Resolution=ADC_Resolution_8b ;
ADC_Initstructure.ADC_ScanConvMode=DISABLE ;
ADC_Initstructure.ADC_ContinuousConvMode=DISABLE;
ADC_Initstructure.ADC_ExternalTrigConvEdge=ADC_ExternalTrigConvEdge_None;
ADC_Initstructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_T1_CC1 ;
ADC_Initstructure.ADC_DataAlign=ADC_DataAlign_Right;
ADC_Initstructure.ADC_NbrOfConversion= 1 ;
ADC_Cmd(ADC1,ENABLE);
ADC_Init(ADC1,&ADC_Initstructure);
ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_56Cycles);
}
int adc_convert () {
ADC_SoftwareStartConv(ADC1);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));//Processing the conversion
return ADC_GetConversionValue(ADC1); //Return the converted data
}
void adc_pin()
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitTypeDef gpioStructure;
gpioStructure.GPIO_Pin = GPIO_Pin_0;
gpioStructure.GPIO_Mode = GPIO_Mode_AN;
gpioStructure.GPIO_Speed = GPIO_Speed_50MHz;
gpioStructure. GPIO_PuPd=GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &gpioStructure);
}
int main()
{
double value,result =0 ;
InitializeLEDs();
adc_pin() ;
adc_config() ;
while(1){
result =(double) adc_convert() ;
value=(result*3)/241 ;
if(value<1 )
{
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(value>=1 && value<2)
{
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(value>2&& value<3)
{
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 if(value>3&& value<5)
{
GPIO_SetBits(GPIOD,GPIO_Pin_12);
GPIO_SetBits(GPIOD,GPIO_Pin_13);
GPIO_SetBits(GPIOD,GPIO_Pin_14);
GPIO_SetBits(GPIOD,GPIO_Pin_15);
}
}
}
Aucun commentaire:
Enregistrer un commentaire