BASIC4MCU | 질문게시판 | ATmega128 / HC-05블루투스 시리얼 모듈을 사용한 블루투스 통신 질문 있습니다..<<2>>
페이지 정보
작성자 범용직렬버스장치 작성일2021-12-04 21:24 조회1,103회 댓글1건본문
안녕하십니까, 이전에 주신 답변으로 큰 도움 되었습니다.
이제 도 레 미 파 솔 라 시 높은도 버튼을
각각 1~8이 아닌, 초기에 블루투스 통신으로 스마트폰에서 지정해보려는 목적으로 아래의 코드를 작성해보았지만..
소리가 나지 않습니다.
asdfghjk를 처음 입력했다면, a를 입력하면 '도', s를 입력하면 '래' 음이 나오도록 하는것이 제 목적이었습니다.
오래 고민하다 한 번 엎어보기도 했지만 왜 작동하지 않는지.. 모르겠습니다..
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#include <avr/io.h>
#include <avr/interrupt.h>
//
#define F_CPU 16000000UL
#define __DELAY_BACKWARD_COMPATIBLE__ // Atmel Studio 7을 사용하는 경우
#include <util/delay.h>
//
#define NULL 0
//
char f_table[]={0,17,43,66,77,97,114,117,137}; // 묵음과 음계에 해당하는 TCNT0값
//
volatile unsigned char rx,tone;
//
char Setting[]="Set the button you want";
void init_uart1() // UART1 초기화 함수
{
UCSR1B = 0x18; // 송신 Transmit(TX), Receive(RX) Enable
UCSR1C = 0x06; // UART Mode, 8Bit Data, No Parity, 1 Stop Bit
UBRR1H = 0; // Baudrate 세팅
UBRR1L = 8; // 16Mhz, 115200 baud (주의 :초기 셋팅 값 확인)
}
void putchar1(char c)
{
while(!(UCSR1A &(1<<UDRE1)));
UDR1=c;
}
char getchar1(void )
{
while(!(UCSR1A &(1<<RXC1)));
return(UDR1);
}
void puts1(char *ptr)
{
while(1)
{
if(*ptr != NULL)
putchar1(*ptr++);
else
return;
}
}
ISR(TIMER0_OVF_vect)
{
if(tone)
{
TCNT0=f_table[tone];
PORTE^=0x10;
} //버저 토글
}
//
int main()
{
char c;
DDRE=0x10;
TCCR0=0x03; TIMSK=0x01; // 오버플로우 인터럽트
UCSR1B=0x18; UBRR1L=8; // 16Mhz,115200 baud(주의:초기 셋팅 값 확인)
sei();
char ch=0;
int i =0;
char str[80]; //임의 배열 선언 후
char prompt[]="Set the Button you want!"; //문자열 저장
char *ptr; //포인터 변수
init_uart1();
ptr= &prompt[0]; //ptr포인터가 prompt[]어레이의 처음을 포인트함
puts1(ptr);
putchar1('\n');
while(1)
{
c=getchar1();
putchar1(c);
while(ch !='\n')
{
ch=getchar1(); //while문 안의 getchar0를 통해
if(ch!='\n') //ch변수에 문자를 입력 받은 후 배열에 각 주소를 저장하도록 함.
str[i]=ch;
i++;
}
if(ch=='\n')
{
if(c==str[1])
{
TCNT0=f_table[1];
}
else if(c==str[2])
{
TCNT0=f_table[2];
}
else if(c==str[3])
{
TCNT0=f_table[3];
}
else if(c==str[4])
{
TCNT0=f_table[4];
}
else if(c==str[5])
{
TCNT0=f_table[5];
}
else if(c==str[6])
{
TCNT0=f_table[6];
}
else if(c==str[7])
{
TCNT0=f_table[7];
}
else if(c==str[8])
{
TCNT0=f_table[8];
}
_delay_ms(500);
tone=0; // 500ms 후 OFF
}
}
}
댓글 1
조회수 1,103master님의 댓글
master 작성일
#include <avr/io.h>
#include <avr/interrupt.h>
//
#define F_CPU 16000000UL
#define __DELAY_BACKWARD_COMPATIBLE__ // Atmel Studio 7을 사용하는 경우
#include <util/delay.h>
//
#define NULL 0
//
char f_table[]={0,17,43,66,77,97,114,117,137}; // 묵음과 음계에 해당하는 TCNT0값
//
volatile unsigned char rx,tone;
//
char Setting[]="Set the button you want";
//
void putchar1(char c){ while(!(UCSR1A&0x20)); UDR1=c; }
char getchar1(void ){ while(!(UCSR1A&0x80)); return(UDR1); }
void puts1(char *s){ while(*s)putchar1(*s++); }
//
ISR(TIMER0_OVF_vect){
if(tone){ TCNT0=f_table[tone]; PORTE^=0x10; } //버저 토글
}
//
int main(){
char c,ch=0,str[80],prompt[]="Set the Button you want!\n";
int i=0;
DDRE=0x10;
TCCR0=0x03; TIMSK=0x01; // 오버플로우 인터럽트
UCSR1B=0x18; UBRR1L=8; // 16Mhz,115200 baud(주의:초기 셋팅 값 확인)
sei();
puts1(prompt);
while(1){
if(UCSR1A&0x80){
c=UDR1; putchar1(c);
if((c>='a')&&(c>='i')){
tone=c-'a'; TCNT0=f_table[tone]; _delay_ms(500); tone=0; // 500ms 후 OFF
}
}
}
}
'a'는 무음(OFF), 'b'부터 도~