原创 STM32 ---时序和延迟

2013-10-17 09:19 1678 4 4 分类: MCU/ 嵌入式

#include<stm32f10x_rcc.h>
#include<stm32f10x_gpio.h>

#include "stm32f10x.h"
#include "stm32f10x_conf.h"

void Delay_ms(uint32_t ms);
void RCC_Configuration(void);
ErrorStatus HSEStartUpStatus;

GPIO_InitTypeDef GPIO_InitStructure;

RCC_ClocksTypeDef RCC_Clocks;

volatile int main(void)

{
 RCC_GetClocksFreq (&RCC_Clocks);

 RCC_Configuration();

 RCC_GetClocksFreq (&RCC_Clocks);

 // init for GPIO (LED)
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP;
        GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_8 | GPIO_Pin_9 ;       // two LED (guess on what pin!!)
        GPIO_Init(GPIOC, &GPIO_InitStructure);

 

    while(1)
    {
        GPIO_WriteBit(GPIOC,GPIO_Pin_8,Bit_RESET);
                GPIO_WriteBit(GPIOC,GPIO_Pin_9,Bit_SET);
                Delay_ms(1000);
                GPIO_WriteBit(GPIOC,GPIO_Pin_9,Bit_RESET);
                GPIO_WriteBit(GPIOC,GPIO_Pin_8,Bit_SET);
                Delay_ms(1000);
    }

}

//-------
void Delay_ms(uint32_t ms)
{
 volatile uint32_t nCount;
 RCC_ClocksTypeDef RCC_Clocks;
    RCC_GetClocksFreq (&RCC_Clocks);

 nCount=(RCC_Clocks.HCLK_Frequency/10000)*ms;
 for (; nCount!=0; nCount--);
}
//------------
void RCC_Configuration(void)
{
    /*RCC system reset(for debug purpose) */
    RCC_DeInit();

    /* Enable HSE */
    RCC_HSEConfig(RCC_HSE_ON);

    /* Wait till HSE is ready */
    HSEStartUpStatus = RCC_WaitForHSEStartUp();

    if (HSEStartUpStatus == SUCCESS)
    {
        /* HCLK = SYSCLK */
        RCC_HCLKConfig(RCC_SYSCLK_Div1);

        /* PCLK2 = HCLK*/
        RCC_PCLK2Config(RCC_HCLK_Div1);

        /* PCLK1 = HCLK*/
        RCC_PCLK1Config(RCC_HCLK_Div1);

        //ADC CLK
        RCC_ADCCLKConfig(RCC_PCLK2_Div2);

        /* PLLCLK = 8MHz * 3 = 24 MHz */
        RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_3);

 

        /* Enable PLL */
        RCC_PLLCmd(ENABLE);

        /* Wait till PLL is ready */
        while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) {}

        /* Select PLL as system clock source */
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

        /* Wait till PLL is used as system clock source */
        while (RCC_GetSYSCLKSource() != 0x08) {}
    }

    /*Then need to enable peripheral clocks ----------------------------------------------*/
}

 

文章评论0条评论)

登录后参与讨论
我要评论
0
4
关闭 站长推荐上一条 /2 下一条