ki@n
Newbie level 5
Hi Everyone
In an AVR project, I wanted to transfer data between master and slave and meanwhile,show both transmitted and received data on two LCDs.
The Master micro works fine but for slave micro it doesn't show the received data on lcd and I can't really figure out why.
Here are my codes (The master is an atmega16 and slave is an atmega32)
Schematic in Proteus:
I have also attached my LCD libraries
In an AVR project, I wanted to transfer data between master and slave and meanwhile,show both transmitted and received data on two LCDs.
The Master micro works fine but for slave micro it doesn't show the received data on lcd and I can't really figure out why.
Here are my codes (The master is an atmega16 and slave is an atmega32)
C:
//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 SPI_init()
{
DDRB = (1<<DDB7)|(0<<DDB6)|(1<<DDB5)|(1<<DDB4);
PORTB = (1<<PB4);
DDRD = 0xff;
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
}
void SPI_data_write(char data)
{
char flush_buffer;
SPDR = data;
while(!(SPSR & (1<<SPIF)));
flush_buffer = SPDR;
}
char SPI_read_data()
{
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();
SPI_init();
lcd_gotoxy(0,0);
lcd_puts("Master Device");
lcd_gotoxy(0,1);
lcd_puts("Data Sent:");
count = 0;
while (1)
{
SPI_data_write(count);
sprintf(buffer,"%d",count);
lcd_gotoxy(12,1);
lcd_puts(buffer);
count++;
_delay_us(25000);
}
}
C:
#define F_CPU 8000000UL
//-------------------------Libraries-------------------------
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdio.h>
#include <string.h>
#include "lcd/lcd.h"
//-------------------------Functions-------------------------
void SPI_init()
{
DDRB = (0<<DDB7)|(1<<DDB6)|(0<<DDB5)|(0<<DDB4);
DDRD = 0xff;
SPCR = (1<<SPE);
}
char spi_read()
{
while(!(SPSR & (1<<SPIF)));
return(SPDR);
}
//------------------------------------------------------------
int main(void)
{
uint8_t count;
char buffer[5];
lcd_init(LCD_DISP_ON);
SPI_init();
lcd_clrscr();
lcd_gotoxy(0,0);
lcd_puts("Slave Device");
lcd_gotoxy(0,1);
lcd_puts("Rec Data:");
while (1)
{
count = spi_read();
sprintf(buffer,"%d",count);
lcd_gotoxy(10,1);
lcd_puts(buffer);
}
}
Schematic in Proteus:
I have also attached my LCD libraries