I'm trying to send some data from RPi to atmega32 and display it on LCD.
Here's atmega's code ic c:
Code: Select all
#include <mega32.h>
#include <delay.h>
#include <glcd.h>
#include <font5x7.h>
// Declare your global variables here
int sw = 0;
void main(void)
{
unsigned char temp;
GLCDINIT_t glcd_init_data;
glcd_init_data.font=font5x7;
glcd_init(&glcd_init_data);
DDRB=(1<<DDB6); // MISO
PORTD=(1<<PORTD7)|(1<<PORTD6)|(1<<PORTD5)|(1<<PORTD4)|(1<<PORTD3)|(1<<PORTD2)|(1<<PORTD1)|(1<<PORTD0); // Activate pullups
SPCR=(1<<SPIE)|(1<<SPE); // Slave mode , Interrupt enabled , Clock = Fcpu / 4
temp=SPSR;
temp=SPDR;
SPDR=PIND;
#asm("sei");
while(1);
}
interrupt [SPI_STC] void SPI_Serial_Transfer_Complete(void)
{
glcd_outtextxyf(5,40,"GOTCHA!");
delay_ms(10);
glcd_clear();
PORTC=SPDR;
SPDR=PIND;
}
And here's RPi's code in c++:
Code: Select all
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include <fcntl.h>
#include <cstring>
#include <iostream>
using namespace std;
/**********************************************************
Declare Global Variables
***********************************************************/
int fd;
unsigned char hello[] = {'H','e','l','l','o',' ',
'A','r','d','u','i','n','o'};
unsigned char result;
/**********************************************************
Declare Functions
***********************************************************/
int spiTxRx(unsigned char txDat);
/**********************************************************
Main
***********************************************************/
int main (void)
{
fd = open("/dev/spidev0.0", O_RDWR);
unsigned int speed = 250000;
ioctl (fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed);
while (1)
{
for (int i = 0; i < sizeof(hello); i++)
{
result = spiTxRx(hello[i]);
cout << result;
usleep (10);
}
}
return 0;
}
/**********************************************************
spiTxRx
Transmits one byte via the SPI device, and returns one byte
as the result.
Establishes a data structure, spi_ioc_transfer as defined
by spidev.h and loads the various members to pass the data
and configuration parameters to the SPI device via IOCTL
Local variables txDat and rxDat are defined and passed by
reference.
***********************************************************/
int spiTxRx(unsigned char txDat)
{
unsigned char rxDat;
struct spi_ioc_transfer spi;
memset (&spi, 0, sizeof (spi));
spi.tx_buf = (unsigned long)&txDat;
spi.rx_buf = (unsigned long)&rxDat;
spi.len = 1;
ioctl (fd, SPI_IOC_MESSAGE(1), &spi);
return rxDat;
}
Any help would be appreciated.
Thanks in advance.