close

Keil uVision5 如何新建專案(不使用官方的函式庫)

在介紹開始前,先把一些重要資料先蒐集到,方便我們後續的程式撰寫,按一下名稱就可以下載了。

開發環境:MDK 5.20

使用MCU:STM32F051R8T6

1.開發板燒錄器: 

2.開發板電路訊息:

3.STM32F0時脈設定工具:

開始要來建立第一個專案,亮滅一個LED當作測試。

 

STEP1:

     執行 Keil uVision5 軟體,選擇 Project->New uVision Project...

1.PNG

STEP2:

    選擇儲存目錄輸入檔案名稱之後,再存檔

    2.PNG

STEP3:

    選擇Device(STM32F051R8)之後,再選OK完成

    3.PNG

STEP4:

    選擇CMSIS->COREDevice->Startup 兩項打勾,再選擇OK完成

    4.PNG

    5.PNG

STEP5:

    滑鼠選擇Source Group 1 後,按滑鼠右鍵選擇 Add New Item to Group 'Source Group 1'...,

    再選擇C File(.c),並在Name欄位輸入 main,再選擇Add完成

    6.PNG

    7.PNG

    8.PNG

STEP6:

    開始寫main.c,代碼如下

#include "stm32f0xx.h"			// File name depends on device used
#include "RTE_Components.h"		// Component selection

volatile uint32_t msTicks;		// Counter for millisecond Interval

/*--------------------------------------------------------------------------------*/
// SysTick Interrupt Handler
/*--------------------------------------------------------------------------------*/
void SysTick_Handler(void) 
{       
	msTicks++;					// Increment Counter
}

/*--------------------------------------------------------------------------------*/
// Delay: delay a number of Systicks
/*--------------------------------------------------------------------------------*/
void Delay(uint32_t dlyTicks)
{
	uint32_t currentTicks;
	
	currentTicks = msTicks;
	while((msTicks - currentTicks) < dlyTicks) __NOP();
}
		
/*--------------------------------------------------------------------------------*/
// Configure SystemCoreClock using HSI
/*--------------------------------------------------------------------------------*/
void ConfigureSystemClock(void)
{
	RCC->CR |= ((uint32_t)RCC_CR_HSION);					// Enable 8MHz HSI RC
	while ((RCC->CR & RCC_CR_HSIRDY) == 0) __NOP();			// Wait for HSI Ready

	RCC->CFGR = RCC_CFGR_SW_HSI;                             // Set HSI as system clock
	while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_HSI);  // Wait for HSI used as system clock

	FLASH->ACR  = FLASH_ACR_PRFTBE;                          // Enable Prefetch Buffer
	FLASH->ACR |= FLASH_ACR_LATENCY;                         // Flash 1 wait state

	RCC->CFGR |= RCC_CFGR_HPRE_DIV1;                         // HCLK = SYSCLK
	RCC->CFGR |= RCC_CFGR_PPRE_DIV1;                         // PCLK = HCLK

	RCC->CR &= ~RCC_CR_PLLON;                                // Disable PLL to set PLL clk 

	//  PLL configuration:  = HSI/2 * 12 = 48 MHz
	RCC->CFGR &= ~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE | RCC_CFGR_PLLMUL);
	#if defined(STM32F042x6) || defined(STM32F048xx)  || defined(STM32F070x6) \
		|| defined(STM32F078xx) || defined(STM32F071xB)  || defined(STM32F072xB) \
		|| defined(STM32F070xB) || defined(STM32F091xC) || defined(STM32F098xx)  || defined(STM32F030xC)
  
		/* HSI used as PLL clock source : SystemCoreClock = HSI/PREDIV * PLLMUL */
		RCC->CFGR2 =  (RCC_CFGR2_PREDIV_DIV2);
		RCC->CFGR |=  (RCC_CFGR_PLLSRC_HSI_PREDIV  | RCC_CFGR_PLLMUL12);
	#else
		/* HSI used as PLL clock source : SystemCoreClock = HSI/2 * PLLMUL */
		RCC->CFGR |=  (RCC_CFGR_PLLSRC_HSI_DIV2 | RCC_CFGR_PLLMUL12);
	#endif

	RCC->CR |= RCC_CR_PLLON;                                 // Enable PLL
	while((RCC->CR & RCC_CR_PLLRDY) == 0) __NOP();           // Wait till PLL is ready

	RCC->CFGR &= ~RCC_CFGR_SW;                               // Select PLL as system clock source
	RCC->CFGR |=  RCC_CFGR_SW_PLL;
	while ((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_PLL);  // Wait till PLL is system clock src
}

/*--------------------------------------------------------------------------------*/
// GPIOC_Init
/*--------------------------------------------------------------------------------*/
void GPIOC_Init(void)
{
	RCC->AHBENR |=  (1ul << 19);					// Enable GPIOC clock

	/* Configure LED (PC.8) pins as push-pull outputs, No pull-up, pull-down */
	GPIOC->MODER   &= ~((3ul << 2*8));				// set GPIOC pin 8 as output pin
	GPIOC->MODER   |=  ((1ul << 2*8));				// 01: General purpose output mode
	
	GPIOC->OTYPER  &= ~((1ul <<   8));				// set pin 8 as O/P push-pull 
	
	GPIOC->OSPEEDR &= ~((3ul << 2*8));				// set pin 8 as medium speed O/P 
	GPIOC->OSPEEDR |=  ((1ul << 2*8));
	
	GPIOC->PUPDR   &= ~((3ul << 2*8));             // set pin 8 as no pull-up & pull-down
}

/*--------------------------------------------------------------------------------*/
// LED_on();    Turn on LED
/*--------------------------------------------------------------------------------*/
void LED_on(void)
{
	GPIOC->BSRR |= ((1ul << 8));		// set pin 8 = 1 to turn on LED
}

/*--------------------------------------------------------------------------------*/
// LED_off();    Turn off LED
/*--------------------------------------------------------------------------------*/
void LED_off(void)
{
	GPIOC->BSRR |= (1ul << (8+16));		// set pin 8 = 0 to turn off LED
}

/*--------------------------------------------------------------------------------*/
// The processor clock is initialized by CMSIS startup + system file
/*--------------------------------------------------------------------------------*/
// User application starts here
int main(void) 
{        
	ConfigureSystemClock();     // Configure system clock
	SystemCoreClockUpdate();    // Set system clock value to variable SystemCoreClock 

	GPIOC_Init();               // Initialize GPIOC pin 8 to control LED
	
	//System Tick Configuration: Initializes the System Timer and its interrupt, 
	//                           and starts the System Tick Timer.
	//                           Counter is in free running mode to generate periodic interrupts.
	//     param [in]  ticks  Number of ticks between two interrupts.
	
	SysTick_Config(SystemCoreClock/1000);       // set up SysTick 1ms interrupt
	
	while(1)
	{
		LED_on();
		Delay(1000);
		
		LED_off();
		Delay(1000);
		
	}
}

STEP7:

    設定一些參數,以利於編譯源代碼

    先選擇左邊的Project->Target1->Options for Target 'Target 1'... ,會開一個視窗,如圖片並依照相關選項選取後,再點選OK完成。

    15.PNG

 

    9.PNG

 

    10.PNG

 

    11.PNG

STEP8:

    選擇選單Project->Build Target ,或是下圖點選Icon並編譯。

    16.PNG

STEP9:

    選擇選單Flash->Download,或是下圖點選Icon,並燒錄到開發板上(必須將開發闆接到電腦並安裝完驅動)。

    17.PNG

STEP10:

     這樣就完成,LED(PC8)就會一秒亮,一秒滅的循環,如圖片為實物和PC8所量測出的波形。

    18.PNG

   

00000004.BMP

00000005.BMP

arrow
arrow
    創作者介紹
    創作者 熊熊 的頭像
    熊熊

    熊熊的部落格

    熊熊 發表在 痞客邦 留言(0) 人氣()