I am using ATMEL studio 6 for programming and novice in embedded systems.At present, I have read the theory working of SPI. I understand that I have to set particular bits to one in the control register for defining master or salve mode of operation and clock polarities, phase etc.

Problem:1) How to configure MOSI, clock and select_line as output pin from the master and MISO as the input pin to the master?.

2) Master and slave both are micro controllers,I just need to transfer and receive data to and from slave. So should the master code be dumped in the master mc and slave code should be dumped in slave mc? or both mc's must have the entire code into them?.

3) What are the main factors and key points should consider while SPI programming?.

Any help, suggestion and directions regarding this would be appreciated.Thanks.


1 Answer 1

  1. You use the DDRx register to define the direction of a pin (Data Direction Register). x is the name of the port you are using. Set the relevant bit to 1 for an output, and 0 for an input.

  2. Does your mouse have Windows installed on it? No. The same with microcontrollers. They only need the code on them to perform their own task. Why would the slave need the software that runs on the master?

  3. SPI is an incredibly simple protocol. The main thing to remember with it is it's full duplex and synchronous. If the slave needs to send data to the master then it is up to the master to request that data - the slave can't just send data at will to the master. Making sure the slave can respond to the SS signal fast enough is also important - you don't want to miss the first bit of the first byte coming in.

  • \$\begingroup\$ What about configuring the MOSI,SS and SCK pins as output from master and MISO as input to master?. Should I set these pins using DDRx or there is some other way for this?. Sorry If my question is lame, just filled with soo many doubts. \$\endgroup\$
    – PsychedGuy
    Commented Mar 24, 2015 at 16:06
  • \$\begingroup\$ Please, Can you provide an example concerning to your answer 1 as to how to do it for SPI pins?. \$\endgroup\$
    – PsychedGuy
    Commented Mar 24, 2015 at 17:39
  • 1
    \$\begingroup\$ You do it in the same way as for any pin. You look to see what the pin is called, say B3, then set or clear bit 3 in DDRB \$\endgroup\$
    – Majenko
    Commented Mar 24, 2015 at 18:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.