Tutorial Microcontroller MCS-51 ATMEL ISP
 

 

Super MCS51 Trainer

Standart MCS51 Trainer

Standart AVR Trainer

Programmer USBASP

Standart ARM Trainer

  6. Experiment ADC

Fig.1. Interfacing Analog to board ARM STM32F4

Fig.2. ADC interface

Fig.3. CMIS Library for ADC experiment

 







#include <stm32f4xx.h>		 // I recommend you have a look at these in the ST firmware folder
#include "math.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_adc.h"
#include "misc.h"
#include "stm32f4xx_rcc.h"
#include "HD44780.h"
#include "stdlib.h"
#include "stdio.h"
//

void ADCinit_SoftTrigger(void);
//
void Delay(__IO uint32_t nCount) {
while(nCount--) {
}
}
//
void ADCinit_SoftTrigger(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2, ENABLE); 
 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); 
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_2; 
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; 
 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; 
 GPIO_Init(GPIOA, &GPIO_InitStructure); 
 ADC_CommonInitStructure.ADC_Mode = ADC_DualMode_RegSimult; 
 ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; 
 ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;            
 ADC_CommonInit(&ADC_CommonInitStructure); 
 ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; 
 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_Init(ADC1, &ADC_InitStructure); 
 ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_3Cycles);            
 ADC_Init(ADC2, &ADC_InitStructure); 
 ADC_RegularChannelConfig(ADC2, ADC_Channel_2, 1, ADC_SampleTime_3Cycles);            
 ADC_Cmd(ADC1, ENABLE); 
 ADC_Cmd(ADC2, ENABLE); 
}
int main(void)
{
 unsigned int i,ADC_Val1;
 float Voltage;
 char FloatNumber1[10];
 LCD_ConfigurePort(GPIOD,GPIO_Pin_7, GPIO_Pin_5, GPIO_Pin_6,0, 0, 0,0,GPIO_Pin_0, GPIO_Pin_1, GPIO_Pin_2, GPIO_Pin_3);
 // RS RW EN D4 D5 D6 D7
 LCD_Initalize(BUS_WIDTH_4, DISPLAY_LINES_2, FONT_5x8);
 ADCinit_SoftTrigger();
 while (1)
 {
           ADC_SoftwareStartConv(ADC1);
           while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET)
           {};
           ADC_Val1 = ADC_GetConversionValue(ADC1);
           Voltage = (float)ADC_Val1*3.3/4095;
           sprintf(FloatNumber1,"%.4f ",Voltage);
           LCD_MoveToPosition(0x40);
           LCD_Print(FloatNumber1);
           Delay (100000);
           }
}