Problem setting the baud rate [RESOLVED]

Hi,

I am new to Arduino, and am trying to use an Arduino Uno to send serial commands--so output only. My problem is that I want to use optoisolators, and the Arduino seems to send the serial commands at too high a speed. I know I should change the baud rate, but strangely, when I change it in the code data is written at the same speed. I am looking at the output on an oscilloscope, and the bits are written at the same rate no matter what I set for the baud rate. My code is attached below--I'd appreciate any help!

Thanks,
Andrea

#include "SPI.h" // necessary library
int ss=10; // using digital pin 10 for SPI slave select

void setup()
{
Serial.begin(300);
pinMode(13, OUTPUT);
pinMode(ss, OUTPUT); // we use this for SS pin
SPI.begin(); // wake up the SPI bus.
SPI.setBitOrder(MSBFIRST); //correct order for AD5764.
SPI.setDataMode(SPI_MODE1); //1 and 3 communicate with DAC. 1 is the only one that works with no clock divider.

}

void setValue(byte DB[3])
{
SPI.transfer(DB[0]); // send command byte. 00 (write-bar, 0) 010 (data register) 100 (select A-D). decimal: A = 16, B = 17, C = 18, D = 19, all = 20.
SPI.transfer(DB[1]); // most significant data bits
SPI.transfer(DB[2]);//lowest 8 data bits
}

void loop()
{
Serial.flush();
byte bytes[3];
bytes[0]=85;
bytes[1]=85;
bytes[2]=85;
setValue(bytes);
delay(500);
}

SPI_comm_loop.ino (906 Bytes)

I think you are confusing the RS-232 Serial port with the SPI port. Serial.begin(300) sets the RS-232 serial port. It has nothing to do with the speed of the SPI port. I really doubt that you can get it as low as 300 baud.
The SPI speed is set by specifying a divider like this:

SPI.setClockDivider(SPI_CLOCK_DIV2);

[edited]This can go from 2 down to 128 in powers of 2 - for example, SPI_CLOCK_DIV32
IIRC the default is SPI_CLOCK_DIV4

Why do you want to slow down the SPI clock?

Pete

Pete,

Thanks for your reply, now I understand. I want to slow down the SPI communication because I am then feeding the outputs through opto isolators before they get to the chip I am trying to control, and the optos are not very fast.

I tried your suggestion, but it just kills the SPI output completely--do I need to include any other libraries?

Andrea

Yes, you do have two different and independent communication modes mixed up. You don't have any Serial.print() statements in you whole sketch so you certainly are not sending any data out the serial pin 1. SPI (small peripheral interface) is a clocked synchronous communications protocol.

For the serial communications mode on arduino:

For SPI communication mode on arduino:

Pete--

Never mind, I had a typo--it works now. Thanks!

Retrolefty--my bad, when I said serial I was talking about SPI.

Andrea

afy2003:
Pete--

Never mind, I had a typo--it works now. Thanks!

Retrolefty--my bad, when I said serial I was talking about SPI.

Andrea

I know, but the Serial.begin(300); in your sketch had nothing to do with SPI

BTW. The Serial.flush() and the Serial.begin(300) do nothing useful, remove them.

Pete