1. This forum section is a read-only archive which contains old newsgroup posts. If you wish to post a query, please do so in one of our main forum sections (here). This way you will get a faster, better response from the members on Motherboard Point.

SPI slave on ATmega162

Discussion in 'Embedded' started by Roman Mashak, Nov 25, 2004.

  1. Roman Mashak

    Roman Mashak Guest

    Hello.

    I'm studying mega162 chip and trying to implement SPI slave capability. I
    wanna send 8 bytes to master side and after reaching last byte get back and
    send again, ie continuosly.

    Later I'd like to implement receiving.

    So, I made the following simple code:

    #include <io.h>
    #include <interrupt.h>
    #include <sig-avr.h>

    char *data = "12345678"+0x00;
    char *t;

    void SPI_SlaveInit(void)
    {
    DDRB = (1 << PB4) | (1 << PB5) | (1 << PB7);
    SPCR = (1 << SPE);
    PORTB |= (1 << PB6);
    }

    int main(void)
    {
    sei();
    SPI_SlaveInit();

    t = data;
    while (1)
    {
    if (*data != 0) {
    SPDR = data;
    data++;
    }
    else data = t;
    }

    return 1;
    }

    According to oscilloscope SCK, SS and MOSI signals (I get from master side)
    are fine, but MISO is some garbage, so it seems I'm not sending any real
    data... While debugging in studio, I can see that SPDR is filled with my
    data, byte by byte, but how can I check this data are moved on to the shift
    register? I suppose only interrupt call...

    I'm using AVR studio and WinAVR compiler.

    Thanks in advance for any help.


    With best regards, Roman Mashak. E-mail:
     
    Roman Mashak, Nov 25, 2004
    #1
    1. Advertisements

  2. Roman Mashak

    Jan leithe Guest

    I haven't studied all your code, but I found two major errors:
    - You have to wait for the previous byte to be sent before you write a new
    value to SPDR. Something like this:
    - You should write whatever data points at to SPDR, not the pointer value
    itself.

    while (1) {
    if (SPSR & (1 <<SPIF)) {
    if (*data == 0) {
    data = t;
    }
    SPDR = *data;
    data++
    }
    }

    -Jan Leithe
     
    Jan leithe, Nov 25, 2004
    #2
    1. Advertisements

  3. Roman Mashak

    Roman Mashak Guest

    Hello, Jan!
    You wrote on Thu, 25 Nov 2004 09:01:04 +0100:

    Jl> I haven't studied all your code, but I found two major errors:
    Jl> - You have to wait for the previous byte to be sent before you write a
    Jl> new value to SPDR. Something like this:
    Jl> - You should write whatever data points at to SPDR, not the pointer
    Jl> value itself.

    Jl> while (1) {
    Jl> if (SPSR & (1 <<SPIF)) {
    Jl> if (*data == 0) {
    Jl> data = t;
    Jl> }
    Jl> SPDR = *data;
    Jl> data++
    Jl> }
    Jl> }
    Thank you for help with C, but unfortunately problems still remains. I
    suppose, it's hardware related...

    With best regards, Roman Mashak. E-mail:
     
    Roman Mashak, Nov 25, 2004
    #3
  4. Roman Mashak

    Roman Mashak Guest

    Hello, Jan!
    You wrote on Thu, 25 Nov 2004 09:01:04 +0100:

    Jl> I haven't studied all your code, but I found two major errors:
    Jl> - You have to wait for the previous byte to be sent before you write a
    Jl> new value to SPDR. Something like this:
    Jl> - You should write whatever data points at to SPDR, not the pointer
    Jl> value itself.
    By the way, while debugging this code in AVR studio (after modification
    according to your notes) I noticed that data put into SPDR register are
    strange. Originally I'm sending string "12345678", but character codes
    stored into SPDR are 0x31, 0x32, 0x33 etc.

    Why ?
    Jl> while (1) {
    Jl> if (SPSR & (1 <<SPIF)) {
    Jl> if (*data == 0) {
    Jl> data = t;
    Jl> }
    Jl> SPDR = *data;
    Jl> data++
    Jl> }
    Jl> }


    With best regards, Roman Mashak. E-mail:
     
    Roman Mashak, Nov 25, 2004
    #4
  5. That is correct. A 0x31 is the hexadecimal ASCII code for character '1',
    0x32 for a '2' and so on....

    Meindert
     
    Meindert Sprang, Nov 25, 2004
    #5
  6. Roman Mashak

    Roman Mashak Guest

    Hello, Meindert!
    You wrote on Thu, 25 Nov 2004 12:27:56 +0100:

    ??>> By the way, while debugging this code in AVR studio (after
    ??>> modification according to your notes) I noticed that data put into
    ??>> SPDR register are strange. Originally I'm sending string "12345678",
    ??>> but character codes stored into SPDR are 0x31, 0x32, 0x33 etc.

    MS> That is correct. A 0x31 is the hexadecimal ASCII code for character
    MS> '1', 0x32 for a '2' and so on....
    oh,, sorry for my stupidity :)

    Anyway, I still don't get output signal looking at oscilloscope. I believe,
    I should make on slave side some checking of SPI is ready, SS checking or
    similar?

    With best regards, Roman Mashak. E-mail:
     
    Roman Mashak, Nov 25, 2004
    #6
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.