shirko20
Member level 2
hello,i wanted to interface 2 atmega8 ic through SPI and i wrote theise code as Master and Slave but when i execute the code in circuit in proteus app it wrote "Testing" in lcd but then nothing and the transfer cant complete wnd the code freezing in the code line:
AND NOTHING.. why the transfer cant be completed also in proteus i connected two ic's pin as below:
MASTER IC | SLAVE IC
MOSI -----> MOSI
MISO -----> MISO
CLK -----> CLK
SS(N-C) SS----> GROUNDED
here is MASTER code:
and here is SLAVE code:
Code:
while(! (SPSR &(1<<SPIF) ));
MASTER IC | SLAVE IC
MOSI -----> MOSI
MISO -----> MISO
CLK -----> CLK
SS(N-C) SS----> GROUNDED
here is MASTER code:
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 /* * SPI_master_slave.c * * Created: 26/11/2015 20:42:51 * Author: Sherko */ #include <avr/io.h> #include <util/delay.h> #include <avr/interrupt.h> #define VCH 0x7b void spi_init_master(void) { SPCR = (1<<SPE) | (1<< SPIE ) | (1<<MSTR) | (1<<SPR0); //DEFINE MOSI AND CLK as output pins DDRB = (1<<DDB3)|(1<<DDB5); sei(); } // ISR(SPI_STC_vect) { } //Function to send and receive data unsigned char spi_tranceiver(unsigned char data) { //set data in buffer of spi SPDR=data; //wait until transaction is complete while ( !(SPSR & (1<<SPIF) )); return(SPDR); } int main(void) { spi_init_master(); unsigned char data; uint16_t x; x=0; DDRC = (1<<DDC0); while(1) { data=spi_tranceiver(++x); if (data==VCH) { PORTC=(1<<PC0); _delay_ms(100); PORTC=0X00; _delay_ms(100); } } }
and here is SLAVE code:
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 /* * SPI_slave_lcd.c * * Created: 26/11/2015 21:09:34 * Author: Sherko */ #include <avr/io.h> #include <util/delay.h> #include <avr/interrupt.h> #include "lcd.h" #define VCH 0x7b void spi_init_slave(void) { SPCR = (1<<SPE) | (1<<SPIE) ; //DEFINE MISO as output pins DDRB = (1<<DDB4) ; sei(); } // ISR(SPI_STC_vect) { } //Function to send and receive data unsigned char spi_tranceiver(unsigned char data) { //set data in buffer of spi SPDR=data; //wait until transaction is complete while ( !(SPSR & (1<<SPIF) )); return(SPDR); } int main(void) { lcd_init(LCD_DISP_ON_CURSOR_BLINK); //Initialize LCD spi_init_slave(); //Initialize slave SPI unsigned char data, buffer[10]; while(1) { lcd_clrscr(); //LCD Clear screen lcd_home(); //LCD move cursor to home lcd_puts("Testing"); lcd_gotoxy(0,1); data = spi_tranceiver(VCH); //Receive data, send ACK itoa(data, buffer, 10); //Convert integer into string lcd_puts(buffer); //Display received data _delay_ms(20); //Wait } }
Last edited: