From: Vladimir Garistov Date: Mon, 27 Feb 2023 09:51:27 +0000 (+0200) Subject: Improved 7-segm_driver_random example X-Git-Url: https://kolegite.com/gitweb/?a=commitdiff_plain;h=6f13f6f1a5dc9f2ec429a24c125d23cf36ed02b8;p=vmks.git Improved 7-segm_driver_random example --- diff --git a/Examples/7-segment/7-segm_driver_random/7-segm_driver_random.ino b/Examples/7-segment/7-segm_driver_random/7-segm_driver_random.ino index 075efc3..00b44e8 100644 --- a/Examples/7-segment/7-segm_driver_random/7-segm_driver_random.ino +++ b/Examples/7-segment/7-segm_driver_random/7-segm_driver_random.ino @@ -1,16 +1,16 @@ #include -#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 diff --git a/Examples/7-segment/7-segm_driver_random/7-segm_driver_random.png b/Examples/7-segment/7-segm_driver_random/7-segm_driver_random.png index e23e584..c71f59e 100644 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