+ Post New Thread
Results 1 to 7 of 7
  1. #1
    Member level 2
    Points: 318, Level: 3
    shirko20's Avatar
    Join Date
    Sep 2013
    Posts
    44
    Helped
    1 / 1
    Points
    318
    Level
    3

    WHERE is my error in this code SPI AVR Atmega8

    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<
    
    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 by shirko20; 26th November 2015 at 20:36.

    •   AltAdvertisment

        
       

  2. #2
    Advanced Member level 5
    Points: 13,595, Level: 28
    nandhu015's Avatar
    Join Date
    Feb 2006
    Location
    India
    Posts
    1,961
    Helped
    298 / 298
    Points
    13,595
    Level
    28
    Blog Entries
    18

    Re: WHERE is my error in this code SPI AVR Atmega8

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



    •   AltAdvertisment

        
       

  3. #3
    Member level 2
    Points: 318, Level: 3
    shirko20's Avatar
    Join Date
    Sep 2013
    Posts
    44
    Helped
    1 / 1
    Points
    318
    Level
    3

    Re: WHERE is my error in this code SPI AVR Atmega8

    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?



    •   AltAdvertisment

        
       

  4. #4
    Member level 2
    Points: 318, Level: 3
    shirko20's Avatar
    Join Date
    Sep 2013
    Posts
    44
    Helped
    1 / 1
    Points
    318
    Level
    3

    Re: WHERE is my error in this code SPI AVR Atmega8

    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<
    - - - 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;



  5. #5
    Member level 2
    Points: 318, Level: 3
    shirko20's Avatar
    Join Date
    Sep 2013
    Posts
    44
    Helped
    1 / 1
    Points
    318
    Level
    3

    Re: WHERE is my error in this code SPI AVR Atmega8

    No one gonna help meeeeeeeeeeeeee please



  6. #6
    Member level 2
    Points: 318, Level: 3
    shirko20's Avatar
    Join Date
    Sep 2013
    Posts
    44
    Helped
    1 / 1
    Points
    318
    Level
    3

    Re: WHERE is my error in this code SPI AVR Atmega8

    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 :)


    1 members found this post helpful.

    •   AltAdvertisment

        
       

  7. #7
    Super Moderator
    Points: 61,971, Level: 60
    Achievements:
    7 years registered
    Awards:
    Most Frequent Poster 3rd Helpful Member

    Join Date
    Apr 2014
    Posts
    12,629
    Helped
    2929 / 2929
    Points
    61,971
    Level
    60

    Re: WHERE is my error in this code SPI AVR Atmega8

    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


    1 members found this post helpful.

--[[ ]]--