BASIC4MCU | 질문게시판 | 답변 : ATmega128을 이용한 DC모터 속도 제어에 대해 질문합니다.
페이지 정보
작성자 master 작성일2018-06-06 01:01 조회13,776회 댓글0건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=1497
본문
// MCU BASIC: https://www.basic4mcu.com// DateTime : 2018-06-06 오전 1:04:02// by Ok-Hyun Park//가변저항을 조절함으로써 DC모터 속도 제어//#include <mega128.h>#include <delay.h>//char seg[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7c,0x07,0x7f,0x67};//bit ENcnt=0,motor_stop=1;int PPR=208,rpm=0;int TimeTicker=0;int data=0;unsigned int temp1=0,temp2=0,cnt=0,encoder_cnt=0;float temp,TimeVal,Krp;//void init_input_capture_func(){TCCR3A=0;if(PINE.7)TCCR3B=0x84;else TCCR3B=0xC4;TCNT3H=0; TCNT3L=0;}//interrupt [TIM0_COMP] void timer0_comp_isr(void){ //1msPORTC|=0x0F;if(cnt==0)PORTA=seg[rpm%10];if(cnt==1)PORTA=seg[(rpm/10)%10];if(cnt==2)PORTA=seg[(rpm/100)%10];if(cnt==3)PORTA=seg[(rpm/1000)%10];PORTC=~(8>>cnt);if(++cnt==4)cnt=0;}//interrupt[TIM3_OVF]void TIM3_OVF_isr(void){if(++TimeTicker>3){ rpm=0; temp1=temp2=0; ENcnt=0; }}//interrupt[TIM3_CAPT]void TIM3_CAPT_isr(void){Krp=(60*16000000/256)/PPR;encoder_cnt++;if(ENcnt==0){ temp1=ICR3L; temp1=temp1+ICR3H*256; TimeTicker=0; }else{temp2=ICR3L; temp2=temp2+ICR3H*256;TimeVal=(float)((TimeTicker*65536+temp2)-temp1);temp=(Krp/TimeVal); temp1=temp2; TimeTicker=0;if(encoder_cnt/(208*1)){ rpm=(int)temp; encoder_cnt=0; }}ENcnt=1;}//void main(void){DDRA=0xff; DDRB=0x21; DDRC=0xff; DDRG=0x03;//ADMUX=0x47; ADCSRA=0xE7;TCCR0=0x0D; OCR0=124; TIMSK=0x02; //16000000/128/(1+124),1msICR1=1023; TCCR1A=0x80; TCCR1B=0x11;#asm("sei");while(1){delay_ms(300); data=ADCW; OCR1A=data;//if(data==0){ motor_stop=1; ETIMSK=0; rpm=0; }else{if(motor_stop ){ motor_stop=0; init_input_capture_func(); ETIMSK=0x24; }}}}
회로에 따라서 PWM 구동하는 방법이 달라집니다.
회로도 첨부해보세요
댓글 0
조회수 13,776등록된 댓글이 없습니다.