]> kolegite.com Git - vmks.git/commitdiff
New examples: counter with LCD and 7-segment; bugfixes
authorvl_garistov <vl_garistov@gmail.com>
Wed, 10 Mar 2021 01:39:40 +0000 (03:39 +0200)
committervl_garistov <vl_garistov@gmail.com>
Wed, 10 Mar 2021 01:39:40 +0000 (03:39 +0200)
Examples/7-segm_bidirectional_counter/7-segm_bidirectional_counter.ino [new file with mode: 0644]
Examples/7-segm_bidirectional_counter/7-segm_bidirectional_counter.png [new file with mode: 0644]
Examples/7-segm_byte_display/7-segm_byte_display.ino [new file with mode: 0644]
Examples/7-segm_raw_dynamic/7-segm_raw_dynamic.ino
Examples/7-segm_raw_dynamic/7-segm_raw_dynamic.png
Examples/7-segm_raw_dynamic_timer/7-segm_raw_dynamic_timer.ino
Examples/LCD_bidirectional_counter/LCD_bidirectional_counter.ino [new file with mode: 0644]
Examples/LCD_bidirectional_counter/LCD_bidirectional_counter.png [new file with mode: 0644]

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 (file)
index 0000000..058a909
--- /dev/null
@@ -0,0 +1,137 @@
+#include <math.h>
+
+#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 (file)
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 (file)
index 0000000..5b55605
--- /dev/null
@@ -0,0 +1,133 @@
+#include <math.h>
+
+#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);
+}
index ec349617d36b721aa2ab0b7f3ef916c237bb645d..8cb03d05151caa581f3ec74b301b60a7dc9d644a 100644 (file)
@@ -1,3 +1,5 @@
+#include <math.h>\r
+\r
 #define A_PIN  12\r
 #define B_PIN  13\r
 #define C_PIN  7\r
 #define EN2_PIN        3\r
 #define EN3_PIN        2\r
 \r
+#define DIGITS 4\r
+\r
 int print_dec(uint16_t value);\r
 int print_digit(uint8_t position, uint8_t digit);\r
 void emulate_timer_ISR(void);\r
 \r
 const uint8_t segm_pins[8] = {A_PIN, B_PIN, C_PIN, D_PIN, E_PIN, F_PIN, G_PIN, DP_PIN};\r
-const uint8_t en_pins[4] = {EN0_PIN, EN1_PIN, EN2_PIN, EN3_PIN};\r
+const uint8_t en_pins[DIGITS] = {EN0_PIN, EN1_PIN, EN2_PIN, EN3_PIN};\r
 \r
 const uint8_t segment_map[16] =\r
 {\r
@@ -39,13 +43,13 @@ const uint8_t segment_map[16] =
        0b01110001      // F\r
 };\r
 \r
-volatile uint8_t display_value[4] = {0};\r
+volatile uint8_t display_value[DIGITS] = {0};\r
 volatile uint8_t current_digit = 0;\r
 \r
 void setup()\r
 {\r
        uint8_t i;\r
-       for (i = 0; i < 4; i++)\r
+       for (i = 0; i < DIGITS; i++)\r
        {\r
                pinMode(en_pins[i], OUTPUT);\r
                digitalWrite(en_pins[i], HIGH);\r
@@ -59,7 +63,7 @@ void setup()
 \r
 void loop()\r
 {\r
-       for (uint16_t cnt = 9999; cnt > 0; cnt--)\r
+       for (uint16_t cnt = (int) pow(10, DIGITS) - 1; cnt > 0; cnt--)\r
        {\r
                print_dec(cnt);\r
                \r
@@ -75,14 +79,14 @@ void loop()
 \r
 int print_dec(uint16_t value)\r
 {\r
-       if (value > 9999)\r
+       if (value > (int) pow(10, DIGITS) - 1)\r
        {\r
                return -1;\r
        }\r
 \r
-       for (uint8_t i = 0; i < 4; i++)\r
+       for (uint8_t i = 0; i < DIGITS; i++)\r
        {\r
-               print_digit(3 - i, value % 10);\r
+               print_digit(DIGITS - 1 - i, value % 10);\r
                value /= 10;\r
        }\r
 \r
@@ -91,7 +95,7 @@ int print_dec(uint16_t value)
 \r
 int print_digit(uint8_t position, uint8_t digit)\r
 {\r
-       if (position > 4 || digit > 15)\r
+       if (position > DIGITS || digit > 15)\r
        {\r
                return -1;\r
        }\r
@@ -106,7 +110,7 @@ void emulate_timer_ISR(void)
        digitalWrite(en_pins[current_digit], HIGH);\r
 \r
        current_digit++;\r
-       if (current_digit > 3)\r
+       if (current_digit > DIGITS - 1)\r
        {\r
                current_digit = 0;\r
        }\r
index 26fb620865d77755bf726e444acb7f6e7820282d..fee62bb3db7431ab97010c693084c6e03cc13c8e 100644 (file)
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
index b14da345e59dc1711e9806ffafd5f19f3c440bbf..35414d5f73d5a12998d8a52d6a687afe9238597a 100644 (file)
@@ -1,3 +1,5 @@
+#include <math.h>\r
+\r
 #define A_PIN  12\r
 #define B_PIN  13\r
 #define C_PIN  7\r
 #define EN2_PIN        3\r
 #define EN3_PIN        2\r
 \r
+#define DIGITS 4\r
+\r
 int print_dec(uint16_t value);\r
 int print_digit(uint8_t position, uint8_t digit);\r
 void timer_init(void);\r
 void gpio_init(void);\r
 \r
 const uint8_t segm_pins[8] = {A_PIN, B_PIN, C_PIN, D_PIN, E_PIN, F_PIN, G_PIN, DP_PIN};\r
-const uint8_t en_pins[4] = {EN0_PIN, EN1_PIN, EN2_PIN, EN3_PIN};\r
+const uint8_t en_pins[DIGITS] = {EN0_PIN, EN1_PIN, EN2_PIN, EN3_PIN};\r
 \r
 const uint8_t segment_map[16] =\r
 {\r
@@ -40,7 +44,7 @@ const uint8_t segment_map[16] =
        0b01110001      // F\r
 };\r
 \r
-volatile uint8_t display_value[4] = {0};\r
+volatile uint8_t display_value[DIGITS] = {0};\r
 volatile uint8_t current_digit = 0;\r
 \r
 void setup()\r
@@ -51,7 +55,7 @@ void setup()
 \r
 void loop()\r
 {\r
-       for (uint16_t cnt = 9999; cnt > 0; cnt--)\r
+       for (uint16_t cnt = (int) pow(10, DIGITS) - 1; cnt > 0; cnt--)\r
        {\r
                print_dec(cnt);\r
                delay(100);\r
@@ -61,14 +65,14 @@ void loop()
 \r
 int print_dec(uint16_t value)\r
 {\r
-       if (value > 9999)\r
+       if (value > (int) pow(10, DIGITS) - 1)\r
        {\r
                return -1;\r
        }\r
 \r
-       for (uint8_t i = 0; i < 4; i++)\r
+       for (uint8_t i = 0; i < DIGITS; i++)\r
        {\r
-               print_digit(3 - i, value % 10);\r
+               print_digit(DIGITS - 1 - i, value % 10);\r
                value /= 10;\r
        }\r
 \r
@@ -77,7 +81,7 @@ int print_dec(uint16_t value)
 \r
 int print_digit(uint8_t position, uint8_t digit)\r
 {\r
-       if (position > 4 || digit > 15)\r
+       if (position > DIGITS || digit > 15)\r
        {\r
                return -1;\r
        }\r
@@ -92,7 +96,7 @@ ISR(TIMER1_OVF_vect)
        digitalWrite(en_pins[current_digit], HIGH);\r
 \r
        current_digit++;\r
-       if (current_digit > 3)\r
+       if (current_digit > DIGITS - 1)\r
        {\r
                current_digit = 0;\r
        }\r
@@ -108,7 +112,7 @@ ISR(TIMER1_OVF_vect)
 void gpio_init(void)\r
 {\r
        uint8_t i;\r
-       for (i = 0; i < 4; i++)\r
+       for (i = 0; i < DIGITS; i++)\r
        {\r
                pinMode(en_pins[i], OUTPUT);\r
                digitalWrite(en_pins[i], HIGH);\r
diff --git a/Examples/LCD_bidirectional_counter/LCD_bidirectional_counter.ino b/Examples/LCD_bidirectional_counter/LCD_bidirectional_counter.ino
new file mode 100644 (file)
index 0000000..573dff5
--- /dev/null
@@ -0,0 +1,45 @@
+#include <LiquidCrystal.h>
+
+#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 (file)
index 0000000..f2a4460
Binary files /dev/null and b/Examples/LCD_bidirectional_counter/LCD_bidirectional_counter.png differ