stm32——GPIO操作实例与注释 STM32入门

相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/*----------------------------------------------------------------------------
* Name: Gpio.c
* Purpose: GPIO usage for STM32
* Version: V1.00
*----------------------------------------------------------------------------
* This file is part of the uVision/ARM development tools.
* This software may only be used under the terms of a valid, current,
* end user licence from KEIL for a compatible version of KEIL software
* development tools. Nothing else gives you the right to use this software.
*
* Copyright (c) 2005-2007 Keil Software. All rights reserved.
*----------------------------------------------------------------------------*/

#include // STM32F10x Library Definitions
#include "STM32_Init.h" // STM32 Initialization

#define S3 0x2000 // PC13: S3
#define S2 0x0001 // PA0 : S2
#define UNBOUNCE_CNT 5 // unbounce the keys

int ledPosCur = 1; // current led position from 0..7
int ledPosOld = 1; // old led position from 0..7


/*----------------------------------------------------------------------------
S2Pressed
check if S2 is pressed (unbounced).
*----------------------------------------------------------------------------*/
int S2Pressed (void) {
static int S2KeyCount = 0, S2KeyPressed = 0;

if (S2KeyPressed) {
if (!((GPIOA->IDR & S2) == 0 )) { // Check if S2 is not pressed
if (S2KeyCount < UNBOUNCE_CNT) S2KeyCount++;
else {
S2KeyPressed = 0;
S2KeyCount = 0;
}
}
}
else {
if (((GPIOA->IDR & S2) == 0 )) { // Check if S2 is pressed
if (S2KeyCount < UNBOUNCE_CNT) S2KeyCount++;
else {
S2KeyPressed = 1;
S2KeyCount = 0;
return (1);
}
}
}
return (0);
}


/*----------------------------------------------------------------------------
S3Pressed
check if S3 is pressed (unbounced).
*----------------------------------------------------------------------------*/
int S3Pressed (void) {
static int S3KeyCount = 0, S3KeyPressed = 0;

if (S3KeyPressed) {
if (!((GPIOC->IDR & S3) == 0 )) { // Check if S3 is not pressed
if (S3KeyCount < UNBOUNCE_CNT) S3KeyCount++;
else {
S3KeyPressed = 0;
S3KeyCount = 0;
}
}
}
else {
if (((GPIOC->IDR & S3) == 0 )) { // Check if S3 is pressed
if (S3KeyCount < UNBOUNCE_CNT) S3KeyCount++;
else {
S3KeyPressed = 1;
S3KeyCount = 0;
return (1);
}
}
}
return (0);
}


/*----------------------------------------------------------------------------
MAIN function
*----------------------------------------------------------------------------*/
int main (void) {


stm32_Init (); // STM32 setup

// GPIOB->ODR |= (1 << (ledPosCur+8)); // switch on initial LED position
GPIOC->ODR |= (1 << (ledPosCur+6)); // switch on initial LED position
while (1) { // Loop forever

if (S2Pressed()) { // S2 pressed
if (ledPosCur > 0)
ledPosCur -= 1; // led position moves to right
}

if (S3Pressed()) { // S3 pressed
// if (ledPosCur < 7) // 8 leds
if (ledPosCur < 3) // 3 times
ledPosCur += 1; // led position moves to left
}

if (ledPosCur != ledPosOld) { // LED osition has changed
// GPIOB->ODR &= ~(1 << (ledPosOld+8)); // switch off old LED position
// GPIOB->ODR |= (1 << (ledPosCur+8)); // switch on current LED position
GPIOC->ODR &= ~(1 << (ledPosOld+6)); // switch off old LED position
GPIOC->ODR |= (1 << (ledPosCur+6)); // switch on current LED position
ledPosOld = ledPosCur;
}
} // end while
} // end main

相关文档
最新文档