Hi mọi người, mấy ngày nay mk đang nghiên cứu viết cái Code cho Led da Mau đa màu loại 4 chân ý, loại này điều khiển bằng 3 hệ chạy độc lập với nhau. mỗi hệ điều khiển 1 chân, chân còn lại nối chung với nguồn. Nhưng mk chả làm được như vậy nó vẫn chạy mấy con Led của Hệ thứ nhất xong rồi tới Hệ 2 và 3( Mk tạm gọi chúng là Hệ). Nói cho dễ hiểu là nếu mà làm mạch thật thì mỗi lần Led chỉ sáng dk 1 trong số 3 màu cơ bản thôi chứ nó không phối màu được với nhau để có thể tạo ra những màu khác.
Phần code của mk viết như sau( đây là hiệu ứng sáng dần, mỗi hệ sử dụng 5 IC 595 để mở rộng thêm chân, tổng 3 hệ là 15 chú). Mk có gửi kèm theo File mô phỏng và Code.
Mọi người xem và giúp đỡ mk với ạ! Thank mn nhiều!
#include<reg52.h>
#include<stdio.h>
#include<intrins.h>
/********** Khai Bao Chan **************/
sbit dulieu_1 = P2^2;
sbit dulieu_2 = P2^3;
sbit dulieu_3 = P2^4;
sbit xung = P2^0;
sbit chot = P2^1;
/***************** Khai Bao Bien *****************/
unsigned int n,m,k;
/************** Tao Thoi Gian Tre ****************/
void delay(unsigned char ms )
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<800;j++);
}
}
/****************** Dich Bit *********************/
void dich1(unsigned char x)
{
int i,tam;
for(i=0;i<8;i++)
{
tam=x;
tam=tam&0x80;
if(tam==0x80) dulieu_1=1;
else dulieu_1=0;
x=x<<1;
xung=0;
xung=1;
}
}
void dich2(unsigned long y)
{
int i,tam;
for(i=0;i<8;i++)
{
tam=y;
tam=tam&0x80;
if(tam==0x80) dulieu_2=1;
else dulieu_2=0;
y=y<<1;
xung=0;
xung=1;
}
}
void dich3(unsigned long z)
{
int i,tam;
for(i=0;i<8;i++)
{
tam=z;
tam=tam&0x80;
if(tam==0x80) dulieu_3=1;
else dulieu_3=0;
z=z<<1;
xung=0;
xung=1;
}
}
void dich_a(unsigned char a1,unsigned char a2,unsigned char a3,unsigned char a4,unsigned char a5)
{
dich1(a5);
dich1(a4);
dich1(a3);
dich1(a2);
dich1(a1);
}
void dich_b(unsigned char b1,unsigned char b2,unsigned char b3,unsigned char b4,unsigned char b5)
{
dich2(b5);
dich2(b4);
dich2(b3);
dich2(b2);
dich2(b1);
}
void dich_c(unsigned char c1,unsigned char c2,unsigned char c3,unsigned char c4,unsigned char c5)
{
dich3(c5);
dich3(c4);
dich3(c3);
dich3(c2);
dich3(c1);
}
/*************************** Chuong Trinh Hieu Ung *********************************/
void hieu_ung_1()
{
int j,p;
unsigned char code led_a1[]={0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_a2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_a3[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_a4[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_a5[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};
for(p=0;p<1;p++) //So Lan Lap Lai Hieu Ung
for(j=0;j<40;j++) //5 IC 595
{
dich_a(led_a1[j],led_a2[j],led_a3[j],led_a4[j],led_a5[j]);
chot=0;
chot=1;
delay(10); //Toc Do
}
dich_a(255,255,255,255,255);
chot=0;
chot=1;
}
void hieu_ung_2()
{
int j,p;
unsigned char code led_b1[]={0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_b2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_b3[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_b4[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_b5[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};
for(p=0;p<1;p++) //So Lan Lap Lai Hieu Ung
for(j=0;j<40;j++) //5 IC 595
{
dich_b(led_b1[j],led_b2[j],led_b3[j],led_b4[j],led_b5[j]);
chot=0;
chot=1;
delay(10); //Toc Do
}
dich_b(255,255,255,255,255);
chot=0;
chot=1;
}
void hieu_ung_3()
{
int j,p;
unsigned char code led_c1[]={0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_c2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_c3[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_c4[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_c5[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};
for(p=0;p<1;p++) //So Lan Lap Lai Hieu Ung
for(j=0;j<40;j++) //5 IC 595
{
dich_c(led_c1[j],led_c2[j],led_c3[j],led_c4[j],led_c5[j]);
chot=0;
chot=1;
delay(10); //Toc Do
}
dich_c(255,255,255,255,255);
chot=0;
chot=1;
}
/************************** Chuong Trinh Chinh ****************************************/
void main(void)
{
hieu_ung_1();
hieu_ung_2();
hieu_ung_3();
}
Phần code của mk viết như sau( đây là hiệu ứng sáng dần, mỗi hệ sử dụng 5 IC 595 để mở rộng thêm chân, tổng 3 hệ là 15 chú). Mk có gửi kèm theo File mô phỏng và Code.
Mọi người xem và giúp đỡ mk với ạ! Thank mn nhiều!
#include<reg52.h>
#include<stdio.h>
#include<intrins.h>
/********** Khai Bao Chan **************/
sbit dulieu_1 = P2^2;
sbit dulieu_2 = P2^3;
sbit dulieu_3 = P2^4;
sbit xung = P2^0;
sbit chot = P2^1;
/***************** Khai Bao Bien *****************/
unsigned int n,m,k;
/************** Tao Thoi Gian Tre ****************/
void delay(unsigned char ms )
{
int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<800;j++);
}
}
/****************** Dich Bit *********************/
void dich1(unsigned char x)
{
int i,tam;
for(i=0;i<8;i++)
{
tam=x;
tam=tam&0x80;
if(tam==0x80) dulieu_1=1;
else dulieu_1=0;
x=x<<1;
xung=0;
xung=1;
}
}
void dich2(unsigned long y)
{
int i,tam;
for(i=0;i<8;i++)
{
tam=y;
tam=tam&0x80;
if(tam==0x80) dulieu_2=1;
else dulieu_2=0;
y=y<<1;
xung=0;
xung=1;
}
}
void dich3(unsigned long z)
{
int i,tam;
for(i=0;i<8;i++)
{
tam=z;
tam=tam&0x80;
if(tam==0x80) dulieu_3=1;
else dulieu_3=0;
z=z<<1;
xung=0;
xung=1;
}
}
void dich_a(unsigned char a1,unsigned char a2,unsigned char a3,unsigned char a4,unsigned char a5)
{
dich1(a5);
dich1(a4);
dich1(a3);
dich1(a2);
dich1(a1);
}
void dich_b(unsigned char b1,unsigned char b2,unsigned char b3,unsigned char b4,unsigned char b5)
{
dich2(b5);
dich2(b4);
dich2(b3);
dich2(b2);
dich2(b1);
}
void dich_c(unsigned char c1,unsigned char c2,unsigned char c3,unsigned char c4,unsigned char c5)
{
dich3(c5);
dich3(c4);
dich3(c3);
dich3(c2);
dich3(c1);
}
/*************************** Chuong Trinh Hieu Ung *********************************/
void hieu_ung_1()
{
int j,p;
unsigned char code led_a1[]={0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_a2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_a3[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_a4[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_a5[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};
for(p=0;p<1;p++) //So Lan Lap Lai Hieu Ung
for(j=0;j<40;j++) //5 IC 595
{
dich_a(led_a1[j],led_a2[j],led_a3[j],led_a4[j],led_a5[j]);
chot=0;
chot=1;
delay(10); //Toc Do
}
dich_a(255,255,255,255,255);
chot=0;
chot=1;
}
void hieu_ung_2()
{
int j,p;
unsigned char code led_b1[]={0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_b2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_b3[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_b4[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_b5[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};
for(p=0;p<1;p++) //So Lan Lap Lai Hieu Ung
for(j=0;j<40;j++) //5 IC 595
{
dich_b(led_b1[j],led_b2[j],led_b3[j],led_b4[j],led_b5[j]);
chot=0;
chot=1;
delay(10); //Toc Do
}
dich_b(255,255,255,255,255);
chot=0;
chot=1;
}
void hieu_ung_3()
{
int j,p;
unsigned char code led_c1[]={0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_c2[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_c3[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_c4[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff};
unsigned char code led_c5[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0x03,0x07,0x0f,0x1f,0x3f,0x7f,0xff};
for(p=0;p<1;p++) //So Lan Lap Lai Hieu Ung
for(j=0;j<40;j++) //5 IC 595
{
dich_c(led_c1[j],led_c2[j],led_c3[j],led_c4[j],led_c5[j]);
chot=0;
chot=1;
delay(10); //Toc Do
}
dich_c(255,255,255,255,255);
chot=0;
chot=1;
}
/************************** Chuong Trinh Chinh ****************************************/
void main(void)
{
hieu_ung_1();
hieu_ung_2();
hieu_ung_3();
}