標題: 首次用stm32實現LED閃爍 [打印本頁]

作者: heise    時間: 2014-8-18 21:56
標題: 首次用stm32實現LED閃爍
通過了近兩個星期的的學習,,,終于成功用stm32成功點亮了LED燈,,,這應該是從51單片機跨向ARM的第一步,,,是一個轉折點是了解并深入學習更高科技的個人的一次偉大嘗試與開端,,,越來越相信通過努力是能收獲成功的,,,

#include "stm32f10x_lib.h"
#include"stdio.h"
void delay_ms(unsigned short time)  //延時函數
{
unsigned short i,j;
for(i=0;i<time;i++)
  for(j=0;j<10260;j++);
}
//void USART_configuration(void)     /*USART通信*/
//{   
//  RCC_PCLK2Config(RCC_HCLK_Div1);
//  USART_Initstructure.GPIO_Pin =  GPIO_Pin_0;
//  USART_Initstructure.GPIO_Speed = GPIO_Speed_50MHz ;
//  USART_Initstructure.GPIO_Mode_Out =  GPIO_Mode_Out_PP ;
//  GPIO_Init(GPIOA, &GPIO_InitStructure);
//  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
//  RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL ,DISABLE );
//  RCC_ClockSecuritySystemCmd(ENABLE);
//  }
void GPIO_configuration(void)
{
    GPIO_InitTypeDef  GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_Out_PP ;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void RCC_configuration(void)
{          
ErrorStatus HSEStartUpStatus;
       RCC_DeInit();   // 將外設RCC寄存器重設為缺省值
  RCC_HSEConfig(RCC_HSE_ON);  //        設置外部高速晶振(HSE)
  HSEStartUpStatus = RCC_WaitForHSEStartUp();         // 等待HSE起振
  if(HSEStartUpStatus == SUCCESS)
  {
    /* Enable Prefetch Buffer 啟用預取緩沖器 */
   FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

   /* Flash 2 wait state FLASH設置2個等待周期*/
   FLASH_SetLatency(FLASH_Latency_2);
/* HCLK(AHB時鐘) = SYSCLK 設置AHB時鐘*/
   RCC_HCLKConfig(RCC_SYSCLK_Div1);
/*內部時鐘不啟動*/
       RCC_HSICmd(DISABLE);       
       /* PLLCLK = 8MHz * 9 = 72 MHz 設置時鐘為72M,設置PLL時鐘源及倍頻系數(<=72M)*/          
            RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/*PLL(鎖相環(huán))使能*/
       RCC_PLLCmd(ENABLE);
/*把APB2設置為72MH*/
RCC_PCLK2Config(RCC_HCLK_Div1);
/* Wait till PLL is ready 等待PLL工作穩(wěn)定*/
   while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
/* Select PLL as system clock source 選擇PLL做為系統時鐘源*/
   RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
           /* Wait till PLL is used as system clock source 準備就緒,開始干活*/
   while(RCC_GetSYSCLKSource() != 0x08);
  }
}
//USART_InitTypeDef        USART_Initstructure;
//void USART_configuration()
//{
//
// USART_Initstructure.USART_BaudRate = 9600;
// USART_Initstructure.USART_WordLength =  USART_WordLength_8b;
//  USART_InitStructure.USART_StopBits = USART_StopBits_1;
//  }
int main()
{
  RCC_configuration();
  GPIO_configuration();
  while(1)
  {       
    delay_ms(1000);
    GPIO_Write(GPIOA, 0x1111111111111111);
delay_ms(1000);
GPIO_Write(GPIOA,0x0000000000000000);
}
  return(1);
}


作者: a110    時間: 2014-8-22 10:54
謝謝樓主分享你的成功經驗




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1