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

📄 pca_smbus_master_f350.c

📁 该程序是于在单片机上实现飞利浦的IIC通讯总线的主机程序
💻 C
📖 第 1 页 / 共 2 页
字号:
//------------------------------------------------------------------------------------
// Includes
//------------------------------------------------------------------------------------
#include <c8051f350.h>                       // SFR declarations

//------------------------------------------------------------------------------------
// Global CONSTANTS
//------------------------------------------------------------------------------------

#define  SYSCLK         24500000             // System clock frequency
#define  SMB_FREQUENCY  50000                // Target SCL clock rate

#define  WRITE          0x00                 // SMBus WRITE command
#define  READ           0x01                 // SMBus READ command

// Device addresses (7 bits, lsb is a don't care)
#define  F350_SLAVE     0xF0                 // Device address for slave target

// Status vector - top 4 bits only
#define  SMB_MTSTA      0xE0                 // (MT) start transmitted
#define  SMB_MTDB       0xC0                 // (MT) data byte transmitted
#define  SMB_MRDB       0x80                 // (MR) data byte received
// End status vector definition

//-----------------------------------------------------------------------------------
// Global VARIABLES
//-----------------------------------------------------------------------------------
unsigned char SMB_DATA_IN;                   // Global holder for SMBus data
                                             // All receive data is written here

unsigned char SMB_DATA_OUT;                  // Global holder for SMBus data.
                                             // All transmit data is read from here

unsigned char TARGET;                        // Target SMBus slave address

bit SMB_BUSY;                                // Software flag to indicate when the
                                             // SMB_Read() or SMB_Write() functions
                                             // have claimed the SMBus

bit SMB_RW;                                  // Software flag to indicate the
                                             // direction of the current transfer



// 16-bit SFR declarations
sfr16    TMR2RL   = 0xca;                    // Timer2 reload registers
sfr16    TMR2     = 0xcc;                    // Timer2 counter registers
sfr16    TMR3RL   = 0x92;                    // Timer2 reload registers
sfr16    TMR3     = 0x94;                    // Timer3 counter registers

//------------------------------------------------------------------------------------
// Function PROTOTYPES
//------------------------------------------------------------------------------------

void SMBus_Init (void);
void Timer1_Init (void);
void Timer3_Init (void);
void Port_Init (void);
void SMBus_ISR (void);
void Timer3_ISR (void);
void SMB_Write (void);
void SMB_Read (void);

//------------------------------------------------------------------------------------
// MAIN Routine
//------------------------------------------------------------------------------------
//
// Main routine performs all configuration tasks, then loops forever sending and
// receiving SMBus data to the slave F300_SLAVE.
//
void main (void)
{
   unsigned char i, error;                   // Test counters

   PCA0MD &= ~0x40;                          // WDTE = 0 (watchdog timer enable bit)

   OSCICN |= 0x03;                           // Set internal oscillator to highest
                                             // setting (24500000)
   Port_Init ();                             // Initialize Crossbar and GPIO

   Timer1_Init ();                           // Configure Timer1 for use as SMBus
                                             // clock source

   Timer3_Init ();                           // Configure Timer3 for use with SMBus
                                             // low timeout detect

   SMBus_Init ();                            // Configure and enable SMBus

   EIE1 |= 1;                                // SMBus interrupt enable
   IE |= 0x20;                               // Timer2 interrupt enable
   EA = 1;                                   // Global interrupt enable


// TEST CODE--------------------------------------------------------------------------

   i = 0;                                    // Output data counter
   error = 0;                                // Error counter
                                  // Turn off LED
   while (1)
   {
      
         // SMBus Write Sequence
 //        SMB_DATA_OUT = i;                   // Define next outgoing byte
         TARGET = F350_SLAVE;                // Target the F300 Slave for next
                                             // SMBus transfer
         SMB_Write();                        // Initiate SMBus write

         // SMBus Read Sequence
         TARGET = F350_SLAVE;                // Target the F310 Slave for next
                                             // SMBus transfer
         SMB_Read();

         // Check transfer data
 //        if(SMB_DATA_IN != SMB_DATA_OUT)     // Received data match transmit data?
 //           error++;                         // Increment error counter if no match
      

                            // Toggle LED

      while (error);                         // Spin here if any errors occur

   }

// END TEST CODE----------------------------------------------------------------------

}

//------------------------------------------------------------------------------------
// Functions
//------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------
// SMB_Write ()
//------------------------------------------------------------------------------------
// Writes a single byte to the slave with address specified by the <TARGET> variable
// Calling sequence:
// 1) Write target slave address to the <TARGET> variable
// 2) Write outgoing data to the <SMB_DATA_OUT> variable
// 3) Call SMB_Write()
//
void SMB_Write ()
{
   while (SMB_BUSY);                         // Wait for SMBus to be free.
   SMB_BUSY = 1;                             // Claim SMBus (set to busy)
   SMB_RW = 0;                               // Mark this transfer as a WRITE
   STA = 1;                                  // Start transfer
}

//------------------------------------------------------------------------------------
// SMB_Read ()
//------------------------------------------------------------------------------------
// Reads a single byte from the slave with address specified by the <TARGET> variable
// Calling sequence:
// 1) Write target slave address to the <TARGET> variable
// 2) Call SMB_Write()
// 3) Read input data from <SMB_DATA_IN> variable
//
void SMB_Read ()
{








   while (SMB_BUSY);                         // Wait for bus to be free.
   SMB_BUSY = 1;                             // Claim SMBus (set to busy)
   SMB_RW = 1;                               // Mark this transfer as a READ

   STA = 1;                                  // Start transfer

   while (SMB_BUSY);                         // Wait for transfer to complete
}

//------------------------------------------------------------------------------------
// Initialization Routines
//------------------------------------------------------------------------------------

//------------------------------------------------------------------------------------
// SMBus_Init()
//------------------------------------------------------------------------------------
//
// SMBus configured as follows:
// - SMBus enabled
// - Slave mode disabled

⌨️ 快捷键说明

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