BASIC4MCU | 질문게시판 | atmega128 fnd led 초음파 buzzer
페이지 정보
작성자 dudu 작성일2019-06-03 17:36 조회9,582회 댓글3건본문
첫번째 소스는 fnd와 led가 같이 작동하는 것이고 두번째 소스는 초음파(ulrasonic )센서를 이용해서 일정거리보다 가까워지면 buzzer가 울리는 것입니다.
이 두가지를 합치려고 하는데 두개가 동시에 작동이 안됩니다 ㅜㅜ
소스 한번만 확인해주세요! 저희가 사용하는 프로그램은 atmel studio 6.2 버전이고, atmega128을 사용하고 실습보드는 HBE-ADK-SV210입니다.
(1)FND-LED 동작소스입니다
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
void LED_control(uint8_t da);
uint8_t FND_data[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x27,0x7F, 0x6F};
int main(void)
{
uint8_t a, k;
DDRD = 0xF0;
PORTD = 0;
DDRA = 0xFF; //FND
PORTA = 0; //FND
while(1)
{
for(a=1; a<2; i++)
{
LED_control(i);
/*for(j=0; j < 100; j++)*/_delay_ms(50);
}
for(k=9; k>0; k--)
{
PORTA = FND_data[k]; //FND 9에서 0까지 줄어드는 시간
_delay_ms(1000);
}
for(a=2; a<3; i++)
{
LED_control(i);
/*for(j=0; j < 100; j++)*/_delay_ms(50);
}
for(k=9; k>0; k--)
{
PORTA = FND_data[k]; //FND 9에서 0까지 줄어드는 시간
_delay_ms(1000);
}
}
}
// LED control function
void LED_control(uint8_t da)
{
PORTD = da<<4;
}}
(2) 초음파센서를 이용해서 BUZZER울리는 소스입니다.
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
enum{IDLE, SEND, RECV};
volatile uint16_t time_count = 0, dist = 0;
volatile uint8_t Ultrasonic_flag = IDLE;
void Ultrasonic_SEND(void);
void Command_write(uint8_t command)
{
PORTG &= ~0x02; // RS LOW
PORTC = command; // command write
_delay_us(1);
PORTG |= 0x04; // E HIGH
_delay_us(1);
PORTG &= ~0x04; // E LOW
_delay_us(41);
}void Data_write(uint8_t data)
{
PORTG |= 0x02; // RS HIGH
PORTC = data; // data write
_delay_us(1);
PORTG |= 0x04; // E HIGH
_delay_us(1);
PORTG &= ~0x04; // E LOW
_delay_us(41);
}
void LCD_init(void)
{
// function set - data 8bit, 2 line, 5x7 dot
Command_write(0x38);
Command_write(0x38);
// Display ON/OFF control - display ON, Cursor OFF, Cursor blink OFF
Command_write(0x0C);
// Clear display
Command_write(0x01);
_delay_ms(2);
// Entry Mode Set - Cursor address increase, Display Shift OFF
Command_write(0x06);
}
void LCD_print(char *str)
{
uint8_t i=0;
while(str[i] !='\0')
{
Data_write(str[i++]);
}
}
void Puts_Dist(char *str,uint16_t data)
{
uint8_t i=0;
// set the cursor to column 0, line 1
Command_write(0x80);
while(str[i]!='\0')
Data_write(str[i++]);
Data_write(data/1000 + '0');
Data_write((data/100)%10 + '0');
Data_write((data/10)%10 + '0');
Data_write(data%10 + '0');
}
int main(void)
{
DDRD = 0x01; // Ultrasonic sensor setup
PORTD = 0x01;
DDRC = 0xFF; // LCD data Output
DDRG = 0x07; // LCD control signal Output
PORTG = 0x00; // RW -> LOW, RS -> LOW, E -> LOW
LCD_init();
//Timer0 init
TCCR0 = 0x02; // 1/8 prescaler
// interrupt cycle: 100/(F_CPU/prescaler) = 0.00005 (F_CPU = 16MHZ) -> 50us
TCNT0 = 0x100 - 100;
TIMSK = 0x01; // Timer 0 overflow interrupt enable
//EXT INT init
// Source : INT1(PD1)
// Interrupt generation : Rising edge
EICRA = 0x0C;
EIMSK = 0x02; // INT1 EXT interrupt enable
EIFR = 0x02;
sei();
while(1)
{
// ultrasonic send
Ultrasonic_flag = SEND;
Ultrasonic_SEND();
// waiting ultrasonic receive
while(Ultrasonic_flag != IDLE);
// distance transmission
Puts_Dist("Dist : ", dist);
_delay_ms(500);
}
}
void Ultrasonic_SEND(void)
{
// 10us Low Pulse
PORTD = 0x00; _delay_us(10);
PORTD = 0x01;
}
SIGNAL(TIMER0_OVF_vect)
{
cli();
// 50us cycle
TCNT0 = 0x100 - 100;
if(Ultrasonic_flag == RECV)
{
time_count++;
}
else
{
time_count = 0;
}
sei();
}
SIGNAL(INT1_vect)
{
cli();
if(Ultrasonic_flag == SEND)
{
// Interrupt generation :falling edge
EICRA = 0x08;
Ultrasonic_flag = RECV;
}
else if(Ultrasonic_flag == RECV)
{
// Interrupt generation : Rising edge
EICRA = 0x0C;
// ultrasonic velocity -> 0.85 cm/50us
dist = time_count * 0.85;
Ultrasonic_flag = IDLE;
if(dist <= 10)
{
DDRB = 0x80;
PORTB = 0;
while(1)
{
PORTB = 0x80;
_delay_ms(100);
PORTB = 0x00;
_delay_ms(100);
}
}
}
}
그리고 (2)번 소스에서 일정거리보다 가까워지면 울리는 BUZZER 를 일정시간 울리고 멈추게 할 수 있을까요,,ㅠㅠ delay를 넣어봐도 안돼서요ㅠㅠㅠ
부탁드립니다ㅠㅠㅠㅠ
댓글 3
조회수 9,582master님의 댓글
master 작성일
void Ultrasonic_SEND(void){
PORTD=0; _delay_us(10); PORTD=1; // 10us Low Pulse
}
무슨 센서를 사용하길래 출력을 부논리로 줄까요?
dudu님의 댓글
dudu
주신 소스로 프로그램을 돌려봤는데, str이 정의가 되어있지 않다고 오류가떠요 ㅠㅠ
어떻게 정의해야할까요,,??
master님의 댓글
master
변수 선언 정도는 해결 할 수 있어야하지 않을까요?
char str[50];