PDA

View Full Version : xin ae giúp đỡ về code xuất dữ liệu 74hc595 quét led matrix


yukul
01-03-2012, 02:33 PM
đây là code của mình để quét led matrix ket hop 74hc595 để tiết kiệm port cho pic.nhưng mình làm nó không chạy.
dưới là code và mô phỏng.mong a e vào giúp đỡ.thanks nhiệt tình luôn.:(


#include <18f4520.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device *=16 ADC=8
//#include <def_877A.h>
#use delay(clock=4000000)
#use fast_io(d)
#byte portd=0x83
#use fast_io(b)
#byte portb=0x81

// Dinh nghia cac chan cho ket noi 74595
#bit clk = 0x81.0 //RB0 //SHcp
#bit data = 0x81.1 //RB1 //Ds
#bit latch = 0x81.2 //RB2 //STcp---
//#bit MR=0x81.3 //Reset master

//khai bao bien
char const bangma[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0x00,0x00,0xfc,0xfc,0xfc,0xff,0xff,0xff, //chu L
0x00,0x00,0xe7,0xe7,0x00,0x00,0xff,0xff, //chu H
0xff,0x03,0x01,0xfc,0xfc,0x01,0x03,0xff, //chu V
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
};
int8 chudata1;//,chudata2;
char const cot[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
int8 k;
int8 n,t;

//KHAI BAO CHUONG TRINH CON
void send_595(int8 temp);
//CHUONG TRINH CHINH
void main()
{
set_tris_b(0x00);
port_b_pullups(false);
set_tris_d(0x00);
while (true)
{
for (k=0;k<32;k++) //trabangma
{
for(n=0;n<=20;n++)//lay du lieu chu
{
for (t=0;t<=7;t++)
{

chudata1=bangma[k+t];
//chudata2=bangma[k+t+7];

output_d(cot[t]);
//send_595(chudata2); //guidulieu
send_595(chudata1); //guidulieu
latch=1; //Chot du lieu
latch=0;
delay_us(600);
}
}
}
}
}
//CHUONG TRINH CON

void send_595(int8 temp) {
#bit flag_bit = temp.7 // bien temp la du lieu 8-bit can gui
int8 i;
//MR=1;
for(i=0;i<=7;i++)
{
if(flag_bit) //NEU FLAG_BIT==1 THI DATA=1
data=1; //bit 1
else data=0; //bit 0
clk=1;
DELAY_US(1);
clk=0;
temp<<=1; // Dich trai 1 bit
}
}
//===========Chuong trinh con hien thi=========

tdm
01-03-2012, 04:40 PM
đây là code của mình để quét led matrix ket hop 74hc595 để tiết kiệm port cho pic.nhưng mình làm nó không chạy.
dưới là code và mô phỏng.mong a e vào giúp đỡ.thanks nhiệt tình luôn.:(


#include <18f4520.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device *=16 ADC=8
//#include <def_877A.h>
#use delay(clock=4000000)
#use fast_io(d)
#byte portd=0x83
#use fast_io(b)
#byte portb=0x81

// Dinh nghia cac chan cho ket noi 74595
#bit clk = 0x81.0 //RB0 //SHcp
#bit data = 0x81.1 //RB1 //Ds
#bit latch = 0x81.2 //RB2 //STcp---
//#bit MR=0x81.3 //Reset master

//khai bao bien
char const bangma[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0x00,0x00,0xfc,0xfc,0xfc,0xff,0xff,0xff, //chu L
0x00,0x00,0xe7,0xe7,0x00,0x00,0xff,0xff, //chu H
0xff,0x03,0x01,0xfc,0xfc,0x01,0x03,0xff, //chu V
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff
};
int8 chudata1;//,chudata2;
char const cot[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
int8 k;
int8 n,t;

//KHAI BAO CHUONG TRINH CON
void send_595(int8 temp);
//CHUONG TRINH CHINH
void main()
{
set_tris_b(0x00);
port_b_pullups(false);
set_tris_d(0x00);
while (true)
{
for (k=0;k<32;k++) //trabangma
{
for(n=0;n<=20;n++)//lay du lieu chu
{
for (t=0;t<=7;t++)
{

chudata1=bangma[k+t];
//chudata2=bangma[k+t+7];

output_d(cot[t]);
//send_595(chudata2); //guidulieu
send_595(chudata1); //guidulieu
latch=1; //Chot du lieu
latch=0;
delay_us(600);
}
}
}
}
}
//CHUONG TRINH CON

void send_595(int8 temp) {
#bit flag_bit = temp.7 // bien temp la du lieu 8-bit can gui
int8 i;
//MR=1;
for(i=0;i<=7;i++)
{
if(flag_bit) //NEU FLAG_BIT==1 THI DATA=1
data=1; //bit 1
else data=0; //bit 0
clk=1;
DELAY_US(1);
clk=0;
temp<<=1; // Dich trai 1 bit
}
}
//===========Chuong trinh con hien thi=========
khai baó sai d/c portb .
khai báo lại như sau thì nó chay thôi.

#use fast_io(d)
#byte portd=0xf83
#use fast_io(b)
#byte portb=0xf81

// Dinh nghia cac chan cho ket noi 74595
#bit clk = 0xf81.0 //RB0 //SHcp
#bit data = 0xf81.1 //RB1 //Ds
#bit latch = 0xf81.2 //RB2 //STcp---

yukul
02-03-2012, 01:30 PM
khai baó sai d/c portb .
khai báo lại như sau thì nó chay thôi.

#use fast_io(d)
#byte portd=0xf83
#use fast_io(b)
#byte portb=0xf81

// Dinh nghia cac chan cho ket noi 74595
#bit clk = 0xf81.0 //RB0 //SHcp
#bit data = 0xf81.1 //RB1 //Ds
#bit latch = 0xf81.2 //RB2 //STcp---

e cám ơn a nhiều ạ.nó chạy rùi nhưng chưa đúng lắm.a có thể nói cho e đoạn code xuất dữ liệu của 74hc595 không ạ.vì e code e tham khảo nên e chưa rõ chỗ đấy.mong a giúp đỡ ạ.