Skip to content

Instantly share code, notes, and snippets.

@JulioAlexanderAguilarAngulo
Last active August 29, 2015 14:11
Show Gist options
  • Save JulioAlexanderAguilarAngulo/76b53b3e45d44d69351a to your computer and use it in GitHub Desktop.
Save JulioAlexanderAguilarAngulo/76b53b3e45d44d69351a to your computer and use it in GitHub Desktop.
STM32 Tutorial 001b: Using Buttons
/*************************************** STM32 Tutorial v.2015
Authors: Julio Alexander Aguilar Angulo. [email protected]
Elodie Pauly. [email protected]
(c) ISEN-TOULON. 2015. www.isen.fr/toulon/
*******************************************************************************/
/**** Tuto 001b: Using Buttons **********/
/******************************************************************************/
#include "stm32l1xx.h" // prototypes and intrinsic declarations
/* Testboard: STM32L152 NUCLEO */
/******* Hardware to use on this Tutorial: Onboard Led and Button ***********/
//------ User Button => PC13 User Led => PA5
/*** Variable declarations ************************************************/
const uint16_t myLed = 1<<5;
const uint16_t myButton= 1<<13;
int i,j,tmp;
void Initsyst(void);
/* Code Starts Here!! ********************************************************/
/* note: iar internals configure the system clock to 32 MHz */
void main()
{
Initsyst();
GPIOA->ODR &= ~myLed;// turn off led as initial condition
while (1)
{
tmp=GPIOC->IDR & myButton;
if (tmp==myButton)
{
GPIOA->ODR &=myLed; // do nothing
}
else
{
GPIOA->ODR ^=myLed; // blink the led
j=200000;
for(i=0;i<=j;i++) // debounce using Software Delay
{
}
}
}
}
/***** Private Functions Definition **********************************/
void Initsyst(void)
{
RCC->AHBENR|=0x05;// affect clock to GPIOA,C ( xxxxxxxxxxxx101)
GPIOA->MODER|=0x5555;// output mode
GPIOA->OTYPER&=0x0000; // push pull
GPIOA->OSPEEDR|=0xFFFF;// High speed out
GPIOA->PUPDR&=0x0000; // no pullup
GPIOC->MODER&= 0x0000;// userbtn input mode
GPIOC->PUPDR&=0x0000; // no pullup
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment