Giới thiệu :
Hôm nay mình sẽ giới thiệu các bạn cách điều khiển 1 dãy led đơn hiển thị hiệu ứng theo hình trái tim ❤ sử dụng pic16f887 . Mời các bạn theo dõi ☺Chuẩn bị :
Led đơn : 32 con ( màu sắc tùy ý ) .Trở : 32 con ( tùy màu sắc led mà chọn ) .
Pic 16F887 .
Lắp mạch :
Code :
#include <16F887.H>#FUSES NOWDT,PUT,NOPROTECT,NOLVP,INTRC_IO
#USE DELAY(CLOCK=8M)
UNSIGNED INT32 I,X,Y,J,CD,DC,F,K;
VOID SANGHET()
{ SET_TRIS_B(0X00);SET_TRIS_C(0X00);SET_TRIS_D(0X00);SET_TRIS_A(0X00);
OUTPUT_B(0XFF); OUTPUT_C(0XFF);OUTPUT_D(0XFF);OUTPUT_A(0XFF);
DELAY_MS(1000);
OUTPUT_B(0X00); OUTPUT_C(0X00);OUTPUT_D(0X00);OUTPUT_A(0X00);
DELAY_MS(1000);
}
VOID SANGDUOI()
{SET_TRIS_b(0X00);SET_TRIS_C(0X00);SET_TRIS_D(0X00);SET_TRIS_A(0X00);
X=0X00000000;
FOR(I=0;I<32;I++)
{
X=(X<<1)+0X01;
OUTPUT_B(X);
OUTPUT_C(X>>8);
OUTPUT_D(X>>16);
OUTPUT_A(X>>24);
DELAY_MS(100);
}
FOR(I=0;I<32;I++)
{ X=(X>>1);
OUTPUT_B(X);
OUTPUT_C(X>>8);
OUTPUT_D(X>>16);
OUTPUT_A(X>>24);
DELAY_MS(100);
}
}
VOID SANGXENKE1()
{
for(I=0;I<2;I++)
{
SET_TRIS_b(0X00);SET_TRIS_C(0X00);SET_TRIS_D(0X00);SET_TRIS_A(0X00);
OUTPUT_B(0XAA); OUTPUT_C(0XAA);OUTPUT_D(0XAA);OUTPUT_A(0XAA); DELAY_MS(300);
OUTPUT_B(0X00); OUTPUT_C(0X00);OUTPUT_D(0X00);OUTPUT_A(0X00);DELAY_MS(100);
OUTPUT_B(0X55); OUTPUT_C(0X55);OUTPUT_D(0X55);OUTPUT_A(0X55); DELAY_MS(300);
OUTPUT_B(0X00); OUTPUT_C(0X00);OUTPUT_D(0X00);OUTPUT_A(0X00);DELAY_MS(100);
}
}
VOID SANGCAP()
{ SET_TRIS_B(0X00); SET_TRIS_C(0X00);SET_TRIS_D(0X00);SET_TRIS_A(0X00);X=0X00000000;Y=0X0000;
FOR (I=0;I<16;I++)
{
X=(X<<1)+0X0001; Y=(Y>>1)+0X8000;
OUTPUT_B(X); OUTPUT_A(Y>>8);
OUTPUT_C(X>>8);OUTPUT_D(Y);
DELAY_MS(500);
}
}
VOID SANGCAP1()
{ SET_TRIS_B(0X00); SET_TRIS_C(0X00);SET_TRIS_D(0X00);SET_TRIS_A(0X00);X=0X00000000;Y=0X0000;
FOR(I=0;I<16;I++)
{
X=(X>>1)+0X8000; Y=(Y<<1)+0X0001;
OUTPUT_C(X>>8);OUTPUT_D(Y);
OUTPUT_B(X);OUTPUT_A(Y>>8);
DELAY_MS(300);
}
}
VOID sangdon2()
{ CD=0X00000000;
FOR ( J=16;J>0;J--)
{
DC=0X00000003;
FOR(I=0;I<J;I++)
{
Y=CD+DC;
OUTPUT_B(Y); OUTPUT_C(Y>>8); OUTPUT_D(Y>>16); OUTPUT_A(Y>>24); DELAY_MS(100);
DELAY_MS(100);
DC=(DC<<2);
}
CD=Y;
}
}
VOID sangdon1()
{ CD=0X00000000;
FOR ( J=32;J>0;J--)
{
DC=0X00000001;
FOR(I=0;I<J;I++)
{
Y=CD+DC;
OUTPUT_B(Y); OUTPUT_C(Y>>8); OUTPUT_D(Y>>16); OUTPUT_A(Y>>24); DELAY_MS(40);
DELAY_MS(100);
DC=(DC<<1);
}
CD=Y;
}
}
VOID SANG1()
{ X=0X00000001;J=0X80000000;
FOR(I=0;I<32;I++)
{
Y=X;
OUTPUT_B(Y); OUTPUT_C(Y>>8); OUTPUT_D(Y>>16); OUTPUT_A(Y>>24);
DELAY_MS(100);
X=(X<<1);
}
FOR (I=0;I<32;I++)
{
F=J;
OUTPUT_B(F); OUTPUT_C(F>>8); OUTPUT_D(F>>16); OUTPUT_A(F>>24);
DELAY_MS(100);
J=J>>1;
}
}
VOID SANGNUA()
{CD=0X00000000;DC=0X00000000;
FOR(I=16;I>0;I--)
{X=0X0001;J=0X8000;
FOR (K=0;K<I;K++)
{
Y=CD+X ; F=DC+J;
OUTPUT_B(Y); OUTPUT_C(Y>>8); OUTPUT_D(F); OUTPUT_A(F>>8);
DELAY_MS(100);
X=(X<<1);J=(J>>1);
}
CD=Y ; DC=F;
}
}
VOID MAIN()
{ SET_TRIS_B(0X00); SET_TRIS_C(0X00);SET_TRIS_D(0X00);SET_TRIS_A(0X00);
WHILE (TRUE)
{
SANGHET();
SANGDUOI();
SANGXENKE1();
SANGCAP();
SANGCAP1();
SANGDON1();
SANGDON2();
SANG1();
SANGNUA();
}
}
Không có nhận xét nào