How to use Atmega128a-au controller SPI Master

Go To Last Post
21 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

 

we are very new to this Atmel Controller, We are trying to integrate DAC through SPI Interface using PORTB0 -> Slave Select, PORTB1 -> Clock, PORTB2 -> MOSI, PORTB3 -> MISO.

 

Configured PORTB - 0,1,2 pins as Output and 3 as input pin.

 

When we are configuring PORTB1 as output then it's getting exit from debug mode and getting reset continuously. Clock is not getting generated.

 

Fuse bits configured as SPI Enabled Atmega103 compatible option disable.

 

In any case SPI Clock is not getting generated.

 

Can any one please help us how to configure SPI as master for Atmega128a.

SPCR=(0<<SPIE) | (1<<SPE) | (0<<DORD) | (1<<MSTR) | (0<<CPOL) | (0<<CPHA) | (0<<SPR1) | (0<<SPR0);
SPSR=(1<<SPI2X);
 

DDRB=(0<<DDB7) | (0<<DDB6) | (0<<DDB5) | (0<<DDB4) | (0<<DDB3) | (1<<DDB2) | (1<<DDB1) | (1<<DDB0);
PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (0<<PORTB1) | (0<<PORTB0);
 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Welcome to AVRFreaks!

 

Post a small complete program that demos the problem using the "<>" code tag in the edit menu (copy/paste your code) and a freak will be able to help you.

A common gottcha with SPI is not setting the CS pin as an output, and having separate input/output functions, you only need one as comm is bi-directional at the same time.

 

Jim

 

 

Keys to wealth:

Invest for cash flow, not capital gains!

Wealth is attracted, not chased! 

Income is proportional to how many you serve!

Lets go Brandon!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

 

 

Welcome to AVRFreaks

 

veeru@styraxinstruments.com wrote:
we are very new to this Atmel Controller

Do you have experience with any other microcontroller(s) ?

 

Check out the Product Page on the Microchip website - lots of Application Notes, etc, to help you there; eg,

 

 

See: https://www.avrfreaks.net/commen...

and: https://www.avrfreaks.net/commen...

 

There are also code example in the datasheet:

 

https://ww1.microchip.com/downlo...

 

ki0bk wrote:
Post a small complete program that demos the problem using the "<>" code tag in the edit menu

+1

 

See Tip #1 in my signature, below, for illustrated instructions on how to post source code:

 

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Mon. Oct 11, 2021 - 01:23 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

The classic "gotcha" on SPI for old model AVrs is that even when you do:

SPCR=(1<<SPE) | (1<<MSTR);

the MSTR" bit will be switched off again if the SS pin is input and pulled low.

 

EDIT: OK so downloaded mega128 datasheet now and SS is PB0 which is being set as output in DDRB so it's probably not the "gotcha" then.

Last Edited: Mon. Oct 11, 2021 - 01:32 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

<

DDRB=(0<<DDB7) | (0<<DDB6) | (1<<DDB5) | (1<<DDB4) | (0<<DDB3) | (1<<DDB2) | (1<<DDB1) | (1<<DDB0);
PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (0<<PORTB1) | (0<<PORTB0);
 

SPCR=(0<<SPIE) | (1<<SPE) | (0<<DORD) | (1<<MSTR) | (0<<CPOL) | (0<<CPHA) | (0<<SPR1) | (1<<SPR0);
SPSR=(1<<SPI2X);

>

 

Please find the attached fuse bits setting for your kind reference.

 

When DDRB1 configured as output mode, then program getting exit from debug and no clock getting generated

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
DDRB=(0<<DDB7) | (0<<DDB6) | (1<<DDB5) | (1<<DDB4) | (0<<DDB3) | (1<<DDB2) | (1<<DDB1) | (1<<DDB0);
PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (0<<PORTB1) | (0<<PORTB0);


SPCR=(0<<SPIE) | (1<<SPE) | (0<<DORD) | (1<<MSTR) | (0<<CPOL) | (0<<CPHA) | (0<<SPR1) | (1<<SPR0);
SPSR=(1<<SPI2X);

 

Please find the attached fuse bits setting for your kind reference.

 

When DDRB1 configured as output mode, then program getting exit from debug and no clock getting generated

 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please find the attached screen shot while debug exits

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


You didn't see the bit of Tip #1 which also shows how to put the pictures in your post?

 

Like this:

 

 

Also, as Jim said in #2, please post a minimum but complete program which demonstrates the problem

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

As advised earlier post the shortest compilable program that shows your problem. Probably something like:

#include <stdlib.h>
#include <avr/io.h>

int main(void)
{
    DDRB = (1 << 2) | (1 << 1) | (1 << 0); // MOSI , SCK and SS to output
    PORTB = (1 << 0); // make sure SS is driven high (no switch out of MSTR mode)

    SPCR = (1 << SPE) | (1 << MSTR);
    while (1)
    {
        SPDR = rand();
        while (!(SPSR & (1 << SPIF)));
        (void) SPDR; // dummy read to clear SPIF bit
    }
}

then show what the scope sees on the SCK and MOSI lines.

Last Edited: Mon. Oct 11, 2021 - 02:41 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please find the attached MOSI and Clock signals for your kind reference.

 

SPI Interface is not stable, sometimes it's working as required and some times there is no response.

 

During running itself it's getting failed.

 

When SPI enabled rest of the functionalities are not working.

 

After disable SPI only rest of interrupts and other functiins getting executed.

 

Please help us how to fix this issue.

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not sure what you mean by "not stable" those signals look solid and perfectly formed. What were you expecting? Are you maybe using the wrong mode (polarity and/or phase)? 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

veeru@styraxinstruments.com wrote:
After disable SPI only rest of interrupts and other functiins getting executed.

Sounds like improper use of ISR()'s, can you post a zip of your project directory?

Jim

 

 

Keys to wealth:

Invest for cash flow, not capital gains!

Wealth is attracted, not chased! 

Income is proportional to how many you serve!

Lets go Brandon!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

veeru@styraxinstruments.com wrote:

Please find the attached MOSI and Clock signals for your kind reference.

 

You've got a dual trace (at least) scope so why not show an image of clock and MOSI on the same screen?

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please find the attached project files for your kind reference.

 

Individual timer interrupt is working fine, when ever PORTB1 configured as output then no code is running. 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please find the attached project files for your kind reference.

 

Individual timer interrupt is working fine, when ever PORTB1 configured as output then no code is running. 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why would you do something ridiculous and only show one wave???...the spi needs a chip select, clock, and data at the proper time with respect to each other

 

Resend with a photo of all 3 on the screen, so you can really tell what is happening

 

Why do you say clock is not generated, when one of your photos clearly shows it??

 

then it's getting exit from debug mode and getting reset continuously. 

Why are you going into a debug mode? You just need to write the value you wish to send to the SPI register & see it coming out the pins

 

You should not even have an ISR at all right now, just use SPI to get the DAC outputting your couple of test values and working.  AFTER that works you can do other things with it (like ISR). 

Who told you to set up an ISR already?

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Wed. Oct 13, 2021 - 05:01 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please find the attachment for your reference 

 

ISR disabled 

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

then no code is running. 

Unless the chip is in reset, some code is always running...what do you mean.

 

Why didn't you ever say whether the DAC is outputting correct voltages or not??   What test values have you tried?  Try several values,  Then send a count & you should see a ramp.

You don't need to mess with ISR at all until the DAC & SPI communicates perfectly.

Do you need an ISR?  What for?  

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Surely, your scope has a 'screen capture' facility? That would give a far clearer picture than a photo of the screen.

 

Please see Tip #1 in my signature, below, for how to put the pictures into the post - where we can see them:

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


 

Oh boy you must have good eyesight!...

 

 

Does this scope have a timebase/zoom control perhaps??

 

Or were you using the zoomed out view to say that your actual "issue" is this in the second attachment ?

 

Last Edited: Wed. Oct 13, 2021 - 08:44 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thankyou for the Support