BASIC4MCU | 질문게시판 | ATMEGA128a 타이머와 부저
페이지 정보
작성자 동스타 작성일2019-05-23 18:20 조회5,168회 댓글0건본문
저희가 코드를 이렇게 작성하여 JKIT-128-1에 실행을 시키면 저희의 의도로는 fnd에 500이 뜨며 500ms 후 0이 되면 이후에 부저가 산토끼로 울려야 합니다.
그런데 스위치를 누르자마자 부저가 울리며 fnd가 0이 되면 산토끼가 울립니다. 코드를 고민을 해보았지만 어떻게 고쳐야 할지 모르겠습니다...
빌드는 틀린 곳이 없었습니다.
#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 16000000UL
#include <util/delay.h>#define DO 0
#define RE 1
#define MI 2
#define FA 3
#define SOL 4
#define RA 5
#define SI 6
#define DDO 7
#define EOS -1
#define STOP 0
#define GO 1
#define ON 0
#define OFF 1volatile int tone;
volatile int cur_time = 500;
volatile int stop_time = 500;
volatile int state = STOP;
unsigned char digit[10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7c, 0x07, 0x7f, 0x67};
unsigned char fnd_sel[4] = {0x01, 0x02, 0x04, 0x08};
char f_table[8] = {17, 43, 66, 77, 97, 114, 117, 137};
int song[] = {SOL,MI,MI,SOL,MI,DO,RE,MI,RE,DO,MI,SOL,DDO,SOL,DDO,SOL,DDO,SOL,MI,SOL,RE,FA,MI,RE,DO,EOS};
ISR(INT4_vect)
{
if(state == STOP)
state = GO;
else
{
state = STOP;
stop_time = cur_time;
}}
ISR(TIMER0_OVF_vect)
{
if (state == ON)
{
PORTB =0x10;
state = OFF;
}
else
{
PORTB = 0x00;
state = ON;
}
TCNT0 = f_table[tone];
}void init()
{
DDRC = 0xff;
DDRG = 0x0f;
DDRE = 0xcf;
PORTC = digit[0];
PORTG = 0x0f;
EICRB = 0x0a;
EIMSK = 0x30;
sei();
}void display_fnd(int count)
{
int i, fnd[4];
fnd[3] = (count/1000)%10;
fnd[2] = (count/100)%10;
fnd[1] = (count/10)%10;
fnd[0] = count%10;
for (i=0;i<4;i++)
{
PORTC = digit[fnd[i]];
PORTG = fnd_sel[i];
if (i%2) _delay_ms(2);
else _delay_ms(3);
}
}int main()
{
init();
int i=0;
DDRB = 0x10;
TCCR0 = 0x03;
TIMSK = 0x01;
TCNT0 = f_table[song[i]];
while (1)
{
if(state == STOP)
{
display_fnd(stop_time);
}
else
{
for (cur_time;cur_time>0;cur_time--)
{
display_fnd(cur_time);
}
for (cur_time;cur_time<=0;)
{
display_fnd(0x00);
state = STOP;
do
{
tone = song[i++];
_delay_ms(500);
} while (tone != EOS);
}
}
}
}
댓글 0
조회수 5,168등록된 댓글이 없습니다.