Need help understanding SPCR on mega 168

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm creating a project and need to adapt another project in to mine. There is this section of code I dont fully understand.

void spi_mInit()
{
   // SPI, master, clock/128 = 93.75khz (187.5 didn't work...)
   SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0)|(1<<DORD)|(1<<CPHA)|(1<<CPOL);
}
unsigned char spi_mSend(unsigned char podatek)   
{

   // Gets information, sends it, waits untill it's sent, then reads the same register (used for both Input and Output) and returns it

   /* Start transmission */
   SPDR = podatek;
   while(!(SPSR & (1<<SPIF)));

   return SPDR;
}

is SPCR tied to port b, and I need port c's register, as far as I can tell from the data sheet its just a Control Register? This code uses port B, I need it to use port C, and also changes the pin assignments. Later on it does a ps2buffer[0]=spi_mSend(0x01); but how does it know what pin to look at?

also the line that sets SPCR, seems not to be in the correct order, is that not order dependent? My data sheet has SPCR as SPIE,SPE,DORD,MSTR,CPOL,CPHA,SPR1,SPRO

is this how it sets up the pin designations?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
is SPCR tied to port b
It is connected to the SPI and not any registers as such.
Quote:
This code uses port B, I need it to use port C,
The SPI is attached to portb, nothing you can do about that. If you want to use other pins than you will need to bit bang the SPI yourself.
Quote:
seems not to be in the correct order, is that not order dependent?
It does not matter, the compiler will adjust their position.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thx.