chore(comments/format): commented and formatted all the c files
This commit is contained in:
parent
f52ce027a6
commit
dbcc449552
5 changed files with 157 additions and 72 deletions
87
ex04/main.c
87
ex04/main.c
|
|
@ -4,52 +4,59 @@
|
|||
|
||||
#include <stdbool.h>
|
||||
|
||||
#define D1 PB0
|
||||
#define D2 PB1
|
||||
#define D3 PB2
|
||||
#define D4 PB4
|
||||
#define D1 PB0
|
||||
#define D2 PB1
|
||||
#define D3 PB2
|
||||
#define D4 PB4
|
||||
|
||||
#define SW1 PD2
|
||||
#define SW2 PD4
|
||||
|
||||
int main(void) {
|
||||
DDRB |= _BV(D4);
|
||||
DDRB |= _BV(D3);
|
||||
DDRB |= _BV(D2);
|
||||
DDRB |= _BV(D1);
|
||||
// ouput pins for leds
|
||||
DDRB |= _BV(D4);
|
||||
DDRB |= _BV(D3);
|
||||
DDRB |= _BV(D2);
|
||||
DDRB |= _BV(D1);
|
||||
|
||||
DDRD &= ~(_BV(SW1));
|
||||
DDRD &= ~(_BV(SW2));
|
||||
// set input (not neccessary since input by default)
|
||||
DDRD &= ~(_BV(SW1));
|
||||
DDRD &= ~(_BV(SW2));
|
||||
|
||||
PORTB |= _BV(D4);
|
||||
PORTB |= _BV(D4);
|
||||
|
||||
uint8_t prev = 0;
|
||||
uint8_t value = 0;
|
||||
while (true) {
|
||||
uint8_t cur = (PIND & (_BV(SW1) | _BV(SW2)));
|
||||
if (prev & (_BV(SW1)) && !(cur & (_BV(SW1)))) {
|
||||
value++;
|
||||
}
|
||||
if (prev & (_BV(SW2)) && !(cur & (_BV(SW2)))) {
|
||||
value--;
|
||||
}
|
||||
prev = cur;
|
||||
if (value & 0b0001)
|
||||
PORTB |= (_BV(D1));
|
||||
else
|
||||
PORTB &= ~(_BV(D1));
|
||||
if (value & 0b0010)
|
||||
PORTB |= (_BV(D2));
|
||||
else
|
||||
PORTB &= ~(_BV(D2));
|
||||
if (value & 0b0100)
|
||||
PORTB |= (_BV(D3));
|
||||
else
|
||||
PORTB &= ~(_BV(D3));
|
||||
if (value & 0b1000)
|
||||
PORTB |= (_BV(D4));
|
||||
else
|
||||
PORTB &= ~(_BV(D4));
|
||||
_delay_ms(20);
|
||||
}
|
||||
uint8_t prev = 0;
|
||||
uint8_t value = 0;
|
||||
while (true) {
|
||||
// lets read both button in a single byte, and keep track of that too
|
||||
uint8_t cur = (PIND & (_BV(SW1) | _BV(SW2)));
|
||||
// check for button SW1
|
||||
if (prev & (_BV(SW1)) && !(cur & (_BV(SW1)))) {
|
||||
value++;
|
||||
}
|
||||
// check for button SW2
|
||||
if (prev & (_BV(SW2)) && !(cur & (_BV(SW2)))) {
|
||||
value--;
|
||||
}
|
||||
prev = cur;
|
||||
|
||||
// for each bit of the counter -> lets turn on or of the led
|
||||
if (value & 0b0001)
|
||||
PORTB |= (_BV(D1));
|
||||
else
|
||||
PORTB &= ~(_BV(D1));
|
||||
if (value & 0b0010)
|
||||
PORTB |= (_BV(D2));
|
||||
else
|
||||
PORTB &= ~(_BV(D2));
|
||||
if (value & 0b0100)
|
||||
PORTB |= (_BV(D3));
|
||||
else
|
||||
PORTB &= ~(_BV(D3));
|
||||
if (value & 0b1000)
|
||||
PORTB |= (_BV(D4));
|
||||
else
|
||||
PORTB &= ~(_BV(D4));
|
||||
_delay_ms(20);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue