Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[AVR] WHERE is my error in this code SPI AVR Atmega8

Status
Not open for further replies.

shirko20

Member level 2
Joined
Sep 29, 2013
Messages
44
Helped
1
Reputation
2
Reaction score
0
Trophy points
6
Activity points
381
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:

Code:
while(! (SPSR &(1<<SPIF) ));
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:

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:

MOSI of master should be connected to MISO of slave and viceversa
 

i tried changing port to what you mention but still no data transfer and even now the code is fozen in the code line i said befor?
 

i found out that the freeze in code line is relative to the interrupt of SPI where i set SPIE bit to 1,and when i disable SPI interrupt and clear SPIE bit then anything gotworking, but why while i enabled global interrupt and i wrote SPI corresponding ISR why SPIF will not be cleared?
and never this line of code be truth?
while(! (SPSR & (1<<SPIF) ));

- - - Updated - - -

hey nandhu015 you were wrong about the connection,the truth is that what i did ,and MOSI must connect to MOSI and MISO must be connected to MISO;
 

No one gonna help meeeeeeeeeeeeee please
 

i found:the whole code on site si wrong .i did write this code according of some sites that i dont want to name but they teach me wrong,error was because of the function where we set it in the Slave .the corresponding function (sending and receiving Function)must be set only in Master and there is no need to set it inside Slave and for sending data through Slave to Maste rjust set the value of SPDR to the data you want to send like this:SPDR=0X09; and receiving function will be done automatically from Slave;hhhhh i am detectin :)
 

    V

    Points: 2
    Helpful Answer Positive Rating
Hi,

MOSI of master should be connected to MISO of slave and viceversa

I don't think so. MOSI means master out slave in.
It is not compareable with RxD and TxD.

Klaus
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top