1 | /******************************************************************************/
|
2 | /* BLINKY.C: LED Flasher */
|
3 | /******************************************************************************/
|
4 | /* This file is part of the uVision/ARM development tools. */
|
5 | /* Copyright (c) 2010 Keil Software. All rights reserved. */
|
6 | /* This software may only be used under the terms of a valid, current, */
|
7 | /* end user licence from KEIL for a compatible version of KEIL software */
|
8 | /* development tools. Nothing else gives you the right to use this software. */
|
9 | /******************************************************************************/
|
10 |
|
11 | #include "stm32f10x.h"
|
12 |
|
13 |
|
14 | #define LED_BLINK_RATE 50 /* rate = LED_BLINK_RATE * systick */
|
15 | #define LED_NUM 2 /* Number of user LEDs */
|
16 | const unsigned long led_mask[] = { 1UL<<8, 1UL<<9 }; // = PORTC 8,9 erkannt
|
17 |
|
18 | #define USER1 0x0001 /* PA0 : USER1 */
|
19 | #define UNBOUNCE_CNT 10 /* unbounce the Push Button */
|
20 |
|
21 |
|
22 | uint32_t ledVal = 1;
|
23 | uint32_t ledOn = 0;
|
24 | uint32_t ledBlink = 0;
|
25 |
|
26 | /*----------------------------------------------------------------------------
|
27 | configer LED pins
|
28 | *----------------------------------------------------------------------------*/
|
29 | __INLINE static void LED_Config(void) {
|
30 |
|
31 | RCC->APB2ENR |= 1 << 4; // 4=???? /* Enable GPIOC clock */
|
32 | GPIOC->CRH &= 0xFFFFFF00; // ???? /* Configure the GPIO for LEDs */
|
33 | GPIOC->CRH |= 0x00000033; // ???? /* Configure the GPIO for LEDs */
|
34 | }
|
35 |
|
36 | /*----------------------------------------------------------------------------
|
37 | Switch on LEDs
|
38 | *----------------------------------------------------------------------------*/
|
39 | __INLINE static void LED_On (uint32_t num) {
|
40 |
|
41 | GPIOC->BSRR = led_mask[num]; /* Turn On LED */
|
42 | }
|
43 |
|
44 |
|
45 | /*----------------------------------------------------------------------------
|
46 | Switch off LEDs
|
47 | *----------------------------------------------------------------------------*/
|
48 | __INLINE static void LED_Off (uint32_t num) {
|
49 |
|
50 | GPIOC->BRR = led_mask[num]; /* Turn Off LED */
|
51 | }
|
52 |
|
53 | /*----------------------------------------------------------------------------
|
54 | Function that outputs value to LEDs
|
55 | *----------------------------------------------------------------------------*/
|
56 | void LED_Out(uint32_t value) {
|
57 | int i;
|
58 |
|
59 | for (i = 0; i < LED_NUM; i++) {
|
60 | if (value & (1<<i)) {
|
61 | LED_On (i);
|
62 | } else {
|
63 | LED_Off(i);
|
64 | }
|
65 | }
|
66 | }
|
67 |
|
68 | /*----------------------------------------------------------------------------
|
69 | configer Button pins
|
70 | *----------------------------------------------------------------------------*/
|
71 | __INLINE static void BTN_Config(void) {
|
72 |
|
73 | RCC->APB2ENR |= 1 << 2; // 2 = ???? /* Enable GPIOA clock */
|
74 | GPIOA->CRL &= 0xFFFFFFF0; // ???? /* Configure the GPIO for BTNs */
|
75 | GPIOA->CRL |= 0x00000008; // ???? /* Configure the GPIO for BTNs */
|
76 | }
|
77 |
|
78 | /*----------------------------------------------------------------------------
|
79 | Read Button pins
|
80 | *----------------------------------------------------------------------------*/
|
81 | __INLINE static uint32_t BTN_Get(void) {
|
82 |
|
83 | return (GPIOA->IDR & 0x0001);
|
84 |
|
85 | }
|
86 |
|
87 |
|
88 | /*----------------------------------------------------------------------------
|
89 | USER1Pressed
|
90 | check if USER1 is pressed (unbounced).
|
91 | *----------------------------------------------------------------------------*/
|
92 | uint32_t BTN_Pressed (void) {
|
93 | static uint32_t USER1KeyCount = 0, USER1KeyPressed = 0;
|
94 |
|
95 | if (USER1KeyPressed) {
|
96 | if ((BTN_Get() == 1 )) { /* Check if USER1 not pressed */
|
97 | if (USER1KeyCount < UNBOUNCE_CNT) USER1KeyCount++;
|
98 | else {
|
99 | USER1KeyPressed = 0;
|
100 | USER1KeyCount = 0;
|
101 | }
|
102 | }
|
103 | }
|
104 | else {
|
105 | if (!(BTN_Get() == 1 )) { /* Check if USER1 pressed */
|
106 | if (USER1KeyCount < UNBOUNCE_CNT) USER1KeyCount++;
|
107 | else {
|
108 | USER1KeyPressed = 1;
|
109 | USER1KeyCount = 0;
|
110 | return (1);
|
111 | }
|
112 | }
|
113 | }
|
114 | return (0);
|
115 | }
|
116 | /*----------------------------------------------------------------------------
|
117 | SysTick_Handler
|
118 | *----------------------------------------------------------------------------*/
|
119 | void SysTick_Handler (void) {
|
120 | static uint32_t ticks;
|
121 |
|
122 | if (ticks++ >= LED_BLINK_RATE) {
|
123 | ticks = 0;
|
124 | ledBlink = 1;
|
125 | }
|
126 |
|
127 | }
|
128 |
|
129 |
|
130 | /*----------------------------------------------------------------------------
|
131 | MAIN function
|
132 | *----------------------------------------------------------------------------*/
|
133 | int main (void) {
|
134 |
|
135 | LED_Config();
|
136 | BTN_Config();
|
137 |
|
138 | SysTick_Config(SystemCoreClock / 100); /* Setup SysTick Timer (10ms) */
|
139 |
|
140 | while(1) {
|
141 | if (BTN_Pressed()) {
|
142 | ledVal += 1;
|
143 | if (ledVal > 3) ledVal = 1;
|
144 | }
|
145 |
|
146 | if (ledBlink == 1) {
|
147 | ledBlink = 0;
|
148 | ledOn ^= 1;
|
149 | if (ledOn == 1)
|
150 | LED_Out (ledVal); /* switch the LEDs on */
|
151 | else
|
152 | LED_Out (0);
|
153 | }
|
154 | }
|
155 |
|
156 | }
|