14 Eylül 2018 Cuma

STM32 VL Discovery ile ARM Programlama - Analog to Digital Converter (ADC Okuma)


Bu kayıdımda STM32VLDISCOVERY kartı ile ADC okuma videom ve kodlar aşağıda.










#include "stm32f10x.h"
#include "stm32f10x_adc.h"
#include "stdio.h"
#include "delay.h"
#include "lcd.h"


//void ADC1_Init(void);
//uint16_t ADC1_Read(void);

uint16_t adcValue;
char sAdcValue[5];

void ADC1_Init()
{
 // Initialization struct
 ADC_InitTypeDef ADC_InitStruct;
 GPIO_InitTypeDef GPIO_InitStruct;
 
 
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
 ADC_InitStruct.ADC_ContinuousConvMode = DISABLE; // Sürekli Mod Iptal, Tek Çevrim isteniyor
 ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right; // Data Hizalamasi Sagdan ve Bastan Baslayacak    D11,...........D0
 ADC_InitStruct.ADC_ExternalTrigConv = DISABLE;  // Dis Tetikleyici Kapali
 ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;  // Dis Tetikleyici Yok
 ADC_InitStruct.ADC_Mode = ADC_Mode_Independent;  // Tek çevrim modu
 ADC_InitStruct.ADC_NbrOfChannel = 1;
 ADC_InitStruct.ADC_ScanConvMode = DISABLE;  // Tarama Modu Kapali, coklu adc okumada is yapar. Tek kanal okuyoruz biz
 ADC_Init(ADC1, &ADC_InitStruct);
 ADC_Cmd(ADC1, ENABLE);
 
 // ADC1 channel 0 (PA0)
 ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_55Cycles5); 
 
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
 GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;
 GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
 GPIO_Init(GPIOA, &GPIO_InitStruct);
}

uint16_t ADC1_Read()
{
 ADC_SoftwareStartConvCmd(ADC1, ENABLE);  // ADC Cevrimine basla
 while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));  // End of Conversion biti 1 olana kadar bekler burda 

 return ADC_GetConversionValue(ADC1); 

}

int main(void)
{
 lcd_init();
 delay_init(); 
 ADC1_Init();
 
 while (1)
 {
  lcd_line1();   
  adcValue = ADC1_Read();  
  lcd_print("ADC Degeri: ");
  sprintf(sAdcValue, "%d   ", adcValue);      
  lcd_print(sAdcValue);  
  delay_ms(50);  
 }
}

Hiç yorum yok:

Yorum Gönder