#include "stm32f10x_lib.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADC1_DR_Address ((u32)0x4001244C)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
vu16 ADCConvertedValue;
ErrorStatus HSEStartUpStatus;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : main
* Description : Main program
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
/* System clocks configuration ---------------------------------------------*/
RCC_Configuration();
/* NVIC configuration ------------------------------------------------------*/
NVIC_Configuration();
/* GPIO configuration ------------------------------------------------------*/
GPIO_Configuration();
/* DMA1 channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA1_Channel1); //復(fù)位DMA1的通道1
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;//DMA對應(yīng)的外設(shè)基礎(chǔ)地址,,這個地址可以在相應(yīng)的DATASHEET上查
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADCConvertedValue; //定義DMA通道存儲器地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //指定外設(shè)為源地址
DMA_InitStructure.DMA_BufferSize = 1; //定義DMA緩存大小為1
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //接收一次后,外設(shè)寄存器地址不變,。
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; //設(shè)置DMA內(nèi)存模式為非遞增模式,。ENABLE?
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //定義外設(shè)數(shù)據(jù)寬度為16位,。
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //定義存儲器數(shù)據(jù)寬度為16位,。
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //設(shè)置DMA為連續(xù)循環(huán)的傳輸模式。
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //高優(yōu)先級
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //禁止DMA通道的M2M傳輸模式,。
DMA_Init(DMA1_Channel1, &DMA_InitStructure); //初始化DMA通道1
/* Enable DMA1 channel1 */
DMA_Cmd(DMA1_Channel1, ENABLE); //使能DMA通道1
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //設(shè)置轉(zhuǎn)換模式為獨立轉(zhuǎn)換 ,。
ADC_InitStructure.ADC_ScanConvMode = ENABLE; //開啟掃描轉(zhuǎn)換模式。
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; // 開啟連續(xù)轉(zhuǎn)換模式,。
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //關(guān)閉ADC外部轉(zhuǎn)換觸發(fā),。
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //設(shè)置為右對齊方式。
ADC_InitStructure.ADC_NbrOfChannel = 1; //開啟通道數(shù)1個,。
ADC_Init(ADC1, &ADC_InitStructure); //ADC初始化 ,。
/* ADC1 regular channel14 configuration */
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_55Cycles5); //規(guī)則組通道設(shè)置
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE); //和DMA關(guān)聯(lián)
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE); //開戶ADC1
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1); //重置校準。
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1)); //等待重置校準完成,。
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1); //開始較準,。
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1)); //等待校準完成。
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //連續(xù)轉(zhuǎn)換開始,。
while (1)
{
}
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void) //配置系統(tǒng)時鐘
{
/* RCC system reset(for debug purpose) */
RCC_DeInit(); //復(fù)位RCC
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON); //使能外部晶振
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待外部晶振穩(wěn)定
if(HSEStartUpStatus == SUCCESS) //如果外部晶振啟動成功則進行下一步操作,。
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //選擇FLASH預(yù)取指緩存的模式
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2); //設(shè)置FLASH存儲器延時時鐘周期數(shù)。
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1); //設(shè)置AHB時鐘=系統(tǒng)時鐘
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1); //設(shè)置APB2=HCLK時鐘
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2); //設(shè)置APB1時鐘=HCLK時鐘/2,。
/* ADCCLK = PCLK2/4 */
RCC_ADCCLKConfig(RCC_PCLK2_Div4); // 設(shè)置ADC時鐘=APB2時鐘/4,。
/* PLLCLK = 8MHz * 7 = 56 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_7); //設(shè)置PLL時鐘源和PLL的輸入時鐘=7倍的HCLK
/* Enable PLL */
RCC_PLLCmd(ENABLE); //使能PLL
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //等待PLL穩(wěn)定。
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //系統(tǒng)時鐘來自PLL
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08) //切換時鐘后等待系統(tǒng)穩(wěn)定。PLL返回用作系統(tǒng)時鐘的時鐘源,。0x08:PLL作為系統(tǒng)時鐘
{
}
}
/* Enable peripheral clocks --------------------------------------------------*/
/* Enable DMA1 clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //啟動DMA1的時鐘
/* Enable ADC1 and GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);//啟動ADC1和GPIOC的時鐘,。
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure PC.04 (ADC Channel14) as analog input -------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
}
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* Input : - file: pointer to the source file name
* - line: assert_param error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif