]> kolegite.com Git - vmks.git/commitdiff
Improved 7-segm_driver_random example
authorVladimir Garistov <vl.garistov@gmail.com>
Mon, 27 Feb 2023 09:51:27 +0000 (11:51 +0200)
committerVladimir Garistov <vl.garistov@gmail.com>
Mon, 27 Feb 2023 09:51:27 +0000 (11:51 +0200)
Examples/7-segment/7-segm_driver_random/7-segm_driver_random.ino
Examples/7-segment/7-segm_driver_random/7-segm_driver_random.png

index 075efc32444477ae87124fe3efc15f119b332564..00b44e81f283a2ebf8c72e23845419a3b7213191 100644 (file)
@@ -1,16 +1,16 @@
 #include <stdlib.h>
 
-#define DATA1_PIN              12
-#define DATA2_PIN              5
-#define DATA3_PIN              4
-#define DATA4_PIN              13
-#define LATCH0_PIN             11
-#define LATCH1_PIN             9
-#define LATCH2_PIN             8
-#define LATCH3_PIN             7
-#define LATCH4_PIN             6
+#define DATA1_PIN              8
+#define DATA2_PIN              9
+#define DATA3_PIN              10
+#define DATA4_PIN              11
+#define LATCH0_PIN             12
+#define LATCH1_PIN             13
+#define LATCH2_PIN             7
+#define LATCH3_PIN             5
+#define LATCH4_PIN             4
 #define LATCH5_PIN             A5
-#define BLANKING_PIN   10
+#define BLANKING_PIN   6
 
 #define BUTTON_PIN             3
 #define POT_PIN                        A4
@@ -29,37 +29,39 @@ void print_7segm(uint32_t val);
 
 void setup()
 {
-       Serial.begin(9600);
-
        pinMode(NC_PIN, INPUT);
-       srand(analogRead(NC_PIN));
-
+       uint16_t seed = analogRead(NC_PIN);
+       Serial.begin(9600);
+       Serial.print("PRNG seed: ");
+       Serial.println(seed);
+       srandom(seed);
+  
        for (uint8_t i = 0; i < 4; i++)
        {
-               pinMode(data_pins[i], OUTPUT);
                digitalWrite(data_pins[i], LOW);
+               pinMode(data_pins[i], OUTPUT);
        }
        for (uint8_t i = 0; i < DIGITS; i++)
        {
-               pinMode(latch_pins[i], OUTPUT);
                digitalWrite(latch_pins[i], HIGH);
+               pinMode(latch_pins[i], OUTPUT);
        }
        pinMode(BUTTON_PIN, INPUT_PULLUP);
        attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), button_ISR, FALLING);
        pinMode(BLANKING_PIN, OUTPUT);
-       digitalWrite(BLANKING_PIN, HIGH);
+       analogWrite(BLANKING_PIN, BRIGHTNESS);
 }
 
 void loop()
 {
        print_7segm(current_value);
-       delay(100);
+       delay(10);
 }
 
 void button_ISR(void)
 {
-       current_value = rand();
-       current_value |= ((uint32_t) rand() << 16);
+       current_value = random();
+       current_value |= ((uint32_t) random() << 16);
        Serial.print(current_value);
 }
 
@@ -73,13 +75,17 @@ void print_7segm(uint32_t val)
                digit = val % 10;
                val /= 10;
                
+               /*
                for (j = 0; j < 4; j++)
                {
                        digitalWrite(data_pins[j], digit & (1 << j));
                }
+               */
+               PORTB = (PORTB & 0xF0) | digit;
+         
                digitalWrite(latch_pins[DIGITS - i - 1], LOW);
-               delay(1);
+               delayMicroseconds(1);
                digitalWrite(latch_pins[DIGITS - i - 1], HIGH);
-               delay(1);
+               delayMicroseconds(1);
        }
-}
+}
\ No newline at end of file
index e23e5848872825076c4130f1bef9a35adf782954..c71f59ed92397aa2da14b4fc008245b6757b9a63 100644 (file)
Binary files a/Examples/7-segment/7-segm_driver_random/7-segm_driver_random.png and b/Examples/7-segment/7-segm_driver_random/7-segm_driver_random.png differ