STM32 is a 12-bit ADC

Publisher:Xiaoxue666Latest update time:2016-10-24 Source: eefocusKeywords:STM32  ADC Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
STM32 is a 12-bit ADC, so the measurement results are basically OK! The program uses DMA to transfer the ADC conversion value and increases the reading speed. The serial port part uses the code in the previous serial port debugging note.

/************************************************************************

Copyright (c) 2008 wormchen

All rights reserved

File name: main.c

Description: Send the ADC conversion value to the PC via the serial port

Main hardware: EMSTM32V1+miniSTMV100 (external 8MRC)

Compilation environment: MDK3.10

************************************************************************/

#include

#include

#define ADC1_DR_Address ((u32)0x4001244C)

#ifdef __GNUC__

/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf

set to 'Yes') calls __io_putchar() */

#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)

#else

#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)

#endif /* __GNUC__ */

vu16 ADC_ConvertedValue;

void RCC_Config(void);

void GPIO_Config(void);

void USART_Config(void);

void DMA_Config(void);

void ADC_Config(void);

void Put_String(u8 *p);

void Delay(vu32 nCount);

int main(void)

{

RCC_Config();

GPIO_Config();

USART_Config();

DMA_Config();

ADC_Config();

while(1)

{

Delay(0x8FFFF);

printf("ADC = %X Volt = %d mv\r\n", ADC_ConvertedValue, ADC_ConvertedValue*3300/4096);

}

}

/*************************************************

函数: void RCC_Config(void)

Function: Configure system clock

Parameters: None

Returns: None

**************************************************/

void RCC_Config(void)

{

ErrorStatus HSEStartUpStatus; //Define external high-speed crystal startup status enumeration variable

RCC_DeInit(); //Reset RCC external device registers to default values

RCC_HSEConfig(RCC_HSE_ON); //Turn on the external high-speed crystal oscillator

HSEStartUpStatus = RCC_WaitForHSEStartUp(); //Wait for the external high-speed clock to be ready

if(HSEStartUpStatus == SUCCESS) //External high-speed clock is ready

{

RCC_HCLKConfig(RCC_SYSCLK_Div1); //Configure AHB (HCLK) clock = SYSCLK

RCC_PCLK2Config(RCC_HCLK_Div1); //Configure APB2(PCLK2) clock = AHB clock

RCC_PCLK1Config(RCC_HCLK_Div2); //Configure APB1(PCLK1) clock = AHB 1/2 clock

RCC_ADCCLKConfig(RCC_PCLK2_Div4); //Configure ADC clock = PCLK2 1/4

RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

//Configure PLL clock == external high-speed crystal clock * 9

RCC_ADCCLKConfig(RCC_PCLK2_Div4); //Configure ADC clock = PCLK2/4

RCC_PLLCmd(ENABLE); //Enable PLL clock

while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //Wait for PLL clock to be ready

{

}

RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //Configure system clock = PLL clock

while(RCC_GetSYSCLKSource() != 0x08) //Check if PLL clock is used as system clock

{

}

}

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA, ENABLE); //Enable DMA clock

RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);

//Enable ADC1, GPIOC clock

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO, ENABLE);

//Turn on GPIOD and AFIO clocks

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //Enable serial port 2 clock

}

/*************************************************

函数: void GPIO_Config(void)

Function: GPIO configuration

Parameters: None

Returns: None

**************************************************/

void GPIO_Config(void)

{

//Set RTS (PD.04), Tx (PD.05) to push-pull output mode

GPIO_InitTypeDef GPIO_InitStructure; //Define GPIO initialization structure

GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE); //Enable GPIO port mapping USART2

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;//选择PIN4 PIN5

GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz; //Pin frequency 50M

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Pin settings push-pull output

GPIO_Init(GPIOD, &GPIO_InitStructure); //Initialize GPIOD

//Configure CTS (PD.03), USART2 Rx (PD.06) as floating point input mode

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_6;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOD, &GPIO_InitStructure);

//Configure PC4 as analog input

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;

GPIO_Init(GPIOC, &GPIO_InitStructure);

}

/*************************************************

函数: void DMA_Config(void)

Function: DMA configuration

Parameters: None

Returns: None

**************************************************/

void DMA_Config(void)

{

DMA_InitTypeDef DMA_InitStructure; //Define DMA initialization structure

DMA_DeInit(DMA_Channel1); //Reset DMA channel 1

DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //Define DMA channel peripheral base address = ADC1_DR_Address

DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; //Define DMA channel memory address

DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //Specify the peripheral as the source address

DMA_InitStructure.DMA_BufferSize = 1; //Define DMA buffer size 1

DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //The current peripheral register address remains unchanged

DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; //The current memory address remains unchanged

DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //Define the peripheral data width as 16 bits

DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //Define memory data width 16 bits

DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //DMA channel operation mode is circular buffer mode

DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA channel priority is high

DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //Disable DMA channel memory to memory transfer

DMA_Init(DMA_Channel1, &DMA_InitStructure); //Initialize DMA channel 1

DMA_Cmd(DMA_Channel1, ENABLE); //Enable DMA channel 1

}

/*************************************************

Function: void ADC_Config(void)

Function: ADC configuration

Parameters: None

Returns: None

**************************************************/

void ADC_Config(void)

{

ADC_InitTypeDef ADC_InitStructure; //Define ADC initialization structure variable

ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC1 and ADC2 work in independent mode

ADC_InitStructure.ADC_ScanConvMode = ENABLE; //Enable scanning

ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //ADC conversion works in continuous mode

ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //Conversion is controlled by software

ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //Conversion data right aligned

ADC_InitStructure.ADC_NbrOfChannel = 1; //Convert channel to channel 1

ADC_Init(ADC1, &ADC_InitStructure); //Initialize ADC

ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_28Cycles5);

//ADC1 selects channel 14, sequencer level 1, sampling time 239.5 cycles

ADC_DMACmd(ADC1, ENABLE); //Enable ADC1 module DMA

ADC_Cmd(ADC1, ENABLE); //Enable ADC1

ADC_ResetCalibration(ADC1); //Reset ADC1 calibration register

while(ADC_GetResetCalibrationStatus(ADC1)); //Wait for ADC1 calibration reset to complete

ADC_StartCalibration(ADC1); //Start ADC1 calibration

while(ADC_GetCalibrationStatus(ADC1)); //Wait for ADC1 calibration to complete

ADC_SoftwareStartConvCmd(ADC1, ENABLE); //Enable ADC1 software to start conversion

}

/*************************************************

Function: void USART_Config(void)

Function: USART configuration

Parameters: None

Returns: None

**************************************************/

void USART_Config(void)

{

USART_InitTypeDef USART_InitStructure; //Define the serial port initialization structure

USART_InitStructure.USART_BaudRate = 115200; //Baud rate 9600

USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8-bit data

USART_InitStructure.USART_StopBits = USART_StopBits_1; //1 stop bit

USART_InitStructure.USART_Parity = USART_Parity_No; //No parity bit

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

//Disable RTSCTS hardware flow control

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // Enable sending and receiving

USART_InitStructure.USART_Clock = USART_Clock_Disable; //Serial port clock disabled

USART_InitStructure.USART_CPOL = USART_CPOL_Low; //Clock falling edge is valid

USART_InitStructure.USART_CPHA = USART_CPHA_2Edge; //Data is captured at the second clock edge

USART_InitStructure.USART_LastBit = USART_LastBit_Disable;

//The clock pulse of the last data bit is not output to the SCLK pin

USART_Init(USART2, &USART_InitStructure); //Initialize serial port 2

USART_Cmd(USART2, ENABLE); //Enable serial port 2

}

/*************************************************

函数: void Put_String(void)

Function: Output string to serial port

Parameters: None

Returns: None

**************************************************/

void Put_String(u8 *p)

{

while(*p)

{

USART_SendData(USART2, *p++);

while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)

{

}

}

}

/*****************************************************

Function: void Delay(vu32 nCount)

Function: Delay for a specified time

Parameter: vu32 nCount Delay specified time

Returns: None

******************************************************/

void Delay(vu32 nCount)

{

for(; nCount != 0; nCount--);

}

/*****************************************************

Function: PUTCHAR_PROTOTYPE

Function: Redirect C library printf function

Parameters: None

Returns: None

*****************************************************/

PUTCHAR_PROTOTYPE

{

USART_SendData(USART2, (u8) ch); //Send one byte of data

while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)

{

}//Wait for sending to complete

return ch;

}

Keywords:STM32  ADC Reference address:STM32 is a 12-bit ADC

Previous article:There are two concepts of priority in STM32 (Cortex-M3)
Next article:Design of wireless sensor network system based on CC2430

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号