⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 f06x_spi0_master.lst

📁 // This program accesses a SPI EEPROM using polled mode access. The F06x MCU // is configured in 4-
💻 LST
📖 第 1 页 / 共 3 页
字号:
C51 COMPILER V8.08   F06X_SPI0_MASTER                                                      02/16/2008 14:38:53 PAGE 1   


C51 COMPILER V8.08, COMPILATION OF MODULE F06X_SPI0_MASTER
OBJECT MODULE PLACED IN F06x_SPI0_Master.OBJ
COMPILER INVOKED BY: C:\SiLabs\MCU\IDEfiles\C51\BIN\C51.exe F06x_SPI0_Master.c DB OE

line level    source

   1          //-----------------------------------------------------------------------------
   2          // F06x_SPI0_Master.c
   3          //-----------------------------------------------------------------------------
   4          // Copyright 2006 Silicon Laboratories, Inc.
   5          // http://www.silabs.com
   6          //
   7          // Program Description:
   8          //
   9          // This program configures a C8051F06x as a 4-wire SPI Single Master.
  10          //
  11          // The SPI clock in this example is limited to 500 kHz when used with the
  12          // SPI0_Slave code example.  During a SPI_Read, the slave needs some time to
  13          // interpret the command and write the appropriate data to the SPI0DAT
  14          // register, and the slave no longer has enough time to complete the
  15          // SPI_READ_BUFFER command with a clock greater than 500 kHz.  For faster SPI
  16          // clocks, a dummy byte between the command and the first byte of Read data
  17          // will be required.
  18          //
  19          // This example is intended to be used with the SPI0_Slave example.
  20          //
  21          // Pinout:
  22          //
  23          // P0.0 - SPI SCK    (digital output, push-pull)
  24          // P0.1 - SPI MISO   (digital input, open-drain)
  25          // P0.2 - SPI MOSI   (digital output, push-pull)
  26          // P0.3 - SPI NSS    (digital output, push-pull)
  27          //
  28          // P1.6 - LED        (digital output, push-pull)
  29          //
  30          // all other port pins unused.
  31          //
  32          //
  33          // How To Test:
  34          //
  35          // 1) Download the code to a F060-TB that is connected as above to
  36          //    another device running the SPI0_Slave code.
  37          // 2) Verify that the J3 jumper is populated.
  38          // 3) Run the code.
  39          // 4) If the communication passes, the LEDs on both the Master and Slave
  40          //    boards will blink slowly. If it fails, the LEDs will be OFF.
  41          //
  42          //
  43          // Target:         C8051F06x
  44          // Tool chain:     Keil C51 7.50 / Keil EVAL C51
  45          // Command Line:   None
  46          //
  47          // Release 1.0
  48          //    -Initial Revision (TP)
  49          //    -14 DEC 2006
  50          //
  51          
  52          //-----------------------------------------------------------------------------
  53          // Includes
  54          //-----------------------------------------------------------------------------
  55          
C51 COMPILER V8.08   F06X_SPI0_MASTER                                                      02/16/2008 14:38:53 PAGE 2   

  56          #include <C8051F060.h>                 // SFR declarations
  57          
  58          //-----------------------------------------------------------------------------
  59          // Global Constants
  60          //-----------------------------------------------------------------------------
  61          
  62          #define SYSCLK             24500000    // Internal oscillator frequency in Hz
  63          
  64          #define SPI_CLOCK          500000      // Maximum SPI clock
  65                                                 // The SPI clock is a maximum of 500 kHz
  66                                                 // when this example is used with
  67                                                 // the SPI0_Slave code example.
  68          
  69          #define MAX_BUFFER_SIZE    8           // Maximum buffer Master will send
  70          
  71          // Instruction Set
  72          #define  SLAVE_LED_ON      0x01        // Turn the Slave LED on
  73          #define  SLAVE_LED_OFF     0x02        // Turn the Slave LED off
  74          #define  SPI_WRITE         0x04        // Send a byte from the Master to the
  75                                                 // Slave
  76          #define  SPI_READ          0x08        // Send a byte from the Slave to the
  77                                                 // Master
  78          #define  SPI_WRITE_BUFFER  0x10        // Send a series of bytes from the
  79                                                 // Master to the Slave
  80          #define  SPI_READ_BUFFER   0x20        // Send a series of bytes from the Slave
  81                                                 // to the Master
  82          #define  ERROR_OCCURRED    0x40        // Indicator for the Slave to tell the
  83                                                 // Master an error occurred
  84          
  85          sbit LED = P1^6;                       // LED='1' means ON
  86          
  87          //-----------------------------------------------------------------------------
  88          // Global Variables
  89          //-----------------------------------------------------------------------------
  90          
  91          unsigned char SPI_Data = 0xA5;
  92          
  93          unsigned char SPI_Data_Array[MAX_BUFFER_SIZE] = {0};
  94          
  95          bit Error_Flag = 0;
  96          
  97          unsigned char Command = 0x00;
  98          
  99          //-----------------------------------------------------------------------------
 100          // Function Prototypes
 101          //-----------------------------------------------------------------------------
 102          
 103          void Watchdog_Init (void);
 104          void Oscillator_Init (void);
 105          void Port_Init (void);
 106          void SPI0_Init (void);
 107          void Init_Device (void);
 108          void SPI_LED_On (void);
 109          void SPI_LED_Off (void);
 110          void SPI_Byte_Write (void);
 111          void SPI_Byte_Read (void);
 112          void SPI_Array_Write (void);
 113          void SPI_Array_Read (void);
 114          
 115          void Delay(void);
 116          
 117          //-----------------------------------------------------------------------------
C51 COMPILER V8.08   F06X_SPI0_MASTER                                                      02/16/2008 14:38:53 PAGE 3   

 118          // main() Routine
 119          //-----------------------------------------------------------------------------
 120          void main (void)
 121          {
 122   1         unsigned char test_value = 0x55;
 123   1         unsigned char test_array[MAX_BUFFER_SIZE] = {1,2,3,4,5,6,7,8};
 124   1         unsigned char i;
 125   1      
 126   1         Init_Device ();                     // Initializes hardware peripherals
 127   1      
 128   1         EA = 1;                             // Enable global interrupts
 129   1      
 130   1         LED = 0;
 131   1      
 132   1         SFRPAGE = LEGACY_PAGE;
 133   1      
 134   1         // TEST BEGIN --------------------------------------------------------------
 135   1      
 136   1         SPI_Data = test_value;
 137   1      
 138   1         // Write a value
 139   1         SPI_Byte_Write ();
 140   1      
 141   1         while (!NSSMD0);                    // Wait until the Write transfer has
 142   1                                             // finished
 143   1      
 144   1         // Read the same value back
 145   1         SPI_Data = 0x00;
 146   1         SPI_Byte_Read ();
 147   1      
 148   1         while (!NSSMD0);                    // Wait until the Read transfer has
 149   1                                             // finished
 150   1      
 151   1         // Check if the sent value and returned value match
 152   1         if (SPI_Data != test_value)
 153   1         {
 154   2            Error_Flag = 1;
 155   2         }
 156   1      
 157   1         // Copy test_array into SPI_Data_Array
 158   1         for (i = 0; i < MAX_BUFFER_SIZE; i++)
 159   1         {
 160   2            SPI_Data_Array[i] = test_array[i];
 161   2                
 162   2         }
 163   1      
 164   1         // Send the array to the slave
 165   1         SPI_Array_Write ();
 166   1      
 167   1         while (!NSSMD0);                    // Wait until the Write transfer has
 168   1                                             // finished
 169   1      
 170   1         // Clear SPI_Data_Array for the SPI_Buffer_Read function
 171   1         for (i = 0; i < MAX_BUFFER_SIZE; i++)
 172   1         {
 173   2            SPI_Data_Array[i] = 0;
 174   2         }
 175   1      
 176   1         // Read the array back from the slave
 177   1         SPI_Array_Read ();
 178   1      
 179   1         while (!NSSMD0);                    // Wait until the Read transfer has
C51 COMPILER V8.08   F06X_SPI0_MASTER                                                      02/16/2008 14:38:53 PAGE 4   

 180   1                                             // finished
 181   1      
 182   1         // Check if the received array matches the sent array
 183   1         for (i = 0; i < MAX_BUFFER_SIZE; i++)
 184   1         {
 185   2            if (SPI_Data_Array[i] != test_array[i])
 186   2            {
 187   3               Error_Flag = 1;
 188   3            }
 189   2         }
 190   1      
 191   1         // END OF TEST -------------------------------------------------------------
 192   1      
 193   1         while (1)
 194   1         {
 195   2            // If no error has occurred, blink the LEDs on the Master and Slave
 196   2            // boards
 197   2            if (Error_Flag == 0)
 198   2            {
 199   3               LED = 1;
 200   3      
 201   3               SPI_LED_On ();
 202   3      
 203   3               while (!NSSMD0);
 204   3      
 205   3               Delay ();
 206   3      
 207   3               SPI_LED_Off ();
 208   3      
 209   3               LED = 0;
 210   3      
 211   3               while (!NSSMD0);
 212   3      
 213   3               Delay ();
 214   3            }
 215   2         };
 216   1      }
 217          
 218          //-----------------------------------------------------------------------------
 219          // Initialization Subroutines
 220          //-----------------------------------------------------------------------------
 221          
 222          //-----------------------------------------------------------------------------
 223          // Watchdog_Init
 224          //-----------------------------------------------------------------------------
 225          //
 226          // Return Value : None
 227          // Parameters   : None
 228          //
 229          // This function disables the watchdog timer.
 230          //
 231          //-----------------------------------------------------------------------------
 232          void Watchdog_Init (void)
 233          {
 234   1         unsigned char SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE
 235   1      
 236   1         SFRPAGE = CONFIG_PAGE;              // Switch to the necessary SFRPAGE
 237   1      
 238   1         WDTCN     = 0xDE;                   // Disable the Watchdog Timer
 239   1         WDTCN     = 0xAD;
 240   1      
 241   1         SFRPAGE = SFRPAGE_save;             // Restore the SFRPAGE
C51 COMPILER V8.08   F06X_SPI0_MASTER                                                      02/16/2008 14:38:53 PAGE 5   

 242   1      }
 243          
 244          //-----------------------------------------------------------------------------
 245          // Oscillator_Init
 246          //-----------------------------------------------------------------------------
 247          //
 248          // Return Value : None
 249          // Parameters   : None
 250          //
 251          // This function initializes the system clock to use the internal oscillator
 252          // at 24.5 MHz.
 253          //
 254          //-----------------------------------------------------------------------------
 255          void Oscillator_Init (void)
 256          {
 257   1         unsigned char SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -