当前位置:文档之家› STM32的ADC(看门狗)(免费)

STM32的ADC(看门狗)(免费)

00001 /**
00002 ******************************************************************************
00003 * @file ADC/AnalogWatchdog/main.c
00004 * @author MCD Application Team
00005 * @version V3.5.0
00006 * @date 08-April-2011
00007 * @brief Main program body
00008 ******************************************************************************
00009 * @attention
00010 *
00011 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00012 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00013 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00014 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00015 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00016 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00017 *
00018 *

© COPYRIGHT 2011 STMicroelectronics


00019 ******************************************************************************
00020 */
00021
00022 /* Includes ------------------------------------------------------------------*/
00023 #include "stm32f10x.h"
00024 #include "stm32_eval.h"
00025
00026 /** @addtogroup STM32F10x_StdPeriph_Examples
00027 * @{
00028 */
00029
00030 /** @addtogroup ADC_AnalogWatchdog
00031 * @{
00032 */
00033
00034 /* Private typedef -----------------------------------------------------------*/
00035 /* Private define ------------------------------------------------------------*/
00036 /* Private macro -------------------------------------------------------------*/
00037 /* Private variables ---------------------------------------------------------*/
00038 ADC_InitTypeDef ADC_InitStructure;
00039
00040 /* Private function prototypes -----------------------------------------------*/
00041 void RCC_Configuration(void);
00042 void GPIO_Configuration(void);
00043 void NVIC_Configuration(void);
00044
00045 /* Private functions ---------------------------------------------------------*/
00046
00047 /**
00048 * @brief Main program
00049 * @param None
00050 * @retval None
00051 */
00052 int main(void)
00053 {
00054 /*!< At this stage the microcontroller clock setting is already configured,
00055 this is done through SystemInit() function which is called from startup
00056 file (startup_stm32f10x_xx.s) before to branch to application main.
00057 To reconfigure the default setting of SystemInit() function, refer to
00058 system_stm32f10x.c file
00059 */
00060
00061 /* System clocks configuration ---------------------------------------------*/
00062 RCC_Configuration();
00063
00064 /* NVIC configuration ------------------------------------------------------*/
00065 NVIC_Configuration();
00066
00067 /* GPIO configuration ------------------------

------------------------------*/
00068 GPIO_Configuration();
00069
00070 /* Configure LED GPIO Pin ------------------------------------------------- */
00071 STM_EVAL_LEDInit(LED1);
00072
00073 /* ADC1 Configuration ------------------------------------------------------*/
00074 ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
00075 ADC_InitStructure.ADC_ScanConvMode = DISABLE;
00076 ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
00077 ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
00078 ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
00079 ADC_InitStructure.ADC_NbrOfChannel = 1;
00080 ADC_Init(ADC1, &ADC_InitStructure);
00081
00082 /* ADC1 regular channel14 configuration */
00083 ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_13Cycles5);
00084
00085 /* Configure high and low analog watchdog thresholds */
00086 ADC_AnalogWatchdogThresholdsConfig(ADC1, 0x0B00, 0x0300);
00087 /* Configure channel14 as the single analog watchdog guarded channel */
00088 ADC_AnalogWatchdogSingleChannelConfig(ADC1, ADC_Channel_14);
00089 /* Enable analog watchdog on one regular channel */
00090 ADC_AnalogWatchdogCmd(ADC1, ADC_AnalogWatchdog_SingleRegEnable);
00091
00092 /* Enable AWD interrupt */
00093 ADC_ITConfig(ADC1, ADC_IT_AWD, ENABLE);
00094
00095 /* Enable ADC1 */
00096 ADC_Cmd(ADC1, ENABLE);
00097
00098 /* Enable ADC1 reset calibration register */
00099 ADC_ResetCalibration(ADC1);
00100 /* Check the end of ADC1 reset calibration register */
00101 while(ADC_GetResetCalibrationStatus(ADC1));
00102
00103 /* Start ADC1 calibration */
00104 ADC_StartCalibration(ADC1);
00105 /* Check the end of ADC1 calibration */
00106 while(ADC_GetCalibrationStatus(ADC1));
00107
00108 /* Start ADC1 Software Conversion */
00109 ADC_SoftwareStartConvCmd(ADC1, ENABLE);
00110
00111 while (1)
00112 {
00113 }
00114 }
00115
00116 /**
00117 * @brief Configures the different system clocks.
00118 * @param None
00119 * @retval None
00120 */
00121 void RCC_Configuration(void)
00122 {
00123 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
00124 /* ADCCLK = PCLK2/2 */
00125 RCC_ADCCLKConfig(RCC_PCLK2_Div2);
00126 #else
00127 /* ADCCLK = PCLK2/4 */
00128 RCC_ADCCLKConfig(RCC_PCLK2_Div4);
00129 #endif
00130 /* Enable peripheral clocks --------------------------------------------------*/
00131 /* Enable ADC1 and GPIO_LED clock */
00132 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
00133 }
00134
00135 /**
00136 * @brief Configures the different GPIO ports.
00137 * @param None
00138 * @retval None
00139 */
00140 void GPIO_Configuration(void)
00141 {
00142 GPIO_InitTypeDef GPIO_InitStructure;
00143
00144 /* Configure PC.04 (ADC Channel14) as analog input -------------------------*/
00145 GPIO_InitStruc

ture.GPIO_Pin = GPIO_Pin_4;
00146 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
00147 GPIO_Init(GPIOC, &GPIO_InitStructure);
00148 }
00149
00150 /**
00151 * @brief Configures NVIC and Vector Table base location.
00152 * @param None
00153 * @retval None
00154 */
00155 void NVIC_Configuration(void)
00156 {
00157 NVIC_InitTypeDef NVIC_InitStructure;
00158
00159 /* Configure and enable ADC interrupt */
00160 #if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL)
00161 NVIC_InitStructure.NVIC_IRQChannel = ADC1_IRQn;
00162 #else
00163 NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
00164 #endif
00165 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
00166 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
00167 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00168 NVIC_Init(&NVIC_InitStructure);
00169 }
00170
00171 #ifdef USE_FULL_ASSERT
00172
00173 /**
00174 * @brief Reports the name of the source file and the source line number
00175 * where the assert_param error has occurred.
00176 * @param file: pointer to the source file name
00177 * @param line: assert_param error line source number
00178 * @retval None
00179 */
00180 void assert_failed(uint8_t* file, uint32_t line)
00181 {
00182 /* User can add his own implementation to report the file name and line number,
00183 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00184
00185 /* Infinite loop */
00186 while (1)
00187 {
00188 }
00189 }
00190
00191 #endif
00192
00193 /**
00194 * @}
00195 */
00196
00197 /**
00198 * @}
00199 */
00200
00201 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

相关主题
文本预览
相关文档 最新文档