Các bạn xem giúp mình sao cái phần dịch led của mình chỉ dịch trái được chữ đầu tiên còn mấy chữ sau thì nó ko dịch được.thank
Code:
#include <mega16.h>
#include <spi.h>
#include<delay.h>
#define LATCH PORTB.3
unsigned char machu[8][4]={
{0 ,0, 0,0},
{113,199,62,249},
{138,40,160,33},
{10 ,40,160,33},
{18 ,40,188,33},
{34 ,40,139,33},
{66 ,40,139,33},
{4 ,199,60,33}
};
unsigned char r=0;
void latch_data(void )
{
LATCH=1;
LATCH=0;
}
unsigned char bit7(unsigned char byte)
{
unsigned char byte1;
byte1=byte;
byte1&=0x80;
return byte1;
}
void chay_trai()
{
unsigned char i,j,temp;
for(i=0;i<8;i++)
{
temp = machu[i][0];
for(j=0;j<3;j++)
{
machu[i][j]<<= 1;
machu[i][j]|= bit7(machu[i][j+1]);
}
machu[i][3] <<= 1;
machu[i][3]|= bit7(temp) ;
}
}
void main(void)
{
unsigned char i;
PORTA=0x00;
DDRA=0x00;
PORTB=0x00;
DDRB=0xFF;
PORTC=0x00;
DDRC=0xff;
PORTD=0x00;
DDRD=0xFF;
TCCR1A=0x00;
TCCR1B=0x02;
TCNT1H=0xFF;
TCNT1L=0x82;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x04;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 4000.000 kHz
// SPI Clock Phase: Cycle Start
// SPI Clock Polarity: High
// SPI Data Order: MSB First
SPCR=0x5C;
SPSR=0x00;
// Global enable interrupts
//#asm("sei")
while (1)
{
chay_trai();
for(i=1;i<=200;i++)
{
for(r=0;r<8;r++)
{
spi(machu[r][0]);
latch_data();
PORTC=r+8;
delay_ms(1);
}
}
}
}

Comment