Program Robot Line Follower

Published on January 2017 | Categories: Documents | Downloads: 33 | Comments: 0 | Views: 400
of 7
Download PDF   Embed   Report

Comments

Content

/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.3 Standard
Automatic Program Generator
© Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date
: 9/24/2013
Author : saya
Company : pakai bajakan yg gratis aja!
Comments:
Chip type
: ATmega16
Program type
: Application
AVR Core Clock frequency: 11.059200 MHz
Memory model
: Small
External RAM size
: 0
Data Stack size
: 256
*****************************************************/
#include <mega16.h>
#include <stdio.h>
#include <delay.h>
// Alphanumeric LCD functions
#include <alcd.h>
#define dir_r
#define dir_l

PORTD.3
PORTD.6

#define
#define
#define
#define

PINC.0
PINC.1
PINC.2
PINC.3

start
menu
up
down

// Timer1 overflow interrupt service routine
unsigned char pwm_kiri,pwm_kanan,pwm_r,pwm_l;
unsigned char lcd[16];
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here
OCR1A=255-pwm_kiri;
OCR1B=255-pwm_kanan;
}
#define ADC_VREF_TYPE 0x20
// 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;
}
// Declare your global variables here
void maju(unsigned char pwm_l,unsigned char pwm_r)
{
dir_l=1;
dir_r=1;
pwm_kiri=pwm_l;
pwm_kanan=pwm_r;
}
void mundur(unsigned char pwm_l,unsigned char pwm_r)
{
dir_l=0;
dir_r=0;
pwm_kiri=pwm_l;
pwm_kanan=pwm_r;
}
void b_kanan(unsigned char pwm_l,unsigned char pwm_r)
{
dir_l=0;
dir_r=1;
pwm_kiri=pwm_l;
pwm_kanan=pwm_r;
}
void b_kiri(unsigned char pwm_l,unsigned char pwm_r)
{
dir_l=1;
dir_r=0;
pwm_kiri=pwm_l;
pwm_kanan=pwm_r;
}
void stop()
{
dir_l=0;
dir_r=0;
pwm_kiri=0;
pwm_kanan=0;
}
void baca_garis()
{
lcd_gotoxy(0,0);
sprintf(lcd,"%3d %3d %3d %3d", read_adc(0),read_adc(1),read_adc(2),read_adc(
3));
lcd_puts(lcd);
lcd_gotoxy(0,1);
sprintf(lcd,"%3d %3d %3d %3d", read_adc(4),read_adc(5),read_adc(6),read_adc(
7));
lcd_puts(lcd);
}

unsigned char sendat;
unsigned char s[9],sensor[9];
void isi_datsens()
{
s[1]=read_adc(0); s[2]=read_adc(1);
;
s[5]=read_adc(4); s[6]=read_adc(5);
;
if
if
if
if
if
if
if
if

(s[1]<=20){sensor[1]=0;}
(s[2]<=20){sensor[2]=0;}
(s[3]<=20){sensor[3]=0;}
(s[4]<=20){sensor[4]=0;}
(s[5]<=20){sensor[5]=0;}
(s[6]<=20){sensor[6]=0;}
(s[7]<=20){sensor[7]=0;}
(s[8]<=20){sensor[8]=0;}

else
else
else
else
else
else
else
else

s[3]=read_adc(2);

s[4]=read_adc(3)

s[7]=read_adc(6);

s[8]=read_adc(7)

{sensor[1]=1;}
{sensor[2]=1;}
{sensor[3]=1;}
{sensor[4]=1;}
{sensor[5]=1;}
{sensor[6]=1;}
{sensor[7]=1;}
{sensor[8]=1;}

sendat=(sensor[8]*1)+(sensor[7]*2)+(sensor[6]*4)+(sensor[5]*8)+(sensor[4]*16)
+(sensor[3]*32+(sensor[2]*64)+(sensor[1]*128));
}
void tampil_logic()
{
isi_datsens();
lcd_gotoxy(0,0);
lcd_putsf("logic 0/1 sensor");
lcd_gotoxy(0,1);
sprintf(lcd,"%i%i%i%i%i%i%i%i",sensor[1],sensor[2],sensor[3],sensor[4],senso
r[5],sensor[6],sensor[7],sensor[8]);
lcd_puts(lcd);
}
void scan()
{
bit x;
isi_datsens();
sendat&=0b11111111;
switch(sendat)
{
case 0b11111110 :
b_kanan(100,100);
case 0b11111100 :
maju(60,130);
case 0b11111001 :
maju(70,120);
case 0b11110011 :
maju(80,110);
case 0b11100111 :
case 0b11001111 :
maju(110,80);
case 0b10011111 :
maju(120,70);
case 0b00111111 :
maju(130,60);
case 0b01111111 :
b_kiri(100,100);
case 0b11111111 :
if(x==0)
{
b_kanan(100,100);
}
else if(x==1)
{
b_kiri(100,100);
}
else

x=0;
x=0;
x=0;
x=0;
x=1;
x=1;
x=1;
x=1;

break;
break;
break;
break;
break;
break;
break;
break;
break;

{
maju(100,100);
}
break;
};
tampil_logic();
}
void scan_cab(unsigned char count)
{
unsigned int i;
unsigned char xx;
xx=0;
while(xx<count)
{
for (i=0;i<1000;i++)

while ((sendat&=0b00111111)!=0b00000000)

for (i=0;i<1000;i++)

while ((sendat&=0b00111111)==0b00000000)

scan();
scan();
xx++;
}
}
void awal()
{
lcd_gotoxy(0,0);
lcd_putsf("1.start 2.sensor");
lcd_gotoxy(0,1);
lcd_putsf("3.logic");
if(start==0)
{
while(1)
{
scan();
}
}
if(menu==0)
{
while(1)
{
lcd_clear();
baca_garis();
}
}
if(up==0)
{
while(1)
{
lcd_clear();
tampil_logic();
}
}

}
void main(void)
{

// Declare your local variables here
// 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 State0=T
PORTA=0x00;
DDRA=0x00;
// 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 State0=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=P State2=P State1=P State0=P
PORTC=0x0F;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=Out Func5=Out Func4=Out Func3=Out Func2=In Func1=In Func0=In
// State7=T State6=0 State5=0 State4=0 State3=0 State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x78;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 1382.400 kHz
// Mode: Fast PWM top=0x00FF
// OC1A output: Non-Inv.
// OC1B output: Non-Inv.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0xA1;
TCCR1B=0x0A;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x2B;
ICR1L=0x33;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization

// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=(1<<JTD);
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x04;
// USART initialization
// USART disabled
UCSRB=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 691.200 kHz
// ADC Voltage Reference: AREF pin
// ADC Auto Trigger Source: ADC Stopped
// Only the 8 most significant bits of
// the AD conversion result are used
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x84;
// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTB Bit 0
// RD - PORTB Bit 1
// EN - PORTB Bit 2
// D4 - PORTB Bit 4
// D5 - PORTB Bit 5
// D6 - PORTB Bit 6
// D7 - PORTB Bit 7
// Characters/line: 16
lcd_init(16);
// Global enable interrupts

#asm("sei")
while (1)
{
// Place your code here
//baca_garis();
//tampil_logic();
//scan();
awal();
}
}

Sponsor Documents

Or use your account on DocShare.tips

Hide

Forgot your password?

Or register your new account on DocShare.tips

Hide

Lost your password? Please enter your email address. You will receive a link to create a new password.

Back to log-in

Close