Hey all,
I'm trying to parse the SparkFun sample code for the knock-off nokia. I'm using an atmega32 micro, and want to use SPI to talk to the Epson micro. I'm using the board SparkFun is offering.
So there's no real choice but to use the 9-bit serial interface, since the A0 line is not brought out. Anyhow, it looks like the sparkfun sample code (using a philips micro) writes two bytes to the SPI data register (in their case, they are using a 16 bit SSP register (SSPDR) unavailable on the Atmega32) -- the first byte is:
0000 0001 to set up for a command in spi_command()
0000 0000 to set up for data in spi_data()
the second byte is the command, or the data.
So that one bit getting toggled is the first bit of the 9-bit stream, and corresponds to A0. So, the first seven bits of the first byte are basically junk, right?
Their code:
Code:
void spi_command(int dat){
int temp;
IOCLR0 |= LCD_CS;
SSPDR = dat & ~0x0100;// & ~0x00000100; // send next SPI channel 0 data
while ((SSPSR & 0x10)) ; // wait for transfer completed
IOSET0 |= LCD_CS;
}
So they basically look like they are hitting chip select, sending two bytes, waiting for xmit to finish and resetting CS.
But that seems weird! The data sheet (p. 13) for the epson says that it reads the first bit at the first clock-edge to set the next byte as command or data. But doesn't writing the first seven bits of the first "dummy" byte get the clock going -- and the epson sees a 0 (the first bit in the dummy byte) at the first clock, every time?
is there something i am missing here? are the sparkfun guys doing some clever timing, such that the clock doesn't start going til the last bit in the first byte gets piped along (seems crazy)? Or will the following C code for the atmega32 work:
Code:
void spi_command(int dat) {
PB4 = 0; //this is the SS (CS) pin on the atmega32 -- just bringing SS low
// to get the slave (the epson micro) ready
SPDR = 0x01; //dummy byte for command
while (!(SPSR & (1<<SPIF))); //just wait for the transfer to end
SPDR = dat; //send the actual command
while (!(SPSR & (1<<SPIF))); //just wait for the transfer to end
PB4 = 1; //all done, bring SS high and exit
}
?
spi_data would be analagous.
any help would be most appreciated. thanks so much!
confused,
jeevan
jeevan [at] media [dot] mit [dott edu