/*******************************************************
 Author:					Manfred Langemann
 mailto:					Manfred.Langemann ät t-online.de
 Begin of project:			04.01.2008
 Latest version generated:	04.01.2008
 Filename:					Main.c
 Description:    			Main routine for testing
 							TWI_Master_main.c
 ********************************************************/
#include <stdio.h>
#include <avr/interrupt.h>
#define F_CPU 16000000
#include "General.h"
#include "RS232.h"
#include "Delay.h"
#include "TWI_Master.h"
#include "lcd.h"
/*
** This main programm demonstrates how to use the 
** implemented TWI master functions. These are:
**	TWIM_Init
**	TWIM_ReadAck
**	TWIM_ReadNack
**	TWIM_Write
**	TWIM_Stop
**
** For testing this program, use the program
** TWI_Slave_main.c in the slave uC and connect the
** two TWI lines properly (don't forget to also
** connect GND between Master and Slave!)
**
** Used uC for Master is ATMega32
*/
int main (void)

	{
	uint8_t		i;
	uint8_t		j=0;
	uint8_t		Data[8];
	uint8_t		SlaveAddress = 15;
/*
** Clear any interrupt
*/
	cli ();
/*
** Wait 1 second for POR
*/
lcd_init(LCD_DISP_ON);
lcd_clrscr();
	Delay_ms (1000);

/*
** Initiate RS232
*/

	RS232_Init ();
	
	lcd_puts ("Hello world...\n");
/*
** Initiate TWI Master Interface with bitrate of 100000 Hz
*/
	if (!TWIM_Init (100000))
		{
		lcd_puts ("Error in initiating TWI interface\n");
		while (1);
		}
/*
** Endless loop
*/
	while (1)
		{

/*
** Read byte(s) from the slave.
** It is implicitely assumed, that the slave will send
** 8 bytes
*/
lcd_clrscr();

	if (!TWIM_Start (SlaveAddress, TWIM_READ))
			{
						TWIM_Stop ();
			lcd_puts("Could not start TWI Bus for READ\n");
	    	}
		else
			{
			for (i=0;i<7;i++)
				{
	        	Data[i] = TWIM_ReadAck ();
				
				
			}
       	Data[7] = TWIM_ReadNack ();
			lcd_puts("Reading Byte ");
			
			TWIM_Stop ();
			Delay_ms (1000);
		
		}
lcd_puts(Data[0]);
lcd_puts(Data[1]);
lcd_puts(Data[2]);
lcd_puts(Data[3]);
lcd_puts(Data[4]);
lcd_puts(Data[5]);
lcd_puts(Data[6]);
lcd_puts(Data[7]);
/*
** Write byte(s) to the slave.
** It is implicitely assumed, that the slave will
** accepts 8 bytes
*/
  	if (!TWIM_Start (SlaveAddress, TWIM_WRITE))
			{
			TWIM_Stop ();
			lcd_puts("Could not start TWI Bus for WRITE\n");
	    	}
		else
			{
			lcd_clrscr();
			for (i=0;i<8;i++)
				{
	        	TWIM_Write (j++);
				lcd_puts("Byte sent:");
				}
			TWIM_Stop ();
			Delay_ms (1000);
			}
/*
** Do something else
*/
		i++;
		}
	return 0;
	}

