-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled_with_switch_2b.c
54 lines (43 loc) · 1.31 KB
/
led_with_switch_2b.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
/*
* led_with_switch_2b.c
*
* Created: 18.12.2019 10:00:00
* Author : Jan
* This program changes the state of an LED (ON/OFF) by pressing a button. Releasing the button does not change the state!
* Implemented without an Interrupt, directly in the main routine!
*
* Connections:
* --> LED to Pin 10 on Arduino
* --> Pin 12 --> Button --> GND
*/
#include <avr/io.h> // The header files come with the drivers for the Arduino
static void led_init(void)
{
DDRB = (1 << 2); // Configures Pin 2 of PORTB as output (should be Pin 10 on arduino); Code: 1 gets shifted by 2
}
static void button_init(void)
{
DDRB = ~(1 << 4); // Configures Pin 4 of PORTB as input (should be Pin 12 on arduino; must be connected to GND via a switch)
PINB |= (1 << 4); // Pull input Pin at 5V as default
}
int main(void)
{
led_init();
button_init();
int a = 0;
while (1)
{
if (a == 0) // prevents that status of the led is toggled when the button is released!
{
if (!(PINB & (1 << 4))) // Button pressed
{
PORTB ^= (1 << 2); // bitwise XOR operation; changes state of bit
a = 1; // prevents that status of led is toggled when the button is released!
}
}
if (PINB & (1 << 4)) // Button NOT pressed
{
a = 0; // prevents that status of led is toggled when the button is released!
}
}
}