新聞中心

        STM32 之 LED

        作者: 時間:2016-12-03 來源:網絡 收藏
        自己的USER文件組下有3個c文件,以后會按照這個程序結構寫程序。

        代碼參考于 “ OPELC思蛻蒙http://bbs.opelc.org/viewthread.php?tid=6441&extra=page%3D1”

        本文引用地址:http://www.104case.com/article/201612/325133.htm

        (1)Main.c 主函數

        (2)Init_External_Device.c 外設初始化函數

        (3)includes.h 自己的c文件用的包含頭文件

        下面是源碼:

        (1)Main

        C語言:Codee#14359
        /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        + 實驗平臺 : ST 官方三合一套件
        + 硬件 : STM32F103C8T6
        + 開發平臺 : IAR For ARM 5.40
        + 仿真器 : J-Link
        + 日期 : 2010-10-14
        ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

        #include "includes.h"

        /*******************************************************************************
        == Main 函數 ==
        *******************************************************************************/
        intmain(void)
        {
        RCC_Configuration();//配置系統時鐘
        NVIC_Configuration();//配置 NVIC 和 Vector Table

        GPIO_Configuration();


        //主循環
        while(1)
        {
        delay();
        //設置指定的數據端口位——LED1熄滅
        GPIO_SetBits(GPIOB,GPIO_Pin_12);
        delay();
        //清除指定的數據端口位——LED1亮
        GPIO_ResetBits(GPIOB,GPIO_Pin_12);
        delay();
        GPIO_SetBits(GPIOB,GPIO_Pin_13);
        delay();
        GPIO_ResetBits(GPIOB,GPIO_Pin_13);
        delay();
        GPIO_SetBits(GPIOB,GPIO_Pin_14);
        delay();
        GPIO_ResetBits(GPIOB,GPIO_Pin_14);
        delay();
        GPIO_SetBits(GPIOB,GPIO_Pin_15);
        delay();
        GPIO_ResetBits(GPIOB,GPIO_Pin_15);

        }
        }

        (2)Init_External_Device.c

        C語言:Codee#14361
        #include "includes.h"

        /*******************************************************************************
        * Function Name : RCC_Configuration
        * Description : Configures the different system clocks.
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        voidRCC_Configuration(void)
        {
        ErrorStatusHSEStartUpStatus;

        //將外設 RCC寄存器重設為缺省值
        RCC_DeInit();

        //設置外部高速晶振(HSE)
        RCC_HSEConfig(RCC_HSE_ON);

        //等待 HSE 起振
        HSEStartUpStatus=RCC_WaitForHSEStartUp();

        if(HSEStartUpStatus==SUCCESS)
        {
        //預取指緩存使能
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

        //設置代碼延時值
        //FLASH_Latency_2 2 延時周期
        FLASH_SetLatency(FLASH_Latency_2);

        //設置 AHB 時鐘(HCLK)
        //RCC_SYSCLK_Div1 AHB 時鐘 = 系統時鐘
        RCC_HCLKConfig(RCC_SYSCLK_Div1);

        //設置高速 AHB 時鐘(PCLK2)
        //RCC_HCLK_Div2 APB1 時鐘 = HCLK / 2
        RCC_PCLK2Config(RCC_HCLK_Div2);

        //設置低速 AHB 時鐘(PCLK1)
        //RCC_HCLK_Div2 APB1 時鐘 = HCLK / 2
        RCC_PCLK1Config(RCC_HCLK_Div2);

        // PLLCLK = 8MHz * 9 = 72 MHz
        //設置 PLL 時鐘源及倍頻系數
        RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);

        //使能或者失能 PLL
        RCC_PLLCmd(ENABLE);

        //等待指定的 RCC 標志位設置成功 等待PLL初始化成功
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET)
        {
        }


        //設置系統時鐘(SYSCLK) 設置PLL為系統時鐘源
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

        //等待PLL成功用作于系統時鐘的時鐘源
        // 0x00:HSI 作為系統時鐘
        // 0x04:HSE作為系統時鐘
        // 0x08:PLL作為系統時鐘
        while(RCC_GetSYSCLKSource()!=0x08)
        {
        }
        }

        //使能或者失能 APB2 外設時鐘
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);

        }


        /*******************************************************************************
        * Function Name : NVIC_Configuration
        * Description : Configures NVIC and Vector Table base location.
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        voidNVIC_Configuration(void)
        {
        #ifdef VECT_TAB_RAM
        /* Set the Vector Table base location at 0x20000000 */
        NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);
        #else/* VECT_TAB_FLASH */
        /* Set the Vector Table base location at 0x08000000 */
        NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
        #endif
        }

        /*******************************************************************************
        * Function Name : GPIO_Configuration
        * Description : Configures the different GPIO ports.
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        voidGPIO_Configuration(void)
        {
        GPIO_InitTypeDefGPIO_InitStructure;

        GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
        GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽
        GPIO_Init(GPIOB,&GPIO_InitStructure);

        }

        /*******************************************************************************
        * Function Name : 延時函數
        *******************************************************************************/

        voiddelay()
        {
        inti;
        for(i=0;i<0xfffff;i++)
        ;
        }

        (3)includes.h

        C語言:Codee#14362

        #ifndef INCLUDES
        #define INCLUDES 1

        //==============================================================================
        // ★★☆☆★★ 包含文件 ★★☆☆★★
        //==============================================================================
        #include "stm32f10x_lib.h"
        #include "stm32f10x_type.h"
        #include "stm32f10x_it.h"

        //==============================================================================
        // ★★☆☆★★ 全局變量 ★★☆☆★★
        //==============================================================================

        //==============================================================================
        // ★★☆☆★★ 調用函數 ★★☆☆★★
        //==============================================================================
        //##### 時鐘部分 #############################################################
        voidRCC_Configuration(void);//配置系統時鐘
        voidNVIC_Configuration(void);//配置 NVIC 和 Vector Table

        //##### I/O部分 ##############################################################
        voidGPIO_Configuration(void);//配置使用的GPIO口

        //##### 其他常用函數 #########################################################
        voiddelay(void);


        #endif




        關鍵詞: STM32LE

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 北宁市| 岚皋县| 镇安县| 江达县| 崇文区| 汽车| 沙河市| 峨眉山市| 泰安市| 桃园市| 偃师市| 枣阳市| 思茅市| 达日县| 西丰县| 无为县| 合肥市| 思南县| 当雄县| 阜康市| 顺平县| 剑河县| 揭西县| 烟台市| 同江市| 长治县| 游戏| 邵阳市| 建昌县| 察哈| 海南省| 德清县| 瑞丽市| 始兴县| 隆尧县| 平舆县| 达州市| 长子县| 辽阳市| 长岭县| 雅安市|