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

全部博文(64)

文章存档

2014年(64)

我的朋友

分类: C/C++

2014-04-26 19:06:40

 20120714.rar   

LED与GPIO的对应关系应为:PC7----LED1

                         PC6----LED2

                         PC5----LED3

                         PC4----LED4

验证代码:

点击(此处)折叠或打开

  1. /* Includes------------------------------*/
  2. #include "stm32f10x.h"

  3. /* Private function prototypes-------------*/
  4. void RCC_Configuration(void);
  5. void GPIO_Configuration(void);

  6. typedef enum {
  7.   LED1 = 0,
  8.   LED2,
  9.   LED3,
  10.   LED4,
  11. }LED_STATE;

  12. LED_STATE led_status;

  13. int main(void)
  14. {
  15.   u32 cnt = 0x000fffff;
  16.   /* System Clocks Configuration */
  17.   RCC_Configuration();
  18.   /* Configure the GPIOC ports */
  19.   GPIO_Configuration();
  20.   led_status = LED1;
  21.   while (1){
  22.     switch (led_status){
  23.     case LED1:
  24.       GPIOC->BSRR = 0x00800080; /* turn on LED1 */
  25.       led_status = LED2;
  26.       break;
  27.     case LED2:
  28.       GPIOC->BSRR = 0x00400040; /* turn on LED2 */
  29.       led_status = LED3;
  30.       break;
  31.     case LED3:
  32.       GPIOC->BSRR = 0x00200020; /* turn on LED3 */
  33.       led_status = LED4;
  34.       break;
  35.     case LED4:
  36.       GPIOC->BSRR = 0x00100010; /* turn on LED4 */
  37.       led_status = LED1;
  38.       break;
  39.     }
  40.     while(cnt--);
  41.     cnt = 0x000fffff;
  42.   }
  43. }
  44. /**
  45.   * @brief Configures the different system clocks.
  46.   * @param None
  47.   * @retval : None
  48.   */
  49. void RCC_Configuration(void)
  50. {
  51.   /* Setup the microcontroller system. Initialize the Embedded Flash
  52.   Interface, initialize the PLL and update the SystemFrequency variable. */
  53.   SystemInit();
  54.   /* GPIOC clock enable */
  55.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  56. }
  57. /**
  58.   * @brief Configure the GPIOC Pins.
  59.   * @param None
  60.   * @retval : None
  61.   */
  62. void GPIO_Configuration(void)
  63. {
  64.   GPIO_InitTypeDef GPIO_InitStructure;
  65.   /* GPIOC configuration: PC4 PC5 PC6 PC7 as led controller */
  66.   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|
  67.   GPIO_Pin_7;
  68.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  69.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  70.   GPIO_Init(GPIOC, &GPIO_InitStructure);
  71. }
注意:在调试代码时候注意第72行GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;,原来我用的是开漏输出模式GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;灯一直不亮,换成现在的推挽输出就正常了。

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