BASIC4MCU | 질문게시판 | Atmega128 코딩 도와주세요,,!
페이지 정보
작성자 화니뿌뿌 작성일2022-06-07 21:18 조회1,361회 댓글0건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=20441
본문
조도센서에 빛의 양에 따라 서보모터와 dc모터가 제어되는 코딩인데요! 서보모터와dc모터가 동시에 돌아가지않고 스위치로 제어도 안됩니다,,! 한번 봐주실수 있을까요?! Atmega8535에서 atmega128로 바꿨습니다,
Pwm->16핀
Dc->17,29핀
서보모터->15핀으로 바꿨습니다,,!
#include <mega128.h> #include <delay.h>
#define ADC_VREF_TYPE 0x20
#define START PINA.1
#define END PINA.2
#define SERVO_FW PINA.3
#define SERVO_BW PINA.4
#define DC_FW PINA.5
#define DC_BW PINA.6
#define AUTO_SW PINA.7
// Read the 8 most significant bits
// of the AD conversion result
unsigned char read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCH;
}
void dc_forward(void){ OCR1B=1000;
PORTD.6=0;
PORTD.7=1; }
void dc_backward(void){ OCR1B=1000;
PORTD.6=1;
PORTD.7=0; }
void dc_stop(void){ OCR1B=0;
}
void servo_fw(void){ if(OCR1A<12)
OCR1A=OCR1A+1; else ;
}
void servo_bw(void){ if(OCR1A>5)
OCR1A=OCR1A-1; else ;
}
void servo_90(void){ OCR1A=12;
}
void servo_70(void){ OCR1A=10;
}
void servo_50(void){ OCR1A=9;
}
void servo_25(void){ OCR1A=7;
}
void servo_0(void){ OCR1A=5;
}
// Declare your global variables here
void main(void) {
unsigned char adc_value; float temp;
adc_value=0;
temp = 0;
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T S tate0 =T
PORTA=0x00;
D DRA =0x0 0;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T S tate0 =T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T S tate0 =T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
/ / State 7=0 St ate6= 0 State 5=0 St ate4= 0 State 3=0 St ate2= 0 State 1=0 S tate0 =0
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization // Clock source: System Clock // Clock value: Timer 0 Stopped // Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00; TCNT0=0x00; OCR0=0x00;
// Timer/Counter 1 initialization // Clock source: System Clock // Clock value: Timer 1 Stopped // Mode: Normal top=FFFFh
// OC1A output: Discon. // OC1B output: Discon. // Noise Canceler: Off
/ Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0xA1; // PWM phase correct, 8bit
TCCR1B=0x03; // f = CLK/8 = 1000000Hz/8 = 125kHz => OCR1A*2*1/f = Pulse width
// OCR1A=43 => 0 degree, OCR1A=94 => 90 degree
// Pulse period = (1/f)*2*256 = (1/125000)*2*256 = 0.004096 s = 4ms TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer 2 Stopped // Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00;
// External Interrupt(s) initialization // INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80;
SFIOR=0x04; //pull-up disable
// ADC initialization
// ADC Clock frequency: 125.000 kHz // ADC Voltage Reference: AREF pin // ADC High Speed Mode: Off
// ADC Auto Trigger Source: None // Only the 8 most significant bits of // the AD conversion result are used ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0x83;
S FIO R&= 0xEF;
delay_ms(1000); OCR1A=5; //90 degree
OCR1B=0; //PORTD.4 PWM no generate
while (1) {
if(START==0&&END==1){ //다 접혀 있을 때 if(DC_FW==0&&DC_BW==1)
dc_forward();
else if(DC_FW==1&&DC_BW==0)
dc_stop(); else
dc_stop(); }
else if(START==1&&END==1){ if(DC_FW==0&&DC_BW==1)
//중간에 있을 때
dc_forward();
else if(DC_FW==1&&DC_BW==0)
dc_backward(); else
dc_stop(); }
else if(START==1&&END==0){ if(DC_FW==0&&DC_BW==1)
//다 펴져 있을 때
dc_stop();
else if(DC_FW==1&&DC_BW==0)
dc_backward(); else
dc_stop();
if(AUTO_SW==0){ //자동조절 스위치 OFF (수동)
if(SERVO_FW==0&&SERVO_BW==1) servo_fw();
else if(SERVO_FW==1&&SERVO_BW==0) servo_bw();
else ;
delay_ms(100); }
else{ //자동조절 스위치 ON
delay_ms(500);
adc_value=read_adc(0); //sensing
temp = adc_value*0.004887; //5V analog voltage convert to
digital voltage
if(temp>0 && temp<=1) servo_90();
else if(temp>1 && temp<=2) servo_70();
else if(temp>2 && temp<=3) servo_50();
else if(temp>3 && temp<=4) servo_25();
else servo_0();
delay_ms(500);
} }
else ; }
}
코딩한번 봐주세요ㅜㅠ
댓글 0
조회수 1,361등록된 댓글이 없습니다.