找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 13843|回复: 0
打印 上一主题 下一主题
收起左侧

STM32控制LED灯闪烁的程序

[复制链接]
跳转到指定楼层
楼主
ID:237810 发表于 2017-10-8 15:28 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这是我修改的一部分主程序里的点亮LED的程序

单片机源程序如下:
  1. /********************************************************************************/
  2. /*  2017年9月15日STM32F103ZET6最小系统开发板  LED3闪烁          PE5-->LED3                        */
  3. /********************************************************************************/
  4. #include "stm32f10x_lib.h"                  

  5. ErrorStatus HSEStartUpStatus;                 
  6. void Delay(vu32 nTime);
  7. void RCC_Configuration(void);
  8. void GPIO_Configuration(void);
  9.        
  10. int main(void)
  11. {
  12.           RCC_Configuration();                        /* 配置系统时钟 */
  13.           GPIO_Configuration();                        /* 配置GPIO  IO口初始化 */

  14.   
  15.         while(1)
  16.     {
  17.            GPIO_ResetBits(GPIOB,GPIO_Pin_5);
  18.            Delay(0XFFFFF);
  19.               GPIO_SetBits(GPIOB,GPIO_Pin_5);
  20.           Delay(0XFFFFF);
  21.       GPIO_ResetBits(GPIOE,GPIO_Pin_5);
  22.           Delay(0XFFFFF);
  23.              GPIO_SetBits(GPIOE,GPIO_Pin_5);
  24.           Delay(0XFFFFF);
  25.            GPIO_ResetBits(GPIOE,GPIO_Pin_6);
  26.           Delay(0XFFFFF);
  27.              GPIO_SetBits(GPIOE,GPIO_Pin_6);
  28.           Delay(0XFFFFF);
  29.           
  30.     }      
  31. }

  32. /*******************************************************************************
  33. * Function Name  : RCC_Configuration
  34. * Description    : Configures the different system clocks.
  35. *******************************************************************************/
  36. void RCC_Configuration(void)
  37. {
  38.           RCC_DeInit();                                                 /* RCC system reset(for debug purpose) */                 
  39.           RCC_HSEConfig(RCC_HSE_ON);                 /*打开外部高速晶振(HSE)*/

  40.   /*等待HSE起振 该函数将等待直到HSE就绪,或者在超时的情况下退出*/
  41.           HSEStartUpStatus = RCC_WaitForHSEStartUp();
  42.   if(HSEStartUpStatus == SUCCESS)
  43.   {
  44.     RCC_HCLKConfig(RCC_SYSCLK_Div1);                   /* 设置AHB时钟(HCLK),AHB时钟 = 系统时钟 */
  45.     RCC_PCLK2Config(RCC_HCLK_Div1);                    /* 设置高速AHB时钟(PCLK2),APB2时钟 = HCLK */
  46.     RCC_PCLK1Config(RCC_HCLK_Div2);                           /* 设置低速AHB时钟(PCLK1),APB1时钟 = HCLK/2 */
  47.     FLASH_SetLatency(FLASH_Latency_2);                /* Flash 2 wait state */
  48.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); /* 使能或者失能预取指缓存,预取指缓存使能 */
  49.     /*        设置PLL时钟源及倍频系数PLL的输入时钟 = HSE时钟频率 PLL输入时钟 x 9 */
  50.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
  51.     RCC_PLLCmd(ENABLE);                                                /*使能PLL*/

  52.     /* Wait till PLL is ready  检查指定的RCC标志位设置与否PLL就绪 */
  53.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
  54.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);        /* 设置系统时钟(SYSCLK)选择PLL作为系统时钟 */
  55.     /* Wait till PLL is used as system clock source 返回用作系统时钟的时钟源0x08:PLL作为系统时钟 */
  56.     while(RCC_GetSYSCLKSource() != 0x08);
  57.   }                                       
  58.           RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOE, ENABLE);         /*        定义输出端口*/
  59. }

  60. /*******************************************************************************
  61. * Function Name  : GPIO_Configuration         PB5-->LD2  PE5-->LD3  PE6-->LD4
  62. * Description    : Configures the different GPIO ports.
  63. *******************************************************************************/
  64. void GPIO_Configuration(void)
  65. {
  66.           GPIO_InitTypeDef GPIO_InitStructure;
  67.                                                                 /* PB5口配置为输出 */
  68.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
  69.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  70.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  71.           GPIO_Init(GPIOB, &GPIO_InitStructure);

  72.                                                                 /* PE5~PE6口配置为输出 */
  73.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6;
  74.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  75.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  76.           GPIO_Init(GPIOE, &GPIO_InitStructure);

  77. }

  78. void Delay(vu32 nCount)                                  /* 延时 */
  79. {
  80.   for(; nCount != 0; nCount--);
  81. }
  82. //while(1)
  83. //{
  84. //    GPIOE->BSSRR = 0xffdf;
  85.   //        Delay(5000000);
  86.         //GPIOE->ODR = 0xffff;                   /* PE5=1 --> 熄灭LED3 */
  87.         //Delay(5000000);
  88.              //
  89. //}
  90. //
复制代码

所有资料51hei提供下载:
LED234.rar (220.28 KB, 下载次数: 60)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表