Hello,
I'm trying to learn SPI protocol of AVR micro-controller AtMega168
I learned about polling method and this is the code
Slave code
It has a problem,
If I uncomment these two lines from master code,
Slave device won't display anything. I don;t have two Arduino boards so I'm using Proteus to simulate the code. In Proteus Serial terminal of Slave cursor just move forward and nothing displays
Also tried another code ,
Master : https://drive.google.com/drive/folders/1WOsMHF9srpg5Q3Afv7FEGhKyXRjUiMdt
Slave : https://drive.google.com/drive/folders/1wVgHuyIr5WdAD0s5Pny0U2znvkGqpUks
here uncommenting only initUSART() causes write collision error. Commenting out all UART related code solves it
Here's the link to USART library
https://drive.google.com/open?id=1qnzAkkWlIqIBm5IBzyj4gg_jKu8RTv7b
I tried to implement interrupt based data transfer, but not successful. I will add that code once I figured out what's happening here
Thanks
I'm trying to learn SPI protocol of AVR micro-controller AtMega168
I learned about polling method and this is the code
Code:
// this program enables SPI communication and
// Sets the AVR into Master mode
#include <avr/io.h>
#include <util/delay.h>
#include "USART.h"
int main (void)
{
char data = 'A';
//initUSART();
//printString("MASTER\r\n");
DDRB |= (1<<2)|(1<<3)|(1<<5); // SCK, MOSI and SS as outputs
DDRB &= ~(1<<4); // MISO as input
SPCR |= (1<<MSTR); // Set as Master
SPCR |= (1<<SPR0)|(1<<SPR1); // divided clock by 128
SPCR |= (1<<SPE); // Enable SPI
while(1)
{
SPDR = data; // send the data
while(!(SPSR & (1<<SPIF))); // wait until transmission is complete
//transmitByte(SPDR);
//data = SPDR;
_delay_ms(100);
// if you have multiple slaves, this is where you want to switch
}
}
Code:
// this program enables SPI communication and
// Sets the AVR into Slave mode
#include <avr/io.h>
#include <util//delay.h>
#include "USART.h"
int main (void)
{
initUSART();
printString("Slave\r\n");
char data;
DDRB &= ~((1<<2)|(1<<3)|(1<<5)); // SCK, MOSI and SS as inputs
DDRB |= (1<<4); // MISO as output
SPCR &= ~(1<<MSTR); // Set as slave
SPCR |= (1<<SPR0)|(1<<SPR1); // divide clock by 128
SPCR |= (1<<SPE); // Enable SPI
while(1)
{
while(!(SPSR & (1<<SPIF))); // wait until all data is received
data = SPDR; // hurray, we now have our data
transmitByte(data);
//_delay_ms(20);
}
}
If I uncomment these two lines from master code,
Code:
initUSART();
printString("MASTER\r\n");
Also tried another code ,
Master : https://drive.google.com/drive/folders/1WOsMHF9srpg5Q3Afv7FEGhKyXRjUiMdt
Slave : https://drive.google.com/drive/folders/1wVgHuyIr5WdAD0s5Pny0U2znvkGqpUks
here uncommenting only initUSART() causes write collision error. Commenting out all UART related code solves it
Here's the link to USART library
https://drive.google.com/open?id=1qnzAkkWlIqIBm5IBzyj4gg_jKu8RTv7b
I tried to implement interrupt based data transfer, but not successful. I will add that code once I figured out what's happening here
Thanks