AVR > ATmega128 AVRstudio ADC CDS 시간측정

TODAY371 TOTAL2,925,234
사이트 이용안내
Login▼/회원가입
최신글보기 질문게시판 기술자료 동영상강좌

아두이노 센서 ATMEGA128 PWM LED 초음파 AVR 블루투스 LCD UART 모터 적외선


BASIC4MCU | AVR | ADC | ATmega128 AVRstudio ADC CDS 시간측정

페이지 정보

작성자 master 작성일2018-06-17 07:38 조회2,788회 댓글0건

본문

 

// DateTime : 2018-06-17 오전 7:41:04
// by Ok-Hyun Park
//
#include <avr/io.h>
#include <avr/interrupt.h>
#define F_CPU 16000000UL // 16 MHz
#include <util/delay.h>
#include <stdio.h>
#include "lcd.h"
//
#define  U08   unsigned char
#define  U16   unsigned int
//
#define  LED_ON    PORTB&=~2 // PB1
#define  LED_OFF   PORTB|=2  // PB1
//
#define  MOTOR_0   PORTA&=~1 // PA0
#define  MOTOR_1   PORTA|=1  // PA0
//
#define  Vmax      50
//
volatile U08 CDS_flag=0,MOTOR_flag=0;
volatile U08 str[20],cnumber[5]={0,0,0,0,0};
volatile U16 CDS_new=0,CDS_avg=0,Sec=0;
//
void DC_Motor_Run_Fwd(int duty){ if(duty>Vmax)duty=VmaxMOTOR_0OCR1A=duty;      }  
void DC_Motor_Run_Rev(int duty){ if(duty>Vmax)duty=VmaxMOTOR_1OCR1A=Vmax-duty; }
void DC_Motor_Stop(void)       { MOTOR_0OCR1A=0; }
//
void DC_Motor_PWM(int Vref){ // DC 모터 PWM 신호 발생 함수
  if     (VrefVmax)VrefVmax;
  else if(Vref<-Vmax)Vref=-Vmax;
  //
  if     (Vref0){ DC_Motor_Run_FwdVref); }
  else if(Vref==0){ DC_Motor_Stop();         }
  else if(Vref0){ DC_Motor_Run_Rev(-Vref); }
}
//
ISR(TIMER3_COMPA_vect){ // 250ms
  static U16 CDS_Sum=0,tp=0,CDS_buf[4]; static U08 CDS_index=0;
  //
  CDS_new=ADC;                  // CDS값을 읽어서
  tp=CDS_buf[CDS_index];        // 배열 값을 변경하기 전에 저장
  CDS_buf[CDS_index]=CDS_new;   // 배열에 저장
  CDS_Sum+=CDS_new;             // 새로운 값을 더해주고
  CDS_Sum-=tp;                  // 이전 값을 빼준 후
  CDS_avg=CDS_Sum/4;            // 4개 평균
  if(++CDS_index>3)CDS_index=0// 0~3 저장포인트 변경
  CDS_flag=1;
  //
  if(CDS_avg>=500){ LED_ON;  if(++Sec==400)MOTOR_flag=1;        } // 100초 이상이면
  else            { LED_OFFif(  Sec>  40)MOTOR_flag=1Sec=0; } //  10초 이상이면
}
//
int main(){
  int duty=50;
  DDRB=0x22LED_OFF// LED(PB1: 출력설정)
  DDRA=0x01;          // 모터(PA0) 
  LcdInit(); LcdCommand(ALLCLR);
  ADCSRA=0xE7;
  TCCR1A=0x82TCCR1B=0x1bICR1=50;
  TCCR3B=0x0COCR3A=15624ETIMSK=0x10//250ms
  //
  sei();
  while(1){
    if(CDS_flag){
      sprintf(str,"CDS value=%5d",CDS_new); LcdMove(0,0); LcdPuts(str);
      sprintf(str,"CDS avg  =%5d",CDS_avg); LcdMove(1,0); LcdPuts(str);
      CDS_flag=0;
    }
    //
    if(MOTOR_flag){ 
      DC_Motor_Run_Fwd(duty); DC_Motor_PWMduty); _delay_ms(2500);
      DC_Motor_Stop();        DC_Motor_PWM(0);     _delay_ms(1000);
      DC_Motor_Run_Rev(duty); DC_Motor_PWM(-duty); _delay_ms(2500);
      DC_Motor_Stop();        DC_Motor_PWM(0);
      MOTOR_flag=0;
    }
  }
}

 

 

댓글 0

조회수 2,788

등록된 댓글이 없습니다.

AVRHOME > AVR > 전체 목록

게시물 검색

2022년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2021년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2020년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2019년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
2018년 1월 2월 3월 4월 5월 6월 7월 8월 9월 10월 11월 12월
Privacy Policy
MCU BASIC ⓒ 2020
모바일버전으로보기