BASIC4MCU | 질문게시판 | atmega128을 사용해서 길을 기억하는 자동차를 만드려고 하는데 s/w 쪽을 어떻게 작성해야 되는지 모르겠습니다.
페이지 정보
작성자 atmega1 작성일2019-11-04 15:19 조회6,332회 댓글0건
https://www.basic4mcu.com/bbs/board.php?bo_table=gac&wr_id=10530
본문
atmega128을 사용해서 길을 기억하는 자동차를 만드려고 하는데 s/w 쪽을 어떻게 작성해야 되는지 모르겠습니다.
#include <mega128.h>
#include <stdio.h>
#include <lcd.h>
#include <delay.h>#define L_IN1 PORTB.0 //L_Motor Dir Control
#define L_IN2 PORTB.1
#define R_IN1 PORTB.2 //R_Motor Dir Control
#define R_IN2 PORTB.3#define L_EN PORTB.5 //L_Motor Control , OCR1A Output Pin
#define R_EN PORTB.6 //R_Motor Control , OCR1B Output Pin#asm
.equ __lcd_port = 0x15; //PORTC - LCD 사용
#endasmvolatile unsigned char mode[3][16] = {"Ready ",Memorizing"};
volatile unsigned char temp_L[16];
volatile unsigned char temp_R[16];
volatile unsigned int L_cnt, R_cnt;volatile unsigned int L_cnt_s[100], L_Dir_s[100];
volatile unsigned int R_cnt_s[100], R_Dir_s[100];
volatile unsigned int L_run_cnt, R_run_cnt, L_top, R_top;volatile bit L_Run,R_Run,L_comp, R_comp, test1, test2, test3;
volatile int L_step, R_step,top, err_L, err_R;
volatile bit L_D, R_D, Memo_flag, md_sel;
volatile bit L_chk_flag, R_chk_flag, L_t_chk, R_t_chk;interrupt [EXT_INT0] void Memo(void)
{
if(L_Run !=1 || R_Run !=1)
md_sel = ~md_sel; //Memrizing mode on/offL_chk_flag = 1;
R_chk_flag = 1;if(L_t_chk ==1)
{
//***************L motor***************
L_Dir_s[L_step] = L_D; //방향 저장
L_cnt_s[L_step] = L_cnt;
}
//**************** R motor**************
if(R_t_chk == 1)
{
R_Dir_s[R_step] = ~R_D; //방향 저장
R_cnt_s[R_step] = R_cnt; //펄스 수 저장
}
//*************** 초기화 *****************
if(L_t_chk==1 || R_t_chk==1)
{
L_t_chk=0;
R_t_chk=0;
md_sel=0;
L_cnt=0;
R_cnt=0;
L_run_cnt=0;
R_run_cnt=0;
L_chk_flag=0;
R_chk_flag=0;
//*********************저장완료**********
memo_flag = 1;
}
}
interrupt [EXT_INT1] void Run_mode(void)
{
if(Memo_flag==1)
{
L_Run=1;
R_Run =1;L_run_cnt=0;
R_run_cnt=0;
}
}//*******************Left_motor**********************
interrupt [EXT_INT4] void Left_motor_capture(void)
{
if(md_sel ==1) /*******save mode*******/
{
L_cut++;if(L_chk_flag==1) // DIR Check
{
L_t_chk=1;if(PINE.5==0)
{
L_D = 0; //Im_Dir Reverse
}
else
{
L_D = 1; // Im_dir Drive
}
L_chk_flag =0;
}if(L_D ^ PINE.5==1)
{
if(L_cnt >2)
{
L_Dir_s[L_step]=L_D; //방향저장
L_cnt_s[L_step]= L_cnt;L_step++;L_chk_flag = 1;
L_cnt = 0;
}
}
}if(Memo_flag ==1)
{
L_run_cnt++;
}
}
//************************Right motor**********************
interrupt [EXT_INT6] void Right_Motor_capture(void){
if(md_sel==1)
{
R_cnt++; //회전 펄스 수 ++if(R_chk_flag ==1) { // Direction check
R_t_chk=1;if(PINE.7 ==0){
R_D=0; //Im_Dir Reverse
}
else {
R_D=1; //Im_dir drive
}
R_chk_flag=0; // chk Flag off, 더이상 방향 체크 없이 카운트만 체크
}
if(R_D^PINE.7==1) //방향 전환 xor 연산을 통해 감지
{
if(R_cnt>2) // 카운트가 2 이상이어야지만 step 증가
{
R_Dir_s[R_step] = ~R_D; // 방향 저장
R_cnt_s[R_step] = R_cnt; // 펄스수 저장R_step++; // Step ++R_chk_flag = 1; //방향 chk flag on
R_cnt = 0; //R_cnt 초기화
}
}
}if(Memo_flag ==1) //저장된 값 출력시 if문을 발생
{
R_run_cnt++;
}
}void main(void)
{
DDRB = 0xff;EICRA = (1<<ISC31) | (0<<ISC30) | (0<<ISC21) | (0<<ISC20) | (0<<ISC11) | (0<<ISC10) | (1<<ISC01) | (0<<ISC00);
EICRB = (0<<ISC71) | (0<<ISC70) | (0<<ISC61) | (0<<ISC60) | (0<<ISC51) | (0<<ISC50) | (1<<ISC41) | (0<<ISC40);
EIMSK = (0<<INT7) | (0<<INT6) | (0<<INT5) | (0<<INT4) | (0<<INT3) | (0<<INT2) | (1<<INT1) | (1<<INT0);
TCCR1A = (0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<COM1C1) | (0<<COM1C0) | (0<<WGM11) | (1<<WGM10);
TCCR1B = (0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (1<<WGM12) | (0<<CS12) | (1<<CS11) | (1<<CS10);
OCR1A = 170;
OCR1B = 170;lcd_init(16);#asm("sei")
while (1)
{
//LCD 상태표시
lcd_gotoxy(0,0);
sprintf(temp_L, "Mode :%s", mode[md_sel]);
lcd_puts(temp_L);lcd_gotoxy(0,1);
sprintf(temp_R, "L : %3d R:%3d", L_step, R_step);
lcd_puts(temp_R);//저장 정보가 있을 때if(Memo_flag == 1&&(L_Run == 1||R_Run==1))
{
//L Motor
if(L_Run ==1)
{
if(L_cnt_s[L_top] !=0)
{
if(L_Dir_s[L_top] ==0)
{
TCCR1A |= (1<<COM1A1) | (1<<COM1A0);
L_IN1 = 1;
L_IN2 = 0; //reverse
}
else
{
TCCR1A |= (1<<COM1A1) | (0<<COM1A0);
L_IN1 = 1;
L_IN2 = 0; //drive
}err_L = L_cnt_s[L_top] - L_run_cnt;if(err_L == 0)
{
RCCR1A |= (0<<COM1A1) | (0<<COM1A0) | (0<<COM1C1) | (0<<COM1C0) | (0<<WGM11) | (1<<WGM10);
L_EN = 0;
L_IN1 = 1;
L_IN2 = 1;
delay_us(30);
L_run_cnt = 0;
L_top++;
}if(L_step < L_top)
{
TCCR1A |= (0<<COM1A1) | (0<<COM1A0) | (0<<COM1C1) | (0<<COM1C0) | (0<<WGM11) | (1<<WGM10);
L_IN1=1;
L_IN2=1;
l_EN =0;
L_run_cnt =0;
L_top = 0;
L_Run = 0;
L_comp = 1;
}
if(L_comp == 1)
L_EN =0;
}
//R MOTOR
if(R_Run == 1)
{
if(R_cnt_s[R_top] !=0)
{
// R방향 파악
if(R_Dir_s[R_top] ==0) //reverse
{
TCCR1A |= (1<<COM1B1) | (0<<COM1B0);
R_IN1 = 0;
R_IN2 = 1;
}
else // Drive
{TCCR1A |= (1<<COM1B1) | (1<<COM1B0);
R_IN1 = 1;
R_IN2 = 0;
}err_R = R_cnt_s[R_top] - R_run_cnt;if(err_R ==0)
{
TCCR1A |= (0<<COM1B1) | (0<<COM1B0) | (0<<COM1C1) | (0<<COM1C0) | (0<<WGM11) | (1<<WGM10);
R_EN = 0;
R_IN1 = 1;
R_IN2 = 1;
delay_us(30);
R_run_cnt = 0;
R_top++;
test1 = 1;
}
}if(R_step < R_top)
{
TCCR1A |= (0<<COM1B1) | (0<<COM1B0) | (0<<COM1C1) | (0<<COM1C0) | (0<<WGM11) | (1<<WGM10);
test1 = 1;
R_IN1 =1;
R_IN2 = 1;
R_EN = 0;R_run_cnt = 0;
R_step = 0;
err_R = 0;
R_Run = 0;
R_comp = 1;
}
if(R_comp == 1)
R_EN = 0;
}if(L_comp == 1 && R_comp == 1)
{
Memo_flag = 0;OCR1A = 0;
OCR1B = 0;TCCR1A = (0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<COM1C1) | (0<<COM1C0) | (0<<WGM11) | (0<<WGM10);
TCCR1B = (0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | 0<<CS12) | (0<<CS11) | (0<<CS10);
L_EN = 0;
R_EN = 0;
}여기서 잘못된 것이 있으면 말좀 해주시면 정말 감사하겠습니다 학교 프로젝트인데 계속 오류가 떠서요ㅠㅠ 오류내용은 이렇게 뜹니다. 처음 작성하는거라 너무 뭐라하지 말아주시고 좋은 마음으로 말해주시길 바랍니다. 감사합니다.
댓글 0
조회수 6,332등록된 댓글이 없습니다.