From: vl_garistov Date: Wed, 10 Mar 2021 01:39:40 +0000 (+0200) Subject: New examples: counter with LCD and 7-segment; bugfixes X-Git-Url: https://kolegite.com/gitweb/?a=commitdiff_plain;h=b073b52312b405882617f626a875bd7628f7a43b;p=vmks.git New examples: counter with LCD and 7-segment; bugfixes --- diff --git a/Examples/7-segm_bidirectional_counter/7-segm_bidirectional_counter.ino b/Examples/7-segm_bidirectional_counter/7-segm_bidirectional_counter.ino new file mode 100644 index 0000000..058a909 --- /dev/null +++ b/Examples/7-segm_bidirectional_counter/7-segm_bidirectional_counter.ino @@ -0,0 +1,137 @@ +#include + +#define A_PIN 12 +#define B_PIN 13 +#define C_PIN 7 +#define D_PIN 8 +#define E_PIN 9 +#define F_PIN 11 +#define G_PIN 10 +#define DP_PIN 6 +#define EN0_PIN 5 +#define EN1_PIN 4 +#define UP_PIN 3 +#define DOWN_PIN 2 + +#define DIGITS 2 + +int print_dec(uint16_t value); +int print_digit(uint8_t position, uint8_t digit); +void emulate_timer_ISR(void); +void counter_up(void); +void counter_down(void); + +const uint8_t segm_pins[8] = {A_PIN, B_PIN, C_PIN, D_PIN, E_PIN, F_PIN, G_PIN, DP_PIN}; +const uint8_t en_pins[DIGITS] = {EN0_PIN, EN1_PIN}; + +const uint8_t segment_map[16] = +{ + // A B C D E F G DP + 0b00000011, // 0 + 0b10011111, // 1 + 0b00100101, // 2 + 0b00001101, // 3 + 0b10011001, // 4 + 0b01001001, // 5 + 0b01000001, // 6 + 0b00011111, // 7 + 0b00000001, // 8 + 0b00001001, // 9 + 0b00010001, // A + 0b11000001, // b + 0b01100011, // C + 0b10000101, // d + 0b01100001, // E + 0b01110001 // F +}; + +volatile uint8_t display_value[DIGITS] = {0}; +volatile uint8_t current_digit = 0; +volatile uint8_t counter = 0; + +void setup() +{ + uint8_t i; + for (i = 0; i < DIGITS; i++) + { + pinMode(en_pins[i], OUTPUT); + digitalWrite(en_pins[i], HIGH); + } + for (i = 0; i < 8; i++) + { + pinMode(segm_pins[i], OUTPUT); + digitalWrite(segm_pins[i], HIGH); + } + pinMode(UP_PIN, INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(UP_PIN), counter_up, FALLING); + pinMode(DOWN_PIN, INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(DOWN_PIN), counter_down, FALLING); +} + +void loop() +{ + print_dec(counter); + + // This is garbage, don't use it! + for(uint8_t i = 0; i < 10; i++) + { + delay(10); + emulate_timer_ISR(); + } +} + +int print_dec(uint16_t value) +{ + if (value > (int) pow(10, DIGITS) - 1) + { + return -1; + } + + for (uint8_t i = 0; i < DIGITS; i++) + { + print_digit(DIGITS - 1 - i, value % 10); + value /= 10; + } + + return 0; +} + +int print_digit(uint8_t position, uint8_t digit) +{ + if (position > DIGITS - 1 || digit > 15) + { + return -1; + } + + display_value[position] = digit; + + return 0; +} + +void emulate_timer_ISR(void) +{ + digitalWrite(en_pins[current_digit], HIGH); + + current_digit++; + if (current_digit > DIGITS - 1) + { + current_digit = 0; + } + + for (uint8_t i = 0; i < 8; i++) + { + digitalWrite(segm_pins[i], segment_map[display_value[current_digit]] & (128 >> i)); + } + + digitalWrite(en_pins[current_digit], LOW); +} + +void counter_up(void) +{ + counter++; +} + +void counter_down(void) +{ + counter--; +} diff --git a/Examples/7-segm_bidirectional_counter/7-segm_bidirectional_counter.png b/Examples/7-segm_bidirectional_counter/7-segm_bidirectional_counter.png new file mode 100644 index 0000000..8aa9918 Binary files /dev/null and b/Examples/7-segm_bidirectional_counter/7-segm_bidirectional_counter.png differ diff --git a/Examples/7-segm_byte_display/7-segm_byte_display.ino b/Examples/7-segm_byte_display/7-segm_byte_display.ino new file mode 100644 index 0000000..5b55605 --- /dev/null +++ b/Examples/7-segm_byte_display/7-segm_byte_display.ino @@ -0,0 +1,133 @@ +#include + +#define SW0_PIN A0 +#define SW1_PIN A1 +#define SW2_PIN A2 +#define SW3_PIN A3 +#define SW4_PIN A4 +#define SW5_PIN A5 +#define SW6_PIN 2 +#define SW7_PIN 3 + +#define A_PIN 12 +#define B_PIN 13 +#define C_PIN 7 +#define D_PIN 8 +#define E_PIN 9 +#define F_PIN 11 +#define G_PIN 10 +#define DP_PIN 6 +#define EN0_PIN 5 +#define EN1_PIN 4 + +#define DIGITS 2 + +int print_digit(uint8_t position, uint8_t digit); +void emulate_timer_ISR(void); +uint8_t read_sw_slow(void); +uint8_t read_sw(void); + +const uint8_t segm_pins[8] = {A_PIN, B_PIN, C_PIN, D_PIN, E_PIN, F_PIN, G_PIN, DP_PIN}; +const uint8_t en_pins[DIGITS] = {EN0_PIN, EN1_PIN}; +const uint8_t sw_pins[8] = {SW0_PIN, SW1_PIN, SW2_PIN, SW3_PIN, SW4_PIN, SW5_PIN, SW6_PIN, SW7_PIN}; + +const uint8_t segment_map[16] = +{ + // A B C D E F G DP + 0b00000011, // 0 + 0b10011111, // 1 + 0b00100101, // 2 + 0b00001101, // 3 + 0b10011001, // 4 + 0b01001001, // 5 + 0b01000001, // 6 + 0b00011111, // 7 + 0b00000001, // 8 + 0b00001001, // 9 + 0b00010001, // A + 0b11000001, // b + 0b01100011, // C + 0b10000101, // d + 0b01100001, // E + 0b01110001 // F +}; + +volatile uint8_t display_value[DIGITS] = {0}; +volatile uint8_t current_digit = 0; + +void setup() +{ + uint8_t i; + for (i = 0; i < DIGITS; i++) + { + pinMode(en_pins[i], OUTPUT); + digitalWrite(en_pins[i], HIGH); + } + for (i = 0; i < 8; i++) + { + pinMode(segm_pins[i], OUTPUT); + digitalWrite(segm_pins[i], HIGH); + pinMode(sw_pins[i], INPUT_PULLUP); + } +} + +void loop() +{ + uint8_t switch_val = read_sw(); + print_digit(0, switch_val >> 4); + print_digit(1, switch_val & 0x0F); + + // This is garbage, don't use it! + for(uint8_t i = 0; i < 10; i++) + { + delay(10); + emulate_timer_ISR(); + } +} + +int print_digit(uint8_t position, uint8_t digit) +{ + if (position > DIGITS - 1 || digit > 15) + { + return -1; + } + + display_value[position] = digit; + + return 0; +} + +void emulate_timer_ISR(void) +{ + digitalWrite(en_pins[current_digit], HIGH); + + current_digit++; + if (current_digit > DIGITS - 1) + { + current_digit = 0; + } + + for (uint8_t i = 0; i < 8; i++) + { + digitalWrite(segm_pins[i], segment_map[display_value[current_digit]] & (128 >> i)); + } + + digitalWrite(en_pins[current_digit], LOW); +} + +uint8_t read_sw_slow(void) +{ + uint8_t switch_value = 0; + + for (uint8_t i = 0; i < 8; i++) + { + switch_value += !digitalRead(sw_pins[i]) << i; + } + + return switch_value; +} + +uint8_t read_sw(void) +{ + return (~PINC & 0x3F) | ((~PIND & 0x0C) << 4); +} diff --git a/Examples/7-segm_raw_dynamic/7-segm_raw_dynamic.ino b/Examples/7-segm_raw_dynamic/7-segm_raw_dynamic.ino index ec34961..8cb03d0 100644 --- a/Examples/7-segm_raw_dynamic/7-segm_raw_dynamic.ino +++ b/Examples/7-segm_raw_dynamic/7-segm_raw_dynamic.ino @@ -1,3 +1,5 @@ +#include + #define A_PIN 12 #define B_PIN 13 #define C_PIN 7 @@ -11,12 +13,14 @@ #define EN2_PIN 3 #define EN3_PIN 2 +#define DIGITS 4 + int print_dec(uint16_t value); int print_digit(uint8_t position, uint8_t digit); void emulate_timer_ISR(void); const uint8_t segm_pins[8] = {A_PIN, B_PIN, C_PIN, D_PIN, E_PIN, F_PIN, G_PIN, DP_PIN}; -const uint8_t en_pins[4] = {EN0_PIN, EN1_PIN, EN2_PIN, EN3_PIN}; +const uint8_t en_pins[DIGITS] = {EN0_PIN, EN1_PIN, EN2_PIN, EN3_PIN}; const uint8_t segment_map[16] = { @@ -39,13 +43,13 @@ const uint8_t segment_map[16] = 0b01110001 // F }; -volatile uint8_t display_value[4] = {0}; +volatile uint8_t display_value[DIGITS] = {0}; volatile uint8_t current_digit = 0; void setup() { uint8_t i; - for (i = 0; i < 4; i++) + for (i = 0; i < DIGITS; i++) { pinMode(en_pins[i], OUTPUT); digitalWrite(en_pins[i], HIGH); @@ -59,7 +63,7 @@ void setup() void loop() { - for (uint16_t cnt = 9999; cnt > 0; cnt--) + for (uint16_t cnt = (int) pow(10, DIGITS) - 1; cnt > 0; cnt--) { print_dec(cnt); @@ -75,14 +79,14 @@ void loop() int print_dec(uint16_t value) { - if (value > 9999) + if (value > (int) pow(10, DIGITS) - 1) { return -1; } - for (uint8_t i = 0; i < 4; i++) + for (uint8_t i = 0; i < DIGITS; i++) { - print_digit(3 - i, value % 10); + print_digit(DIGITS - 1 - i, value % 10); value /= 10; } @@ -91,7 +95,7 @@ int print_dec(uint16_t value) int print_digit(uint8_t position, uint8_t digit) { - if (position > 4 || digit > 15) + if (position > DIGITS || digit > 15) { return -1; } @@ -106,7 +110,7 @@ void emulate_timer_ISR(void) digitalWrite(en_pins[current_digit], HIGH); current_digit++; - if (current_digit > 3) + if (current_digit > DIGITS - 1) { current_digit = 0; } diff --git a/Examples/7-segm_raw_dynamic/7-segm_raw_dynamic.png b/Examples/7-segm_raw_dynamic/7-segm_raw_dynamic.png index 26fb620..fee62bb 100644 Binary files a/Examples/7-segm_raw_dynamic/7-segm_raw_dynamic.png and b/Examples/7-segm_raw_dynamic/7-segm_raw_dynamic.png differ diff --git a/Examples/7-segm_raw_dynamic_timer/7-segm_raw_dynamic_timer.ino b/Examples/7-segm_raw_dynamic_timer/7-segm_raw_dynamic_timer.ino index b14da34..35414d5 100644 --- a/Examples/7-segm_raw_dynamic_timer/7-segm_raw_dynamic_timer.ino +++ b/Examples/7-segm_raw_dynamic_timer/7-segm_raw_dynamic_timer.ino @@ -1,3 +1,5 @@ +#include + #define A_PIN 12 #define B_PIN 13 #define C_PIN 7 @@ -11,13 +13,15 @@ #define EN2_PIN 3 #define EN3_PIN 2 +#define DIGITS 4 + int print_dec(uint16_t value); int print_digit(uint8_t position, uint8_t digit); void timer_init(void); void gpio_init(void); const uint8_t segm_pins[8] = {A_PIN, B_PIN, C_PIN, D_PIN, E_PIN, F_PIN, G_PIN, DP_PIN}; -const uint8_t en_pins[4] = {EN0_PIN, EN1_PIN, EN2_PIN, EN3_PIN}; +const uint8_t en_pins[DIGITS] = {EN0_PIN, EN1_PIN, EN2_PIN, EN3_PIN}; const uint8_t segment_map[16] = { @@ -40,7 +44,7 @@ const uint8_t segment_map[16] = 0b01110001 // F }; -volatile uint8_t display_value[4] = {0}; +volatile uint8_t display_value[DIGITS] = {0}; volatile uint8_t current_digit = 0; void setup() @@ -51,7 +55,7 @@ void setup() void loop() { - for (uint16_t cnt = 9999; cnt > 0; cnt--) + for (uint16_t cnt = (int) pow(10, DIGITS) - 1; cnt > 0; cnt--) { print_dec(cnt); delay(100); @@ -61,14 +65,14 @@ void loop() int print_dec(uint16_t value) { - if (value > 9999) + if (value > (int) pow(10, DIGITS) - 1) { return -1; } - for (uint8_t i = 0; i < 4; i++) + for (uint8_t i = 0; i < DIGITS; i++) { - print_digit(3 - i, value % 10); + print_digit(DIGITS - 1 - i, value % 10); value /= 10; } @@ -77,7 +81,7 @@ int print_dec(uint16_t value) int print_digit(uint8_t position, uint8_t digit) { - if (position > 4 || digit > 15) + if (position > DIGITS || digit > 15) { return -1; } @@ -92,7 +96,7 @@ ISR(TIMER1_OVF_vect) digitalWrite(en_pins[current_digit], HIGH); current_digit++; - if (current_digit > 3) + if (current_digit > DIGITS - 1) { current_digit = 0; } @@ -108,7 +112,7 @@ ISR(TIMER1_OVF_vect) void gpio_init(void) { uint8_t i; - for (i = 0; i < 4; i++) + for (i = 0; i < DIGITS; i++) { pinMode(en_pins[i], OUTPUT); digitalWrite(en_pins[i], HIGH); diff --git a/Examples/LCD_bidirectional_counter/LCD_bidirectional_counter.ino b/Examples/LCD_bidirectional_counter/LCD_bidirectional_counter.ino new file mode 100644 index 0000000..573dff5 --- /dev/null +++ b/Examples/LCD_bidirectional_counter/LCD_bidirectional_counter.ino @@ -0,0 +1,45 @@ +#include + +#define UP_PIN 3 +#define DOWN_PIN 2 + +#define RS_PIN 12 +#define EN_PIN 11 +#define D4_PIN 7 +#define D5_PIN 6 +#define D6_PIN 5 +#define D7_PIN 4 + +#define COLUMNS 16 +#define ROWS 2 + +void counter_up(void); +void counter_down(void); + +LiquidCrystal lcd(RS_PIN, EN_PIN, D4_PIN, D5_PIN, D6_PIN, D7_PIN); +volatile uint8_t counter = 0; + +void setup() +{ + lcd.begin(COLUMNS, ROWS); + pinMode(UP_PIN, INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(UP_PIN), counter_up, FALLING); + pinMode(DOWN_PIN, INPUT_PULLUP); + attachInterrupt(digitalPinToInterrupt(DOWN_PIN), counter_down, FALLING); +} + +void loop() +{ + lcd.setCursor(7, 0); + lcd.print(counter); +} + +void counter_up(void) +{ + counter++; +} + +void counter_down(void) +{ + counter--; +} diff --git a/Examples/LCD_bidirectional_counter/LCD_bidirectional_counter.png b/Examples/LCD_bidirectional_counter/LCD_bidirectional_counter.png new file mode 100644 index 0000000..f2a4460 Binary files /dev/null and b/Examples/LCD_bidirectional_counter/LCD_bidirectional_counter.png differ