Arduino – Display module 8×7 segmenten (75HC595)
Hardware
Dit 7 segment, 8 digits display gebruikt 75HC595 ICs en kan met 3 I/O aansluitingen aangestuurd worden.
- Voltage: 3.3 – 5V.
- Aangestuurd door 74HC595 ICs.
- Gebruikt maar 3 I/O poorten van een microcontroller.
Pinout
Pin: | Functie: |
---|---|
01 | +5v |
02 | GND |
03 | DIO (of ook wel DIN) dit is de DATA pin |
04 | SCK dit is de CLOCK pin |
05 | RCK dit is de LATCH pin |
Informatie?
De ICs gebruiken binaire data met een shift register om de segmenten aan te sturen.
Aansluiten op de Arduino
Sluit het volgende aan volgens onderstaand overzicht:
Arduino pin: | Display pin: |
---|---|
+5v | +5v |
GND | GND |
D11 | 03 (DIO) |
D12 | 04 (SCK / CLOCK) |
D13 | 05 (RCK / LATCH) |
Basic Script
Het nadeel van dit script is dat je telkens de refresh in de LOOP moet hebben, laat je de arduino andere “activiteiten” doen, dan flikkert het display.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
// Configureer de pinnen. int latchPin = 13; int clockPin = 12; int dataPin = 11; void setup () { // Configureer de pin modes. pinMode(latchPin,OUTPUT); pinMode(clockPin,OUTPUT); pinMode(dataPin,OUTPUT); } void loop() { // laat de nummers 1 t/m 8 op het display zien. showNum(12345678); } void disNum(int Num_Qua, int Num_Pos, int Num_Dec) { int Num_Qua_B = 0; int Num_Pos_B = 0; switch (Num_Qua) { case 0:Num_Qua_B = 192;break; case 1:Num_Qua_B = 249;break; case 2:Num_Qua_B = 164;break; case 3:Num_Qua_B = 176;break; case 4:Num_Qua_B = 153;break; case 5:Num_Qua_B = 146;break; case 6:Num_Qua_B = 130;break; case 7:Num_Qua_B = 248;break; case 8:Num_Qua_B = 128;break; case 9:Num_Qua_B = 144;break; default: Num_Qua_B = 255;break; }; switch (Num_Pos) { case 1:Num_Pos_B = 1;break; case 2:Num_Pos_B = 2;break; case 3:Num_Pos_B = 4;break; case 4:Num_Pos_B = 8;break; case 5:Num_Pos_B = 16;break; case 6:Num_Pos_B = 32;break; case 7:Num_Pos_B = 64;break; case 8:Num_Pos_B = 128;break; default: Num_Pos_B = 0;break; }; if (Num_Dec == 1) { Num_Qua_B = Num_Qua_B + 128; }; digitalWrite(latchPin,LOW); int L = Num_Qua_B; int R = Num_Pos_B; shiftOut(dataPin,clockPin,MSBFIRST,R); shiftOut(dataPin,clockPin,MSBFIRST,L); digitalWrite(latchPin,HIGH); delayMicroseconds(100); } void showNum(double number) { long num = number; int ShowDec [8]; for (int x = 1; x < 9; x++) { ShowDec [x] = 0; } if (num < 10000000){ if (num < 1000000){ num = number*100; ShowDec[6]=1;ShowDec[7]=0; } else { num = number*10; ShowDec[7]=1; } } int ShowNumber [8]; ShowNumber[1] = (num/10000000)%10; ShowNumber[2] = (num/1000000)%10; ShowNumber[3] = (num/100000)%10; ShowNumber[4] = (num/10000)%10; ShowNumber[5] = (num/1000)%10; ShowNumber[6] = (num/100)%10; ShowNumber[7] = (num/10)%10; ShowNumber[8] = (num/1)%10; int dectest = 0; for (int x = 1; x<8; x++) { if ((dectest == 0)and(ShowNumber[x] == 0)) { ShowNumber[x] = 11; } else { dectest = 1; } } disNum(ShowNumber[1],1,ShowDec[1]); disNum(ShowNumber[2],2,ShowDec[2]); disNum(ShowNumber[3],3,ShowDec[3]); disNum(ShowNumber[4],4,ShowDec[4]); disNum(ShowNumber[5],5,ShowDec[5]); disNum(ShowNumber[6],6,ShowDec[6]); disNum(ShowNumber[7],7,ShowDec[7]); disNum(ShowNumber[8],8,ShowDec[8]); } |
Script met timer functie
Dit script heeft geen last meer van vertragingen in het loop script, de functie om het display te verversen is gekoppeld aan een timer functie!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
const int clock = 12; //SCK const int latch = 13; //RCK const int data = 11; //DIO byte value[] ={ B11111001, // 1 B10100100, // 2 B10110000, // 3 B10011001, // 4 B10010010, // 5 B10000010, // 6 B11111000, // 7 B10000000, // 8 B10010000, // 9 B11000000, // 0 B11111111};// display nothing byte digit[] ={ B00000001, // left segment B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000}; // right segment byte ii = 0; byte v0, v1,v2,v3,v4,v5,v6,v7; byte vvalue = value[0]; void setup() { pinMode(clock, OUTPUT); pinMode(latch, OUTPUT); pinMode(data, OUTPUT); cli();//stop interrupts //set timer0 interrupt at 980Hz TCCR0A = 0;// set entire TCCR0A register to 0 TCCR0B = 0;// same for TCCR0B TCNT0 = 0;//initialize counter value to 0 OCR0A = 255;//(must be 16000000 / (prescaler*255) = 980 Hz TCCR0A |= (1 << WGM01); TCCR0B |= (1 << CS01) | (1 << CS00); //prescaler = 64 TIMSK0 |= (1 << OCIE0A); sei();//allow interrupts v0=v1=v2=v3=v4=v5=v6=v7=10; // default: show nothing } ISR(TIMER0_COMPA_vect){ ii++; if (ii==8) ii=0; if (ii==0) { vvalue = v0; } else if (ii==1) { vvalue = v1; } else if (ii==2) { vvalue = v2; } else if (ii==3) { vvalue = v3; } else if (ii==4) { vvalue = v4; } else if (ii==5) { vvalue = v5; } else if (ii==6) { vvalue = v6; } else if (ii==7) { vvalue = v7; } digitalWrite(latch,LOW); shiftOut(data,clock,MSBFIRST,B11111111); shiftOut(data,clock,MSBFIRST,B11111111); digitalWrite(latch,HIGH); digitalWrite(latch,LOW); shiftOut(data,clock,MSBFIRST,digit[ii]); shiftOut(data,clock,MSBFIRST,value[vvalue]); digitalWrite(latch,HIGH); } int a, b,t,d; void loop() {; // v[n] = digit nummer (start=0) = getal (start=0, uit=10) v0 = 0; delay(1000); v2 = 2; delay(1000); v4 = 4; delay(1000); v6 = 6; delay(1000); v1 = 1; delay(1000); v3 = 3; delay(1000); v5 = 5; delay(1000); v7 = 7; delay(1000); v0=v1=v2=v3=v4=v5=v6=v7=10; //reset display delay(1000); } |
Script met timer functie (demo)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
const int clock = 12; //SCK const int latch = 13; //RCK const int data = 11; //DIO byte value[] ={ B11111001, // 1 B10100100, // 2 B10110000, // 3 B10011001, // 4 B10010010, // 5 B10000010, // 6 B11111000, // 7 B10000000, // 8 B10010000, // 9 B11000000, // 0 B11111111};// display nothing byte digit[] ={ B00000001, // left segment B00000010, B00000100, B00001000, B00010000, B00100000, B01000000, B10000000}; // right segment byte ii = 0; byte v0, v1,v2,v3,v4,v5,v6,v7; byte vvalue = value[0]; void setup() { pinMode(clock, OUTPUT); pinMode(latch, OUTPUT); pinMode(data, OUTPUT); cli();//stop interrupts //set timer0 interrupt at 980Hz TCCR0A = 0;// set entire TCCR0A register to 0 TCCR0B = 0;// same for TCCR0B TCNT0 = 0;//initialize counter value to 0 OCR0A = 255;//(must be 16000000 / (prescaler*255) = 980 Hz TCCR0A |= (1 << WGM01); TCCR0B |= (1 << CS01) | (1 << CS00); //prescaler = 64 TIMSK0 |= (1 << OCIE0A); sei();//allow interrupts v0=v1=v2=v3=v4=v5=v6=v7=10; // default: show nothing } ISR(TIMER0_COMPA_vect){ ii++; if (ii==8) ii=0; if (ii==0) { vvalue = v0; } else if (ii==1) { vvalue = v1; } else if (ii==2) { vvalue = v2; } else if (ii==3) { vvalue = v3; } else if (ii==4) { vvalue = v4; } else if (ii==5) { vvalue = v5; } else if (ii==6) { vvalue = v6; } else if (ii==7) { vvalue = v7; } digitalWrite(latch,LOW); shiftOut(data,clock,MSBFIRST,B11111111); shiftOut(data,clock,MSBFIRST,B11111111); digitalWrite(latch,HIGH); digitalWrite(latch,LOW); shiftOut(data,clock,MSBFIRST,digit[ii]); shiftOut(data,clock,MSBFIRST,value[vvalue]); digitalWrite(latch,HIGH); } int a, b,t,d; void loop() {; b = random(0,11); // value to display t = random(25,100); // time to count d = random(0,8); // digit to change switch (d) { case 0: for (a=0;a<=b;++a) {v0 = a; delay(t);} break; case 1: for (a=0;a<=b;++a) {v1 = a; delay(t);} break; case 2: for (a=0;a<=b;++a) {v2 = a; delay(t);} break; case 3: for (a=0;a<=b;++a) {v3 = a; delay(t);} break; case 4: for (a=0;a<=b;++a) {v4 = a; delay(t);} break; case 5: for (a=0;a<=b;++a) {v5 = a; delay(t);} break; case 6: for (a=0;a<=b;++a) {v6 = a; delay(t);} break; case 7: for (a=0;a<=b;++a) {v7 = a; delay(t);} break; default: v0=v1=v2=v3=v4=v5=v6=v7=10; break; } } |