-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInterfacing-toggle-and-push-button-switches.c
128 lines (101 loc) · 3.25 KB
/
Interfacing-toggle-and-push-button-switches.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <avr/io.h>
#include <util/delay.h>
// Definitions for ATmega2560 and Interfacing of Toggle and push button
#if defined(__AVR_ATmega2560__)
#define toggle_button_ddr_reg DDRF
#define toggle_button_port_reg PORTF
#define toggle_button_pin_reg PINF
#define toggle_button_pin PF1
#define push_button_ddr_reg DDRF
#define push_button_port_reg PORTF
#define push_button_pin_reg PINF
#define push_button_pin PF0
#define led_ddr_reg DDRA
#define led_port_reg PORTA
#define led_1_pin PA0
#define led_2_pin PA1
#endif
//---------------------------------- FUNCTIONS ---------------------------------------------------------
//-----------------------------CONFIGURATION FUNCTIONS --------------------------------------------------
/**
* @brief Function to make **ONLY** Toggle and push button Switch pin as input and pull it up internally
*/
void toggle_and_push_button_switch_config (void) {
// Make **ONLY** Toogle Switch pin as input
toggle_button_ddr_reg &= ~( 1 << toggle_button_pin );
// Make **ONLY** Toggle Switch pin internally pull-up
toggle_button_port_reg |= ( 1 << toggle_button_pin );
// Make **ONLY** Push button Switch pin as input
push_button_ddr_reg &= ~( 1 << push_button_pin );
// Make **ONLY** Push button Switch pin internally pull-up
push_button_port_reg |= ( 1 << push_button_pin );
}
/**
* @brief Function to make **ONLY** 'led_1_pin' and 'led_2_pin' as output and initially set it to low
*/
void led_pin_config (void) {
// Make 'led_1_pin' as output
led_ddr_reg |= ( 1 << led_1_pin );
// Set 'led_1_pin' to low initially
led_port_reg &= ~( 1 << led_1_pin );
// Make 'led_2_pin' as output
led_ddr_reg |= ( 1 << led_2_pin );
// Set 'led_2_pin' to low initially
led_port_reg &= ~( 1 << led_2_pin );
}
//------------------------------------- LED RELATED FUNCTIONS -----------------------------------------
/**
* @brief Function to set LED 1 pin to high, hence turn on LED 1
*/
void led_1_on(void){
// Turn on all LEDs
led_port_reg |= (1 << led_1_pin);
}
/**
* @brief Function to set LED 1 pin to low, hence turn off LED 1
*/
void led_1_off(void){
// Turn off all LEDs
led_port_reg &= ~(1 << led_1_pin);
}
/**
* @brief Function to set LED 2 pin to high, hence turn on LED 2
*/
void led_2_on(void){
// Turn on all LEDs
led_port_reg |= (1 << led_2_pin);
}
/**
* @brief Function to set LED 2 pin to low, hence turn off LED 2
*/
void led_2_off(void){
// Turn off all LEDs
led_port_reg &= ~(1 << led_2_pin);
}
//---------------------------------- MAIN ----------------------------------------------------------------
int main(void)
{
// Initialize the necessary devices (Led, Toggle and push button switch) required for the experiment.
toggle_and_push_button_switch_config();
led_pin_config();
while (1)
{
// If the push button Switch is NOT pressed
if ((push_button_pin_reg & (1 << push_button_pin)) == (1 << push_button_pin)) {
_delay_ms(100);
led_1_off(); //Turn off LED 1
}
else {
_delay_ms(100);
led_1_on(); //Turn on LED 1
}
if ((toggle_button_pin_reg & (1 << toggle_button_pin)) == (1 << toggle_button_pin)) {
_delay_ms(100);
led_2_off(); //Turn off LED 2
}
else {
_delay_ms(100);
led_2_on(); //Turn on LED 2
}
}
}