专注电子技术学习与研究
当前位置:单片机教程网 >> STM32 >> 浏览文章

stm32库开发之流水灯

作者:寒竹子   来源:用户上传   点击数:  更新时间:2014年05月18日   【字体:

 /*****************主要的用户自己编写的文件*********************/

/**
  *********************************************************************
  * @file      : main.c
  * @author    : xr
  * @date      : 2014年5月13日08:31:58
  * @version   : V1.0
  * @brief     : STM32库函数实现流水灯  STM32F103VE T6 MCU
  *********************************************************************
  * @attention
  * 实验平台   : 野火STM32-MINI开发板
  * 系统时钟   : 采用默认的72MHZ
  * 硬件连接   : --------------------------
  *              |   PB0 -- LED1          |
  *              |   PC4 -- LED2          |
  *              |   PC3 -- LED3          |
  *            --------------------------
  * 库版本    :  ST官方3.5.0版本
  *********************************************************************
  */
#include "stm32f10x.h" /* 上帝之手 STM32官方库头文件 */
#include "led.h"
 
static void Delay( __IO uint32_t counter );
 
/**
  * @brief    : 主函数
  * @param    : 无
  * @retval   : 无
  */
int main(void)
{
    /* 初始化LED控制端口 */
    Config_LED_GPIO();
    
    while (1)
    {
        LED1( ON ); //LED1亮
        Delay( 0x36EE80 );  //延时50ms, xms / 1000 * 72000000 ->HEX = 0x36EE80
        LED1( OFF ); //LED1灭
        Delay( 0x36EE80 );
        
        LED2( ON );
        Delay( 0x36EE80 );
        LED2( OFF );
        Delay( 0x36EE80 );
        
        LED3( ON );
        Delay( 0x36EE80 );
        LED3( OFF );
        Delay( 0x36EE80 );
    }
}
 
/**
  * @brief    : 不精确延时
  * @param    : None
  * @retval   : None
  */
static void Delay( __IO uint32_t counter )
{
    while ( counter-- );
}
 
/***********************END OF FILE*************************************************/
#ifndef _LED_H_
#define _LED_H_
 
#include "stm32f10x.h" //使用stm32库
 
/**
  * 控制LED小灯的亮灭
  * 1 -- OFF
  * 0 -- ON
  */
#define OFF 1
#define ON 0
 
/* 带参宏 如C++中的内联函数一样 */
#define LED1(a) if (a) \
                      GPIO_SetBits(GPIOB, GPIO_Pin_0);\
                      else     \
                      GPIO_ResetBits(GPIOB, GPIO_Pin_0)
                      
#define LED2(a) if (a) \
                      GPIO_SetBits(GPIOC, GPIO_Pin_4);\
                      else     \
                      GPIO_ResetBits(GPIOC, GPIO_Pin_4)
                      
#define LED3(a) if (a) \
                      GPIO_SetBits(GPIOC, GPIO_Pin_3);\
                      else     \
                      GPIO_ResetBits(GPIOC, GPIO_Pin_3)
                    
void Config_LED_GPIO(void);
 
#endif //_LED_H_
/********************************************END OF FILE******/
/**
  ************************************************************
  * @file    : led.c
  * @author  : xr
  * @date    : 2014年5月13日08:53:07
  * @version : V1.0
  * @brief   : led 应用函数库
  ************************************************************
  * @attention
  * 实验平台 : 野火STM32-ISO-MIN开发板
  * 硬件连接 : --------------------------
  *            |   PB0 -- LED1          |
  *            |   PC4 -- LED2          |
  *            |   PC3 -- LED3          |
  *            --------------------------
  * 库版本  :  ST官方3.5.0
  ************************************************************
  */
#include "led.h"
 
/**
  * @brief    :  配置LED用到的I/O口
  * @param    :  None
  * @retval   :  None
  */
void Config_LED_GPIO(void)
{
    /* 定义一个GPIO_InitTypeDef类型的结构体 */
    GPIO_InitTypeDef GPIO_InitStructure;
    
    /* 开启GPIOB的外设时钟 */
    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
    /* 开启GPIOC的外设时钟 */
    RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC, ENABLE );
    /* 开启LED引脚的外设时钟 */
   // RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC, ENABLE );
    /* 选择GPIOB引脚 */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    /* 设置引脚模式为通用强推挽输出 */
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    /* 配置GPIOB引脚的速率为50M */
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    /* 调用STM32库函数初始化GPIOB0 */
    GPIO_Init( GPIOB, &GPIO_InitStructure );
    
    /* 选择GPIOC 3-4 */
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4;
    /* 调用库函数初始化LED2 3的控制引脚 */
    GPIO_Init( GPIOC, &GPIO_InitStructure ); //以上已经设置了50M速率,故这里无需再设置!
    
    /* 关闭所有LED等 */
    GPIO_SetBits( GPIOB, GPIO_Pin_0 );
    
    GPIO_SetBits( GPIOC, GPIO_Pin_4 | GPIO_Pin_3 );
    
}
 
/******************************************END OF FILE***********/ 
关闭窗口