|
工作環(huán)境說明:
1.硬件平臺(tái): DX開發(fā)板主板 + DX103核心板
2.硬件平臺(tái): Eclipse IDE for C/C++ Developers + GNU Tools ARM Embedded
(1) IDE: Eclipse IDE for C/C++ Developers
(2) 編譯器: GNU Tools ARM Embedded編譯器,支持STM32F407最新的HAL驅(qū)動(dòng)庫
用Eclipse IDE for C/C++ Developers新建一個(gè)C工程模板,主函數(shù)代碼如下:
//
// This file is part of the GNU ARM Eclipse distribution.
// Copyright (c) 2014 Liviu Ionescu.
//
// ----------------------------------------------------------------------------
#include <stdio.h>
#include "diag/Trace.h"
#include "Timer.h"
#include "BlinkLed.h"
// ----------------------------------------------------------------------------
//
// STM32F4 led blink sample (trace via ITM).
//
// In debug configurations, demonstrate how to print a greeting message
// on the trace device. In release configurations the message is
// simply discarded.
//
// To demonstrate POSIX retargetting, reroute the STDOUT a.n.d STDERR to the
// trace device a.n.d display messages on both of them.
//
// Then demonstrates how to blink a led with 1Hz, using a
// continuous loop a.n.d SysTick delays.
//
// On DEBUG, the uptime in seconds is also displayed on the trace device.
//
// Trace support is enabled by adding the TRACE macro definition.
// By default the trace messages are forwarded to the ITM output,
// but can be rerouted to any device o.r completely suppressed, by
// changing the definitions required in system/src/diag/trace_impl.c
// (currently OS_USE_TRACE_ITM, OS_USE_TRACE_SEMIHOSTING_DEBUG/_STDOUT).
//
// ----- Timing definitions -------------------------------------------------
// Keep the LED on for 2/3 of a second.
#define BLINK_ON_TICKS (TIMER_FREQUENCY_HZ * 2 / 3)
#define BLINK_OFF_TICKS (TIMER_FREQUENCY_HZ - BLINK_ON_TICKS)
// ----- main() ---------------------------------------------------------------
// Sample pragmas to cope with warnings. Please note the related line at
// the end of this function, used to pop the compiler diagnostics status.
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wmissing-declarations"
#pragma GCC diagnostic ignored "-Wreturn-type"
/*
* 大蝦開發(fā)板的5個(gè)LED指示燈對(duì)應(yīng)的GPIO
* PA0: LED1
* PB0: LED2
* PB14: LED3
* PD13: LED4
* PD12: LED5
*
* 大蝦開發(fā)板的按鍵對(duì)應(yīng)的GPIO
* PB10: K1
* PC2: K2
*
* */
int main(int argc, char* argv[])
{
// By customising __initialize_args() it is possible to pass arguments,
// for example when running tests with semihosting you can pass various
// options to the test.
// trace_dump_args(argc, argv);
// Send a greeting to the trace device (skipped on Release).
trace_puts("Hello ARM World!");
// The standard output a.n.d the standard error should be forwarded to
// the trace device. For this to work, a redirection in _write.c is
// required.
puts("Standard output message.");
fprintf(stderr, "Standard error message.");
// At this stage the system clock should have already been configured
// at high speed.
trace_printf("System clock: %uHz", SystemCoreClock);
timer_start();
blink_led_init();
uint32_t seconds = 0;
while (1)
{
// blink_led_on();
timer_sleep(BLINK_ON_TICKS);
// blink_led_off();
timer_sleep(BLINK_OFF_TICKS);
++seconds;
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_0);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_14);
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_13);
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_12);
// Count seconds on the trace device.
trace_printf("Second %u", seconds);
}
}
#pragma GCC diagnostic pop
// 在BlinkLed.c中初始化DX開發(fā)板的5個(gè)LED指示燈,以及K1、K2鍵對(duì)應(yīng)的GPIO
// 用STM32CubeMX生成的兩個(gè)中斷函數(shù)整個(gè)拷貝過來
void blink_led_init()
{
// Enable GPIO Peripheral clock
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;
RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN;
GPIO_InitTypeDef GPIO_InitStructure;
// Configure pin in output push/pull mode
GPIO_InitStructure.Pin = GPIO_PIN_0;
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
GPIO_InitStructure.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_14;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.Pin = GPIO_PIN_13 | GPIO_PIN_12;
HAL_GPIO_Init(GPIOD, &GPIO_InitStructure);
/*Configure GPIO pin : PB10 */
GPIO_InitStructure.Pin = GPIO_PIN_10;
GPIO_InitStructure.Mode = GPIO_MODE_IT_FALLING; /* 下降沿中斷 */
GPIO_InitStructure.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
/* EXTI interrupt init */
HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
}
// 以下兩個(gè)中斷函數(shù)由stm32cubemx軟件自動(dòng)生成,很方便
/**
* @brief This function handles EXTI Line[15:10] interrupts.
*/
void EXTI15_10_IRQHandler(void)
{
/* USER CODE BEGIN EXTI15_10_IRQn 0 */
/* USER CODE END EXTI15_10_IRQn 0 */
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_10); /* 此處設(shè)置斷點(diǎn),全速運(yùn)行,然后按下K1,就能到達(dá)這里 */
/* USER CODE BEGIN EXTI15_10_IRQn 1 */
/* USER CODE END EXTI15_10_IRQn 1 */
}
/**
* @brief This function handles EXTI Line2 interrupt.
*/
void EXTI2_IRQHandler(void)
{
/* USER CODE BEGIN EXTI2_IRQn 0 */
/* USER CODE END EXTI2_IRQn 0 */
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_2); /* 此處設(shè)置斷點(diǎn),全速運(yùn)行,然后按下K2,就能到達(dá)這里 */
/* USER CODE BEGIN EXTI2_IRQn 1 */
/* USER CODE END EXTI2_IRQn 1 */
}
2015_3_16_22_28_44.jpg (195.15 KB, 下載次數(shù): 213)
下載附件
2015-4-19 01:01 上傳
【圖片】進(jìn)入KEY1按鍵中斷(PB10).jpg,在中斷函數(shù)中設(shè)置斷點(diǎn),全速運(yùn)行,然后按開發(fā)板的K1鍵
|
|