tinhnv92
19-03-2013, 09:32 AM
Chào tất cả a c e trên diễn đàn . E đang làm bài tập quét matrix 8x8 dùng PIC 18f2550 . e dùng IC 74HC595 để quét cột được nói vào PORTC(SH_CP -> PORTCbits.RC0, ST_CP -> PORTCbits.RC2, RS-> PORTCbits.RC3) , và dữ liệu được truyền từ PORTB như hình vẽ đính kèm ..
Em lập trình bằng MPLAB
Nhưng nó không chạy ! Xin a e trên diễn đàn giúp ,
ở dưới đây là code , còn
#include<p18f2550.h>
#include<delays.h>
unsigned char font[8]=
{
0xff,0xDf,0xBf,0x7f,0x00,0xff,0xff,0xff // 1;
};
// DINH NGHIA CAC HAM DIEU KHIEN 74HC595
#define SH_CP PORTCbits.RC0 //11 (chan vao xung clock) Khi có 1 xung clock tích cuc o suon duong( 0 lên 1) thì 1bit duoc dich vào ic
#define DS PORTCbits.RC1 //14 Ðau vào du lieu noi tiep . Tai 1 thoi diem xung clock chi dua vào duoc 1 bit
#define ST_CP PORTCbits.RC2 //12 xung clock chot du lieu. Khi có 1 xung clock tích c?c ? su?n duong thì cho phép xu?t d? li?u trên các chân output.(chú ý chi?u d?ch d? li?u t? Qa=>Qh)
unsigned char j, column= 0x01; // khai bao bien toan cuc column;
void write_digit()
{
int i ;
//column = 0x01 ;
ST_CP=0; //same as RC1=0; enable write to shift register
SH_CP=0; //same as RC0=0; enable write to bit
//this is the loop that sends the data bit by bit to the 74HC595 since its 8 bits to send we loop it 8 times.
for ( i=0;i<8;i++)
{ // this sends the bit to the register and "dig_num" is the digit we want to display from the array Digit[].
// testbit(Digit[dig_num],i) takes bit i from the char in the array "Digit[dig_num]" and puts that on pin DS
// which is our data pin.
DS = column ; //if you have common cathode display use "DS=testbit(Digit[dig_num],i)^1"
SH_CP=1; //same as RC1=1; write bit
SH_CP=0; //same as RC1=0;move on to next bit
column = column <<1;
}
ST_CP =1 ; //same as RC0=1; write the new bits to the register.
}
unsigned char repeat ,num ,count;
//---------------------------------------------------------------
void main()
{
TRISC = 0x00 ; // Khai bao PORT B,C la ngo ra
LATC = 0x00 ; // Write gia tri o cong C ban dau la muc thap
TRISB = 0x00 ;
while (1)
{
for(num = 0 ; num < 7; num++ )
{
column = 0x01;
for (repeat=0; repeat<20; repeat++ )
{
for (count = num*8; count < (num*8+8);count++)
{
PORTB = font[count];
write_digit() ;
Delay1KTCYx(2);
column = column <<1 ; // e dang phan van neu thay doi column thi trong ham co thay doi khong???
}
}
}
}
}
Em lập trình bằng MPLAB
Nhưng nó không chạy ! Xin a e trên diễn đàn giúp ,
ở dưới đây là code , còn
#include<p18f2550.h>
#include<delays.h>
unsigned char font[8]=
{
0xff,0xDf,0xBf,0x7f,0x00,0xff,0xff,0xff // 1;
};
// DINH NGHIA CAC HAM DIEU KHIEN 74HC595
#define SH_CP PORTCbits.RC0 //11 (chan vao xung clock) Khi có 1 xung clock tích cuc o suon duong( 0 lên 1) thì 1bit duoc dich vào ic
#define DS PORTCbits.RC1 //14 Ðau vào du lieu noi tiep . Tai 1 thoi diem xung clock chi dua vào duoc 1 bit
#define ST_CP PORTCbits.RC2 //12 xung clock chot du lieu. Khi có 1 xung clock tích c?c ? su?n duong thì cho phép xu?t d? li?u trên các chân output.(chú ý chi?u d?ch d? li?u t? Qa=>Qh)
unsigned char j, column= 0x01; // khai bao bien toan cuc column;
void write_digit()
{
int i ;
//column = 0x01 ;
ST_CP=0; //same as RC1=0; enable write to shift register
SH_CP=0; //same as RC0=0; enable write to bit
//this is the loop that sends the data bit by bit to the 74HC595 since its 8 bits to send we loop it 8 times.
for ( i=0;i<8;i++)
{ // this sends the bit to the register and "dig_num" is the digit we want to display from the array Digit[].
// testbit(Digit[dig_num],i) takes bit i from the char in the array "Digit[dig_num]" and puts that on pin DS
// which is our data pin.
DS = column ; //if you have common cathode display use "DS=testbit(Digit[dig_num],i)^1"
SH_CP=1; //same as RC1=1; write bit
SH_CP=0; //same as RC1=0;move on to next bit
column = column <<1;
}
ST_CP =1 ; //same as RC0=1; write the new bits to the register.
}
unsigned char repeat ,num ,count;
//---------------------------------------------------------------
void main()
{
TRISC = 0x00 ; // Khai bao PORT B,C la ngo ra
LATC = 0x00 ; // Write gia tri o cong C ban dau la muc thap
TRISB = 0x00 ;
while (1)
{
for(num = 0 ; num < 7; num++ )
{
column = 0x01;
for (repeat=0; repeat<20; repeat++ )
{
for (count = num*8; count < (num*8+8);count++)
{
PORTB = font[count];
write_digit() ;
Delay1KTCYx(2);
column = column <<1 ; // e dang phan van neu thay doi column thi trong ham co thay doi khong???
}
}
}
}
}