Guest User

Untitled

a guest
Jul 16th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.01 KB | None | 0 0
  1. /***********************************************************************************
  2.  
  3.   York University CSE3215
  4.   Main.c
  5.   Jan 09, 2006
  6.  
  7. ***********************************************************************************/
  8. #include <hidef.h>            /* common defines and macros */
  9. #include <mc9s12dp256.h>      /* MicroController derivative information */
  10.  
  11.  
  12. #pragma LINK_INFO DERIVATIVE "mc9s12dp256b"
  13.  
  14. void main(void) {
  15.  
  16. volatile byte data;
  17.  
  18. //PLL_init();  // set system clock frequency to 24 MHz
  19.  
  20.   // SPI communication initialization as a master
  21.   SPI0BR  = 0b00000011;    // Baud rate = 24MHz/16 = 1.5MHz 0x53;
  22.   SPI0CR2 = 0b00000010;  // SPI0CR2_SPISWAI=1 : Stop SPI clocks when in wait mode
  23.   SPI0CR1 = 0b01010110;  // Enable SPI ; Master mode ; CPHA = 1 ;
  24.  
  25.   WOMS = 0;
  26.  
  27.                                                  // SSOE=1 (SS input with MODF feature)      
  28.   /*DDRS |= 0x61;   // 0b 0 1 1 0 0 0 0 1 : PS7(SS) and PS4(MISO) are input and PS6(SCK)
  29.  
  30.                                // and PS5(MOSI) are output, PS0 is output for setting and clearing slave's SS
  31.  
  32.   PTS &= (~0x01);                  // Low slave's SS*/
  33.  
  34.  
  35.   for( ; ; ){  
  36.    
  37.     //PTS &= (~0x01);              // Low slave's SS
  38.                
  39.     // Send a byte to the slave
  40.     while( SPI0SR_SPTEF == 0); // Wait until SPIDR becomes empty.
  41.     SPI0DR = 0x04;                         //  Clear the flag SPI0SR_STEF
  42.  
  43.     while( SPI0SR_SPIF == 0 );   // Wait until the end of an SPI transfer.  
  44.     data = SPI0DR;                        // Read or write to clear SPI0SR_SPIF flag      
  45.    
  46.     // Receive a byte from the slave
  47.     while(SPI0SR_SPTEF == 0);  // Wait until the SPI0DR is empty
  48.     SPI0DR = 0x00;                         // Read SPI0DR to clear the SPI0SR_SPTEF flag
  49.     while(SPI0SR_SPIF == 0);      // Wait until a SPI transfer is finished  
  50.     data = SPI0DR;                         // Write the received data to data          
  51.    
  52.     //PTS |= (0x01);               // High slave's SS          
  53.   }
  54. }
Add Comment
Please, Sign In to add comment