BASIC4MCU | 질문게시판 | ATmega 128 질문
페이지 정보
작성자 master 작성일2018-12-10 08:52 조회3,724회 댓글0건본문
1번 )후방감지센서 ,lcd, 부저 3개사용하는 코드인데 lcd 부분을 제거하고 싶습니다.
2번) 추가로 거리마다 부저가 울릴때 led를 키고 싶다면 어느부분에 명령어를 입력해야합니까?
1번이 급하니 빠른답변 부탁드립니다. (내공500)
1번)------------------------------------------------------------------------
#include <avr/io.h>
#include <avr/interrupt.h>#define PE4_Clear (PORTE &= 0xEF)
#define PE4_Set (PORTE |= 0x10)
#define PE4_IN (DDRE &= 0xEF)
#define PE4_OUT (DDRE |= 0x10)#define LCD_RS_1 (PORTG |= 0x01)
#define LCD_RS_0 (PORTG &= 0xFE)#define LCD_RW_1 (PORTG |= 0x02)
#define LCD_RW_0 (PORTG &= 0xFD)#define EN_1 (PORTG |= 0x04)
#define EN_0 (PORTG &= 0xFB)#define TEMPERATURE 25
unsigned short tick=0, pulse_check=0, pulse_end=0;
void us_delay(unsigned int us_time)
{
unsigned int i;for(i=0; i<us_time; i++) // 4 cycle +
{
asm("PUSH R0"); // 2 cycle +
asm("POP R0"); // 2 cycle +
asm("PUSH R0"); // 2 cycle +
asm("POP R0"); // 2 cycle + =12 cycle for 0592MHZ
asm("PUSH R0"); // 2 cycle +
asm("POP R0"); // 2 cycle = 16 cycle = 1us for 16MHz
}
}void ms_delay(unsigned int ms_time)
{
unsigned int i;for(i=0; i<ms_time;i++)
us_delay(1000);
}//===================================
// LCD display
void E_Pulse(void)
{
EN_1;us_delay(100);
EN_0;
}void LCD_init(void)
{
ms_delay(40);PORTC = 0x38; // Function Set
E_Pulse();
us_delay(40);PORTC = 0x0c; // DisPlay ON/OFF Control
us_delay(40);
E_Pulse();
PORTC = 0x01; // Display Clear
ms_delay(2);
E_Pulse();PORTC = 0x06; // Entry Mode Set
E_Pulse();
}void Write_Char(unsigned char buf)
{
LCD_RS_1;
LCD_RW_0;
PORTC=buf;
E_Pulse();
}
void LCD_cmd(unsigned char cmd)
{
LCD_RS_0;
LCD_RW_0;
PORTC=cmd;
E_Pulse();
}void LCD_Disp(char x,char y)
{
LCD_RS_0;
LCD_RW_0;if(y==0) PORTC = x + 0x80;
else if(y==1) PORTC = x + 0xc0;
E_Pulse();
}void LCD_Write(char x, char y,char *str)
{
LCD_Disp(x,y);
while(*str)
Write_Char(*str++);
}
//========================================
// ultra sound
void init_EXT_INT(void)
{
EIMSK |= (1<<INT4); // INT4 Interrupt Set
EICRB |= (1<<ISC41)|(1<<ISC40); // INT4 Rising Edge / Interrupt Set
EIFR = 1<<INT4; // Interrupt Flag
}void init_TIMER_COMPA(void)
{
TCCR0 |= (1<<WGM01); // CTC Mode
TCCR0 |= (1<<CS01); // clk/8 Prescaler
TCNT0 = 0;
OCR0 = 19; // 1cycle -> 20us = 1/(16Mhz/(2*8*(19+1)))
TIMSK = (1<<OCIE0); // T/C0 Output Compare Match Interrup Enable
TIFR = 0x00;
}SIGNAL(TIMER0_COMP_vect)
{
tick++; // 20usec 단위 업 카운트
}SIGNAL(INT4_vect)
{
unsigned short pulse_tick;pulse_tick = tick;
if(EICRB & 0x03) // 상승에지 인가?
{
EICRB &= 0x00; // INT4 low state set
tick = 0; // tick clear
}
else
{
EICRB |= (1<<ISC41)|(1<<ISC40); // INT4 Rising Edge /Interrupt Set
pulse_end = pulse_tick; // echo pulse count
}
}//======================================
// port init
void initPort(void)
{
DDRB = 0xff;
DDRC = 0xff;
DDRG = 0xff;
DDRE = 0xef;
PORTE = 0x10;
}void buzzer(unsigned int mdistance){
if(mdistance>=2000){
PORTB = 0xff;
}
else if(mdistance>=1750){
PORTB = 0xff;
ms_delay(1000);
PORTB = 0x00;
}
else if(mdistance>=1500){
PORTB = 0xff;
ms_delay(800);
PORTB = 0x00;
}
else if(mdistance>=1250){
PORTB = 0xff;
ms_delay(600);
PORTB = 0x00;
}
else if(mdistance>=1000){
PORTB = 0xff;
ms_delay(400);
PORTB = 0x00;
}
else if(mdistance>=750){
PORTB = 0xff;
ms_delay(200);
PORTB = 0x00;
}
else if(mdistance>=500){
PORTB = 0xff;
ms_delay(100);
PORTB = 0x00;
}
else if(mdistance>=250){
PORTB = 0xff;
ms_delay(10);
PORTB = 0x00;
}
else{
PORTB = 0x00;
}}
int main(void)
{
unsigned char thousand, hundred, ten, one;
float distance;initPort();
LCD_init();init_EXT_INT();
init_TIMER_COMPA();LCD_cmd(0x01);
ms_delay(50);LCD_Write(0, 0, "ultra sound");
LCD_Write(0, 1, "Distance:");
LCD_Write(13, 1, "mm");
ms_delay(50);while(1)
{
cli(); // interrupt clear
PE4_Clear;
PE4_OUT; // PE4 pin is output
us_delay(500);PE4_Set; // output set during 5us
us_delay(5);PE4_Clear; // output clear
us_delay(100);PE4_IN; // PE4 pin is input
us_delay(100);sei(); // interrupt enable
/* distance = velocity * time */
distance = (331.5+(0.6*TEMPERATURE))*(pulse_end*0.00001/2)*1000;/* distance digit display */
buzzer(distance);
thousand = distance/1000;
distance = distance - (thousand * 1000);hundred = distance /100;
distance = distance - (hundred * 100);ten = distance/10;
distance = distance - (ten * 10);one = distance;
LCD_Disp(9, 1);
Write_Char(thousand+48); // 1000 자리 출력
LCD_Disp(10, 1);
Write_Char(hundred+48); // 100 자리 출력
LCD_Disp(11, 1);
Write_Char(ten+48); // 10 자리 출력
LCD_Disp(12, 1);
Write_Char(one+48); // 1 자리 출력ms_delay(50);
}
return 0;
}
2번)------------------------------------------------------------------------
#include <avr/io.h>
/* ====================================== LED 제어를 위한 정의 ======================================== */
#define DDR_LED DDRD
#define PORT_LED PORTD/* ======================================= Main 함수 구현 ============================================ */
int main(void)
{
int i, j; //- Dummy for문을 위한 변수 선언
DDR_LED = 0xF0; //- DDRD의 4, 5, 6, 7번 Bit를 1로 Set (출력 기능으로 설정)
PORT_LED = 0xF0; //- PORTD의 모든 Bit를 Off로 초기화//- 반복문을 통해 LED를 순서대로 제어하기
while(1)
{
PORT_LED = 0xE0 & ~(1<<4); //- LED PortD 4번 핀 on
for(i=0; i<1000; i++) //- Delay를 위한 Dummy for문
for(j=0; j<1000; j++);PORT_LED = 0xD0 & ~(1<<5); //- LED PortD 5번 핀 on
for(i=0; i<1000; i++) //- Delay를 위한 Dummy for문
for(j=0; j<1000; j++);PORT_LED = 0xB0 & ~(1<<6); //- LED PortD 6번 핀 on
for(i=0; i<1000; i++) //- Delay를 위한 Dummy for문
for(j=0; j<1000; j++);PORT_LED = 0x70 & ~(1<<7); //- LED PortD 7번 핀 on
for(i=0; i<1000; i++) //- Delay를 위한 Dummy for문
for(j=0; j<1000; j++);
}
return 0;
}
//
// DateTime : 2018-12-06 오후 5:34:02// by Ok-Hyun Park//#include <avr/io.h>#include <avr/interrupt.h>//#define PE4_Clear (PORTE&=0xEF)#define PE4_Set (PORTE|=0x10)#define PE4_IN (DDRE&=0xEF)#define PE4_OUT (DDRE|=0x10)//#define DDR_LED DDRD#define PORT_LED PORTD//#define TEMPERATURE 25//unsigned short tick=0,pulse_check=0,pulse_end=0;//void us_delay(unsigned int us_time){unsigned int i;for(i=0; i<us_time; i++){ // 4 cycle+asm("PUSH R0"); // 2 cycle+asm("POP R0"); // 2 cycle+asm("PUSH R0"); // 2 cycle+asm("POP R0"); // 2 cycle+=12 cycle for 0592MHZasm("PUSH R0"); // 2 cycle+asm("POP R0"); // 2 cycle=16 cycle=1us for 16MHz}}//void ms_delay(unsigned int ms_time){unsigned int i;for(i=0; i<ms_time; i++)us_delay(1000);}//===================================//========================================// ultra soundvoid init_EXT_INT(void){EIMSK|=(1<<INT4); // INT4 Interrupt SetEICRB|=(1<<ISC41)|(1<<ISC40); // INT4 Rising Edge/Interrupt SetEIFR=1<<INT4; // Interrupt Flag}//void init_TIMER_COMPA(void){TCCR0|=(1<<WGM01); // CTC ModeTCCR0|=(1<<CS01); // clk/8 PrescalerTCNT0=0;OCR0=19; // 1cycle->20us=1/(16Mhz/(2*8*(19+1)))TIMSK=(1<<OCIE0); // T/C0 Output Compare Match Interrup EnableTIFR=0xFF;}//SIGNAL(TIMER0_COMP_vect){tick++; // 20usec 단위 업 카운트}//SIGNAL(INT4_vect){unsigned short pulse_tick;pulse_tick=tick;if(EICRB&0x03){ EICRB&=0x00; tick=0; } // 상승에지else{ pulse_end=pulse_tick; EICRB|=(1<<ISC41)|(1<<ISC40); }}//======================================// port initvoid initPort(void){DDRB=0xff;DDRE=0xef;PORTE=0x10;}//void buzzer(unsigned int mdistance){if (mdistance>=2000){ PORT_LED=0x70; PORTB=0xff; }else if(mdistance>=1750){ PORT_LED=0x70; PORTB=0xff; ms_delay(1000); PORTB=0; }else if(mdistance>=1500){ PORT_LED=0x70; PORTB=0xff; ms_delay( 800); PORTB=0; }else if(mdistance>=1250){ PORT_LED=0xB0; PORTB=0xff; ms_delay( 600); PORTB=0; }else if(mdistance>=1000){ PORT_LED=0xB0; PORTB=0xff; ms_delay( 400); PORTB=0; }else if(mdistance>= 750){ PORT_LED=0xD0; PORTB=0xff; ms_delay( 200); PORTB=0; }else if(mdistance>= 500){ PORT_LED=0xD0; PORTB=0xff; ms_delay( 100); PORTB=0; }else if(mdistance>= 250){ PORT_LED=0x00; PORTB=0xff; ms_delay( 10); PORTB=0; }else { PORT_LED=0x00; PORTB=0x00; }}//int main(void){float distance;initPort();DDR_LED=0xF0; //-DDRD의 4,5,6,7번 Bit를 1로 Set(출력 기능으로 설정)PORT_LED=0xF0; //-PORTD의 모든 Bit를 Off로 초기화init_EXT_INT();init_TIMER_COMPA();while(1){cli();// interrupt clearPE4_Clear; PE4_OUT; us_delay(500); // PE4 pin is outputPE4_Set; us_delay(5); PE4_Clear; // PE4 pin is setus_delay(100); PE4_IN; us_delay(100); // PE4 pin is inputsei();// interrupt enable/*distance=velocity*time*/distance=(331.5+(0.6*TEMPERATURE))*(pulse_end*0.00001/2)*1000;/*distance digit display*/buzzer(distance);ms_delay(50);}return 0;}
댓글 0
조회수 3,724등록된 댓글이 없습니다.