Tutorial Microcontroller MCS-51 ATMEL ISP
 

 

Super MCS51 Trainer

Standart MCS51 Trainer

Standart AVR Trainer

Programmer USBASP

Standart ARM Trainer

 

Experiment ADC 2 channel with DMA

1. Prepare your circuit as shown in Fig.1


Fig.1. ADC interface

2. Include standart CMIS library

Fig.2. Using standart library CMSIS

3. Listing Program

#include <stm32f4xx.h>		 
#include "math.h"
#include "stm32f4xx_usart.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_adc.h"
#include "stm32f4xx_dma.h"
#include "misc.h"
#include "stm32f4xx_rcc.h"
#include "HD44780.h"
#include "stdlib.h"
#include "stdio.h"
//
__IO uint16_t ADCBuffer[2];
void usart_puts(char *data);
void USART3_PutChar(char c);
void initadc();
void Delay(__IO uint32_t nCount)
{ while(nCount--) { } }
void initadc()
{ //init pin adc GPIO_InitTypeDef GPIO_InitStructure; 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);
 //init DMA --> lihat tabel di ref manual
 DMA_InitTypeDef DMA_InitStructure;
 DMA_StructInit(&DMA_InitStructure);
 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
 DMA_InitStructure.DMA_Channel = DMA_Channel_0;
 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
 DMA_InitStructure.DMA_BufferSize = 2;
 DMA_InitStructure.DMA_Priority = DMA_Priority_High;
 DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
 DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
 DMA_InitStructure.DMA_FIFOThreshold = 0;
 DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
 DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
           /* config of memory */
 DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)ADCBuffer;
 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
           /* config of peripheral */
 DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
 DMA_Init(DMA2_Stream0, &DMA_InitStructure);
 DMA_Cmd(DMA2_Stream0, ENABLE);
 ADC_InitTypeDef ADC_InitStructure;
 ADC_CommonInitTypeDef ADC_CommonInitStructure;
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
 ADC_StructInit(&ADC_InitStructure);
 ADC_CommonStructInit(&ADC_CommonInitStructure);
 ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
 ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;
 ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
 ADC_CommonInit(&ADC_CommonInitStructure);
 ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b;
 ADC_InitStructure.ADC_ScanConvMode = ENABLE;
 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
 ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
 ADC_InitStructure.ADC_ExternalTrigConv = DISABLE;
 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
 ADC_InitStructure.ADC_NbrOfConversion = 2;
 ADC_Init(ADC1, &ADC_InitStructure);
 ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_480Cycles);
 ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 2, ADC_SampleTime_480Cycles);
 ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
 ADC_DMACmd(ADC1, ENABLE);
 ADC_Cmd(ADC1, ENABLE);
}

//

void init_USART3(uint32_t baudrate)
{
           GPIO_InitTypeDef GPIO_InitStruct;
           USART_InitTypeDef USART_InitStruct; 
           NVIC_InitTypeDef NVIC_InitStructure;
           RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
           RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
           GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; 
           GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; 
           GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; 
           GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; 
           GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
           GPIO_Init(GPIOD, &GPIO_InitStruct); 
           GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);
           GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3);
           USART_InitStruct.USART_BaudRate = baudrate; 
           USART_InitStruct.USART_WordLength = USART_WordLength_8b;
           USART_InitStruct.USART_StopBits = USART_StopBits_1; 
           USART_InitStruct.USART_Parity = USART_Parity_No; 
           USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;            
           USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; 
           USART_Init(USART3, &USART_InitStruct);
           USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
           NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; 
           NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
           NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
           NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
           NVIC_Init(&NVIC_InitStructure); 
           USART_Cmd(USART3, ENABLE);
}
           
void USART_puts(USART_TypeDef* USARTx, volatile char *s)
{
while(*s)
{ while( !(USARTx->SR & 0x00000040) ); USART_SendData(USARTx, *s); *s++; } }

int main(void)
{
char buf[20];
char DecimalNumber[5];
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);
init_USART3(115200); // initialize USART1 @ 9600 baud
initadc();
ADC_SoftwareStartConv(ADC1);
 while (1)
 {
           
  USART_SendData(USART3,ADCBuffer[0]); //ok dg. ComPort1.Read(Buffer,1);
  USART_SendData(USART3,ADCBuffer[1]); //ok dg. ComPort1.Read(Buffer,1);
           
  sprintf(buf,"%0.2f", (float_t)(ADCBuffer[0]*3.3)/4095);
  LCD_MoveToPosition(0x00);
  LCD_Print(buf); 
  sprintf(buf,"%0.2f", (float_t)(ADCBuffer[0]*3.3)/4095);
  LCD_MoveToPosition(0x40);
  LCD_Print(buf); 
  Delay(10000);
 }
}