Chào mọi người trong diễn đàn !
Em đang làm về led ma trận 7 màu.Hiện tại e mới chỉ test hiển thị chữ A sáng 1 màu.em tắt hết led trong while(1) của hàm main,bật led bằng tràn timer0.Nhưng thực tế nó bị nháy led liên tục,không ra chữ nào.Mọi người giúp em với ạ.(Em up cả project lên thì web báo lỗi)
Em cảm ơn
Hàm main :
#include <avr/io.h>
#define F_CPU 8000000UL
#include <util/delay.h>
#include "74hc595.h"
#include "timer0.h"
#include "font.h"
#define sbi(port , bit) (port)|=(1<<(bit)) //set bit -> 1
#define cbi(port, bit) (port) &= ~(1 << (bit)) //clear bit -> 0
#define bit_is_set(sfr,bit) (_SFR_BYTE(sfr) & _BV(bit)) //hoi bit 1
#define bit_is_clear(sfr,bit) (!(_SFR_BYTE(sfr) & _BV(bit))) //hoi bit 0
void main(){
int i;
timer0(); //kich hoat hien thi bang timer0
while(1){
convert(0xff,0xff,0xff,0xff); //tat tat ca led
}
}
Hàm 74hc595:
#include <avr/io.h>
#define F_CPU 8000000UL
#include <util/delay.h>
#define sbi(port , bit) (port)|=(1<<(bit)) //set bit -> 1
#define cbi(port, bit) (port) &= ~(1 << (bit)) //clear bit -> 0
#define DDRX DDRA
#define PORTX PORTA
#define SCK1 sbi(PORTX,6) //set chan 11(SH_CP)-xung clock len =1,suong duong 0->1 =>1 bit dc dich vao IC
#define DS1 sbi(PORTX,4) //set chan 14(DS)-du lieu len =1
#define LAT1 sbi(PORTX,5) //set chan 12(ST_CP)-chot,xuat du lieu len =1. suong duong 0->1
#define SCK0 cbi(PORTX,6) //clear chan 11 =0;
#define DS0 cbi(PORTX,4) //clear chan 14 =0;
#define LAT0 cbi(PORTX,5)
void convert(int byte1,int byte2,int byte3,int byte4){
DDRX = 0xff; //PORTA output
int bit_q,i;
bit_q = byte4; //gia tri byte4 la gia tri can dich vao IC
for(i=0;i<8;i++) { //kiem tra su xuat hien cua bit1 trong day 8bit khoi tao cua byte4;
//DS1 = (bit_q & 0x80)? 1:0; //so sanh bit_q & 0x80 va 0x80,dung =>DS=1 ; sai =>DS=0;
PORTX = bit_q; //kq : DS7|DS6|DS5|DS4|DS3|DS2|DS1|DS0
PORTX &= 0x80;
if(PORTX == 0x80) DS1; //lay dc bit1
else DS0; //lay dc bit0
SCK0;
_delay_us(5); //xung kich 74hc595 phai >5us
SCK1; // 0->1 dich bit vao IC
bit_q = bit_q<<1; //kiem tra lan luot tu D7->D6->...->D0
}
bit_q = byte3; //gia tri byte3 la gia tri can dich vao IC
for(i=0;i<8;i++) { //kiem tra su xuat hien cua bit1 trong day 8bit khoi tao cua byte3;
//DS1 = (bit_q & 0x80)? 1:0; //so sanh bit_q & 0x80 va 0x80,dung =>DS=1 ; sai =>DS=0;
PORTX = bit_q; //kq : DS7|DS6|DS5|DS4|DS3|DS2|DS1|DS0
PORTX &= 0x80;
if(PORTX == 0x80) DS1; //lay dc bit1
else DS0; //lay dc bit0
SCK0;
_delay_us(5); //xung kich 74hc595 phai >5us
SCK1; // 0->1 dich bit vao IC
bit_q = bit_q<<1; //kiem tra lan luot tu D7->D6->...->D0
}
bit_q = byte2; //gia tri byte1 la gia tri can dich vao IC
for(i=0;i<8;i++) { //kiem tra su xuat hien cua bit1 trong day 8bit khoi tao cua byte2;
//DS1 = (bit_q & 0x80)? 1:0; //so sanh bit_q & 0x80 va 0x80,dung =>DS=1 ; sai =>DS=0;
PORTX = bit_q; //kq : DS7|DS6|DS5|DS4|DS3|DS2|DS1|DS0
PORTX &= 0x80;
if(PORTX == 0x80) DS1; //lay dc bit1
else DS0; //lay dc bit0
SCK0;
_delay_us(5); //xung kich 74hc595 phai >5us
SCK1; // 0->1 dich bit vao IC
bit_q = bit_q<<1; //kiem tra lan luot tu D7->D6->...->D0
}
bit_q = byte1; //gia tri byte1 la gia tri can dich vao IC
for(i=0;i<8;i++) { //kiem tra su xuat hien cua bit1 trong day 8bit khoi tao cua byte1;
//DS1 = (bit_q & 0x80)? 1:0; //so sanh bit_q & 0x80 va 0x80,dung =>DS=1 ; sai =>DS=0;
PORTX = bit_q; //kq : DS7|DS6|DS5|DS4|DS3|DS2|DS1|DS0
PORTX &= 0x80;
if(PORTX == 0x80) DS1; //lay dc bit1
else DS0; //lay dc bit0
SCK0;
_delay_us(5); //xung kich 74hc595 phai >5us
SCK1; // 0->1 dich bit vao IC
bit_q = bit_q<<1; //kiem tra lan luot tu D7->D6->...->D0
}
LAT0;
_delay_us(5); //xung kich 74hc595 phai >5us
LAT1; //0->1 : xuat du lieu
_delay_us(5); //xung kich 74hc595 phai >5us
}
Hàm timer0:
#include <avr/io.h>
#define F_CPU 8000000UL
#include <avr/interrupt.h>
#include <util/delay.h>
#define sbi(port , bit) (port)|=(1<<(bit)) //set bit -> 1
#define cbi(port, bit) (port) &= ~(1 << (bit)) //clear bit -> 0
#define bit_is_set(sfr,bit) (_SFR_BYTE(sfr) & _BV(bit)) //hoi bit 1
#define bit_is_clear(sfr,bit) (!(_SFR_BYTE(sfr) & _BV(bit))) //hoi bit 0
#define blue 1
#define red 2
#define green 3
void timer0(){
//timer0 : 20ms
TCCR0 |=(1<<CS12)|(1<<CS10); //(1<<CS12)|(0<<CS11)|(1<<CS10);prescaler = 1024
TCNT0= 98; //khoi tao TCNT1=255-(thoi_gian/((1/F_crystal)*prescaler))=255 - 156.25 ; thoi_gian(s)
TIMSK |=(1<<TOIE0); //cho phep ngat khi tran T/C0
sei(); //set bit I (SREG) gloabal interrupt
}
//phuc vu ngat tran T/C1
ISR (TIMER0_OVF_vect ){
TCNT0=98; //gan lai TCNT1 sau khi tran
A_upper_single(blue); //hien thi chu A mau xanh
}
Hàm font chữ:
#include <avr/io.h>
#define F_CPU 8000000UL
#include <avr/interrupt.h>
#include <util/delay.h>
#define sbi(port , bit) (port)|=(1<<(bit)) //set bit -> 1
#define cbi(port, bit) (port) &= ~(1 << (bit)) //clear bit -> 0
#define bit_is_set(sfr,bit) (_SFR_BYTE(sfr) & _BV(bit)) //hoi bit 1
#define bit_is_clear(sfr,bit) (!(_SFR_BYTE(sfr) & _BV(bit))) //hoi bit 0
#define blue 1
#define red 2
#define green 3
int hang[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
int A[]={0xff,0x03,0x01,0xcc,0xcc,0x01,0x03,0xff};
int a[]={0xff,0x8d,0x06,0x36,0x36,0x00,0x81,0x7f};
///// convert(hang,blue,red,green);
void A_upper_single(int x){ //chu A hoa 1 mau
int i;
for(i=0;i<8;i++){
if(x == blue){
convert(hang[i],A[i],0xff,0xff);
_delay_ms(1);
}
if(x == red){
convert(hang[i],0xff,A[i],0xff);
_delay_ms(1);
}
if(x == green){
convert(hang[i],0xff,0xff,A[i]);
_delay_ms(1);
}
}
}
void a_lower_single(int x){ //chu a thuong 1 mau
int i;
for(i=0;i<8;i++){
if(x == blue){
convert(hang[i],a[i],0xff,0xff);
_delay_ms(1);
}
if(x == red){
convert(hang[i],0xff,a[i],0xff);
_delay_ms(1);
}
if(x == green){
convert(hang[i],0xff,0xff,a[i]);
_delay_ms(1);
}
}
}
/////////////////////////////////////////////////////////////////////
Em đang làm về led ma trận 7 màu.Hiện tại e mới chỉ test hiển thị chữ A sáng 1 màu.em tắt hết led trong while(1) của hàm main,bật led bằng tràn timer0.Nhưng thực tế nó bị nháy led liên tục,không ra chữ nào.Mọi người giúp em với ạ.(Em up cả project lên thì web báo lỗi)
Em cảm ơn
Hàm main :
#include <avr/io.h>
#define F_CPU 8000000UL
#include <util/delay.h>
#include "74hc595.h"
#include "timer0.h"
#include "font.h"
#define sbi(port , bit) (port)|=(1<<(bit)) //set bit -> 1
#define cbi(port, bit) (port) &= ~(1 << (bit)) //clear bit -> 0
#define bit_is_set(sfr,bit) (_SFR_BYTE(sfr) & _BV(bit)) //hoi bit 1
#define bit_is_clear(sfr,bit) (!(_SFR_BYTE(sfr) & _BV(bit))) //hoi bit 0
void main(){
int i;
timer0(); //kich hoat hien thi bang timer0
while(1){
convert(0xff,0xff,0xff,0xff); //tat tat ca led
}
}
Hàm 74hc595:
#include <avr/io.h>
#define F_CPU 8000000UL
#include <util/delay.h>
#define sbi(port , bit) (port)|=(1<<(bit)) //set bit -> 1
#define cbi(port, bit) (port) &= ~(1 << (bit)) //clear bit -> 0
#define DDRX DDRA
#define PORTX PORTA
#define SCK1 sbi(PORTX,6) //set chan 11(SH_CP)-xung clock len =1,suong duong 0->1 =>1 bit dc dich vao IC
#define DS1 sbi(PORTX,4) //set chan 14(DS)-du lieu len =1
#define LAT1 sbi(PORTX,5) //set chan 12(ST_CP)-chot,xuat du lieu len =1. suong duong 0->1
#define SCK0 cbi(PORTX,6) //clear chan 11 =0;
#define DS0 cbi(PORTX,4) //clear chan 14 =0;
#define LAT0 cbi(PORTX,5)
void convert(int byte1,int byte2,int byte3,int byte4){
DDRX = 0xff; //PORTA output
int bit_q,i;
bit_q = byte4; //gia tri byte4 la gia tri can dich vao IC
for(i=0;i<8;i++) { //kiem tra su xuat hien cua bit1 trong day 8bit khoi tao cua byte4;
//DS1 = (bit_q & 0x80)? 1:0; //so sanh bit_q & 0x80 va 0x80,dung =>DS=1 ; sai =>DS=0;
PORTX = bit_q; //kq : DS7|DS6|DS5|DS4|DS3|DS2|DS1|DS0
PORTX &= 0x80;
if(PORTX == 0x80) DS1; //lay dc bit1
else DS0; //lay dc bit0
SCK0;
_delay_us(5); //xung kich 74hc595 phai >5us
SCK1; // 0->1 dich bit vao IC
bit_q = bit_q<<1; //kiem tra lan luot tu D7->D6->...->D0
}
bit_q = byte3; //gia tri byte3 la gia tri can dich vao IC
for(i=0;i<8;i++) { //kiem tra su xuat hien cua bit1 trong day 8bit khoi tao cua byte3;
//DS1 = (bit_q & 0x80)? 1:0; //so sanh bit_q & 0x80 va 0x80,dung =>DS=1 ; sai =>DS=0;
PORTX = bit_q; //kq : DS7|DS6|DS5|DS4|DS3|DS2|DS1|DS0
PORTX &= 0x80;
if(PORTX == 0x80) DS1; //lay dc bit1
else DS0; //lay dc bit0
SCK0;
_delay_us(5); //xung kich 74hc595 phai >5us
SCK1; // 0->1 dich bit vao IC
bit_q = bit_q<<1; //kiem tra lan luot tu D7->D6->...->D0
}
bit_q = byte2; //gia tri byte1 la gia tri can dich vao IC
for(i=0;i<8;i++) { //kiem tra su xuat hien cua bit1 trong day 8bit khoi tao cua byte2;
//DS1 = (bit_q & 0x80)? 1:0; //so sanh bit_q & 0x80 va 0x80,dung =>DS=1 ; sai =>DS=0;
PORTX = bit_q; //kq : DS7|DS6|DS5|DS4|DS3|DS2|DS1|DS0
PORTX &= 0x80;
if(PORTX == 0x80) DS1; //lay dc bit1
else DS0; //lay dc bit0
SCK0;
_delay_us(5); //xung kich 74hc595 phai >5us
SCK1; // 0->1 dich bit vao IC
bit_q = bit_q<<1; //kiem tra lan luot tu D7->D6->...->D0
}
bit_q = byte1; //gia tri byte1 la gia tri can dich vao IC
for(i=0;i<8;i++) { //kiem tra su xuat hien cua bit1 trong day 8bit khoi tao cua byte1;
//DS1 = (bit_q & 0x80)? 1:0; //so sanh bit_q & 0x80 va 0x80,dung =>DS=1 ; sai =>DS=0;
PORTX = bit_q; //kq : DS7|DS6|DS5|DS4|DS3|DS2|DS1|DS0
PORTX &= 0x80;
if(PORTX == 0x80) DS1; //lay dc bit1
else DS0; //lay dc bit0
SCK0;
_delay_us(5); //xung kich 74hc595 phai >5us
SCK1; // 0->1 dich bit vao IC
bit_q = bit_q<<1; //kiem tra lan luot tu D7->D6->...->D0
}
LAT0;
_delay_us(5); //xung kich 74hc595 phai >5us
LAT1; //0->1 : xuat du lieu
_delay_us(5); //xung kich 74hc595 phai >5us
}
Hàm timer0:
#include <avr/io.h>
#define F_CPU 8000000UL
#include <avr/interrupt.h>
#include <util/delay.h>
#define sbi(port , bit) (port)|=(1<<(bit)) //set bit -> 1
#define cbi(port, bit) (port) &= ~(1 << (bit)) //clear bit -> 0
#define bit_is_set(sfr,bit) (_SFR_BYTE(sfr) & _BV(bit)) //hoi bit 1
#define bit_is_clear(sfr,bit) (!(_SFR_BYTE(sfr) & _BV(bit))) //hoi bit 0
#define blue 1
#define red 2
#define green 3
void timer0(){
//timer0 : 20ms
TCCR0 |=(1<<CS12)|(1<<CS10); //(1<<CS12)|(0<<CS11)|(1<<CS10);prescaler = 1024
TCNT0= 98; //khoi tao TCNT1=255-(thoi_gian/((1/F_crystal)*prescaler))=255 - 156.25 ; thoi_gian(s)
TIMSK |=(1<<TOIE0); //cho phep ngat khi tran T/C0
sei(); //set bit I (SREG) gloabal interrupt
}
//phuc vu ngat tran T/C1
ISR (TIMER0_OVF_vect ){
TCNT0=98; //gan lai TCNT1 sau khi tran
A_upper_single(blue); //hien thi chu A mau xanh
}
Hàm font chữ:
#include <avr/io.h>
#define F_CPU 8000000UL
#include <avr/interrupt.h>
#include <util/delay.h>
#define sbi(port , bit) (port)|=(1<<(bit)) //set bit -> 1
#define cbi(port, bit) (port) &= ~(1 << (bit)) //clear bit -> 0
#define bit_is_set(sfr,bit) (_SFR_BYTE(sfr) & _BV(bit)) //hoi bit 1
#define bit_is_clear(sfr,bit) (!(_SFR_BYTE(sfr) & _BV(bit))) //hoi bit 0
#define blue 1
#define red 2
#define green 3
int hang[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
int A[]={0xff,0x03,0x01,0xcc,0xcc,0x01,0x03,0xff};
int a[]={0xff,0x8d,0x06,0x36,0x36,0x00,0x81,0x7f};
///// convert(hang,blue,red,green);
void A_upper_single(int x){ //chu A hoa 1 mau
int i;
for(i=0;i<8;i++){
if(x == blue){
convert(hang[i],A[i],0xff,0xff);
_delay_ms(1);
}
if(x == red){
convert(hang[i],0xff,A[i],0xff);
_delay_ms(1);
}
if(x == green){
convert(hang[i],0xff,0xff,A[i]);
_delay_ms(1);
}
}
}
void a_lower_single(int x){ //chu a thuong 1 mau
int i;
for(i=0;i<8;i++){
if(x == blue){
convert(hang[i],a[i],0xff,0xff);
_delay_ms(1);
}
if(x == red){
convert(hang[i],0xff,a[i],0xff);
_delay_ms(1);
}
if(x == green){
convert(hang[i],0xff,0xff,a[i]);
_delay_ms(1);
}
}
}
/////////////////////////////////////////////////////////////////////
Comment