init mod04 ex02
This commit is contained in:
72
module04/ex02/main.c
Normal file
72
module04/ex02/main.c
Normal file
@@ -0,0 +1,72 @@
|
||||
#include "header.h"
|
||||
|
||||
typedef struct {
|
||||
int *value;
|
||||
int max;
|
||||
int min;
|
||||
} IncrementParams;
|
||||
|
||||
void print_binary(int value) {
|
||||
int mask_3 = 0b1000;
|
||||
int bit_at_3 = value & mask_3;
|
||||
int bit_at_4 = bit_at_3 << 1;
|
||||
PORTB = (value & 0b111) + bit_at_4;
|
||||
}
|
||||
|
||||
void increment_int(IncrementParams *params) {
|
||||
int *value = params->value;
|
||||
int max = params->max;
|
||||
*value = (*value >= max) ? max : ++(*value);
|
||||
}
|
||||
|
||||
void decrement_int(IncrementParams *params) {
|
||||
int *value = params->value;
|
||||
int min = params->min;
|
||||
*value = (*value <= min) ? min : --(*value);
|
||||
}
|
||||
|
||||
void increment_led(void *param) {
|
||||
IncrementParams *params = (IncrementParams *)param;
|
||||
increment_int(params);
|
||||
print_binary(*(params->value));
|
||||
}
|
||||
|
||||
void decrement_led(void *param) {
|
||||
IncrementParams *params = (IncrementParams *)param;
|
||||
decrement_int(params);
|
||||
print_binary(*(params->value));
|
||||
}
|
||||
|
||||
void on_press(int bit, void (*action)(void*), void *params) {
|
||||
if ((TEST(PIND, bit)) == 0) {
|
||||
action(params);
|
||||
_delay_ms(20);
|
||||
while ((TEST(PIND, bit)) == 0) {
|
||||
continue;
|
||||
}
|
||||
_delay_ms(20);
|
||||
}
|
||||
}
|
||||
|
||||
// write a program that increments and decrements a binary number using the buttons, and displays the result on the LEDs
|
||||
int main() {
|
||||
MODE_OUTPUT(LED1);
|
||||
MODE_OUTPUT(LED2);
|
||||
MODE_OUTPUT(LED3);
|
||||
MODE_OUTPUT(LED4);
|
||||
MODE_INPUT(BUTTON1);
|
||||
MODE_INPUT(BUTTON2);
|
||||
|
||||
int value = 0;
|
||||
int max = 15;
|
||||
int min = 0;
|
||||
IncrementParams params = {&value, max};
|
||||
|
||||
while(1) {
|
||||
on_press(SW1, increment_led, ¶ms);
|
||||
on_press(SW2, decrement_led, ¶ms);
|
||||
}
|
||||
}
|
||||
|
||||
ISR(TIMER0_COMPA_vect) {
|
||||
}
|
||||
Reference in New Issue
Block a user