www.pudn.com > NVIC.rar > main.c, change:2008-12-24,size:11714b


/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** 
* File Name          : main.c 
* Author             : MCD Application Team 
* Version            : V2.0.1 
* Date               : 06/13/2008 
* Description        : Main program body 
******************************************************************************** 
* THE PRESENT FIRMWARE 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 FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING 
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 
*******************************************************************************/ 
 
/* Includes ------------------------------------------------------------------*/ 
#include "stm32f10x_lib.h" 
#include "main.h" 
#include "platform_config.h" 
 
/* Private typedef -----------------------------------------------------------*/ 
/* Private define ------------------------------------------------------------*/ 
/* Private macro -------------------------------------------------------------*/ 
/* Private variables ---------------------------------------------------------*/ 
static vu32 TimingDelay = 0;  
NVIC_InitTypeDef NVIC_InitStructure; 
ErrorStatus HSEStartUpStatus; 
   
/* Private function prototypes -----------------------------------------------*/ 
void RCC_Configuration(void); 
void GPIO_Configuration(void); 
void TIM_Configuration(void); 
 
/* Private functions ---------------------------------------------------------*/ 
 
/******************************************************************************* 
* Function Name  : main 
* Description    : Main program. 
* Input          : None 
* Output         : None 
* Return         : None 
*******************************************************************************/ 
int main(void) 
{ 
#ifdef DEBUG 
  debug(); 
#endif 
   
  /* Configure the system clocks */ 
  RCC_Configuration(); 
 
  /* Configure GPIOs */ 
  GPIO_Configuration(); 
 
  /* Configure TIMs */ 
  TIM_Configuration(); 
        
#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 systick */     
  SysTick_Config(); 
  /* Initialize the LCD */ 
  STM3210E_LCD_Init(); 
  /* Clear the LCD */  
  LCD_Clear(White); 
  LCD_SetBackColor(Blue); 
  /* Set the LCD Text Color */ 
  LCD_SetTextColor(White);  
 
  LCD_DisplayStringLine(Line0, "   EM_STM32E Demo   "); 
  LCD_DisplayStringLine(Line1, "     NVIC_Test      "); 
  LCD_DisplayStringLine(Line2, " welcome to Embest  "); 
  LCD_DisplayStringLine(Line3, " www.embedinfo.com  "); 
  LCD_SetBackColor(White);                        // Set the Back Color */ 
  LCD_SetTextColor(Blue);                         // Set the Text Color */ 
 
  /* Configure one bit for preemption priority */ 
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); 
   
  /* Enable the TIM2 Interrupt */ 
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel; 
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
  NVIC_Init(&NVIC_InitStructure); 
   
  /* Enable the TIM3 Interrupt */ 
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQChannel; 
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 
  NVIC_Init(&NVIC_InitStructure); 
  
  /* Enable the TIM4 Interrupt */ 
  NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQChannel; 
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; 
  NVIC_Init(&NVIC_InitStructure);  
     
  while (1) 
  {      
  } 
} 
 
/******************************************************************************* 
* 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(); 
 
  /* Enable HSE */ 
  RCC_HSEConfig(RCC_HSE_ON); 
 
  /* Wait till HSE is ready */ 
  HSEStartUpStatus = RCC_WaitForHSEStartUp(); 
 
  if(HSEStartUpStatus == SUCCESS) 
  { 
    /* Enable Prefetch Buffer */ 
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); 
 
    /* Flash 2 wait state */ 
    FLASH_SetLatency(FLASH_Latency_2); 
  
    /* HCLK = SYSCLK */ 
    RCC_HCLKConfig(RCC_SYSCLK_Div1);  
   
    /* PCLK2 = HCLK */ 
    RCC_PCLK2Config(RCC_HCLK_Div1);  
 
    /* PCLK1 = HCLK/2 */ 
    RCC_PCLK1Config(RCC_HCLK_Div2); 
 
    /* PLLCLK = 8MHz * 9 = 72 MHz */ 
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); 
 
    /* Enable PLL */  
    RCC_PLLCmd(ENABLE); 
 
    /* Wait till PLL is ready */ 
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) 
    { 
    } 
 
    /* Select PLL as system clock source */ 
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); 
 
    /* Wait till PLL is used as system clock source */ 
    while(RCC_GetSYSCLKSource() != 0x08) 
    { 
    } 
  } 
     
  /* Enable GPIO_LED clock */ 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_LED, ENABLE); 
 
  /* Enable TIM2, TIM3 and TIM4 clocks */ 
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 | RCC_APB1Periph_TIM3 | 
                         RCC_APB1Periph_TIM4, ENABLE); 
} 
 
/******************************************************************************* 
* Function Name  : GPIO_Configuration 
* Description    : Configures the used GPIO pins. 
* Input          : None 
* Output         : None 
* Return         : None 
*******************************************************************************/ 
void GPIO_Configuration(void) 
{ 
  GPIO_InitTypeDef GPIO_InitStructure; 
 
  /* Configure GPIO_LED pin 6, GPIO_LED pin 7, GPIO_LED pin 8 as output push-pull */ 
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_Init(GPIO_LED, &GPIO_InitStructure); 
} 
 
/******************************************************************************* 
* Function Name  : TIM_Configuration 
* Description    : Configures the used Timers. 
* Input          : None 
* Output         : None 
* Return         : None 
*******************************************************************************/ 
void TIM_Configuration(void) 
{  
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure; 
  TIM_OCInitTypeDef  TIM_OCInitStructure; 
 
  /* TIM2 configuration */ 
  TIM_TimeBaseStructure.TIM_Period = 0x4AF;           
  TIM_TimeBaseStructure.TIM_Prescaler = 0xEA5F;        
  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;     
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;   
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0000; 
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); 
   
  TIM_OCStructInit(&TIM_OCInitStructure); 
  /* Output Compare Timing Mode configuration: Channel1 */ 
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Timing;                    
  TIM_OCInitStructure.TIM_Pulse = 0x0;   
  TIM_OC1Init(TIM2, &TIM_OCInitStructure); 
   
  /* TIM3 configuration */ 
  TIM_TimeBaseStructure.TIM_Period = 0x95F; 
           
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); 
  /* Output Compare Timing Mode configuration: Channel1 */ 
  TIM_OC1Init(TIM3, &TIM_OCInitStructure); 
   
  /* TIM4 configuration */ 
  TIM_TimeBaseStructure.TIM_Period = 0xE0F;   
         
  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); 
  /* Output Compare Timing Mode configuration: Channel1 */ 
  TIM_OC1Init(TIM4, &TIM_OCInitStructure); 
 
  /* TIM2 enable counter */ 
  TIM_Cmd(TIM2, ENABLE); 
  /* TIM3 enable counter */ 
  TIM_Cmd(TIM3, ENABLE); 
  /* TIM4 enable counter */ 
  TIM_Cmd(TIM4, ENABLE); 
 
  /* Immediate load of TIM2 Precaler value */ 
  TIM_PrescalerConfig(TIM2, 0xEA5F, TIM_PSCReloadMode_Immediate); 
  /* Immediate load of TIM3 Precaler value */   
  TIM_PrescalerConfig(TIM3, 0xEA5F, TIM_PSCReloadMode_Immediate); 
  /* Immediate load of TIM4 Precaler value */ 
  TIM_PrescalerConfig(TIM4, 0xEA5F, TIM_PSCReloadMode_Immediate); 
 
  /* Clear TIM2 update pending flag */ 
  TIM_ClearFlag(TIM2, TIM_FLAG_Update); 
  /* Clear TIM3 update pending flag */ 
  TIM_ClearFlag(TIM3, TIM_FLAG_Update); 
  /* Clear TIM4 update pending flag */ 
  TIM_ClearFlag(TIM4, TIM_FLAG_Update); 
 
  /* Enable TIM2 Update interrupt */ 
  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); 
  /* Enable TIM3 Update interrupt */ 
  TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); 
  /* Enable TIM4 Update interrupt */ 
  TIM_ITConfig(TIM4, TIM_IT_Update, 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 
 
/******************************************************************************* 
* Function Name  : SysTick_Config 
* Description    : Configure a SysTick Base time to 10 ms. 
* Input          : None 
* Output         : None 
* Return         : None 
*******************************************************************************/ 
 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); 
} 
 
/******************************************************************************* 
* Function Name  : Delay 
* Description    : Inserts a delay time. 
* Input          : nCount: specifies the delay time length (time base 10 ms). 
* Output         : None 
* Return         : None 
*******************************************************************************/ 
void Delay(u32 nCount) 
{ 
  TimingDelay = nCount; 
 
  /* Enable the SysTick Counter */ 
  SysTick_CounterCmd(SysTick_Counter_Enable); 
   
  while(TimingDelay != 0) 
  { 
  } 
 
  /* Disable the SysTick Counter */ 
  SysTick_CounterCmd(SysTick_Counter_Disable); 
 
  /* Clear the SysTick Counter */ 
  SysTick_CounterCmd(SysTick_Counter_Clear); 
} 
 
/******************************************************************************* 
* Function Name  : Decrement_TimingDelay 
* Description    : Decrements the TimingDelay variable. 
* Input          : None 
* Output         : TimingDelay 
* Return         : None 
*******************************************************************************/ 
void Decrement_TimingDelay(void) 
{ 
  if (TimingDelay != 0x00) 
  { 
    TimingDelay--; 
  } 
} 
 
/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/