找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 5305|回复: 1
收起左侧

STM32F103驱动led闪烁程序源码

[复制链接]
ID:142045 发表于 2018-7-9 13:54 | 显示全部楼层 |阅读模式
每隔一段时间闪烁 本例程PB9闪烁
IMG20180709134925.jpg IMG20180709134919.jpg 0.png

单片机源程序如下:

  1. /******************************************************************************8
  2. 实验现象:        每隔一段时间闪烁

  3. *******************************************************************************/

  4. #include "stm32f10x_lib.h"

  5. /******************************** 变量定义 ---------------------------------------------------------*/
  6. GPIO_InitTypeDef GPIO_InitStructure;
  7. ErrorStatus HSEStartUpStatus;

  8. /*********************************声明函数 -----------------------------------------------*/
  9. void RCC_Configuration(void);
  10. void NVIC_Configuration(void);
  11. void Delay(vu32 nCount);


  12. /*******************************************************************************
  13.                                   主函数
  14. *******************************************************************************/
  15. int main(void)
  16. {
  17. #ifdef DEBUG
  18.   debug();
  19. #endif

  20.   RCC_Configuration(); //系统时钟配置函数   

  21.   NVIC_Configuration();         //NVIC配置函数

  22.   //使能APB2总线外设时钟                         
  23.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);//定义工作端口,目前使用的是A口和B口
  24.   GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);  //关闭调试 端口重新映射  使用仿真器调试时,不能用此语            

  25.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;  // 选择所有脚                          
  26.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  //配置成推挽式输出
  27.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //输出模式下 I/O输出速度 50M HZ
  28.   GPIO_Init(GPIOA, &GPIO_InitStructure);  //初始化PA口
  29.   GPIO_Init(GPIOB, &GPIO_InitStructure);  //初始化PB口                                                                                                                         
  30.                                                                                                                                                                                                                                
  31.   while (1)
  32.   {
  33.           GPIO_Write(GPIOB, 0xffff); //写一个字数据到PB口
  34.         Delay(0x8FFFFF); // 延时
  35.         GPIO_Write(GPIOB, 0x0000); //写一个字数据到PB口
  36.         Delay(0x8FFFFF); // 延时

  37.   }
  38. }
  39. /*******************************************************************************
  40. *                           配置RCC
  41. *******************************************************************************/
  42. void RCC_Configuration(void)
  43. {   
  44. //复位RCC外部设备寄存器到默认值
  45.   RCC_DeInit();

  46.   //打开外部高速晶振
  47.   RCC_HSEConfig(RCC_HSE_ON);

  48.    //等待外部高速时钟准备好
  49.   HSEStartUpStatus = RCC_WaitForHSEStartUp();

  50.   if(HSEStartUpStatus == SUCCESS)   //外部高速时钟已经准别好
  51.   {                                                                    
  52.     //开启FLASH的预取功能
  53.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  54.     //FLASH延迟2个周期
  55.     FLASH_SetLatency(FLASH_Latency_2);
  56.        
  57.   //配置AHB(HCLK)时钟=SYSCLK
  58.     RCC_HCLKConfig(RCC_SYSCLK_Div1);  
  59.   
  60.    //配置APB2(PCLK2)钟=AHB时钟
  61.     RCC_PCLK2Config(RCC_HCLK_Div1);

  62.     //配置APB1(PCLK1)钟=AHB 1/2时钟
  63.     RCC_PCLK1Config(RCC_HCLK_Div2);

  64.      //配置PLL时钟 == 外部高速晶体时钟*9  PLLCLK = 8MHz * 9 = 72 MHz
  65.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  66.     //使能PLL时钟
  67.     RCC_PLLCmd(ENABLE);

  68.    //等待PLL时钟就绪
  69.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  70.     {
  71.     }

  72.   //配置系统时钟 = PLL时钟
  73.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  74.    //检查PLL时钟是否作为系统时钟
  75.     while(RCC_GetSYSCLKSource() != 0x08)
  76.     {
  77.     }
  78.   }
  79. }

  80. /*******************************************************************************
  81. *                             NVIC配置函数
  82. *******************************************************************************/
  83. void NVIC_Configuration(void)
  84. {
  85. #ifdef  VECT_TAB_RAM  
  86.   /* Set the Vector Table base location at 0x20000000 */
  87.   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  88. #else  /* VECT_TAB_FLASH  */
  89.   /* Set the Vector Table base location at 0x08000000 */
  90.   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
  91. #endif
  92. }

  93. /*******************************************************************************
  94. *                              延时函数
  95. *******************************************************************************/
  96. void Delay(vu32 nCount)
  97. {
  98.   for(; nCount != 0; nCount--);
  99. }

  100. #ifdef  DEBUG
  101. /*******************************************************************************
  102. * Function Name  : assert_failed
  103. * Description    : Reports the name of the source file and the source line number
  104. *                  where the assert_param error has occurred.
  105. * Input          : - file: pointer to the source file name
  106. *                  - line: assert_param error line source number
  107. * Output         : None
  108. * Return         : None
  109. *******************************************************************************/
  110. void assert_failed(u8* file, u32 line)
  111. {
  112.   /* User can add his own implementation to report the file name and line number,
  113.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  114.   /* Infinite loop */
  115.   while (1)
  116.   {
  117.   }
  118. }
  119. #endif

  120. /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

复制代码

所有资料51hei提供下载:
LED灯闪烁.rar (225.62 KB, 下载次数: 46)

评分

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

查看全部评分

回复

使用道具 举报

ID:473907 发表于 2019-8-30 16:13 | 显示全部楼层
你这个程序在 keil5 中报错~~~~
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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