新聞中心

        STM32 之 KEY

        作者: 時間:2016-12-03 來源:網絡 收藏
        在win7下的rcc始終不對,在xp下面就是正常。現在還沒有明白原因。

        包含文件

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

        (1)Main

        C語言:Codee#14612

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

        #include "includes.h"

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

        GPIO_Configuration();


        LED1_HIGH;LED2_HIGH;LED3_HIGH;LED4_HIGH;// 初始化讓燈全滅

        //主循環
        while(1)
        {
        if(KEY_UP==0)
        {LED1_LOW;}
        if(KEY_DOWN==0)
        {LED2_LOW;}
        if(KEY_LEFT==0)
        {LED3_LOW;}
        if(KEY_RIGHT==0)
        {LED4_LOW;}

        if(KEY_SELECT==0x00)
        {LED1_HIGH;LED2_HIGH;LED3_HIGH;LED4_HIGH;}

        }
        }

        (2)Init_External_Device.c

        C語言:Codee#14614

        #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)
        {
        }
        }

        //RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL, ENABLE);


        //使能或者失能 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_LED_PORTB;
        GPIO_InitTypeDefGPIO_InitStructure_KEY_PORTA;
        GPIO_InitTypeDefGPIO_InitStructure_KEY_PORTB;
        GPIO_InitTypeDefGPIO_InitStructure_KEY_PORTC;

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

        //==== KEY =======================================================
        GPIO_InitStructure_KEY_PORTA.GPIO_Pin=GPIO_Pin_0;
        GPIO_InitStructure_KEY_PORTA.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_InitStructure_KEY_PORTA.GPIO_Mode=GPIO_Mode_IPU;//上拉輸入
        GPIO_Init(GPIOA,&GPIO_InitStructure_KEY_PORTA);

        GPIO_InitStructure_KEY_PORTB.GPIO_Pin=GPIO_Pin_7;
        GPIO_InitStructure_KEY_PORTB.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_InitStructure_KEY_PORTB.GPIO_Mode=GPIO_Mode_IPU;//上拉輸入
        GPIO_Init(GPIOB,&GPIO_InitStructure_KEY_PORTB);

        GPIO_InitStructure_KEY_PORTC.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
        GPIO_InitStructure_KEY_PORTC.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_InitStructure_KEY_PORTC.GPIO_Mode=GPIO_Mode_IPU;//上拉輸入
        GPIO_Init(GPIOC,&GPIO_InitStructure_KEY_PORTC);

        }

        /*******************************************************************************
        * Function Name : 延時函數
        *******************************************************************************/
        voiddelay()
        {
        inti;
        for(i=0;i<0xfffff;i++)
        ;
        }

        (3)includes.h

        C語言:Codee#14615
        #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);


        //==============================================================================
        // ★★☆☆★★ IO口定義 ★★☆☆★★
        //==============================================================================
        #define LED1_HIGH ( GPIO_SetBits(GPIOB, GPIO_Pin_12) )
        #define LED2_HIGH ( GPIO_SetBits(GPIOB, GPIO_Pin_13) )
        #define LED3_HIGH ( GPIO_SetBits(GPIOB, GPIO_Pin_14) )
        #define LED4_HIGH ( GPIO_SetBits(GPIOB, GPIO_Pin_15) )

        #define LED1_LOW ( GPIO_ResetBits(GPIOB, GPIO_Pin_12) )
        #define LED2_LOW ( GPIO_ResetBits(GPIOB, GPIO_Pin_13) )
        #define LED3_LOW ( GPIO_ResetBits(GPIOB, GPIO_Pin_14) )
        #define LED4_LOW ( GPIO_ResetBits(GPIOB, GPIO_Pin_15) )

        #define KEY_UP ( GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_14) )
        #define KEY_DOWN ( GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) )
        #define KEY_LEFT ( GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_15) )
        #define KEY_RIGHT ( GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13) )
        #define KEY_SELECT ( GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_7) )


        #endif



        關鍵詞: STM32KE

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 崇左市| 南康市| 明溪县| 永平县| 罗平县| 漳州市| 双桥区| 平乡县| 南阳市| 正阳县| 昂仁县| 洛扎县| 渭南市| 桃江县| 龙州县| 广宁县| 新化县| 偃师市| 双辽市| 静乐县| 内丘县| 岑溪市| 贵南县| 原阳县| 宜兰市| 石首市| 内丘县| 中江县| 藁城市| 綦江县| 银川市| 平顺县| 东兰县| 志丹县| 广宗县| 太原市| 浙江省| 商水县| 石首市| 龙山县| 焦作市|