BASIC4MCU | 질문게시판 | atmega128 라인트레이서 코드와 회로도 질문...몇일 째 머리아파 죽겠네요ㅠ
페이지 정보
작성자 왕두일 작성일2024-06-06 00:49 조회590회 댓글2건본문
현재 이 회로도로 납땜을 모두 끝낸 후 프로그램을 넣어봐도 모터만 돌고 수광 발광부가 작동을 안합니다...led도 수광 발광부 가리면 꺼져야 하는데 동작을 안하구요...
첫번째 사진의 오른쪽 위 발광부는 현재 회로도와 다르게 그라운더 쪽에 음극 연결해서 회로 구성한 상태인데 회로도와 똑같이 해야하는지 잘 모르겠어요ㅠㅠ
아트멜 스튜디오 사용 중이구 위 회로도에 맞는 프로그램도 같이 알려주시면 감사하겠습니다...머리 깨질 것 같네요..
댓글 2
조회수 590master님의 댓글
master 작성일
https://cafe.naver.com/circuitsmanual/37011
강좌글 참고하시고요
적외선 LED와 수신센서는 적외선이라서 눈에 안보이죠
하나씩 테스트 하시고요
작성한 코드를 첨부해서 질문해주세요
왕두일님의 댓글
왕두일 작성일
코드는 아래와 같이 넣었습니다
#include <iom128.h>
#define HIGH 1
#define LOW 0
#define Buzzer PORTA_Bit1
#define RMotor_dir_one PORTB_Bit2
#define RMotor_dir_two PORTB_Bit3
#define LMotor_dir_one PORTB_Bit4
#define LMotor_dir_two PORTB_Bit7
#define RMotor_en PORTB_Bit6
#define LMotor_en PORTB_Bit5
#define Front_LED PORTC_Bit0
#define Left_LED PORTC_Bit1
#define Center_LED PORTC_Bit2
#define Right_LED PORTC_Bit3
#define ll_LED PORTC_Bit4
#define rr_LED PORTC_Bit5
#define Front_sen PORTF_Bit0
#define Left_sen PORTF_Bit1
#define Center_sen PORTF_Bit2
#define Rignt_sen PORTF_Bit3
#define ll_sen PORTF_Bit4
#define rr_sen PORTF_Bit5
unsigned char i;
short R_speed=100, L_speed=100;
unsigned char flag=2;
unsigned short time_val;
unsigned char A_sens_val=0, B_sens_val=0, C_sens_val=0, D_sens_val=0, E_sens_val=0, F_sens_val=0;
unsigned char front_digital_val=0,left_digital_val=0, center_digital_val=0, right_digital_val=0, ll_digital_val=0, rr_digital_val=0;
unsigned short front_compare_val=50;
unsigned short line_compare_val=80;
unsigned char speed_arr[5]={140, 120, 100, 80, 0};
unsigned short delay_val=500;
unsigned char total_sens_val=0;
void delay(unsigned long del);
void sensing(void);
void stop(void);
void go(short Len, short Ren);
void back(unsigned short Len, unsigned short Ren);
void constrain(short min, short max);
void adc_sensing(unsigned char pin);
/*#pragma vector=INT0_vect
__interrupt void INT0_interrupt(void)
{
Front_LED=1;
stop();
delay(500);
back(120, 120);
delay(2000);
stop();
delay(1000);
} */
void main(void)
{
DDRA=0x02;
DDRB=0xfc;
DDRC=0x0f;
DDRF=0xf0;
EIMSK=0x01;
EICRA=0x03;
TCCR1A=0xa3;
TCCR1B=0x02;
TCCR1C=0x00;
TCNT1=0x00;
SREG=0x80;
delay(3000);
while(1)
{
for(i=0; i<=5; i++){
adc_sensing(i);
}
sensing();
constrain(0, 150);
go(L_speed, R_speed);
}
}
void delay(unsigned long del)
{
del=del*10;
while(del--);
}
void sensing(void)
{
total_sens_val=rr_digital_val+right_digital_val*2+center_digital_val*4+left_digital_val*8+ll_digital_val*16;
if(total_sens_val==16){
L_speed=speed_arr[4];
R_speed=speed_arr[0];
flag=1;
}
if(total_sens_val==24){ //11000
L_speed=speed_arr[3];
R_speed=speed_arr[0];
flag=1;
}
if(total_sens_val==8){ //01000
L_speed=speed_arr[2];
R_speed=speed_arr[0];
}
if(total_sens_val==12){ //01100
L_speed=speed_arr[1];
R_speed=speed_arr[0];
}
if(total_sens_val==4){ //00100
L_speed=speed_arr[0];
R_speed=speed_arr[0];
flag=2;
}
if(total_sens_val==6){ //00110
L_speed=speed_arr[0];
R_speed=speed_arr[1];
}
if(total_sens_val==2){ //00010
L_speed=speed_arr[0];
R_speed=speed_arr[2];
}
if(total_sens_val==3){ //00011
L_speed=speed_arr[0];
R_speed=speed_arr[3];
flag=3;
}
if(total_sens_val==1){ //00001
L_speed=speed_arr[0];
R_speed=speed_arr[4];
flag=3;
}
if(total_sens_val==0){
while(!total_sens_val&4){ //가운데 센서 감지될때까지
if(flag==1){
L_speed=speed_arr[4];
R_speed=speed_arr[0];
}
if(flag==2){
L_speed=speed_arr[0];
R_speed=speed_arr[0];
}
if(flag==3){
L_speed=speed_arr[0];
R_speed=speed_arr[4];
}
}
}
}
void constrain(short min, short max)
{
if(R_speed>max) R_speed=max;
if(R_speed<min) R_speed=min;
if(L_speed>max) L_speed=max;
if(L_speed<min) L_speed=min;
}
void adc_sensing(unsigned char pin) //0~5핀 입력
{
switch(pin){
/*case 0: //전방 감지 센서 ADC
ADMUX=0x40;
ADCSR=0xc6; //1110 0110
while((ADCSR & 0x10)==0);
A_sens_val=ADC;
if(A_sens_val<front_compare_val){
Front_LED=1;
front_digital_val=1;
}else{
Front_LED=0;
front_digital_val=0;
}
break; */
case 1: //왼쪽 감지 센서 ADC
ADMUX=0x41;
ADCSR=0xc6; //1100 0110
while((ADCSR & 0x10)==0);
B_sens_val=ADC;
if(B_sens_val>line_compare_val){
Left_LED=1;
left_digital_val=1;
}else{
Left_LED=0;
left_digital_val=0;
}
break;
case 2: //가운데 감지 센서 ADC
ADMUX=0x42;
ADCSR=0xc6;
while((ADCSR & 0x10)==0);
C_sens_val=ADC;
if(C_sens_val>line_compare_val){
Center_LED=1;
center_digital_val=1;
}else{
Center_LED=0;
center_digital_val=0;
}
break;
case 3: //오른쪽 감지 센서 ADC
ADMUX=0x43;
ADCSR=0xc6;
while((ADCSR & 0x10)==0);
D_sens_val=ADC;
if(D_sens_val>line_compare_val){
Right_LED=1;
right_digital_val=1;
}else{
Right_LED=0;
right_digital_val=0;
}
break;
case 4:
ADMUX=0x44;
ADCSR=0xc6;
while((ADCSR & 0x10)==0);
E_sens_val=ADC;
if(E_sens_val>line_compare_val){
ll_LED=1;
ll_digital_val=1;
}else{
ll_LED=0;
ll_digital_val=0;
}
break;
case 5:
ADMUX=0x45;
ADCSR=0xc6;
while((ADCSR & 0x10)==0);
F_sens_val=ADC;
if(F_sens_val>line_compare_val){
rr_LED=1;
rr_digital_val=1;
}else{
rr_LED=0;
rr_digital_val=0;
}
break;
default:
break;
}
ADCSR&=0xdf;
}
//////////////모터 정지, 전진, 후진///////////////
void stop(void)
{
RMotor_dir_one=0;
RMotor_dir_two=0;
LMotor_dir_one=0;
LMotor_dir_two=0;
RMotor_en=0;
LMotor_en=0;
}
void go(short Len, short Ren)
{
RMotor_dir_one=HIGH;
RMotor_dir_two=LOW;
LMotor_dir_one=HIGH;
LMotor_dir_two=LOW; //최대 1023
OCR1A=Ren;
OCR1B=Len; //PB5
}
void back(unsigned short Len, unsigned short Ren)
{
RMotor_dir_one=!HIGH;
RMotor_dir_two=!LOW;
LMotor_dir_one=!HIGH;
LMotor_dir_two=!LOW;
OCR1B=Ren;
OCR1A=Len; //PB5
}