Chinaunix首页 | 论坛 | 博客
  • 博客访问: 128500
  • 博文数量: 64
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2014-04-26 18:44
文章分类

全部博文(64)

文章存档

2014年(64)

我的朋友

分类: C/C++

2014-04-26 19:05:14

原文地址:万利开发板STM3210B之GPIO 作者:

 GPIO.rar   

点击(此处)折叠或打开

  1. /******************** (C) COPYRIGHT 2008 STMicroelectronics ********************
  2. * File Name : main.c
  3. * Author : MCD Application Team
  4. * Version : V2.0.3
  5. * Date : 09/22/2008
  6. * Description : Main program body.
  7. *******************************************************************************/
  8. #include "stm32f10x_lib.h"


  9. ErrorStatus HSEStartUpStatus;

  10. void RCC_Configuration(void);
  11. void GPIO_Configuration(void);
  12. void Delay(vu32 nCount);

  13. int main(void)
  14. {

  15.   /* System Clocks Configuration **********************************************/
  16.   RCC_Configuration();

  17.   /* Configure GPIOC PORT ***************************************************/
  18.   GPIO_Configuration();
  19.  
  20.  while (1)
  21.   {
  22.     /* Turn on LED1 */
  23.     GPIO_SetBits(GPIOC, GPIO_Pin_7);
  24.     /* Insert delay */
  25.     Delay(0xAFFFF);

  26.     /* Turn on LED2 and LED3 */
  27.     GPIO_SetBits(GPIOC, GPIO_Pin_6 | GPIO_Pin_5);
  28.     /* Turn off LED1 */
  29.     GPIO_ResetBits(GPIOC, GPIO_Pin_7);
  30.     /* Insert delay */
  31.     Delay(0xAFFFF);

  32.     /* Turn on LED4 */
  33.     GPIO_SetBits(GPIOC, GPIO_Pin_4);
  34.     /* Turn off LED2 and LED3 */
  35.    GPIO_ResetBits(GPIOC, GPIO_Pin_6| GPIO_Pin_5);
  36.     /* Insert delay */
  37.     Delay(0xAFFFF);

  38.     /* Turn off LED4 */
  39.     GPIO_ResetBits(GPIOC, GPIO_Pin_4);
  40.   }
  41. }

  42. /*******************************************************************************
  43. * Function Name : RCC_Configuration
  44. * Description : Configures the different system clocks.
  45. * Input : None
  46. * Output : None
  47. * Return : None
  48. *******************************************************************************/
  49. void RCC_Configuration(void)
  50. {
  51.   /* RCC system reset(for debug purpose) */
  52.   RCC_DeInit();

  53.   /* Enable HSE */
  54.   RCC_HSEConfig(RCC_HSE_ON);

  55.   /* Wait till HSE is ready */
  56.   HSEStartUpStatus = RCC_WaitForHSEStartUp();

  57.   if(HSEStartUpStatus == SUCCESS)
  58.   {
  59.     /* Enable Prefetch Buffer */
  60.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  61.     /* Flash 2 wait state */
  62.     FLASH_SetLatency(FLASH_Latency_2);
  63.      
  64.     /* HCLK = SYSCLK */
  65.     RCC_HCLKConfig(RCC_SYSCLK_Div1);
  66.   
  67.     /* PCLK2 = HCLK */
  68.     RCC_PCLK2Config(RCC_HCLK_Div1);

  69.     /* PCLK1 = HCLK/2 */
  70.     RCC_PCLK1Config(RCC_HCLK_Div2);

  71.     /* PLLCLK = 8MHz * 9 = 72 MHz */
  72.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  73.     /* Enable PLL */
  74.     RCC_PLLCmd(ENABLE);

  75.     /* Wait till PLL is ready */
  76.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  77.     {
  78.     }

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

  81.     /* Wait till PLL is used as system clock source */
  82.     while(RCC_GetSYSCLKSource() != 0x08)
  83.     {
  84.     }
  85.   }
  86. }


  87. /*******************************************************************************
  88. * Function Name : GPIO_Configuration
  89. * Description : Configures GPIOC PORT.
  90. * Input : None
  91. * Output : None
  92. * Return : None
  93. *******************************************************************************/
  94.  void GPIO_Configuration(void)
  95.  {
  96.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

  97.   GPIO_InitTypeDef GPIO_InitStructure;

  98.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5 | GPIO_Pin_4;
  99.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  100.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  101.   GPIO_Init(GPIOC, &GPIO_InitStructure);
  102.  }
  103. /*******************************************************************************
  104. * Function Name : Delay
  105. * Description : Inserts a delay time.
  106. * Input : nCount: specifies the delay time length.
  107. * Output : None
  108. * Return : None
  109. *******************************************************************************/
  110. void Delay(vu32 nCount)
  111. {
  112.   for(; nCount != 0; nCount--);
  113. }
  114. /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
   这个例程很简单,但是延时函数的延时不怎么精确,基本延时=1/72us,修正后将其精确基本延时1ms,用SysTick来实现。

点击(此处)折叠或打开

  1. /*******************************************************************************
  2. * Function Name : SysTick_Configuration
  3. * Description : Configures the SysTick to generate an interrupt each 1 millisecond.
  4. * Input : None
  5. * Output : None
  6. * Return : None
  7. *******************************************************************************/
  8. void SysTick_Configuration(void)
  9. {
  10.   /* Select AHB clock(HCLK) as SysTick clock source */
  11.   SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);

  12.   /* Set SysTick Priority to 3 */
  13.   NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0);
  14.    
  15.   /* SysTick interrupt each 1ms with HCLK equal to 72MHz */
  16.   SysTick_SetReload(72000);

  17.   /* Enable the SysTick Interrupt */
  18.   SysTick_ITConfig(ENABLE);
  19. }

点击(此处)折叠或打开

  1. /*******************************************************************************
  2. * Function Name : Delay
  3. * Description : Inserts a delay time.
  4. * Input : nTime: specifies the delay time length, in milliseconds.
  5. * Output : None
  6. * Return : None
  7. *******************************************************************************/
  8. void Delay(u32 nTime)
  9. {
  10. /* Enable the SysTick Counter */
  11. SysTick_CounterCmd(SysTick_Counter_Enable);
  12. TimingDelay = nTime;
  13. while(TimingDelay != 0);
  14. /* Disable the SysTick Counter 经测试可以不要*/
  15. SysTick_CounterCmd(SysTick_Counter_Disable);
  16. /* Clear the SysTick Counter 经测试可以不要*/
  17. SysTick_CounterCmd(SysTick_Counter_Clear);
  18. }
中断处理程序

点击(此处)折叠或打开

  1. /*******************************************************************************
  2. * Function Name : SysTickHandler
  3. * Description : This function handles SysTick Handler.
  4. * Input : None
  5. * Output : None
  6. * Return : None
  7. *******************************************************************************/
  8. void SysTickHandler(void)
  9. {
  10. TimingDelay--;
  11. }
 GPIO_SysTick.rar   
系统时钟定时器的周期与驱动的时钟频率和Reload值相关。
RCC通过AHB时钟(HCLK)8分频后作为Cortex系统定时器(SysTick)的外部时钟(这是默认的)。
驱动系统时钟定时器的时钟源有两个:HCLK或(HCLK/8默认),通过调用SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK)或SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8)选择。再用HCLK的时候需要写调用函数SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK),而(HCLK/8默认)不用。

点击(此处)折叠或打开

  1. /* SysTick end of count event each 1ms with input clock equal to 9MHz (HCLK/8, default) */
  2. SysTick_SetReload(9000);
  3. /* Enable SysTick interrupt */
  4. SysTick_ITConfig(ENABLE);

阅读(849) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~