BASIC4MCU | 질문게시판 | atmega128 ADC 입력에 대한 코인형 진동모터 출력 질문입니다.
페이지 정보
작성자 집에가고싶어요 작성일2022-12-02 07:01 조회1,345회 댓글0건본문
atmega128로 청각장애인 전용 넥밴드를 구현하려고 합니다.
마이크를 좌,우,후방에 각각 하나씩 총 3개를 연결하여 ADC 변환하여 마이크 당 하나의 코인형 진동모터와 연결하였고,
마이크 입력 중 가장 큰 값이 입력된 마이크의 진동모터에만 진동이 잠깐 울리도록 하려고 코드를 짜봤습니다.
그런데 분명 소리가 큰 쪽에서 진동이 설정한 시간만큼 울리는 것 같은데, 가끔씩 소리 입력을 주지 않아도 진동이 계속 유지될 때가 있습니다.
손으로 잡아서 든다던가, 어떠한 물체에 진동모터 전선이 걸린다던가 할 때 유독 더 이런 오류가 자주 발생하는 것 같습니다만... 정확한 문제를 찾지 못하였습니다.
오실로스코프로 if문이 잘 작동되는지 확인하고 싶어서 PORTE를 추가하여 LED와 연결해봤는데, 소리 입력시 LED가 순차적으로 점등되어야 하는데
소리입력이나 진동모터와 관련 없이 셋 중 하나의 LED로 계속 유지가 됩니다. 전압 또한 잠깐 진동모터가 구동될때만 PORTE에 가해져야하는데 계속 5V 전압이
유지된 채로 있습니다..
제 생각에는 제가 짠 delayCnt 에 대한 루프가 제대로 안 돌아가는 것으로 생각하고 있는데 혹시 잘못된 부분이 있는지 여쭤보고 싶습니다.
진동모터에 저항은 따로 달아주지 않았고, 출력될 때 약 3.3v가 진동모터에 걸리는 것을 확인했습니다.
코드 한 번 확인해주시면 정말 감사하겠습니다..!!
#define F_CPU 12000000UL
#include
#include
#include
#include
int cnt=0;
int delayCnt=0;
int adcCnt=0;
int testCnt=0;
int max;
int normal;
int danger;
int soundback, soundleft, soundright;
ISR(ADC_vect)
{
adcCnt++;
if(cnt==1)
{
soundback=ADCW;
}
if(cnt==2)
{
soundleft=ADCW;
}
if(cnt==3)
{
soundright=ADCW;
cnt=0;
}
}
ISR(TIMER0_OVF_vect){
if((PINA&0x01)==0)
{
PORTC=0x01;
normal=300;
danger=500;
}
if((PINA&0x02)==0)
{
PORTC=0x02;
normal=400;
danger=900;
}
if((soundback>=500)&&(soundback<700))
{
PORTE=0x01;
}
if((soundback>=700)&&(soundback<1000))
{
PORTE=0x02;
}
if((soundback>=1000)&&(soundback<=1023))
{
PORTE=0x04;
}
else if(soundback<500)
{
PORTE=0x00;
}
TCNT0=68;
cnt++;
if(cnt==1){
ADMUX=0x40;
}
else if(cnt==2){
ADMUX=0x41;
}
else if(cnt==3){
ADMUX=0x42;
}
ADCSRA|=(1<); >
if(adcCnt>=3){
delayCnt++; // 3ms마다 증가
if((soundback>soundleft)&&(soundback>soundright)){
if((soundback>normal)&&(soundback<=danger)){
if ((delayCnt>0)&&(delayCnt<=100))
{
PORTE=0x01;
PORTB=0x01;
}
else if((delayCnt>100)&&(delayCnt<=150))
{
PORTB=0x00;
}
else if(delayCnt>=150)
{
delayCnt=0;
}
}
}
if((soundleft>soundback)&&(soundleft>soundright)){
if((soundleft>normal)&&(soundleft<=danger)){
if ((delayCnt>0)&&(delayCnt<=50))
{
PORTB=0x02;
}
else if((delayCnt>50)&&(delayCnt<100))
{
PORTB=0x00;
}
else if(delayCnt>=100)
{
delayCnt=0;
}
}
}
if((soundright>soundback)&&(soundright>soundleft)){
if((soundright>normal)&&(soundright<=danger)){
if ((delayCnt>0)&&(delayCnt<=50))
{
PORTB=0x04;
}
else if((delayCnt>50)&&(delayCnt<100))
{
PORTB=0x00;
}
else if(delayCnt>=100)
{
delayCnt=0;
}
}
}
adcCnt=0;
}
}
int main(void)
{
DDRA=0x00; // LED 스위치 입력 (A0, A1)
DDRC=0XFF; // LED 출력 (C0, C1)
DDRB = 0xff; // vibration motor output진동모터 출력
DDRD = 0xff;
PORTD = 0x00;
DDRE = 0xff;
PORTC = 0x00; // 초기설정 LED 소등
PORTB = 0x00; // 초기설정 진동모터 동작안함
PORTE = 0x00;
normal=0;
danger=0;
cli();
TCCR0=0x04;
//TCCR2=0x03; // 64 prescale
TCNT0=68; // 68 --> 255 --> overflow
//TCNT2=68;
//TIMSK=0x41; // timer0,2 overflow enable
TIMSK=0x01; // timer0 overflow enable
ADCSRA = (1 << ADEN) | (1 << ADSC) | (0 << ADFR) | (1<)>
sei();
while (1)
{
}
}
댓글 0
조회수 1,345등록된 댓글이 없습니다.