Sending data to 2 ports without delay

So here's my problem...

I am sending Data into a 16 bit DSpiC.

I want to send a 16bit word, which is no problem for the pic. However on the atmel side i have to process such data as (at least) two different instructions. This will now create a delay between the time the lower 8 bits are sent and the time the upper 8 bits are sent, so if the PIC makes a reading during this time it may get the wrong data.

Any ideas on how to solve this?

Add a 16-bit (or two 8-bit) shift registers. Shift the data out and then latch all 16 bits in the output register simultaneously.

If you use the SPI interface you can shift data out at 8 megabits per second.

johnwasser:
Add a 16-bit (or two 8-bit) shift registers. Shift the data out and then latch all 16 bits in the output register simultaneously.

If you use the SPI interface you can shift data out at 8 megabits per second.

I am exactly doing that at the moment.
And I am also looking at a way to use the SPI bus. Can only find toturials with normal digital pins.

Can you point me to some relevant topic regarding that?

Regards

Simple example, assuming two 74HC595 shift registers

#include <SPI.h> // or use Import Library: SPI to let the IDE add this
// # include & # define statements must occur before any code
byte csPin = 10; // to shift register RCLK pin
int dataWord = 0xAB; // some random data

void setup(){
pinMode (csPin, OUTPUT);
digitalWrite (csPin, HIGH);
SPI.begin();
// sets up D13 (clock, to SRCLK pin), D12 (serial date from device if used), D11 (serial data to device, to Serial data in pin)
// user connects SRCLR to +5, OE/ to GND
// 2 devices are daisy chained - D13, D10 to both devices; D11 to device 1; Q7 from device 1 to Serial data in on device 2

}
void loop(){
digitalWrite (cSPin, LOW); // connects to RCLK
SPI.transfer (highByte (dataWord); // send out upper 8 bits MSB first 
SPI.transfer (lowByte (dataword); // send out lower 8 bits
digitalWrite (csPin, HIGH); // data output changes on this rising edge
}

CrossRoads:
Simple example, assuming two 74HC595 shift registers

CrossRoads,

I just received the second shift register and finished the 16 bit setup.

Since I will only be transfering 10bits data I was wondering if I could use the remaining 6 bits to drive an HD44780 type LCD?
If so, can you point me on the right direction of how to archieve this?

Regards

You can.
Have to manipulate the 6 bits & 10 bits and send out all 16 when one set or the other changed.

int combined;
int upper6 = 0b1111110000000000; // shows bit positions used
int lower10 = 0b0000001111111111;

// time to change upper 6:
combined = combined & 0b0000001111111111; // clear upper 6, leave lower 10 alone
combined = combined | upper6; // mix the upper6 in

// send out the data

// time to change lower 10:
combined = combined & 0b1111110000000000; // clear lower 10, leave upper 6 alone
combined = combined | lower10; // mix the lower 10 in

// send out the data

CrossRoads:
You can.
Have to manipulate the 6 bits & 10 bits and send out all 16 when one set or the other changed.

I was thinking in something along the lines of that, but summing whatever the bit I want to enable. For example, I know thew MSB on a 16bit word is the equivalent to 0b1000000000000000 or 32768. So By adding 32768 to the value i already have I can lit that single bit without changing the others. The same would be valid for the remaining 5 MSBs.

Then I would do something like:

LCD_D0 = 32768xD0
....

Total Outpout = My 10bit variable + LCD_D0+.....

And I would sum them all. But that would take 10x more processing, right?

But the pertinent question is how to I get a library and actually extract the data that was originally meant for the display before it reaches whatever outputs it was intended to (Paralell, other SR)? For example I was using
fmalpartida library which already uses a SR, but not on the SPI port. So I would have to figure out where the data was being sent before going to the SR and use it to print as you described above. This is the part I am finding hard tough.

https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home

You don't want to sum, that can produce unintended effects when bits rollover & stuff. Much cleaner to perform bit-wise AND and OR functions.

Can't help you on the libary part, you need a better programmer to help there.

Thank you crossroads.

So I have a 6 bit variable as you advised.
Now I have a few data lines: RS E D4 D5 D6 D7

How will i define Enable (E) as being the first digit then?
I am guessing i could use bit manipulation and just shift bits arround perhaps? Would you be able to advise?

On a physical port I would Write:

digitalWrite(_enable_pin, HIGH);   
digitalWrite(_enable_pin, LOW);

How would I do this with a variable?

Same way I described above. Manipulate the bit in the variable, shift it out, manipulate it back, send it out again.

CrossRoads:
You can.
Have to manipulate the 6 bits & 10 bits and send out all 16 when one set or the other changed.

int combined;
int upper6 = 0b1111110000000000; // shows bit positions used
int lower10 = 0b0000001111111111;

// time to change upper 6:
combined = combined & 0b0000001111111111; // clear upper 6, leave lower 10 alone
combined = combined | upper6; // mix the upper6 in

// send out the data

// time to change lower 10:
combined = combined & 0b1111110000000000; // clear lower 10, leave upper 6 alone
combined = combined | lower10; // mix the lower 10 in

// send out the data

Back to this issue again.

Suppose I want to send 32bits of data via SPI using the above method.
How would this be done?

Regards

casemod:
Suppose I want to send 32bits of data via SPI using the above method.
How would this be done?

Call SPI.transfer() for each of the four bytes in your 32-bit value.

johnwasser:

casemod:
Suppose I want to send 32bits of data via SPI using the above method.
How would this be done?

Call SPI.transfer() for each of the four bytes in your 32-bit value.

Ok, so i am guessing I set whatever line a want for CS low, transfer all the 4 bites, as I do with the two above and then turn the line high again, is this right?

casemod:

johnwasser:
Ok, so i am guessing I set whatever line a want for CS low, transfer all the 4 bites, as I do with the two above and then turn the line high again, is this right?

Yes.