野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9452|回复: 6

关于库函数点亮LED灯的操作问题

[复制链接]
发表于 2015-10-3 22:05:07 | 显示全部楼层 |阅读模式
本帖最后由 wanyisq 于 2015-10-3 22:08 编辑

在按照STM32库开发实战指南这本书对手上的开发板进行点亮LED操作发现,4颗LED只有最后一颗亮。贴出代码和原理图,谁能帮我看看四颗LED分别接在PD8、PD9、PD10、PD11 。运行程序后只有PD11亮。


led.h文件
  1. #ifndef __LED_H
  2. #define __LED_H

  3. #include "stm32f10x.h"

  4. #define ON 0
  5. #define OFF 1
  6. #define LED1(a) if(a) \
  7.                                                                                         GPIO_ResetBits(GPIOD,GPIO_Pin_8);\
  8.                                                                                         else \
  9.                                                                                         GPIO_SetBits(GPIOD,GPIO_Pin_8)
  10. #define LED2(a) if(a) \
  11.                                                                                         GPIO_ResetBits(GPIOD,GPIO_Pin_9);\
  12.                                                                                         else \
  13.                                                                                         GPIO_SetBits(GPIOD,GPIO_Pin_9)

  14. #define LED3(a) if(a) \
  15.                                                                                         GPIO_ResetBits(GPIOD,GPIO_Pin_10);\
  16.                                                                                         else \
  17.                                                                                         GPIO_SetBits(GPIOD,GPIO_Pin_10)

  18. #define LED4(a) if(a) \
  19.                                                                                         GPIO_ResetBits(GPIOD,GPIO_Pin_11);\
  20.                                                                                         else \
  21.                                                                                         GPIO_SetBits(GPIOD,GPIO_Pin_11)               

  22.                                                                                        
  23. void LED_GPIO_Config(void);



  24. #endif
复制代码

led.c
  1. #include "led.h"
  2. void LED_GPIO_Config(void)
  3. {
  4.         GPIO_InitTypeDef GPIO_InitStructure;
  5.        
  6.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD,ENABLE);
  7.        
  8.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 ;
  9.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 ;
  10.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 ;
  11.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 ;
  12.        
  13.         GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  14.         GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  15.                
  16.         GPIO_Init(GPIOD, &GPIO_InitStructure);
  17.        
  18.         GPIO_SetBits(GPIOD,GPIO_Pin_8);
  19.         GPIO_SetBits(GPIOD,GPIO_Pin_9);
  20.         GPIO_SetBits(GPIOD,GPIO_Pin_10);
  21.         GPIO_SetBits(GPIOD,GPIO_Pin_11);
  22. }
复制代码


main.c
  1. #include "stm32f10x.h"
  2. #include "led.h"


  3. void Delay(__IO u32 nCount);

  4. int  main(void)
  5. {
  6.         LED_GPIO_Config();
  7.        

  8.         while(1)
  9.         {       
  10.                 Delay(0x0fffef);
  11.                 LED1(ON);
  12.                 Delay(0x0fffef);
  13.                 LED2(ON);       
  14.                 Delay(0x0fffef);
  15.                 LED3(ON);       
  16.                 Delay(0x0fffef);
  17.        
  18.                 LED4(ON);       
  19.                 Delay(0x0fffef);
  20.                                
  21.         }

  22. }

  23. void Delay(__IO u32 nCount)
  24. {
  25.         for(; nCount !=0; nCount-- );
  26. }


复制代码


4.jpg
回复

使用道具 举报

发表于 2015-10-3 22:25:06 | 显示全部楼层
就一个LED4亮了,哈哈哈哈哈。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-10-3 22:32:18 | 显示全部楼层
十一月的萧邦 发表于 2015-10-3 22:25
就一个LED4亮了,哈哈哈哈哈。

你倒是告诉我为什么啊,我向你请教。教教我
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-10-3 22:33:43 | 显示全部楼层
十一月的萧邦 发表于 2015-10-3 22:25
就一个LED4亮了,哈哈哈哈哈。

我是按着野火的这本库开发指南一步一步来的呀,不知道问题到底出在哪
回复 支持 反对

使用道具 举报

发表于 2015-10-3 23:30:08 | 显示全部楼层
wanyisq 发表于 2015-10-3 22:33
我是按着野火的这本库开发指南一步一步来的呀,不知道问题到底出在哪

问题出在这儿:

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8
...
...
...

其实起作用的是最后一句话GPIO_Pin_11
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-10-3 23:47:17 | 显示全部楼层
十一月的萧邦 发表于 2015-10-3 23:30
问题出在这儿:

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8

还有一个问题。 RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOD,ENABLE);这里错了。我已经知道问题在哪了。谢谢你
回复 支持 反对

使用道具 举报

发表于 2016-8-14 09:11:29 | 显示全部楼层
我也遇上这问题,请问怎么改?
回复 支持 反对

使用道具 举报

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

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-4-27 15:37 , Processed in 0.032155 second(s), 26 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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