找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32控制LED代码

[复制链接]
跳转到指定楼层
楼主
ID:325383 发表于 2018-5-8 20:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
1设计要求
控制实验平台的发光二极管LED1、LED2、LED3、LED4,使它们有规律的点亮,具体顺序如下:
LED1亮->LED2亮->LED3亮->LED4亮,如此反复,当按下开发板上的B3按钮时,灯全部熄灭,
2秒钟后发光二极管全部点亮,再过一秒钟后返回被中断前的状态,有规律的依次点亮。

2 硬件电路设计
在评估板上,LED1、LED2、LED3、LED4分别与PC6、PC7、PC8、PC9相连,按键Key与PB9相连。

3软件程序设计
根据任务要求,程序内容主要包括:
(1)    配置PB口第9个引脚作为外部中断,下降延触发;
(2)    读取端口数据输出寄存器GPIOC_ODR[15:0]的值,因为C口[9:6]位和四个LED灯连通。   
(3)    初始化时,LED依次点亮,当按下Key键时产生中断;
(4)    中断服务子程序内容:灯全部熄灭,2秒钟后发光二极管全部点亮,再过1秒钟后中断返回。
整个工程包含3个源文件:STM32F10x.s、stm32f10x_it.c和main.c,其中STM32F10x.s为启动代码,
所有中断服务子程序均在stm32f10x_it.c中,其它函数则在main.c中。下面分别介绍相关的函数,
具体程序清单见参考程序。
GPIO_Configuration函数负责配置GPIO端口,其中GPIO_InitStructure数据结构包含所有GPIO端口
配置所需各项,函数GPIO_Init则实现某个端口的配置。在STM32F10xR.LIB和STM3210xD.LIB中均已
定义,读者可通过查阅5.3节所述的ST公司发布的《ARM?-based 32-bit MCU STM32F101xx and
STM32F103xx Firmware Library》手册来了解该数据结构以及相关驱动函数。
NVIC_Configuration函数用于配置嵌入式中断控制器,相关内容将在后面章节介绍,此处需要将PB9
引脚配置为外部中断。
EXTI9_5_IRQHandler函数是PB9的中断服务子程序,当按下Key按钮之后触发EXTI9_5中断,在中断服
务子程序中先将灯全部熄灭,延迟2秒后全部点亮,再延迟1秒之后退出中断服务子程序。
SysTick_Configuration函数用于配置和允许系统时钟中断,系统时钟中断服务子程序SysTickHandler
函数则用于产生1毫秒的延时,这样Delay函数就可以通过开关系统时钟计数器来实现精确延时了。

4 运行过程
(1)    使用Keil uVision3 通过ULINK 2仿真器连接实验板,打开实验例程目录GPIO_TEST子目录下的
GPIO.Uv2例程,编译链接工程;
(2)    选择软件调试模式,点击MDK 的Debug菜单,选择Start/Stop Debug Session项或Ctrl+F5键,
在逻辑分析仪中添加GPIOC_ODR.6、GPIOC_ODR.7、GPIOC_ODR.8、GPIOC_ODR.9,点击Run按钮即可在
逻辑分析仪中看到如图7-11;
(3)    选择硬件调试模式,选择Start/Stop Debug Session项或Ctrl+F5键,下载程序并运行,观察LED
灯的变化情况;当程序运行在while循环体内时,按Key键,程序进入中断服务子程序EXTI9_5_IRQHandler(),
单步运行,观察LED的变化情况。
(4)    退出Debug模式,打开Flash菜单>Download,将程序下载到开发板的Flash中,按RESET键复位,
观察LED灯的情况,正常情况应如表7-6所列。

LED灯状态    说    明
LED1    LED2    LED3    LED4   
亮        灭        灭        灭        程序正常运行,发光二极管依次点亮
灭        亮        灭        灭   
灭        灭        亮        灭   
灭        灭        灭        亮   
灭        灭        灭        灭          外部信号输入,发生中断,执行中断处理程序
亮        亮        亮        亮   

单片机源程序:
  1. /*******************************************************************************
  2. * File Name          : main.c
  3. * Author             : Wuhan R&D Center, Embest
  4. * Date First Issued  : 08/08/2008
  5. * Description        : Main program body
  6. ********************************************************************************/

  7. /* Includes ------------------------------------------------------------------*/
  8. #include "stm32f10x_lib.h"


  9. /* Private typedef -----------------------------------------------------------*/
  10. /* Private define ------------------------------------------------------------*/
  11. /* Private macro -------------------------------------------------------------*/
  12. /* Private variables ---------------------------------------------------------*/
  13. #define ADC1_DR_Address    ((u32)0x4001244C)

  14. unsigned short int ADC_ConvertedValue;
  15. GPIO_InitTypeDef GPIO_InitStructure;
  16. ADC_InitTypeDef   ADC_InitStructure;
  17. DMA_InitTypeDef   DMA_InitStructure;
  18. EXTI_InitTypeDef EXTI_InitStructure;
  19. ErrorStatus HSEStartUpStatus;
  20. extern vu32 TimingDelay;



  21. uc8 table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,                                //数码管编码
  22.                         0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

  23. /* Private function prototypes -----------------------------------------------*/
  24. void RCC_Configuration(void);
  25. void NVIC_Configuration(void);
  26. void GPIO_Configuration(void);
  27. void Delay(vu32 nTime);
  28. void SysTick_Configuration(void);
  29. void SetupLED (void) ;        
  30. extern void SetupADC    (void);
  31.         
  32. /* Private functions ---------------------------------------------------------*/

  33. /*******************************************************************************
  34. * Function Name  : main
  35. * Description    : Main program.
  36. * Input          : None
  37. * Output         : None
  38. * Return         : None
  39. *******************************************************************************/
  40. int main(void)
  41. {
  42. u8 i;
  43. #ifdef DEBUG
  44.   debug();
  45. #endif
  46.    
  47.   /* Configure the system clocks */
  48.   RCC_Configuration();
  49.   SysTick_Configuration();
  50.    
  51.   /* NVIC Configuration */
  52.   NVIC_Configuration();

  53.   /* Configure the GPIO ports */
  54.   GPIO_Configuration();
  55.                                                       



  56.         for(i=0;i<8;i++)
  57.            {
  58.                             GPIOE->ODR = 0xffffff00 | (1<<i);
  59.                          GPIOD->ODR = 0xffffffff ;
  60.                          Delay(100);
  61.                 }
  62.    for(;;)
  63.    {
  64.               for(i=0;i<8;i++)
  65.            {
  66.                             GPIOE->ODR = 0xffffffff & ~(1<<i);
  67.                          GPIOD->ODR = 0xffffff00 | table[i];
  68.                          Delay(800);
  69.                          //Delay(1);
  70.                 }
  71.         }   

  72. }

  73. /*******************************************************************************
  74. * Function Name  : SysTick_Configuration
  75. * Description    : Configures the SysTick to generate an interrupt each 1 millisecond.
  76. * Input          : None
  77. * Output         : None
  78. * Return         : None
  79. *******************************************************************************/
  80. void SysTick_Configuration(void)
  81. {
  82.   /* Select AHB clock(HCLK) as SysTick clock source */
  83.   SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);

  84.   /* Set SysTick Priority to 3 */
  85.   NVIC_SystemHandlerPriorityConfig(SystemHandler_SysTick, 3, 0);
  86.    
  87.   /* SysTick interrupt each 1ms with HCLK equal to 72MHz */
  88.   SysTick_SetReload(72000);

  89.   /* Enable the SysTick Interrupt */
  90.   SysTick_ITConfig(ENABLE);
  91. }



  92. /*******************************************************************************
  93. * Function Name  : RCC_Configuration
  94. * Description    : Configures the different system clocks.
  95. * Input          : None
  96. * Output         : None
  97. * Return         : None
  98. *******************************************************************************/
  99. void RCC_Configuration(void)
  100. {
  101.   /* RCC system reset(for debug purpose) */
  102.   RCC_DeInit();

  103.   /* Enable HSE */
  104.   RCC_HSEConfig(RCC_HSE_ON);

  105.   /* Wait till HSE is ready */
  106.   HSEStartUpStatus = RCC_WaitForHSEStartUp();

  107.   if(HSEStartUpStatus == SUCCESS)
  108.   {
  109.     /* HCLK = SYSCLK */
  110.     RCC_HCLKConfig(RCC_SYSCLK_Div1);
  111.   
  112.     /* PCLK2 = HCLK */
  113.     RCC_PCLK2Config(RCC_HCLK_Div1);

  114.     /* PCLK1 = HCLK/2 */
  115.     RCC_PCLK1Config(RCC_HCLK_Div2);

  116.     /* Flash 2 wait state */
  117.     FLASH_SetLatency(FLASH_Latency_2);
  118.     /* Enable Prefetch Buffer */
  119.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

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

  122.     /* Enable PLL */
  123.     RCC_PLLCmd(ENABLE);

  124.     /* Wait till PLL is ready */
  125.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  126.     {
  127.     }

  128.     /* Select PLL as system clock source */
  129.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  130.     /* Wait till PLL is used as system clock source */
  131.     while(RCC_GetSYSCLKSource() != 0x08)
  132.     {
  133.     }
  134.   }
  135.    
  136.   /* Enable GPIOB, GPIOC and AFIO clocks */
  137.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC |
  138.                          RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE , ENABLE);
  139. }

  140. /*******************************************************************************
  141. * Function Name  : NVIC_Configuration
  142. * Description    : Configures Vector Table base location.
  143. * Input          : None
  144. * Output         : None
  145. * Return         : None
  146. *******************************************************************************/
  147. void NVIC_Configuration(void)
  148. {
  149. NVIC_InitTypeDef NVIC_InitStructure;
  150.   
  151. #ifdef  VECT_TAB_RAM  
  152.   /* Set the Vector Table base location at 0x20000000 */
  153.   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  154. #else  /* VECT_TAB_FLASH  */
  155.   /* Set the Vector Table base location at 0x08000000 */
  156.   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
  157. #endif

  158.   /* Configure one bit for preemption priority */
  159.   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  160.   
  161.   /* Enable the EXTI9_5 Interrupt */
  162.   NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQChannel;
  163.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  164.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  165.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  166.   NVIC_Init(&NVIC_InitStructure);
  167. }


  168. /*******************************************************************************
  169. * Function Name  : GPIO_Configuration
  170. * Description    : Configures the different GPIO ports.
  171. * Input          : None
  172. * Output         : None
  173. * Return         : None
  174. *******************************************************************************/
  175. void GPIO_Configuration(void)
  176. {
  177.   GPIO_InitTypeDef GPIO_InitStructure;

  178.   /* Configure PD. as Output push-pull */
  179.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  180.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  181.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  182.   GPIO_Init(GPIOD, &GPIO_InitStructure);

  183.   /* Configure PE. as Output push-pull */
  184.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  185.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  186.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  187.   GPIO_Init(GPIOE, &GPIO_InitStructure);
  188.    

  189. }


  190. /*******************************************************************************
  191. * Function Name  : Delay
  192. * Description    : Inserts a delay time.
  193. * Input          : nTime: specifies the delay time length, in milliseconds.
  194. * Output         : None
  195. * Return         : None
  196. *******************************************************************************/
  197. void Delay(u32 nTime)
  198. {
  199.   /* Enable the SysTick Counter */
  200.   SysTick_CounterCmd(SysTick_Counter_Enable);
  201.   
  202.   TimingDelay = nTime;

  203.   while(TimingDelay != 0);

  204.   /* Disable the SysTick Counter */
  205.   SysTick_CounterCmd(SysTick_Counter_Disable);
  206.   /* Clear the SysTick Counter */
  207.   SysTick_CounterCmd(SysTick_Counter_Clear);
  208. }

  209. /*******************************************************************************
  210. * Function Name  : Delay
  211. * Description    : Inserts a delay time.
  212. * Input          : nCount: specifies the delay time length.
  213. * Output         : None
  214. * Return         : None
  215. *******************************************************************************/
  216. /*
  217. void Delay(vu32 nCount)
  218. {
  219.   for(; nCount != 0; nCount--);
  220. ……………………

  221. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
2.数码管.rar (52.23 KB, 下载次数: 19)


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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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