BASIC4MCU | 질문게시판 | ATMEGA JKIT 스위치 부저
페이지 정보
작성자 코딩 작성일2023-05-19 13:04 조회62회 댓글0건본문
저희가 코딩을 스위치를 눌렀을 경우에 FND가 500에서 0이 되게 한 다음 부저가 산토끼 노래로 울리도록 설정을 하였습니다. 그런데 JKIT 연결을 하고 실행을 해보니 먼저 부저가 한번 울린 다음에 스위치 눌렀을 경우에 작동을 합니다 . 이걸 처음에 부저가 안울리고 있다가 스위치 눌렀을 때 작동하도록 수정을 어떻게 할 수 있을까요??
#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 1
volatile 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 if (cur_time > 0)
{
for (cur_time; cur_time > 0; cur_time--)
{
display_fnd(cur_time);
}
}
else
{
display_fnd(0x00);
state = STOP;
if (tone != EOS)
{
tone = song[i++];
_delay_ms(500);
}
}
}
}
댓글 0
조회수 62등록된 댓글이 없습니다.