forked from BENAMO/stm32-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blinky.c
78 lines (65 loc) · 2.31 KB
/
blinky.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/****************************************************************************************************/
/* */
/* Name: blinky.c */
/* */
/* Purpose: Test Program for STM32VLDiscovery - blinks the LEDs on and off at different rates */
/* (Green = 4 x Slower than Blue). Green is turned off if the button is pressed. */
/* */
/* Created: 21st September 2010 */
/* */
/* Author: Paul Robson ([email protected]) */
/* Simonsson Fun Techonlogies (original work on code for STM32) */
/* */
/****************************************************************************************************/
#include "stm32f10x.h"
#define STACK_TOP 0x20002000 /* This can move quite a lot ! */
void nmi_handler(void);
void hardfault_handler(void);
void delay(void);
int main(void);
/* Four vectors - the starting stack pointer value, code entry point and NMI and Hard-Fault handlers */
unsigned int * myvectors[4]
__attribute__ ((section("vectors")))= {
(unsigned int *) STACK_TOP,
(unsigned int *) main,
(unsigned int *) nmi_handler,
(unsigned int *) hardfault_handler
};
int main(void){
int n = 0;
int button;
RCC->APB2ENR |= 0x10 | 0x04; /* Enable the GPIOA (bit 2) and GPIOC (bit 8) */
GPIOC->CRH = 0x11; /* Set GPIOC Pin 8 and Pin 9 to outputs */
GPIOA->CRL = 0x04; /* Set GPIOA Pin 0 to input floating */
while(1)
{
delay(); /* A short delay */
button = ((GPIOA->IDR & 0x1) == 0); /* Read the button - the button pulls down PA0 to logic 0 */
n++; /* Count the delays */
if (n & 1) { /* Copy bit 0 of counter into GPIOC:Pin 8 */
GPIOC->BSRR = 1<<8 ;
} else {
GPIOC->BSRR = 1<<24;
}
if ((n & 4) && button) { /* Copy bit 4 of counter into GPIOC:Pin 9 if button pressed */
GPIOC->BSRR = 1<<9 ;
} else {
GPIOC->BSRR = 1<<25;
}
}
}
void delay(void)
{
int i = 100000; /* About 1/4 second delay */
while (i-- > 0) {
asm("nop"); /* This stops it optimising code out */
}
}
void nmi_handler(void)
{
return ;
}
void hardfault_handler(void)
{
return ;
}