TheLegace

SPI_Dragon12

Apr 1st, 2012
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.96 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. #define D_1MS (24000/13)
  12. #pragma LINK_INFO DERIVATIVE "mc9s12dp256b"
  13.  
  14. void delay(unsigned int num);
  15.  
  16.  
  17. void main(void) {
  18.  
  19. volatile byte data;
  20.  
  21. //PLL_init();  // set system clock frequency to 24 MHz
  22.  
  23.   EnableInterrupts;
  24.   DDRJ = 0xFF;
  25.   DDRB = 0xFF;
  26.   PTJ = 0x00;
  27.   // SPI communication initialization as a master
  28.   SPI0BR  = 0b00100101;    // Baud rate = 24MHz/16 = 1.5MHz 0x53;
  29.   SPI0CR2 = 0b00010000;  // SPI0CR2_SPISWAI=1 : Stop SPI clocks when in wait mode
  30.   SPI0CR1 = 0b01010010;  // Enable SPI ; Master mode ; CPHA = 1 ;
  31.  
  32.   WOMS = 0;
  33.  
  34.                                                  // SSOE=1 (SS input with MODF feature)      
  35.   /*DDRS |= 0x61;   // 0b 0 1 1 0 0 0 0 1 : PS7(SS) and PS4(MISO) are input and PS6(SCK)
  36.  
  37.                                // and PS5(MOSI) are output, PS0 is output for setting and clearing slave's SS
  38.  
  39.   PTS &= (~0x01);                  // Low slave's SS*/
  40.  
  41.  
  42.   for( ; ; ){  
  43.    
  44.     //PTS &= (~0x01);              // Low slave's SS
  45.                
  46.     // Send a byte to the slave
  47.     while( SPI0SR_SPTEF == 0); // Wait until SPIDR becomes empty.
  48.     SPI0DR = 0xAA;                         //  Clear the flag SPI0SR_STEF
  49.  
  50.     while( SPI0SR_SPIF == 0 );   // Wait until the end of an SPI transfer.  
  51.     data = SPI0DR;                        // Read or write to clear SPI0SR_SPIF flag
  52.    
  53.     PORTB = ~PORTB;
  54.    
  55.     delay(450);      
  56.    
  57.        
  58.   }
  59. }
  60.  
  61.  
  62. void delay(unsigned int num)
  63.   {
  64.     volatile unsigned int i;
  65.     while (num > 0)
  66.     {
  67.       i = D_1MS;
  68.     while (i > 0)
  69.     {
  70.       i = i - 1;
  71.     }
  72.     num = num - 1;
  73.   }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment