/************************************************************************
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;
}
Previous article:There are two concepts of priority in STM32 (Cortex-M3)
Next article:Design of wireless sensor network system based on CC2430
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Sony employee stole $154 million in public funds and exchanged them for Bitcoin: when it was recovered, it appreciated to over $180 million
- 8-bit, 16-bit and 32-bit MCUs, are more bits better?
- [Evaluation of EVAL-M3-TS6-665PN development board] 3. Power-on test
- Get a gift for grabbing the post: Watch the live broadcast and gain an in-depth understanding of the principle, operation, and waterproof structure design of ST's latest MEMS barometer
- CPAFC Carrier Synchronization
- When should I use a PWM controller?
- Goodbye 2019, hello 2020!
- SensorTile.box unpacking issues
- Are the EX0 bit in the IE register and the IE0 bit in the TCON register in the 51 microcontroller duplicated?
- Detailed diagram of microwave oven capacitor connection