Pages: [1]   Go Down
Author Topic: SPI Port Pins  (Read 443 times)
0 Members and 1 Guest are viewing this topic.
Temple, Texas
Offline Offline
Sr. Member
****
Karma: 14
Posts: 354
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to read/write an SD Card.  Am having no luck whatsoever...

But my question is specifically, I have gotten some code from two places which has different assignments for the SPI pins. 
To wit:
From http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1290279801
Code:
// Ports for Arduino Mega //
int PIN_CS = PINB0; // chip select
int PIN_MOSI = PINB2;    // master out slave in
int PIN_MISO = PINB3;    // master in slave out
int PIN_CLOCK = PINB1;   // clock
and http://www.arduino.cc/playground/Code/SDCARD
Code:
// Ports
int PIN_CS = PINB2;      // chip select
int PIN_MOSI = PINB3;    // master out slave in
int PIN_MISO = PINB4;    // master in slave out
int PIN_CLOCK = PINB5;   // clock

Since I am using an Arduino UNO with ATMega328, I should use the second section above, NOT the ones for the Arduino Mega, correct?  I guess I was confused at first when I saw the "Mega" I thought it was correct because I was using an AT"Mega".  But in composing this post I have discovered there is an Arduino Mega.  Ha, I guess I just answered my own question smiley  But please someone tell me that my answer is correct!! smiley

Thanks,
John
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 176
Posts: 12285
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You should use the constants provided by the Arduino environment...

Code:
SS
MOSI
MISO
SCK
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You shouldn't need to refer to the pins at all, excepting the SS pin. I have examples of reading/writing using SPI here. I don't refer to the pins in it. Leave that to the SPI library.

http://www.gammon.com.au/forum/?id=10892

You should be able to use the SdFat library to access the card without too many problems.

The SS pin is declared in:

Code:
#include "pins_arduino.h"
Logged

0
Offline Offline
Shannon Member
****
Karma: 159
Posts: 10409
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Insert Quote
You shouldn't need to refer to the pins at all, excepting the SS pin. I have examples of reading/writing using SPI here. I don't refer to the pins in it. Leave that to the SPI library.

Wrong - you need to explicitly set SCLK and MOSI as outputs - this doesn't happen automatically when in SPI master mode.  In master mode SS is not used/relevant - you can use any pin or pins as the chip selects and you have to set them up by hand.

If you are using slave mode (as when using SPI to control one arduino from another) you have to explicitly set MISO as an output but SS is used automatically by the hardware.
Logged

[ I won't respond to messages, use the forum please ]

Seattle, WA
Offline Offline
God Member
*****
Karma: 7
Posts: 673
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Wrong - you need to explicitly set SCLK and MOSI as outputs - this doesn't happen automatically when in SPI master mode.

Uhhh..  What?

Code:
void SPIClass::begin() {
  // Set direction register for SCK and MOSI pin.
  // MISO pin automatically overrides to INPUT.
  // When the SS pin is set as OUTPUT, it can be used as
  // a general purpose output port (it doesn't influence
  // SPI operations).

  pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(SS, OUTPUT);
Logged


0
Offline Offline
God Member
*****
Karma: 0
Posts: 594
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wrong - you need to explicitly set SCLK and MOSI as outputs - this doesn't happen automatically when in SPI master mode.

Uhhh..  What?

Code:
void SPIClass::begin() {
  // Set direction register for SCK and MOSI pin.
  // MISO pin automatically overrides to INPUT.
  // When the SS pin is set as OUTPUT, it can be used as
  // a general purpose output port (it doesn't influence
  // SPI operations).

  pinMode(SCK, OUTPUT);
  pinMode(MOSI, OUTPUT);
  pinMode(SS, OUTPUT);

Exactly, MarkT is correct, they are explicitly set, he wasn't talking using the library though
Logged

Freelance engineer, consultant, contractor. Graduated from UW in 2013.

Left Coast, CA (USA)
Offline Offline
Brattain Member
*****
Karma: 331
Posts: 16459
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You shouldn't need to refer to the pins at all, excepting the SS pin. I have examples of reading/writing using SPI here. I don't refer to the pins in it. Leave that to the SPI library.

http://www.gammon.com.au/forum/?id=10892

You should be able to use the SdFat library to access the card without too many problems.

The SS pin is declared in:

Code:
#include "pins_arduino.h"


I agree. Here is a simple test program using the hardware SPI library to test out a new 2 channel 12 bit DAC module I recently obtained. Not the total lack of any pinmode commands and that the SS pin name didn't need to be defined, the library does it all.

Code:
// Continous ramping from 0-5vdc analog output on 2 channel DAC module based on MCP4922 13 bit DAC device
// Hardware SPI version
// uses pin 13 (or 52) for SCK, pin 11 (or 51) for MOSI, pin 10 (or 53) for SS
// outputs both channels 0-5vdc in continous loop
// leftyretro 3/31/11

#include <SPI.h>
#include "pins_arduino.h"  // defines hardware SPI pins to use
const char channelA =  0;  // module has two independent D/A outputs, A & B avalible
const char channelB =  1;

void setup() {
 SPI.begin();  // initialize hardware SPI: uses pin 13 (or 52) for SCK and pin 11 (or 51) for MOSI
               // pin 10 (or 53) for slave select, SS
 }
 
void loop()
 {
   for (int i=0; i < 4096; i++)   // DAC 12bit count value range is 0-4095
   {
    dacWrite(channelA, i);
    dacWrite(channelB, i);
   }
 }

//**********************
// MCP4922 Write Data
// *********************

void dacWrite(unsigned char DAC_Channel, unsigned int DAC_Data)  
{
  switch (DAC_Channel)
  {
    case 0x00: DAC_Data |= 0x3000;   //selects channal A, gain = 1, + 12 bit DAC value
  break;
    case 0x01: DAC_Data |= 0xB000;   //selects channel B, gain = 1, + 12 bit DAC value
  break;
  }
  digitalWrite(SS,LOW);  //enable Slave Select  SS is pin 10 (or 53) 
  SPI.transfer(highByte(DAC_Data)); // shift out high 8 bits
  SPI.transfer(lowByte(DAC_Data));  // shift out low 8 bits
  digitalWrite(SS,HIGH); // disable Slave Select, DAC value is transfered and latched in module     
  }   


Lefty
Logged

Pages: [1]   Go Up
Jump to: