Hi, I plan to buy a Arduino Duemilanove and use it for generating two continuous digital output signals at 1MHz. I also need synchronize the two output signals by having a 200 ns time delay between them. Is the Arduino Duemilanove capable of doing this?
It runs at 16MHz, that is 62.5ns per clock cycle.
Most of the !assembler! instructions take 1 clock cycle, some 2 and 3. In the best case the chip could run 3 instructions, if an interrupt fires your timing will be off by much more than that, lots of jitter.
So basically... no
two continuous digital output signals at 1MHz
Why two, is it the same signal repeated? If so then it is one, if they are slightly different or a different phase then the no get bigger.
Hi, thanks for the reply.
Actually, I need three signals to control a device, one signal is the clock signal, and one is a 384 bit data signal, and another strobe signal that inform the device the start and stop of the data.
Sounds like SPI.
The arduino can send with up to 8Mhz using the internal hardware SPI circuitry.
#define __spi_clock 13 // SCK - hardware SPI
#define __spi_latch 10
#define __spi_data 11 // MOSI - hardware SPI
#define __spi_data_in 12 // MISO - hardware SPI
void setup(void) {
pinMode(__spi_clock,OUTPUT);
pinMode(__spi_latch,OUTPUT);
pinMode(__spi_data,OUTPUT);
pinMode(__spi_data_in,INPUT);
setup_hardware_spi();
}
void loop(void) {
byte data;
// whatever
digitalWrite(__spi_latch,LOW);
spi_transfer(data);
digitalWrite(__spi_latch,HIGH);
// whatever
}
void setup_hardware_spi(void) {
byte clr;
// spi prescaler:
// SPI2X SPR1 SPR0
// 0 0 0 fosc/4
// 0 0 1 fosc/16
// 0 1 0 fosc/64
// 0 1 1 fosc/128
// 1 0 0 fosc/2
// 1 0 1 fosc/8
// 1 1 0 fosc/32
// 1 1 1 fosc/64
SPCR |= ( (1<<SPE) | (1<<MSTR) ); // enable SPI as master
SPCR &= ~ ( (1<<SPR1) | (1<<SPR0) ); // clear prescaler bits
clr=SPSR; // clear SPI status reg
clr=SPDR; // clear SPI data reg
SPSR |= (1<<SPI2X); // set prescaler bits
}
byte spi_transfer(byte data) {
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // wait until done
{
};
return SPDR; // return the received byte
}
This minimal example repeatedly sends 1 byte out of the hardware SPI pins using F_CPU/2 on the clock line. If you want to remove overhead, replace the digitalWrite() functions for the latch_pin with direct port manipulation.
Thanks, it seems like what I need.
Can I wire both the data and clock signals out to the controlled device?
You must do that, of course.
These are the pin numbers.
#define __spi_clock 13 // SCK - hardware SPI
#define __spi_latch 10
#define __spi_data 11 // MOSI - hardware SPI
#define __spi_data_in 12 // MISO - hardware SPI
11,12,13 are fixed and cannot be changed for this purpose. You can choose any other GPIO pin for the latch though. Here I just used #10 for convenience.
@stork
If you had asked a sensible question we would have got there a lot sooner. You need to see:- Arduino Playground - Spi
SPI is a byte-synchronious signal. Using a high-frequency carrier as 4 or 8 MHz is of limited use.
(1) it is not a 200 ns signal (but 128 or 256)
(2) The average information thruput will be considerably lower, depending on the computations needed to generate or retrieve each byte.
With an enormous amount of tricks, a 20 MHz AVR chip can generate 192 pixels video in a line of 52us/64us = 200ns Pixelclock, 3MHz payload.
Hi all, thanks very much for the help! I checked the SPI library and try to write my code, but the problem is that I don't use the pin 12 MISO SPI master in, slave out. Instead, besides the clock signal, I need two master(here is the duemilanove ) out and slave(the device to be controlled) in signals, where one is data, the other is a strobe signal to inform the slaved device the start and stop of data transfer. Is there a way to change pin 12 to be master out?
Also, in the method byte transfer(byte b, byte delay) in spi library,
can the delay be defined as 200 ns?
/Users/nandu/Desktop/timing diagram.png
I need two master(here is the duemilanove ) out
Why, I don't think you do. If you have two devices you connect devices input to the same master out pin. Then by using separate chip enables you select which chip you are talking to.
Can you explain exactly what you are trying to do as we haven't had a real explanation yet.
sorry about that. I was trying to attach a pic but don't know how.
what I want to do is use the arduino to send control signals to a device. It needs three synchronized signals:
- a clock signal at 1 MHz
- a serial data signal that has 384 bits where each bit can be set high/low and last 500 ns when the clock signal pulse is also at logic high.
- a strobe signal that informs the device to start receiving the data by sending a single pulse.
Still not to clear:-
I was trying to attach a pic
Attach it to what?
what I want to do is use the arduino to send control signals to a device
What device? Is it the device that the PIC was going to be attached to or is it another device?
How much tolerance has this 1MHz signal got? Is the data synchronised on the rising edge or falling of this clock?
384 bits is 48 bytes, that seems a lot of information?
each bit can be set high/low and last 500 ns
Do you mean it is high for 500nS in some cases or low for the same period. 1MHz is 1000nS so we are talking about half a clock period?
Has the clock got to be operating even when data is not being transferred.
Is this mystery device an LCD?
I am not sure if you expect us to be mind readers but the details supplied so far are not conducive to getting a full answer.
I am not sure, but I think maybe he was intending/wanting to attach an image file to a post to explain what he wanted to do (hint: you need to use an external service like photobucket and link it in the post)...
I think he means "pic" as in "picture" (by the way, spelling out such words in a forum like this makes for quicker understanding), not "Microchip PIC microcontroller".
Then again, maybe I am completely wrong...?
where each bit can be set high/low and last 500 ns when the clock signal pulse is also at logic high
This is rather uncommon. Generally - but not exclusively - a bit is strobed by either the rising or the falling edge of a clock. Can you please link the datasheet of this - as Mike said: mysterious - device?
And how does the 200ns come in?
Sorry, I was talking about attaching pictures here:-)
I am planning to use arduino duemilanove as the microcomputer in the above circuits to send the "DATA IN", "CLOCK" and "STROBE" signals to the output connector on the right hand side.
And the details of the timing diagrams are as the following. The DATA IN is a 384 bits serial signal.
The 200ns is the delay parameter I am thinking to use in the transfer(byte, delay) method.
All right, so this is the most common data interface of the world
How to read such a timing diagram?
(1) It says that you should not do anything faster than 1MHz. It does not say how much slower you can do it, but 100kHz would be a sound assumption. So there is no reason for any high-speed stress.
(2) It says the data bits are read with the rising edge of the clock.
(3) To give the receiver a chance, this clock pulse shall not be dropped before 500ns (but can be longer of course)
(4) So that the signal can stabilize on the data line, it is recommended that you should apply this rising clock not less than 200ns after the data has been set. The diagram is quite firm here, but i am sure this delay can be reduced, if needed, but it is not needed in your application...)
What does that mean for you?
I should propose that you just use digitalWrite (or better digtalWriteFast) to generate that signal in a tight loop. This will undoubtedtly work. If it is to slow for you (but why?), then you can change to hardware assisted SPI.
Thanks for the clarification. But if I just use the loop to generate the data. How to synchronize it to the clock and strobe signals?
You have to output all the signals, e.g.:
digitalWriteFast(clockPin, LOW);
for (i=0; i <384;++i) {
bit =getBit(i);
digitalWriteFast(clockPin, LOW);
digitalWriteFast(dataPin, bit);
digitalWriteFast(clockPin, HIGH);
}
digitalWriteFast(clockPin, LOW);