原创 EKSTM32F ad程序的部分解释 希望大家共同讨论

2008-6-6 21:41 3092 9 9 分类: MCU/ 嵌入式

/******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
* File Name          : main.c
* Author             : MCD Application Team
* Version            : V1.0
* Date               : 10/08/2007
* Description        : Main program body
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/


/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
#include "lcd.h"


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADC1_DR_Address    ((u32)0x4001244C)


/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ADC_InitTypeDef ADC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
vu16 ADC_ConvertedValue;
ErrorStatus HSEStartUpStatus;


/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void SysTick_Config(void);
void LcdShow_Init(void);


/* Private functions ---------------------------------------------------------*/
void delay(void);
void delay()
{
int i,j;
for (i=0; i<0xfffff; i++) j++;
}


char display[4]={0,0};
/*******************************************************************************
* 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();


  /* Configure the systick */
  SysTick_Config();
  LcdShow_Init();


  /* DMA channel1 configuration ----------------------------------------------*/
  DMA_DeInit(DMA_Channel1);//复位DMA_Channel1
  DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;//外围设备 地址
  DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue;//memory 地址
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//外围设备 做为源
  DMA_InitStructure.DMA_BufferSize = 1;//数据单元尺寸
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//外围地址是否自动增长 disable不增长
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;//memory 是否自动增长
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;//外围设备寄存器尺寸 16位
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;//memory 尺寸 16位
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//dma 循环模式
  DMA_InitStructure.DMA_Priority = DMA_Priority_High;//dma 通道优先级
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;//memory- to-memory转换
  DMA_Init(DMA_Channel1, &DMA_InitStructure);//初始化dma通道1


  /* Enable DMA channel1 */
  DMA_Cmd(DMA_Channel1, ENABLE);//使能dma通道1


  /* ADC1 configuration ------------------------------------------------------*/
  ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;//adc 工作模式 adc1 adc2 单独工作
  ADC_InitStructure.ADC_ScanConvMode = ENABLE;//是否使能多通道搜索 或单通道
  ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//是否 启动 连续转换模式
  ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//非外部触发启动转换 软件触发
  ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//ad转换结果右对齐
  ADC_InitStructure.ADC_NbrOfChannel = 1;//adc规则转换通道数量
  ADC_Init(ADC1, &ADC_InitStructure);//


  /* ADC1 regular channel10 configuration */
  ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_55Cycles5);//配置转换规则
                         //通道adc1 adc通道 规则转换序列号 转换频率
  /* Enable ADC1 DMA */
  ADC_DMACmd(ADC1, ENABLE);//使能adc1 dma


  /* Enable ADC1 */
  ADC_Cmd(ADC1, ENABLE);//外围adc1 使能转换


  /* Enable ADC1 reset calibaration register */
  ADC_ResetCalibration(ADC1);//复位adc1 校准
  /* Check the end of ADC1 reset calibration register */
  while(ADC_GetResetCalibrationStatus(ADC1));//等待复位完成


  /* Start ADC1 calibaration */
  ADC_StartCalibration(ADC1);//开始adc1 校准转换
  /* Check the end of ADC1 calibration */
  while(ADC_GetCalibrationStatus(ADC1));//等待转换完成


  /* Start ADC1 Software Conversion */
  ADC_SoftwareStartConvCmd(ADC1, ENABLE);//软件使能adc转换


#if 1
    while(1)
  {
    vu16 value = ADC_ConvertedValue;


    u8 num1 = value&0xf;
    u8 num2 = (value&0xff)>>4;
    u8 num3= (value&0xfff)>>8;
    u8 num4 = value>>12;
    if (num1>9)
      display[3]=num1+(65-10);
    else
      display[3]=num1+(48-0);


    if (num2>9)
      display[2]=num2+(65-10);
    else
      display[2]=num2+(48-0);


    if (num3>9)
      display[1]=num3+(65-10);
    else
      display[1]=num3+(48-0);


    if (num4>9)
      display[0]=num4+(65-10);
    else
      display[0]=num4+(48-0);


    write_string(display);
    delay();
  }
#endif
}


/*******************************************************************************
* Function Name  : RCC_Configuration
* Description    : Configures the different system clocks.
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
void RCC_Configuration(void)
{
  /* RCC system reset(for debug purpose) */
  RCC_DeInit();//rcc复位


  /* Enable HSE */
  RCC_HSEConfig(RCC_HSE_ON);//使能hse 外部时钟External High Speed oscillator (HSE).


  /* Wait till HSE is ready */
  HSEStartUpStatus = RCC_WaitForHSEStartUp();//等待HSE启动


  if(HSEStartUpStatus == SUCCESS)//启动成功
  {
    /* Enable Prefetch Buffer */
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);//使能flash 时钟欲分频器


    /* Flash 2 wait state */
    FLASH_SetLatency(FLASH_Latency_2);//flash code潜伏周期 latency FLASH_Latency_2Two Latency cycles.


    /* HCLK = SYSCLK */
    RCC_HCLKConfig(RCC_SYSCLK_Div1);//配置ahb时钟


    /* PCLK2 = HCLK */
    RCC_PCLK2Config(RCC_HCLK_Div1);//Configures the High Speed APB clock (PCLK2).


    /* PCLK1 = HCLK/2 */
    RCC_PCLK1Config(RCC_HCLK_Div2);//Configures the Low Speed APB clock (PCLK1).


    /* ADCCLK = PCLK2/4 */
    RCC_ADCCLKConfig(RCC_PCLK2_Div4);//Configures the ADC clock (ADCCLK).


    /* PLLCLK = 4MHz * 14 = 56 MHz */
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_14);//Configures the PLL clock source and multiplication factor.


    /* Enable PLL */
    RCC_PLLCmd(ENABLE);//使能pll


    /* Wait till PLL is ready */
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)//等待pllready
    {
    }


    /* Select PLL as system clock source */
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);


    /* Wait till PLL is used as system clock source */
    while(RCC_GetSYSCLKSource() != 0x08)//0x08: PLL used as system clock
    {
    }
  }


/* Enable peripheral clocks --------------------------------------------------*/
  /* Enable DMA clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA, ENABLE);//使能dma 时钟


  /* Enable ADC1 and GPIOC clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);//使能adc1 时钟


  /* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE and AFIO clocks */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |RCC_APB2Periph_GPIOC
         | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);//


  /* TIM2 clocks enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);//
}


/*******************************************************************************
* 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.00 (ADC Channel10) as analog input -------------------------*/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;//端口0
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入
  GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化


  // for LCD display
  /* Configure PE.00 -- PE.15 as Output push-pull : SEG0~15 */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOE, &GPIO_InitStructure);


  /* Configure PC.04 -- PC.11 as Output push-pull  : COM1~4 */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  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)
{
  NVIC_InitTypeDef NVIC_InitStructure;


#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


    /* Configure the Priority Group to 2 bits */
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);


  /* enabling interrupt */
  NVIC_InitStructure.NVIC_IRQChannel=USB_LP_CAN_RX0_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);


  /* enabling interrupt */
  NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);


  /* Configure the SysTick handler priority */
  NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 2, 0);
}



void SysTick_Config(void)
{
  /* Configure HCLK clock as SysTick clock source */
  SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);//


  /* SysTick interrupt each 100 Hz with HCLK equal to 72MHz */
  SysTick_SetReload(720000);


  /* Enable the SysTick Interrupt */
  SysTick_ITConfig(ENABLE);


  /* Enable the SysTick Counter */
  SysTick_CounterCmd(SysTick_Counter_Enable);
}


void LcdShow_Init(void)
{
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_OCInitTypeDef  TIM_OCInitStructure;


  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 8000;
  TIM_TimeBaseStructure.TIM_Prescaler = 17;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);


  TIM_ARRPreloadConfig(TIM2,DISABLE);
  /* only counter overflow/underflow generate U interrupt */
  TIM_UpdateRequestConfig(TIM2,TIM_UpdateSource_Regular);


  /* Output Compare Timing Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;
  TIM_OCInitStructure.TIM_Channel = TIM_Channel_1;
  TIM_OCInitStructure.TIM_Pulse = 4000;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  TIM_OCInit(TIM2, &TIM_OCInitStructure);


  TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Disable);


  /* TIM IT enable */
  TIM_ITConfig(TIM2, TIM_IT_CC1 | TIM_IT_Update, ENABLE);


  /* TIM2 enable counter */
  TIM_Cmd(TIM2, ENABLE);
}


#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


/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
EE直播间
更多
我要评论
0
9
关闭 站长推荐上一条 /3 下一条