Lampu LED seolah-olah berjalan dari satu led ke led berikutnya.. dari led-1 ke led-16.. karena 74HC595 fungsinya mengubah input data serial ke parallel dengan 8-bit.. karena jumlah led ada 16 buah dibutuhkan dua buah 74hc595.. jadi disini menggeser bit-0 ke bit-7 lalu lanjutkan ke 74hc595 yang kedua.. dan terus berulang-ulang..
Sketch tanpa library shift register 74hc595..
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const int SH = 8; | |
const int DS = 9; | |
const int ST = 10; | |
unsigned int j; | |
void _74HC595(char x) | |
{ | |
int k; | |
for(k=0;k<8;k++) | |
{ | |
if((x & 0x80)==0x80) | |
{ | |
digitalWrite(DS,HIGH); | |
} | |
else | |
{ | |
digitalWrite(DS,LOW); | |
} | |
x=x<<1; | |
digitalWrite(SH,LOW); | |
digitalWrite(SH,HIGH); | |
} | |
} | |
void LED( unsigned char temp1,unsigned char temp2)//unsigned char temp3,unsigned char temp4,unsigned char temp5,unsigned char temp6,unsigned char temp7,unsigned char temp8) | |
{ | |
_74HC595(temp1); | |
_74HC595(temp2); | |
digitalWrite(ST,LOW); | |
digitalWrite(ST,HIGH); | |
} | |
void LED1() | |
{ | |
unsigned char chay[9]={0x01,0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; | |
for(j=1;j<=8;j++) | |
{ | |
LED(0,chay[j]); | |
delay(20); | |
} | |
for(j=1;j<=8;j++) | |
{ | |
LED(chay[j],0); | |
delay(20); | |
} | |
} | |
void LED2() | |
{ | |
unsigned char ledcode[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01}; | |
for(j=0;j<=7;j++) | |
{ | |
LED(ledcode[j],0); | |
delay(20); | |
} | |
for(j=0;j<=7;j++) | |
{ | |
LED(0,ledcode[j]); | |
delay(20); | |
} | |
} | |
//============================================================= | |
void LED3() | |
{ | |
unsigned char ledcode[8]={0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff}; | |
for(j=0;j<=7;j++) | |
{ | |
LED(0,ledcode[j]); | |
delay(20); | |
} | |
for(j=0;j<=7;j++) | |
{ | |
LED(ledcode[j],255); | |
delay(20); | |
} | |
} | |
void LED4() | |
{ | |
unsigned char dan[8]={0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff}; | |
for(j=0;j<=7;j++) | |
{ | |
LED(dan[j],0); | |
delay(20); | |
} | |
for(j=0;j<=7;j++) | |
{ | |
LED(255,dan[j]); | |
delay(20); | |
} | |
} | |
void setup() { | |
// put your setup code here, to run once: | |
pinMode(SH, OUTPUT); | |
pinMode(DS, OUTPUT); | |
pinMode(ST, OUTPUT); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
while(1) | |
{ | |
LED1(); | |
LED2(); | |
LED3(); | |
LED4(); | |
} | |
} |
Post a Comment for "Bermain Running LED dengan Arduino dan Shift Register 74HC595"
Komentar hanya untuk yang baik-baik dan tidak spam