2개의 Matrix를 이용한 숫자 슬라이드(ATmega128)

전의 포스트에서 숫자를 슬라이드 하여 오른쪽에서 왼쪽으로 흘러가는 동작을 구현하도록 만들었다

이번의 코드는 하나의 도트 매트릭스가 아닌 2개의 매트릭스를 연결하여 이동하도록 구현하였다.

1) 회로도

2) 코드
#define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

int num[10][8][2]={
{{0x18,0xfe},{0x24,0xfd},{0x24,0xfb},{0x24,0xf7},{0x24,0xef},{0x24,0xdf},{0x24,0xbf},{0x18,0x7f}},
{{0x10,0xfe},{0x18,0xfd},{0x14,0xfb},{0x10,0xf7},{0x10,0xef},{0x10,0xdf},{0x10,0xbf},{0x3c,0x7f}},
{{0x00,0xfe},{0x18,0xfd},{0x24,0xfb},{0x20,0xf7},{0x10,0xef},{0x08,0xdf},{0x04,0xbf},{0x3c,0x7f}},
{{0x1c,0xfe},{0x22,0xfd},{0x20,0xfb},{0x10,0xf7},{0x10,0xef},{0x20,0xdf},{0x22,0xbf},{0x1c,0x7f}},
{{0x20,0xfe},{0x30,0xfd},{0x28,0xfb},{0x24,0xf7},{0x22,0xef},{0xfe,0xdf},{0x20,0xbf},{0x20,0x7f}},
{{0x3e,0xfe},{0x02,0xfd},{0x02,0xfb},{0x1e,0xf7},{0x20,0xef},{0x20,0xdf},{0x22,0xbf},{0x1c,0x7f}},
{{0x18,0xfe},{0x24,0xfd},{0x04,0xfb},{0x04,0xf7},{0x1c,0xef},{0x24,0xdf},{0x24,0xbf},{0x18,0x7f}},
{{0x3e,0xfe},{0x20,0xfd},{0x20,0xfb},{0x10,0xf7},{0x08,0xef},{0x08,0xdf},{0x08,0xbf},{0x08,0x7f}},
{{0x00,0xfe},{0x18,0xfd},{0x24,0xfb},{0x24,0xf7},{0x18,0xef},{0x24,0xdf},{0x24,0xbf},{0x18,0x7f}},
{{0x18,0xfe},{0x24,0xfd},{0x24,0xfb},{0x38,0xf7},{0x20,0xef},{0x20,0xdf},{0x24,0xbf},{0x18,0x7f}}
};

int count=0,flag=0,sector=0;

int pattern=0,numPattern=10;
volatile unsigned  long timer0 = 0;
long int show[8][2];
long unsigned int show2[8][2];
//int show[8][8];//완료
int buffer[8];
int select_matrix=0,next=0;

ISR(TIMER0_OVF_vect){
timer0++;//0.016마다 발생, 0.016384
//(2slide)
if(timer0 % 1 == 0){//1.04초  //18.3105로 할시 0.3초
count++;
if(count == 8){
count=0;
for(int i =0; i<8 ; i++){
show2[i][0] = show2[i][0]>>1;
show[i][0] = show[i][0]>>1;
}
flag++;

if(flag == 24){
sector++;
setDisplay(sector);
flag=0;
}
}
}
if(sector == 10){
sector=0;
setDisplay(sector);
}
}

void setDisplay(int pattern){
for(int i=0;i<8;i++){
for(int j=0;j<2;j++){
show2[i][j]=num[pattern][i][j];
show[i][j]=num[pattern][i][j];
}
}
for(int i=0;i<8;i++){
show2[i][0]=show[i][0]<<8;
show[i][0]=show[i][0]<<16;
}
}

void slideDisplay(int pattern){
for(int j=0;j<8;j++){
PORTF=show2[j][1];
PORTD=show2[j][0];
PORTE=show[j][0];
_delay_ms(1);
}
}

int main(void){
DDRE=0xff;
DDRF=0xff;
DDRD=0xff;

TCCR0 = 1<<CS02 | 1<<CS01 | 1<<CS00; // T=(1/16M) *250 * 1024분주 = 0.016초
TIMSK |= 1<<TOIE0;  // 타이머 카운터0 인터럽트 활성화
sei();//set inturrept

setDisplay(pattern);//show에 numPattern[0][][]저장

while(1){
slideDisplay(pattern);
}
}


지금의 코드는 두개의 매트릭스를 이용하지만 하나의 숫자가 완전히 사라지기까지 기다렸다가 다음의 숫자가 나오기에 수정이 필요하다.


+)수정
1) 회로도

2) 코드
#define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

int num[10][8][2]={
{{0x18,0xfe},{0x24,0xfd},{0x24,0xfb},{0x24,0xf7},{0x24,0xef},{0x24,0xdf},{0x24,0xbf},{0x18,0x7f}},
{{0x10,0xfe},{0x18,0xfd},{0x14,0xfb},{0x10,0xf7},{0x10,0xef},{0x10,0xdf},{0x10,0xbf},{0x3c,0x7f}},
{{0x00,0xfe},{0x18,0xfd},{0x24,0xfb},{0x20,0xf7},{0x10,0xef},{0x08,0xdf},{0x04,0xbf},{0x3c,0x7f}},
{{0x1c,0xfe},{0x22,0xfd},{0x20,0xfb},{0x10,0xf7},{0x10,0xef},{0x20,0xdf},{0x22,0xbf},{0x1c,0x7f}},
{{0x20,0xfe},{0x30,0xfd},{0x28,0xfb},{0x24,0xf7},{0x22,0xef},{0xfe,0xdf},{0x20,0xbf},{0x20,0x7f}},
{{0x3e,0xfe},{0x02,0xfd},{0x02,0xfb},{0x1e,0xf7},{0x20,0xef},{0x20,0xdf},{0x22,0xbf},{0x1c,0x7f}},
{{0x18,0xfe},{0x24,0xfd},{0x04,0xfb},{0x04,0xf7},{0x1c,0xef},{0x24,0xdf},{0x24,0xbf},{0x18,0x7f}},
{{0x3e,0xfe},{0x20,0xfd},{0x20,0xfb},{0x10,0xf7},{0x08,0xef},{0x08,0xdf},{0x08,0xbf},{0x08,0x7f}},
{{0x00,0xfe},{0x18,0xfd},{0x24,0xfb},{0x24,0xf7},{0x18,0xef},{0x24,0xdf},{0x24,0xbf},{0x18,0x7f}},
{{0x18,0xfe},{0x24,0xfd},{0x24,0xfb},{0x38,0xf7},{0x20,0xef},{0x20,0xdf},{0x24,0xbf},{0x18,0x7f}}
};
int empty[8][2]={0,};

int count=0,flag=0,sector=0,flag2=0;

int pattern=0;
volatile unsigned  long timer0 = 0;
long unsigned int show[8][2];
long unsigned int show2[8][2];
//int show[8][8];//완료

ISR(TIMER0_OVF_vect){
timer0++;//0.016마다 발생, 0.016384
//(2slide)
if(timer0 % 1 == 0){//1.04초  //18.3105로 할시 0.3초
count++;
if(count == 8){
count=0;
for(int i =0; i<8 ; i++){
show2[i][0] = show2[i][0]>>1;
show[i][0] = show[i][0]>>1;
}
flag++;

if(flag == 16){
setDisplay(sector);
sector++;
setDisplay2(sector);
flag=0;
}
}
}
if(sector == 10){
sector=0;
setDisplay2(sector);
}

}

void setDisplay(int pattern){
for(int i=0;i<8;i++){
for(int j=0;j<2;j++){
show[i][j]=num[pattern][i][j];
}
}
for(int i=0;i<8;i++){
show[i][0]=show[i][0]<<8;
}
}
void setDisplay2(int pattern){
for(int i=0;i<8;i++){
for(int j=0;j<2;j++){
show2[i][j]=num[pattern][i][j];
}
}
for(int i=0;i<8;i++){
show2[i][0]=show2[i][0]<<8;
}
}

void slideDisplay(){
for(int j=0;j<8;j++){
PORTF=show2[j][1];
PORTD=show2[j][0];
PORTE=show[j][0];
_delay_ms(1);
}
}

int main(void){
DDRE=0xff;
DDRD=0xff;
DDRF=0xff;

TCCR0 = 1<<CS02 | 1<<CS01 | 1<<CS00; // T=(1/16M) *250 * 1024분주 = 0.016초
TIMSK |= 1<<TOIE0;  // 타이머 카운터0 인터럽트 활성화
sei();//set inturrept

setDisplay2(pattern);//show에 num[0][][]저장
setDisplay(empty);

while(1){
slideDisplay();
}
}

숫자가 자연스럽게 흐르나 매끄럽게 이어지지 않기에 수정이 필요한 상황이다.

댓글

이 블로그의 인기 게시물

python 에서 resize 이벤트 만들기

PLC와 아두이노 그리고 온도 센서를 이용하여 램프 점등

C언어로 로또 프로그램 만들기