generated from dirkarnez/EIE3105_ATmega328P_Application
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
80 lines (64 loc) · 1.3 KB
/
main.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
#include <avr/io.h>
#include <string.h>
void usart_init()
{
UCSR0B = (1<<TXEN0) | (1<<RXEN0);
UCSR0C = (1<<UCSZ00) | (1<<UCSZ01);
UBRR0L = 0xCF;
}
void usart_send(unsigned char ch)
{
while(!(UCSR0A &(1<<UDRE0)));
UDR0 = ch;
}
void usart_read(unsigned char* ptr_ch)
{
while(!(UCSR0A & (1<<RXC0)));
*ptr_ch = UDR0;
}
void print(char* message, size_t length) {
for (unsigned char i = 0; i <= length; i++) {
usart_send(*(message + i));
}
}
int main(void)
{
DDRB = 0x00;
DDRD = 0xFF;
PORTD = 0x00;
unsigned char last_state = 0;
unsigned char current_pb = 0;
unsigned char previous_pb = 0;
char o[] = "o\n";
char f[] = "f\n";
usart_init();
while (1) {
current_pb = PINB;
if (((current_pb > 1) && ((previous_pb & 0x10) == 0) )) {
print(o, strlen(o));
if (last_state == 0) {
last_state = 1;
PORTD = 0xFF;
} else if (last_state == 1) {
last_state = 0;
PORTD = 0x00;
}
} else {
print(f, strlen(f));
}
previous_pb = current_pb;
}
// while (1) {
// current_pb = PINB;
// if (((current_pb & 0x10) == 0x10) && ((previous_pb & 0x10) == 0x00) ) {
// if (last_state == 0) {
// last_state = 1;
// PORTD = 0xFF;
// } else if (last_state == 1) {
// last_state = 0;
// PORTD = 0x00;
// }
// }
// previous_pb = current_pb;
// }
}