티스토리 뷰

카테고리 없음

텀프소스

대싕:) 2018. 11. 9. 23:41

#include <mega128.h>

#include <delay.h>

#include <stdio.h>

#include "lcd.h" 


/*초음파 센서 정의부*/

#define TRI_SIG PORTB.4  

#define ECHO_1 PIND.0

#define ECHO_2 PIND.1

#define ECHO_3 PIND.2

/*초음파 센서 정의부*/


unsigned int dist;

int flag = 1;

int controll = 0;


// 초음파 거리 저장

unsigned short echoL1, echoL2, echoL3;


/* 서보 PWM */

interrupt[TIM3_COMPA] void timer3_compare(void) {

PORTB.0 = 0;

}

interrupt[TIM3_OVF] void timer3_overflow(void) {

PORTB.0 = 1;

}

/* 서보 PWM */


/* DC MOTOR PWM */

interrupt[TIM0_COMP] void timer0_compare(void) {

PORTC.1 = 0;    PORTC.2 = 0;

}

interrupt[TIM0_OVF] void timer0_overflow(void) {

PORTC.1 = 1;    PORTC.2 = 1;

}

/* DC MOTOR PWM */


// 초음파 센서 거리

void printEcho(unsigned char line, short m_echo) {

unsigned char Decimal[5];

Decimal[4] = '0' + m_echo / 10000;        m_echo = m_echo % 10000;

Decimal[3] = '0' + m_echo / 1000;        m_echo = m_echo % 1000;

Decimal[2] = '0' + m_echo / 100;        m_echo = m_echo % 100;

Decimal[1] = '0' + m_echo / 10;            m_echo = m_echo % 10;

Decimal[0] = '0' + m_echo / 1;


if (line == '1') {

LCD_pos(0, 6);        LCD_Data(Decimal[4]);

LCD_pos(0, 7);        LCD_Data(Decimal[3]);

LCD_pos(0, 8);        LCD_Data(Decimal[2]);

LCD_pos(0, 9);        LCD_Data(Decimal[1]);

LCD_pos(0, 10);        LCD_Data(Decimal[0]);

}

else {

LCD_pos(0, 7);        LCD_Data(Decimal[4]);

LCD_pos(0, 8);        LCD_Data(Decimal[3]);

LCD_pos(0, 9);        LCD_Data(Decimal[2]);

LCD_pos(0, 10);        LCD_Data(Decimal[1]);

LCD_pos(0, 11);        LCD_Data(Decimal[0]);

}

}


void getEcho1(void)

{

while (!ECHO_1);//high가 될때

TCNT1 = 0; TCCR1B = 2; //카운터시작 , 0.5분주

while (ECHO_1);//low가 될때

TCCR1B = 8; //카운터 정지 TCCR1B = 0도 가능

// echoL1 = TCNT1 / 116;//CM변경

}


void getEcho2(void)

{

while (!ECHO_2);//high가 될때

TCNT1 = 0; TCCR1B = 2; //카운터시작 , 0.5분주

while (ECHO_2);//low가 될때

TCCR1B = 8; //카운터 정지 TCCR1B = 0도 가능

// echoL2 = TCNT1 / 116;//CM변경  =

}



void getEcho3(void)

{

while (!ECHO_3);//high가 될때

TCNT1 = 0; TCCR1B = 2; //카운터시작 , 0.5분주

while (ECHO_3);//low가 될때

TCCR1B = 8; //카운터 정지 TCCR1B = 0도 가능

// echoL3 = TCNT1 / 116;//CM변경

}


void timer3_Init(void) {

TCCR3A = 1 << WGM31;

TCCR3B = 1 << WGM32 | 1 << CS31 | 1 << CS30 | 1 << WGM33;

ICR3H = 0x13;

ICR3L = 0x87;

ETIMSK = 1 << TOIE3 | 1 << OCIE3A;

}


void timer0_Init(void) {

TCCR0 = 1 << WGM01 | 1 << WGM00 | 1 << CS01;

}

void TIMSK_setting(void) {

TIMSK = 1 << OCIE0 | 1 << TOIE0;

}

void Port_Init(void) {

DDRD = 0x01;

DDRA = 0xff;

DDRG = 0xff;

DDRC = 0xff;

DDRE = 0x00;

DDRB = 0xff;

}


void init_uart0(void)

{

//시리얼 통신을 통한 디버깅용

UCSR0A = 0b00000000;

UCSR0B = 0b10011000;

UCSR0C = 0b10000110;

UBRR0H = 0;

UBRR0L = 103;


printf("STARTING SYSTEM ... \n\r");

}

void test_SONIC()

{

PORTB.4 = 1; PORTB.5 = 1; PORTB.6 = 1;

delay_ms(100);

PORTB.4 = 0; PORTB.5 = 0; PORTB.6 = 0;


}



void main(void)

{

timer3_Init();

    Port_Init();

    timer0_Init();

    LCD_Init();

    init_uart0();

    TIMSK_setting();


    SREG |= 0x80;


    PORTC.3 = 1;

    PORTC.4 = 0;

    PORTC.5 = 1;

    PORTC.6 = 0;


    OCR0 = 255;

    //   TCCR1A=0; TCCR1B = 8;




    LCD_pos(0, 0);

    LCD_STR("Ech1:");


    LCD_pos(1, 0);

    LCD_STR("Echo2:");


    LCD_pos(1, 0);

    LCD_STR("Echo3:");



    while (1)

    {

        TRI_SIG = 1;

        delay_us(10);

        TRI_SIG = 0;

        getEcho1();

        getEcho2();

        getEcho3();

        delay_ms(10);


        printEcho('1', echoL1);

        printEcho('2', echoL2);

        //printEcho('2', echoL3);




      /*

        printf("distance front     : %d \n" ,range);


        printf("distance left      : %d \n" , range1);


        printf("distance right     : %d \n" , range2);

      */


        if (echoL2 < 25 && echoL3 > 25) { OCR3AH = 0x01; OCR3AL = 0xF4; }

        else if (echoL2 > 25 && echoL3 < 25) { OCR3AH = 0x00; OCR3AL = 0xFA; }

        else { OCR3AH = 0x01; OCR3AL = 0x77; }




    }

}

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
글 보관함