



// Standard Input/Output functions
#include <stdio.h>  
#include <string.h>
#include <delay.h>
#include <mega16.h> 
#include <math.h>


#define ADC_VREF_TYPE 0x00

flash static unsigned char  screen[] = {
0,254,2,2,2,2,2,2,2,2,2,130,130,194,194,194
, 194,194,194,194,194,194,194,130,130,2,130,130,194,194,194,226
, 226,226,226,226,194,194,194,130,130,130,194,194,194,226,226,226
, 226,226,226,194,194,194,130,130,2,2,2,2,2,2,2,2
, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,254,0
, 0,255,0,0,0,192,240,248,252,254,255,127,31,31,15,7
, 7,7,7,7,199,255,255,255,255,255,255,159,143,143,135,135
, 135,135,135,199,255,255,255,255,255,191,159,143,135,135,135,135
, 135,135,135,135,143,159,159,159,159,158,128,128,0,0,0,0
, 0,0,0,0,0,0,0,0,248,248,24,24,24,24,24,184
, 240,224,0,24,24,24,24,248,248,24,24,24,24,0,192,240
, 48,24,24,24,24,48,240,192,0,0,224,240,152,152,24,24
, 56,112,96,0,0,0,0,0,0,0,0,0,0,0,255,0
, 0,255,0,0,0,63,255,255,255,255,255,224,128,0,0,0
, 0,0,0,0,7,15,15,143,207,207,207,207,143,15,15,15
, 15,15,15,15,15,15,143,207,255,255,255,255,255,127,31,15
, 15,15,15,15,15,143,207,255,255,255,255,255,63,0,0,0
, 0,0,0,0,0,0,0,0,127,127,3,3,3,7,15,61
, 121,96,64,0,0,0,0,127,127,0,0,0,0,0,15,63
, 48,96,96,96,96,48,63,15,0,0,24,57,113,99,99,99
, 119,62,28,0,0,0,0,0,0,0,0,0,0,0,255,0
, 0,255,0,0,0,0,0,1,3,7,15,31,31,63,63,62
, 62,62,62,62,62,63,63,31,31,15,15,31,31,63,63,62
, 62,62,62,62,63,63,31,31,15,15,31,31,63,63,62,62
, 62,62,63,63,31,31,31,15,7,7,3,0,0,0,0,0
, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,0
, 0,255,0,0,0,8,8,248,248,8,8,0,192,224,160,224
, 192,0,192,224,32,32,32,0,248,248,32,224,192,0,224,224
, 32,224,192,0,232,232,0,248,248,192,96,32,0,192,224,160
, 224,192,0,224,224,96,32,32,0,0,160,160,224,192,0,224
, 224,96,32,32,0,248,248,32,224,192,0,192,224,160,224,192
, 0,232,232,0,32,248,248,32,0,0,0,0,0,16,152,200
, 72,120,48,0,240,248,8,8,248,240,0,240,248,8,8,248
, 240,0,192,224,176,248,248,128,0,0,0,0,0,0,255,0
, 0,255,0,0,0,0,0,3,3,0,0,0,1,3,2,2
, 2,0,1,3,2,2,2,0,3,3,0,3,3,0,3,3
, 0,3,3,0,3,3,0,3,3,1,3,2,0,1,3,2
, 2,2,0,3,3,0,0,0,0,1,3,2,3,3,0,3
, 3,0,0,0,0,3,3,2,3,1,0,1,3,2,2,2
, 0,3,3,0,0,1,3,2,0,0,0,0,0,3,3,2
, 2,2,2,0,1,3,2,2,3,1,0,1,3,2,2,3
, 1,0,0,0,0,3,3,0,0,0,0,0,0,0,255,0
, 0,255,0,0,0,0,8,56,240,240,56,8,0,112,248,136
, 248,112,0,248,248,8,248,248,0,0,0,0,0,254,254,62
, 248,248,62,254,254,0,64,232,168,248,240,0,248,248,24,8
, 8,0,254,254,112,216,136,0,120,248,128,248,248,0,144,184
, 168,232,72,0,0,0,0,0,254,254,56,108,198,130,0,112
, 248,168,184,176,0,248,248,8,248,248,8,248,240,0,248,248
, 136,248,112,0,8,252,254,10,2,0,0,0,0,0,0,0
, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,0
, 0,127,64,64,64,64,64,64,64,64,64,64,64,64,64,64
, 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64
, 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64
, 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64
, 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64
, 64,64,64,64,64,64,64,64,64,64,64,64,64,64,67,67
, 64,64,64,64,64,64,64,64,64,64,64,64,64,64,64,64
, 64,64,64,64,64,64,64,64,64,64,64,64,64,64,127,0
};

#include <GLCD_CAVR.c>


unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input|ADC_VREF_TYPE;
// start der wandlung
ADCSRA|=0x40;
// warten bis wandler "habe fertig" 
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}

int kennlinie ( int sensor)
{
int y;
	if (sensor>662)y = (sensor-1537)/-0.154;
	else if (sensor>511 & sensor<=662)y = (sensor-1115)/-0.08;	  	
	else if (sensor>355 & sensor<=511)y = (sensor-824)/-0.041;
	else if (sensor>273 & sensor<=355)y = (sensor-598)/-0.021;
	else if (sensor>190 & sensor<=273)y = (sensor-441)/-0.0111;
   	else if (sensor>120 & sensor<=190)y = (sensor-320)/-0.0057;
   	else y =0; 
   	
   	return y;
}

void main(void)
{
// Declare your local variables here
char text_buffer[21],text_buffer2[21];
float sensor_A,sensor_B,x,y;
int i;

// Port A initialization
PORTA=0x00;
DDRA=0x00;

// Port B initialization
PORTB=0xFF;
DDRB=0xFF;

// Port C initialization
PORTC=0xFF;
DDRC=0x00;

// Port D initialization
PORTD=0x00;
DDRD=0xFF;


/*
// Port E initialization
PORTE=0x00;
DDRE=0x00;

// Port F initialization
PORTF=0x00;
DDRF=0x00;
*/
// ADC initialization
// ADC Clock frequency: 125,000 kHz
// ADC Voltage Reference: AREF pin
// ADC High Speed Mode: Off
// ADC Auto Trigger Source: None
ADMUX=ADC_VREF_TYPE;
ADCSRA=0x85;
SFIOR&=0xEF;


LCD_on();
LCD_init();

print_screen();

delay_ms(1000);

paint(4,4,120,0);
paint(5,4,120,0);
paint(6,4,120,0);
paint(7,4,120,64);

while (1)
	{
	sensor_A =0;
	for(i=0;i<100;i++)
		{
		sensor_A += read_adc(0);
		sensor_B += read_adc(1);     	
                }
  	
   	sensor_A /=100;
   	sensor_B /=100;
	
	sensor_A = kennlinie(sensor_A);
   	sensor_B = kennlinie(sensor_B);
   	
   	x = sensor_A * 0.00292;
   	y = sensor_B * 0.00292;
   	
   	if (x<0) x=0;
   	if (y<0) y=0;

      	
      	
      	if (y==0 & x ==0)
      	{
      	sprintf(text_buffer,"Sensor 1: ---    ");
      	sprintf(text_buffer2,"Sensor 2: ---    ");
      	}
      	else if (x==0 & y!=0)
      	{ 
      	sprintf(text_buffer,"Sensor 1: ---    ");
      	sprintf(text_buffer2,"Sensor 2: %.2fcm",y);
      	}
      	else if (y==0 & x !=0)
      	{ 
      	sprintf(text_buffer,"Sensor 1: %.2fcm",x);      	
      	sprintf(text_buffer2,"Sensor 2: ---    ");  
      	}
      	else if (y!=0 & x !=0)
      	{ 
      	sprintf(text_buffer,"Sensor 1: %.2fcm",x);
      	sprintf(text_buffer2,"Sensor 2: %.2fcm",y);
      	} 
      	
      	set_pos(5,5);
      	print_string(text_buffer);
      	set_pos(6,5);
      	print_string(text_buffer2);
      	delay_ms(100);      
      	};
}
