학습목표    1

학습주제    1

예습내용    1

실습방법    1

        1

LED 자동차 속도와 방향표시

학습목표

4개의 녹색 LED 자동차 속도에 따라 점멸속도를 조절하고 적색 LED 방향을 표시한다.

학습주제

 

예습내용

 

실습방법

   

리모콘으로 모형 자동차제어 부분에 6개의  LED 추가하고 이를 프로그램에 추가 하였다.

 

보기1  모형자동차의 속도와 회전 표시

#include <mega128.h>

#include <delay.h> 

       

eeprom char vel;

char mode=0,m_cnt=0x01,iter=0;

 

void main(void)

{         

// A,D채널=입력

PORTA=0x00;

DDRA=0x00;

PORTD=0x00;

DDRD=0x00;

 

// B,C채널=출력

PORTB=0x00;

DDRB=0xFF;

PORTC=0x00;

DDRC=0xFF;

PORTE=0x00;

DDRE=0xFF;

 

// Global enable interrupts

#asm("sei")

 

// (1) Fast PWM Mode   ATmega128(L) Data Book P100  주기조절

// 16M(ClkTOS)/1024=15625 Hz

TCCR0 = 0x6f;

// ATmega128(L) Data Book P102   듀티비조절  초기듀티=50    

OCR0 = vel; 

 

while (1)

      {

       if(PINA.0==1) {

          switch(PIND) {

                       case 0x00 :                          // 전진

                                      PORTC=0x06;

                                      mode=1;

                                      //m_cnt=0x01;

                                      break;

                       case 0x01 :                          // 후진

                                      PORTC=0x09;

                                      mode=1;

                                      //m_cnt=0x01;

                                      break;

                       case 0x02 :                          // 우회전

                                      PORTC=0x0a;

                                      mode=2;

                                      break;

                       case 0x03 :                          // 좌회전

                                      PORTC=0x05;

                                      mode=3;

                                      break;

                       case 0x44 :                          // 정지

                                      PORTC=0x00;

                                      mode=0;

                                      break;

                       case 0x04 :                          // 속도증가

                                      if(vel!=0xff)

                                                vel++;

                                                OCR0 = vel; 

                                                     break;

                                      case 0x05 :                          // 속도감소

                                      if(vel!=0x00)

                                                vel--;

                                                OCR0 = vel; 

                                                     break;

          }

                       delay_ms(10);

            }

            

           // 속도에 따라  Led 4개의 점멸 속도를 다르게 하고 좌회전 우회전을 표시한다.?

                             switch(mode) {

                                           case 0 :                 // 정지 

                                                         PORTE=~0x00;

                       break;

                          case 1 :                 // 전진 , 후진

                                       if(iter > (300-vel)) {

                                                    m_cnt<<=1; 

                                     if(m_cnt>=0x10 || m_cnt==0x00)

                                                m_cnt=0x01;

                                      PORTE=~m_cnt;       //m_cnt 보수를 출력     

                                       iter=0;

                        }

                        break;

          case 2 :                 // 우회전

                        if(iter > 100) {

                                                                       if(m_cnt != 0x00)

                                                                                      m_cnt=0x00;

                                                                       else

                                                                                      m_cnt=0x20;

                                                                       PORTE=~m_cnt;

                                                                       iter=0;

                                                         }

                                                         break; 

                                           case 3 :                 // 좌회전

                        if(iter > 100) {

                                                                       if(m_cnt != 0x00)

                                                                                      m_cnt=0x00;

                                                                       else

                                                                                      m_cnt=0x10;

                                                                       PORTE=~m_cnt;

                                                                       iter=0;

                                                         }

                                                         break;

      }

      iter++;

      delay_ms(1);

 

      }

}

처음