www.pudn.com > webserver.zip > Inithardware.c, change:2011-11-17,size:7418b


/******************************************************************************* 
* 文件名             : Inithardware.c 
* 编者               : 杨朝友 
* 编写日期		     : 08/10/2009 
* 说明		         : 硬件初始化,对系统和外设进行初始化 
********************************************************************************/ 
 
/* Includes ------------------------------------------------------------------*/ 
//#include "stm32f10x_lib.h" 
#include "stm32f10x.h" 
#include "feetdefine.h" 
#include "stm32f10x_rcc.h" 
void COM_Set(u8 ComNum); 
/* Private typedef -----------------------------------------------------------*/ 
/* Private define ------------------------------------------------------------*/ 
/* Private macro -------------------------------------------------------------*/ 
/* Private variables ---------------------------------------------------------*/ 
void RCC_Configuration(void) 
{ 
  /*使能USART2,USART3时钟,*/ 
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 |RCC_APB1Periph_USART3 ,  ENABLE); //| RCC_APB1Periph_UART5  
  /* Enable USART1 and GPIOA clock */ 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 | RCC_APB2Periph_AFIO | 
  RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB |   
  RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE); 
} 
 
/******************************************************************************* 
* Function Name  : NVIC_Configuration 
* Description    : Configures Vector Table base location. 
*******************************************************************************/ 
void NVIC_Configuration(void) 
{ 
NVIC_InitTypeDef NVIC_InitStructure; 
EXTI_InitTypeDef EXTI_InitStructure; 
/* Initializes the EXTI_InitStructure */ 
EXTI_StructInit(&EXTI_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, 0x8000);  
#endif 
  /*开串口1接收中断*/ 
  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; 
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; 
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
 
  NVIC_Init(&NVIC_InitStructure); 
   
  /*开串口2接收中断*/ 
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; 
  NVIC_Init(&NVIC_InitStructure); 
   
  /*开串口3接收中断*/ 
  NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; 
  NVIC_Init(&NVIC_InitStructure); 
  /*开串口5接收中断*/ 
  NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; 
  NVIC_Init(&NVIC_InitStructure);	 
 
  /*开定时器1中断*/ 
 /* NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn; 
  NVIC_Init(&NVIC_InitStructure);  */ 
   
  /* 使能 RTC 中断 */ 
 /* NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn; 
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; 
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; 
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
  NVIC_Init(&NVIC_InitStructure);*/	   
} 
 
/******************************************************************************* 
* 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.06 as output push-pull */ 
  GPIO_InitStructure.GPIO_Pin = USART1_485_CONTRL_PIN; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
  GPIO_Init(USART1_485_CONTRLX, &GPIO_InitStructure); 
 
GPIO_InitStructure.GPIO_Pin = USART2_485_CONTRL_PIN; 
GPIO_Init(USART2_485_CONTRLX, &GPIO_InitStructure);   
 
GPIO_InitStructure.GPIO_Pin = USART3_485_CONTRL_PIN; 
GPIO_Init(USART3_485_CONTRLX, &GPIO_InitStructure);   
 
GPIO_InitStructure.GPIO_Pin = USART4_485_CONTRL_PIN; 
GPIO_Init(USART4_485_CONTRLX, &GPIO_InitStructure);   
 
USART1_485_CONTRL_RE_ENABLE(); 
USART2_485_CONTRL_RE_ENABLE(); 
USART3_485_CONTRL_RE_ENABLE(); 
USART4_485_CONTRL_RE_ENABLE(); 
 
} 
 
/******************************************************************************* 
* Function Name  : USART_Configuration 
* Description    : Configures the USART1. 
* Input          : None 
* Output         : None 
* Return         : None 
*******************************************************************************/ 
void USART_Configuration(void) 
{ 
 /* USART_InitTypeDef USART_InitStructure;*/ 
  USART_ClockInitTypeDef  USART_ClockInitStructure; 
/* USART 初始化  */ 
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable; 
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low; 
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge; 
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;	 
 
 
/* 配置 USART1 */ 
USART_ClockInit(USART1, &USART_ClockInitStructure); 
USART_ClockInit(USART2, &USART_ClockInitStructure); 
USART_ClockInit(USART3, &USART_ClockInitStructure); 
USART_ClockInit(UART5, &USART_ClockInitStructure); 
COM_Set(COM1); 
COM_Set(COM2); 
COM_Set(COM3); 
COM_Set(COM4); 
  /* 使能 USART1,开中断 */ 
  USART_Cmd(USART1, ENABLE); 
  USART_ClearITPendingBit(USART1,USART_IT_RXNE);  //清串口接收中断标志  
  USART_ITConfig(USART1,USART_IT_RXNE, ENABLE);//使能串口接收中断 
   
  USART_Cmd(USART2, ENABLE); 
  USART_ClearITPendingBit(USART2,USART_IT_RXNE);  //清串口接收中断标志  
  USART_ITConfig(USART2,USART_IT_RXNE, ENABLE);//使能串口接收中断 
 
  USART_Cmd(USART3, ENABLE); 
  USART_ClearITPendingBit(USART3,USART_IT_RXNE);  //清串口接收中断标志  
  USART_ITConfig(USART3,USART_IT_RXNE, ENABLE);//使能串口接收中断 
 
  USART_Cmd(UART5, ENABLE); 
  USART_ClearITPendingBit(UART5,USART_IT_RXNE);  //清串口接收中断标志  
  USART_ITConfig(UART5,USART_IT_RXNE, ENABLE);//使能串口接收中断	 
} 
 
 
/******************************************************************************* 
* Function Name  : 看门狗配置 
* Description    : 硬件初始化,对系统和外设进行初始化 
*******************************************************************************/ 
void WWDG_Configuration(void) 
{ 
  /* Enable WWDG clock */ 
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE); 
  /* WWDG clock counter = (PCLK1/4096)/8 = 1100Hz (~0.9ms)  */ 
  WWDG_SetPrescaler(WWDG_Prescaler_8); 
 
  /* Set Window value to 0x41 */ 
  WWDG_SetWindowValue(0x7F); 
 
  /* Enable WWDG and set counter value to 0x7F, WWDG timeout = ~0.9 ms * 64= 57 ms */ 
  WWDG_Enable(0x7F); 
    /* Clear EWI flag */ 
  WWDG_ClearFlag(); 
 
  /* Enable EW interrupt */ 
  WWDG_EnableIT(); 
} 
 
 
 
/******************************************************************************* 
* Function Name  : USART_ReceiveData 
* Description    : 硬件初始化,对系统和外设进行初始化 
*******************************************************************************/ 
void InitHardWare(void) 
{  /* Configure the system clocks */  
  //RCC_Configuration();	 
  
  /*flash data configuration*/ 
//  Data_init(); 
  /* Configure the GPIOs */ 
  GPIO_Configuration(); 
       
  /* NVIC Configuration */ 
  NVIC_Configuration(); 
 
  /* Configure the USART */ 
  USART_Configuration(); 
 
  /*Configure the RTC*/ 
 // rtc_config(); 
  /* Configure the TIMR */ 
 // TIME_Configuration(); 
  /* Configure the WWDG */ 
 // WWDG_Configuration(); 
}