Selasa, 26 Juni 2012

Testing SPI communication

Here is a simple code written in c to test the SPI communication using atmega16 the avr code vision compiler is used here.

>> In SPI communication : one uc is programmed as a master (initiates communication) and the other uc is programmed as a slave.

Here is the master C code

// SPI initialization
// SPI Type: Master
// SPI Clock Rate: 1000.000 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0x50;
SPSR=0x00;

while (1)
      {
      // Place your code here
      PORTB.4=0;
      SPDR=PINA;
      while(SPSR.7==0)
      {}
      status=SPSR;
      PORTC=SPDR;
      };

Here is the slave C code

// SPI initialization
// SPI Type: Slave
// SPI Clock Rate: 1000.000 kHz
// SPI Clock Phase: Cycle Half
// SPI Clock Polarity: Low
// SPI Data Order: MSB First
SPCR=0x40;
SPSR=0x00;
while (1)
      {
      // Place your code here
      SPDR=PINA;
      while(SPSR.7==0)
      {}
      status=SPSR;
      PORTC=SPDR;
      };



Proteus simulation




Here is the link of the master and slave codevision projects and proteus simulation :

0 komentar:

Posting Komentar