Chip type : ATmega16
Program type : Application
Clock frequency : 11.059200 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#include <delay.h>
#include <stdio.h>
#define t1 PINB.0
#define t2 PINB.1
#define t3 PINB.2
#define t4 PINB.3
#define t5 PINB.4
#define t6 PINB.7
#define pwmka OCR1A
#define pwmki OCR1B
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTB
#endasm
#include <lcd.h>
#define ADC_VREF_TYPE 0x60
// 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);
// 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
char buff[33];
int i,j;
unsigned char kecepatanki,kecepatanka;
eeprom int garis[10],back[10],tengah[10];
unsigned char sen[10],sensor;
void lcd_kedip(int ulangi)
{
for(i=0;i<ulangi;i++)
{
delay_ms(100);
delay_ms(100);
}
}
void maju(unsigned char ki,unsigned char ka)
{
pwmka=ka;
pwmki=ki;
//dir kiri
PORTD.2=1;
PORTD.3=0;
//dir kanan
PORTD.6=0;
PORTD.7=1;
}
void mundur(unsigned char ki,unsigned char ka)
{
pwmka=ka;
pwmki=ki;
//dir kiri
PORTD.2=0;
PORTD.3=1;
//dir kanan
PORTD.6=1;
PORTD.7=0;
}
void kiri(unsigned char ki,unsigned char ka)
{
pwmka=ka;
pwmki=ki;
//dir kiri
PORTD.2=0;
PORTD.3=1;
//dir kanan
PORTD.6=0;
PORTD.7=1;
}
void kanan(unsigned char ki,unsigned char ka)
{
pwmka=ka;
pwmki=ki;
//dir kiri
PORTD.2=1;
PORTD.3=0;
//dir kanan
PORTD.6=1;
PORTD.7=0;
}
void tes_sensor()
{
if(t1==0)
{
for(i=0;i<8;i++)
{
lcd_gotoxy(0,0);
sprintf(buff,"s:%d -> %d ",i,read_adc(i));
lcd_puts(buff);
delay_ms(1000);
}
}
}
void scan_garis()
{
for(i=0;i<8;i++)
{
garis[i]=read_adc(i);
lcd_gotoxy(0,0);
lcd_putsf("GARIS ");
lcd_gotoxy(0,1);
sprintf(buff,"s:%d --> %d ",i,garis[i]);
lcd_puts(buff);
delay_ms(200);
}
}
void scan_back()
{
for(i=0;i<8;i++)
{
back[i]=read_adc(i);
lcd_gotoxy(0,0);
lcd_putsf("BACK GROUND");
lcd_gotoxy(0,1);
sprintf(buff,"s:%d --> %d ",i,back[i]);
lcd_puts(buff);
delay_ms(200);
}
}
void hit_tengah()
{
for(i=0;i<8;i++)
{
tengah[i]=(back[i]+garis[i])/2;
lcd_gotoxy(0,0);
lcd_putsf("TENGAH ");
lcd_gotoxy(0,1);
sprintf(buff,"s:%d --> %d ",i,tengah[i]);
lcd_puts(buff);
delay_ms(200);
}
}
void konvert_logic()
{
for(i=0;i<8;i++)
{
if(read_adc(i)>tengah[i])
{
sen[i]=1;
}
else if(read_adc(i)<tengah[i])
{
sen[i]=0;
}
}
}
void logika()
{
sensor=(sen[7]*128)+(sen[6]*64)+(sen[5]*32)+(sen[4]*16)+(sen[3]*8)+(sen[2]*4)+(sen[1]*2)+(sen[0]*1);
}
/*
void logika()
{
sensor=(sen[7]*1)+(sen[6]*2)+(sen[5]*4)+(sen[4]*8)+(sen[3]*16)+(sen[2]*32)+(sen[1]*64)+(sen[0]*128);
}
*/
void scan()
{
switch(sensor)
{
case 0b00000001: maju(200,180); break;
case 0b00000011: maju(200,160); break;
case 0b00000111: maju(200,140); break;
case 0b00001111: maju(200,120); break;
case 0b00011000: maju(200,200); break;
case 0b00001000: maju(200,200); break;
case 0b00010000: maju(200,200); break;
case 0b11110000: maju(120,200); break;
case 0b11100000: maju(140,200); break;
case 0b11000000: maju(160,200); break;
case 0b10000000: maju(180,200); break;
}
}
void scen()
{
switch(sensor)
{
case 0b00000000: maju(200,200); break;
case 0b00011000: maju(200,200); break;
case 0b11111111: maju(200,200); break;
case 0b00001111: maju(200,200); break;
case 0b00000111: maju(200,200); break;
case 0b00000011: maju(200,200); break;
case 0b00000001: maju(200,200); break;
case 0b00000010: maju(200,200); break;
case 0b00000110: maju(200,200); break;
case 0b00001110: maju(200,200); break;
case 0b00000100: maju(200,200); break;
case 0b00001100: maju(200,200); break;
case 0b00001000: maju(200,200); break;
}
}
void cek_sensor()
{
konvert_logic();
logika();
lcd_gotoxy(0,0);
lcd_putsf("CEK SENSOR ");
lcd_gotoxy(0,1);
sprintf(buff,"%d%d%d%d%d%d%d%d",sen[7],sen[6],sen[5],sen[4],sen[3],sen[2],sen[1],sen[0]);
lcd_puts(buff);
}
void cekdatasensor()
{
for(i=0;i<8;i++)
{
lcd_gotoxy(0,0);
sprintf(buff,"data ke = %d ",i);
lcd_puts(buff);
lcd_gotoxy(0,1);
sprintf(buff,"sensing = %d ",read_adc(i));
lcd_puts(buff);
delay_ms(1000);
}
}
void stop()
{
PORTD.3=1;
PORTD.4=1;
PORTD.6=1;
PORTD.7=1;
pwmka=0;
pwmki=0;
}
void scan_count(unsigned char xcounter)
{ unsigned char counter;
unsigned int i;
counter=0;
while(counter<xcounter)
{ for(i=0;i<1000;i++) while((sensor & 0b11111111)!=0b00011000) scan();
for(i=0;i<90;i++) while((sensor & 0b11111111)==0b00000000) scan();
counter++;};
stop();
}
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=0x08;
DDRB=0x08;
// 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=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0xFC;
// 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: Fast PWM top=00FFh
// OC1A output: Non-Inv.
// OC1B output: Non-Inv.
// 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;
TCCR1B=0x09;
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=0x00;
// ADC initialization
// ADC Clock frequency: 691.200 kHz
// ADC Voltage Reference: AVCC pin
// ADC Auto Trigger Source: None
// Only the 8 most significant bits of
// the AD conversion result are used
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x84;
// LCD module initialization
lcd_init(16);
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("Mufid Program");
while (1)
{
if(t4==0)
{
scan_garis();
delay_ms(500);
scan_back();
hit_tengah();
}
if(t3==0)
{
lcd_clear();
while(1)
{
konvert_logic();
logika();
cek_sensor();
scan();
}
}
};
}
0 komentar:
Posting Komentar