/* INU_MH.c
 * Created: 17.5.2013 15:17:47
 * Author: Martin
 */ 
#include <avr/io.h>				// vložení knihovny
#include <avr/interrupt.h>
#include <stdio.h>					// zahrnutí základní knihovny pro I/O funkce.
#include <string.h>					// zahrnutí základní knihovny pro práci s řetezci.
#include <stdlib.h>					// zahrnutí knihovny pro převod řetězců na celá císla.
#include <ctype.h>					// zahrnutí knihovny pro vyhodnocování řetězců.
#include "lcd_h.h"
#include "i2cmaster.h"
#include "delay8Mhz.h"

#define F_CPU 8000000UL				// krystal 8MHz
#define USART_BAUDRATE 4800
#define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)							//ul musi byt

#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24
#define CTRL_REG6 0x25
#define CRA_REG_M 0x00
#define CRB_REG_M 0x01
#define MR_REG_M  0x02

#define cervena  0x07
#define zluta	 0x06
#define zelena   0x05
//---------------------------------------------------prefunkce usart-------------------------------------------------
void usart_init();
unsigned int usart_getch();
void USARTWriteChar( unsigned char data);
void USARTWriteString(char *s);
//-------------------------------------------------------------------------------------------------------------------
void nmea_GPRMC(unsigned long a);
void cteni_a_posl(char p);
void ledky(unsigned char b);
void zapis_pamet(unsigned long b,unsigned char *m);
//---------------------------------------------------prefunkce i2c---------------------------------------------------
void setup_IMU();
void send();
void getGyroValues();
void setupL3G20(int scale);
void setupLSM303_A(int scale);
void getAccelValues();
void setupLSM303_M();
void getMagValues();
void writeRegister(char deviceAddress, char address, char val);
unsigned int readRegister(char deviceAddress, char address);
//--------------------------pro pamet-------------------------------------------------
void CE (int hodnota_3);
unsigned char Read(unsigned long Dst);
void Byte_Program(unsigned long Dst, unsigned char byte);
void PametWriteString(unsigned long Dst,char *s);
void WREN();
void EWSR();
void WRSR(unsigned char byte);
void Chip_Erase();
unsigned int SPI_SlaveReceive();
void SPI_MasterTransmit(char cData);
void SPI_MasterInit(void);
void my_delay (int zpozdeni);
//--------------------------------------------------------------------------------
char L3G20_SAD		= 0b11010110; //I2C address of the L3G20
char LSM303_A_SAD	= 0b00110010; //I2C address of the LSM303
char LSM303_M_SAD	= 0b00111100; //I2C address of the LSM303

int x;
int y;
int z;
int t;
char   retez_x[15];
char   retez_y[15];
char   retez_z[15];
char   teplota[10];
//-----------------------------------------------
unsigned char	pam, dat;	//
unsigned long	ad ;			//adresa v pameti
unsigned char	ad_uloz[4];
int a;
//----------------------------------------------- 
unsigned char value,i,lati_value[11],lati_dir, longi_value[12], longi_dir, time[12], altitude[6], stav, rych[10], kurz[10] ;	//altitude neni vyuzita
unsigned char datag[70],data;
unsigned char data_imu_g[40];
unsigned char data_imu_a[40];
unsigned char data_imu_m[40];
//------------------------------------------------------------------------------------------------------------------

unsigned char prepinac;
int main(void)
{
	//------------------------------inicializace------------------------
	
	
	DDRD=0b11100011;
	PORTD=0b11111111;
	setup_IMU();
	//ad = 0;
	cteni_a_posl(0);
	cteni_a_posl(0);
	USARTWriteString("Volba tlacitkem");
	//------------------------------------------------------------------
	while(1)
	{
		while(1)
		{
/*
ošetřit tlačítka
*/
			if ((PIND&0b00000100)==0)			//smazání dat z paměti
			{
				PORTD &= ~(1 << cervena);
				prepinac = 1;
				_delay_ms(1000);
				break;
			}
			if ((PIND&0b00001000)==0)			//zaslání všech dat po seriové lince
			{
				PORTD &= ~(1 << zluta);
				prepinac = 2;
				_delay_ms(1000);
				break;
			}
			if ((PIND&0b00010000)==0)			//start/stop zaznamenávání dat
			{
				PORTD &= ~(1 << zelena);
				PORTD &= ~(1 << cervena);
				prepinac = 3;
				_delay_ms(1000);
				break;
			}
		}		
		 
    switch (prepinac)
	{
	  case 1:										//smazání dat z paměti
				
		
				EWSR();									// mazani pameti
				WRSR(0b00000000);
				WREN();
				Chip_Erase();
		
				_delay_ms(1000);
				USARTWriteString("Smazani pameti probehlo");
				prepinac=0;
				ad = 0x0A;
				ledky(cervena);
				break;
	 
	  case 2:													//zaslání všech dat po seriové lince
	
// 	/*
// 	funkce pro čtení dat z paměti
// 	funkce pro posílání dat přes uart
// 	zakončení
// 	*/
// 	//-----------------------------------nacteni minule posledni adresy z pameti----------- 
// 			ad=0x000100;
// // 			i=0;
// // 			dat = Read(ad);
// // 			while(dat != 0xff)									
// // 			{
// // 				ad=ad+dat;
// // 				ad++;
// // 				dat = Read(ad);
// // 			}
// // 			if (ad == 0xff) ad= 0x0A;
// // 			else ad =	ad + 1;
// 	//----------------------------------------------------------------------------								
// 										
// 			i=0;							
// 			dat = Read(ad);
// 			while(dat != 0xff)									//vypis dat
// 			{
// 				USARTWriteChar(dat);
// 				ad++;
// 				dat = Read(ad);
// 				//napsat podminku pri konci pameti
// 			}
			
			cteni_a_posl(1);
//			USARTWriteString("Konec prenosu dat");
		
			ledky(zluta);

			break;	

		 case 3:			//start/stop zaznamenávání dat
				while((PIND&0b00001000)!=0)
				{
					
		//-----------------------------------nacteni minule posledni adresy z pameti----------- 
// 				ad=0x000003;
// 				i=0;
// 				dat = Read(ad);
// 				while(dat != 0xff)									
// 				{
// 					ad=ad+dat;
// 					ad++;
// 					dat = Read(ad);
// 				}
// 				if (ad == 0xff) ad= 0x0A;
// 				else ad =	ad + 1;
				//ad= 0x0A;
//----------------------------------------------------------------------------
				//funkce pro vyhledání gps věty a rozpitvání
					nmea_GPRMC(ad);
				//funkce pro získání dat z imu

				for(i=0;i<5;i++)
				{
					getGyroValues();									// This will update x, y, and z with new values
					zapis_pamet(ad,data_imu_g);
					getAccelValues();									// This will update x, y, and z with new values
 					zapis_pamet(ad,data_imu_a);
					getMagValues();										// This will update x, y, and z with new values
					zapis_pamet(ad,data_imu_m);
				}								
				}
				//zapsat hodnotu adresz do pameti
				sprintf(ad_uloz, "%i", ad);
				zapis_pamet(0x03,ad_uloz);		
				prepinac = 0;
				PORTD |= (1<<cervena);	// zhasne cervena led
				PORTD |= (1<<zluta);	// zhasne zlutou led
//				USARTWriteString("Konec zapisu");
				ledky(zelena);
				break;						//Po uvolneni tlacitka navrat do volby	
		 
		 
		 default:			// reset
		 
			USARTWriteString("Chybka se vloudila");
			break;
		}
				
	}
    
}

//---------------------------------------------------------------funkce GYRO----------------------------------------------
void setup_IMU(){

	i2c_init();
  usart_init();
  SPI_MasterInit();
  _delay_ms(100);

	 USARTWriteString("starting up L3G4200D");
	setupL3G20(200);									// Configure L3G4200  - 250, 500 or 2000 deg/sec
	setupLSM303_A(4);									// Configure L3G4200  - 2, 4, 8 or 2000 G
	setupLSM303_M();					
	 USARTWriteString("setup done");
	_delay_ms(1500);									//wait for the sensor to be ready 
}
/*//void send(){
	
// 	USARTWriteString("X:");
// 	USARTWriteString(retez_x);
// 	USARTWriteChar(9);
// 	PametWriteString(ad,"X:");
// 	PametWriteString(ad,retez_x);
// 	PametWriteString(ad,9);
	
// 	USARTWriteString("Y:");
// 	USARTWriteString(retez_y);
// 	USARTWriteChar(9);
	
// 	PametWriteString(ad,"Y:");
// 	PametWriteString(ad,retez_y);
// 	PametWriteString(ad,9);
// 	
// 	USARTWriteString("Z:");
// 	USARTWriteString(retez_z);
// 	USARTWriteChar(9);
	
// 	PametWriteString(ad,"Z:");
// 	PametWriteString(ad,retez_z);
// 	PametWriteString(ad,9);
	
// 	USARTWriteString("T:");
// 	USARTWriteString(teplota);
// 	USARTWriteChar(9);
// 	
// 	USARTWriteChar(10);
// 	USARTWriteChar(13);
// 		
// 	PametWriteString(ad,10);
// 	PametWriteString(ad,13);

  //_delay_ms(500);										//Just here to slow down the serial to make it more readable
//}*/
void getGyroValues(){

	char xMSB = readRegister(L3G20_SAD, 0x29);
	char xLSB = readRegister(L3G20_SAD, 0x28);
	x = ((xMSB << 8) | xLSB);

	char yMSB = readRegister(L3G20_SAD, 0x2B);
	char yLSB = readRegister(L3G20_SAD, 0x2A);
	y = ((yMSB << 8) | yLSB);

	char zMSB = readRegister(L3G20_SAD, 0x2D);
	char zLSB = readRegister(L3G20_SAD, 0x2C);
	z = ((zMSB << 8) | zLSB);
	
	char tEMP = readRegister(L3G20_SAD, 0x26);
	t=34-tEMP;
	
  //------------------konverze intu na retezec---------------------
             
	sprintf(retez_x, "%i", x);   
	sprintf(retez_y, "%i", y);   
	sprintf(retez_z, "%i", z);   
    sprintf(teplota, "%i", t);   
	sprintf(data_imu_g, "G:%c%s%c%s%c%s%c%c", 9,retez_x,9,retez_y,9,retez_z,10,13);
}
void setupL3G20(int scale)
{
  writeRegister(L3G20_SAD, CTRL_REG1, 0b00001111);
  writeRegister(L3G20_SAD, CTRL_REG2, 0b00000000);
  writeRegister(L3G20_SAD, CTRL_REG3, 0b00001000);

  if(scale == 250){
    writeRegister(L3G20_SAD, CTRL_REG4, 0b00000000);}
	else if(scale == 500){
    writeRegister(L3G20_SAD, CTRL_REG4, 0b00010000);}
	else{
    writeRegister(L3G20_SAD, CTRL_REG4, 0b00110000);}

  writeRegister(L3G20_SAD, CTRL_REG5, 0b00000000);
}
void writeRegister(char deviceAddress, char address, char val) {
	
	i2c_start_wait(deviceAddress+I2C_WRITE);
	i2c_write(address);
	i2c_write(val);
	i2c_stop();
}
unsigned int readRegister(char deviceAddress, char address){

    unsigned char ret;
	
	i2c_start_wait(deviceAddress+I2C_WRITE);
	i2c_write(address);
	i2c_rep_start(deviceAddress+I2C_READ);
    ret = i2c_readNak();
	i2c_stop();
	
    return ret;
}
//---------------------------------------------------------------funkce akcel a mag----------------------------------------------
void setupLSM303_A(int scale)
{
  writeRegister(LSM303_A_SAD, CTRL_REG1, 0b00110111);	//25Hz
  writeRegister(LSM303_A_SAD, CTRL_REG2, 0b00000000);
  writeRegister(LSM303_A_SAD, CTRL_REG3, 0b00001000);
  
  if(scale == 2){
    writeRegister(LSM303_A_SAD, CTRL_REG4, 0b00000000);}
  else if(scale == 4){
    writeRegister(LSM303_A_SAD, CTRL_REG4, 0b00010000);}
  else if(scale == 8){
    writeRegister(LSM303_A_SAD, CTRL_REG4, 0b00100000);}
   else{
    writeRegister(LSM303_A_SAD, CTRL_REG4, 0b00110000);}
	
  writeRegister(LSM303_A_SAD, CTRL_REG5, 0b00000000);
  writeRegister(LSM303_A_SAD, CTRL_REG6, 0b00000000);
  
}
void getAccelValues(){

	char xMSB = readRegister(LSM303_A_SAD, 0x29);
	char xLSB = readRegister(LSM303_A_SAD, 0x28);
	x = ((xMSB << 8) | xLSB);

	char yMSB = readRegister(LSM303_A_SAD, 0x2B);
	char yLSB = readRegister(LSM303_A_SAD, 0x2A);
	y = ((yMSB << 8) | yLSB);

	char zMSB = readRegister(LSM303_A_SAD, 0x2D);
	char zLSB = readRegister(LSM303_A_SAD, 0x2C);
	z = ((zMSB << 8) | zLSB);
	
	//teplota = "neni";
	
//------------------konverze intu na retezec---------------------
             
	sprintf(retez_x, "%i", x);   
	sprintf(retez_y, "%i", y);   
	sprintf(retez_z, "%i", z);
	sprintf(data_imu_a, "A:%c%s%c%s%c%s%c%c", 9,retez_x,9,retez_y,9,retez_z,10,13);       
}
void setupLSM303_M()
{
  writeRegister(LSM303_M_SAD, CRA_REG_M, 0b10010100);		//teplotni senzor zapnut, 00, 30Hz, 00

  writeRegister(LSM303_M_SAD, CRB_REG_M, 0b10000000);		// 4.0 Gauss

  writeRegister(LSM303_M_SAD, MR_REG_M, 0b00000000);
}
void getMagValues(){

	char xMSB = readRegister(LSM303_M_SAD, 0x03);
	char xLSB = readRegister(LSM303_M_SAD, 0x04);
	x = ((xMSB << 8) | xLSB);

	char yMSB = readRegister(LSM303_M_SAD, 0x07);
	char yLSB = readRegister(LSM303_M_SAD, 0x08);
	y = ((yMSB << 8) | yLSB);

	char zMSB = readRegister(LSM303_M_SAD, 0x05);
	char zLSB = readRegister(LSM303_M_SAD, 0x06);
	z = ((zMSB << 8) | zLSB);
	
	char tMSB = readRegister(LSM303_M_SAD, 0x31);
	char tLSB = readRegister(LSM303_M_SAD, 0x32);
	t = ((tMSB << 8) | tLSB);
	
	
  //------------------konverze intu na retezec---------------------
             
	sprintf(retez_x, "%i", x);   
	sprintf(retez_y, "%i", y);   
	sprintf(retez_z, "%i", z);   
    sprintf(teplota, "%i", t);   
	sprintf(data_imu_m, "M:%c%s%c%s%c%s%c%c", 9,retez_x,9,retez_y,9,retez_z,10,13);
}
//---------------------------------------------------------------funkce usart--------------------------------------------------------------	
void usart_init()
{
	UCSRB |= (1<<RXCIE) | (1 << RXEN) | (1 << TXEN);		// Turn on the transmission and reception circuitry
	UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);	// Use 8-bit character sizes
	UBRRL = BAUD_PRESCALE;									// Load lower 8-bits of the baud rate value into the low byte of the UBRR register
	UBRRH = (BAUD_PRESCALE >> 8);							// Load upper 8-bits of the baud rate value into the high byte of the UBRR register
}
unsigned int usart_getch()
{
	while ((UCSRA & (1 << RXC)) == 0);						// Do nothing until data have been recieved and is ready to be read from UDR
	return(UDR);											// return the byte
}
void USARTWriteChar( unsigned char data)
{
while(!(UCSRA & (1<<UDRE)));								// počká, dokud vysálač není připraven
	UCSRA|= (1<<TXC);										// vynuluje bit TxC
	UDR= data;												// zapíše data do buffru USARTu
}			
void USARTWriteString(char *s)								//posle po usartu retezec
{
	while (*s)
	USARTWriteChar(*s++);
}	
//----------------------------------------------------------------end usart----------------------------------------------------------
//---------------------------------------------------------------SPI pamet----------------------------------------------------------
void CE (int hodnota_3)				// Volba CE / SS
{
	if (hodnota_3 == 1)
	{
		PORTB  = 1 << PB4;
		//_delay_ms(0);
	}
				
	if (hodnota_3 == 0)
	{
		PORTB  = 0 << PB4;
		//_delay_ms(0);
	}
}
unsigned char Read(unsigned long Dst) 
{
	unsigned char byte = 0;	

	CE(0);			/* enable device */
	SPI_MasterTransmit(0x03); 		/* read command */
	SPI_MasterTransmit(((Dst & 0xFFFFFF) >> 16));	/* send 3 address bytes */
	SPI_MasterTransmit(((Dst & 0xFFFF) >> 8));
	SPI_MasterTransmit(Dst & 0xFF);
	byte = SPI_SlaveReceive();
	CE(1);			/* disable device */
	return byte;			/* return one byte read */
}
void Byte_Program(unsigned long Dst, unsigned char byte)
{
	CE(0);											/* enable device */
	SPI_MasterTransmit(0x02); 						/* send Byte Program command */
	SPI_MasterTransmit(((Dst & 0xFFFFFF) >> 16));	/* send 3 address bytes */
	SPI_MasterTransmit(((Dst & 0xFFFF) >> 8));
	SPI_MasterTransmit(Dst & 0xFF);
										
	SPI_MasterTransmit(byte);						/* send byte to be programmed */
	CE(1);											/* disable device */
}
unsigned char Read_Status_Register()
{
	unsigned char byte = 0;
	CE(0);			/* enable device */
	SPI_MasterTransmit(0x05);		/* send RDSR command */
	byte = SPI_SlaveReceive();		/* receive byte */
	CE(1);			/* disable device */
	return byte;
}
void WREN()
{
	CE(0);			/* enable device */
	SPI_MasterTransmit(0x06);		/* send WREN command */
	//SPI_MasterTransmit(0x04);
	CE(1);			/* disable device */
}
void EWSR()
{
	CE(0);			/* enable device */
	SPI_MasterTransmit(0x50);		/* enable writing to the status register */
	CE(1);			/* disable device */
}
void WRSR(unsigned char byte)
{
	CE(0);			/* enable device */
	SPI_MasterTransmit(0x01);		/* select write to status register */
	SPI_MasterTransmit(byte);		/* data that will change the status of BPx 
					   or BPL (only bits 2,3,4,5,7 can be written) */
	CE(1);			/* disable the device */
}
void Chip_Erase()
{						
	CE(0);				/* enable device */
	SPI_MasterTransmit(0x60);
	_delay_ms(100);			/* send Chip Erase command (60h or C7h) */
	CE(1);				/* disable device */
}
//-----------------------------------------------SPI atmega16 ---------------------------------
void SPI_MasterInit(void)
{
	/* Set MOSI and SCK output, all others input */
	DDRB = (1<<DDB5)|(1<<DDB7)|(1<<DDB4)|(0<<DDB6);
	/* Enable SPI,MSB first, Master, SPI mode 0, set clock rate fck/4 */
	SPCR = (1<<SPE)|(0<<DORD)|(1<<MSTR)|(1<<CPOL)|(1<<CPHA)|(1<<SPR1)|(1<<SPR0);
}
unsigned int SPI_SlaveReceive()
{
	/* Wait for reception complete */
	SPDR = 0xFF;
	while(!(SPSR & (1<<SPIF)));
	/* Return data register */
	return (SPDR);
}
void SPI_MasterTransmit(char cData)
{
	/* Start transmission */
	SPDR = cData;
	/* Wait for transmission complete */
	while(!(SPSR & (1<<SPIF)));
	cData=SPDR;
}
//----------------------------------------------------------------------------------------------------
void nmea_GPRMC(unsigned long a)
{	
	while(1){
		PORTD |= (1<<zelena);

		value=usart_getch();
		if(value=='$')
		{
			value=usart_getch();
			if(value=='G')
			{
				value=usart_getch();
				if(value=='P')
				{
					value=usart_getch();
					if(value=='R')
					{
						value=usart_getch();
						if(value=='M')
						{
							value=usart_getch();
							if(value=='C')
							{
								value=usart_getch();
								if(value==',')
								{
									while(value!=',')											//osetrujici cyklus
									{
										value=usart_getch();
									}
//-------------------------------------------------------------									
									time[0]=usart_getch();										//cteni casu (hhmmss.nnn)
									value=time[0];
									for(i=1;value!=',';i++)
									{
										time[i]=usart_getch();
										value=time[i];
									}
									time[i-1]=9;
//-------------------------------------------------------------																			
									while(value!=',')											//osetrujici cyklus
 									{
										value=usart_getch();
 									}
//-------------------------------------------------------------										
									stav=usart_getch();											// stav (A - ok, V varovani)
								//	if (stav == 'A') PORTD &= ~(1 << cervena);						// jestli je stav A, tak cervena LED zhasne
											
//-------------------------------------------------------------	
									value=stav;
									while(value!=',')											//osetrujici cyklus
									{
										value=usart_getch();
									}
//-------------------------------------------------------------										
									lati_value[0]=usart_getch();								//cteni zemepisne sirky (ssmm.mmmm)
									value=lati_value[0];
									for(i=1;value!=',';i++)
									{
										lati_value[i]=usart_getch();
										value=lati_value[i];
									}
									lati_value[i-1]=9;
									lati_dir=usart_getch();										//cteni polokoule (N - severni, S - jizni)
//-------------------------------------------------------------	
									value=usart_getch();
									while(value!=',')											//osetrujici cyklus
									{
										value=usart_getch();							
									}
//-------------------------------------------------------------										
									longi_value[0]=usart_getch();								//cteni zemepisne delky (sssmm.mmmm)
									value=longi_value[0];
									for(i=1;value!=',';i++)
									{
										longi_value[i]=usart_getch();
										value=longi_value[i];
									}
									longi_value[i-1]=9;
									longi_dir=usart_getch();									//cteni polokoule (E - vychodni, W - yapadni)
									value=usart_getch();									
//-------------------------------------------------------------	
									while(value!=',')											//osetrujici cyklus
									{
										value=usart_getch();							
									}
//-------------------------------------------------------------										
									rych[0]=usart_getch();										//cteni rychlosti v horizontalni rovine (x.xx)
									value=rych[0];
									for(i=1;value!=',';i++)
									{
										rych[i]=usart_getch();
										value=rych[i];
									}
									rych[i-1]=9;
//-------------------------------------------------------------																					
									while(value!=',')											//osetrujici cyklus
									{
										value=usart_getch();							
									}
//-------------------------------------------------------------										
									kurz[0]=usart_getch();										//cteni kurzu pohybu (xx.xx)
									value=kurz[0];
									for(i=1;value!=',';i++)
									{
										kurz[i]=usart_getch();
										value=kurz[i];
									}
									kurz[i-1]=9;
//-------------------------------------------------------------									
																					//rozsviti led 
									PORTD &= ~(1 << zelena);
//-------------------------------------------Vysilani dat po uartu--------------------------
									if (stav == 'A')
									{
 										sprintf(datag, "GPS:%c%s%c%s%c%c%c%s%c%c%c%s%c%s%c%c",9,time,9,lati_value,9,lati_dir,9,longi_value,9,longi_dir,9,rych,9,kurz,10,13);				//zapsani vsech parametru do datag //10 = LF: Line Feed (posun o 1 řádek dolů), 13 = CR: Carriage Return (návrat tiskové hlavičky na začátek) 
										PORTD |= (1<<cervena);	// zhasne cervena led
										PORTD |= (1<<zluta);	// zhasne zlutou led
									}
									else
									{
										sprintf(datag, "GPS:%c%s%c%c",9,time,10,13);
										PORTD &= ~(1 << zluta);
									}	
								
//---------------------------------------------Pamet zapis-----------------------------------------
									zapis_pamet(a,datag);

									break;
								}
							}
						}
					}
				}
			}
		}
	}

	return (ad);
}
void cteni_a_posl(char p)				// precte honoty ulozene na pameti od adresy "a" do bunky s hodnotou 0xff, hodnotu odesle po uartu
{				
	
		unsigned long a=0x00000A;																					
		unsigned char dat = Read(a);
		while(dat != 0xff)									//vypis kurzu
		{
			if (p == 1)USARTWriteChar(dat);
			a++;
			dat = Read(a);
		}
	ad = a;						// vrati hodnotu prvni volne adresy pro dalsi zapis
	return(ad);
}
void ledky(unsigned char b)
{	
	for (i=0;i<4;i++)
	{
		PORTD &= ~(1 << b);
		_delay_ms(250);
		PORTD |= (1<<b);
		_delay_ms(250);
	}						
}		
void zapis_pamet(unsigned long b,unsigned char *m)
{
	//i=0;
	while (*m /*= '\0'*/)								
	{	
		EWSR();							
		WRSR(0b00000000);
		WREN();									// zapis na adresu z 0x0 - 0x3fffff jeden bajt
		Byte_Program(b, *m++);
		/*i++;*/
		b++;
	}
/*	USARTWriteString("zaznam na pamet");*/	
	ad = b;						// vrati hodnotu prvni volne adresy pro dalsi zapis
	return(ad);
}	
																																												