//Master Code for ATmega16
#define F_CPU 8000000UL
//--------------------Libraries--------------------
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <string.h>
#include "lcd1/lcd.h"
//-------------------Functions-----------------------------------------------------------------
void masterSPI_init()//SPI Ports initialization
{
DDRB = (1<<DDB7)|(0<<DDB6)|(1<<DDB5)|(1<<DDB4);//SCK->Out MISO->In MOSI & SS -> Out
PORTB = (1<<PB4);//SS held high
DDRD = 0xff;//for sending data to LCD
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0); //enable SPI and Master mode,
}
void master_data_write(char data)//witing the data to SPDR
{
SPDR = data;
while(!(SPSR & (1<<SPIF)));//wait untill the data is sent,wihtout this while loop data overrun will occurs
}
char master_read_data()//for storing junk data recived from slave
{
SPDR = 0xff;
while(!(SPSR & (1<<SPIF)));
return(SPDR);
}
//-------------------Functions-----------------------------------------------------------------
int main(void)
{
uint8_t count;
char buffer[5];
lcd_init(LCD_DISP_ON);
lcd_clrscr();//LCD clear
masterSPI_init();
lcd_gotoxy(0,0);
lcd_puts("Master Device");
lcd_gotoxy(0,1);
lcd_puts("Data Sent:");
count = 0;
while (1)
{
master_data_write(count);
sprintf(buffer,"%d",count);//storing int in a string
lcd_gotoxy(12,1);
lcd_puts(buffer);
count++;
_delay_us(25000);
}
}