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

📄 i2c.lst

📁 主要介绍日历时钟的编程和注意的事项.主要应用于单片机8051.
💻 LST
📖 第 1 页 / 共 2 页
字号:
C51 COMPILER V6.12  I2C                                                                    05/24/2006 15:23:18 PAGE 1   


C51 COMPILER V6.12, COMPILATION OF MODULE I2C
OBJECT MODULE PLACED IN I2C.OBJ
COMPILER INVOKED BY: c:\KEIL\C51\BIN\C51.exe I2C.c DB OE

stmt level    source

   1          #include <c8051f020.h>					// SFR declarations
   2          #include "INTRINS.H"
   3          
   4          //------------------------------------------------------------------------------------
   5          // Global CONSTANTS
   6          //------------------------------------------------------------------------------------
   7          
   8          #define 	WRITE	0x00						// SMBus WRITE command
   9          #define		READ 	0x01						// SMBus READ command
  10          
  11          // Device addresses (7 bits, lsb is a don't care)
  12          #define		CLOCK3530_ADDRESS_RESET		0x60		//1 ack
  13          #define		CLOCK3530_ADDRESS_STATUS	0x62		//2 ack
  14          #define		CLOCK3530_ADDRESS_DATEHOUR	0x64		//8 ack  year month day week hour minute second
  15          #define		CLOCK3530_ADDRESS_HOUR		0x66		//4 ack  hour minute second
  16          #define		CLOCK3530_ADDRESS_INT1		0x68		//3 ack  
  17          #define		CLOCK3530_ADDRESS_INT2		0x6A		//3 ack  
  18          
  19          union 
  20          {
  21          unsigned char ClockString[7];
  22          struct RealClock
  23          	{
  24          		unsigned char Year,Month,Day,Week,Hour,Minute,Second;
  25          	} RT;
  26          } RealTime;
  27          
  28          
  29          // SMBus states:
  30          // MT = Master Transmitter
  31          // MR = Master Receiver
  32          #define	SMB_BUS_ERROR	0x00			// (all modes) BUS ERROR
  33          #define	SMB_START		0x08			// (MT & MR) START transmitted
  34          #define	SMB_RP_START	0x10			// (MT & MR) repeated START
  35          #define	SMB_MTADDACK	0x18			// (MT) Slave address + W transmitted;
  36          										//  ACK received
  37          #define	SMB_MTADDNACK	0x20			// (MT) Slave address + W transmitted;
  38          										//  NACK received
  39          #define	SMB_MTDBACK		0x28			// (MT) data byte transmitted; ACK rec'vd
  40          #define	SMB_MTDBNACK	0x30			// (MT) data byte transmitted; NACK rec'vd
  41          #define	SMB_MTARBLOST	0x38			// (MT) arbitration lost
  42          #define	SMB_MRADDACK	0x40			// (MR) Slave address + R transmitted;
  43          										//  ACK received
  44          #define	SMB_MRADDNACK	0x48			// (MR) Slave address + R transmitted;
  45          										//  NACK received
  46          #define	SMB_MRDBACK		0x50			// (MR) data byte rec'vd; ACK transmitted
  47          #define	SMB_MRDBNACK	0x58			// (MR) data byte rec'vd; NACK transmitted
  48          
  49          
  50          //-----------------------------------------------------------------------------------
  51          //Global VARIABLES
  52          //-----------------------------------------------------------------------------------
  53          char COMMAND;			// Holds the slave address + R/W bit for use in the SMBus ISR.
  54          
  55          unsigned char *I2CDataBuff;						
C51 COMPILER V6.12  I2C                                                                    05/24/2006 15:23:18 PAGE 2   

  56          
  57          char BYTE_NUMBER;							// Used by ISR to check what data has just been
  58          												// sent - High address byte, Low byte, or data byte
  59          
  60          unsigned char HIGH_ADD, LOW_ADD;		// High & Low byte for EEPROM memory address
  61          				
  62          bit SM_BUSY;								// This bit is set when a send or receive
  63          												// is started. It is cleared by the
  64          												// ISR when the operation is finished.
  65          
  66          
  67          //------------------------------------------------------------------------------------
  68          // Function PROTOTYPES
  69          //------------------------------------------------------------------------------------
  70          
  71          void SMBus_ISR (void);								
  72          
  73          //------------------------------------------------------------------------------------
  74          // MAIN Routine
  75          //------------------------------------------------------------------------------------
  76          //
  77          // Main routine configures the crossbar and SMBus, and tests
  78          // the SMBus interface between the three EEPROMs
  79          
  80          
  81          void ResetRealClock(void)
  82          {
  83   1      	while (SM_BUSY);									// Wait for SMBus to be free.
  84   1      	SM_BUSY = 1;										// Occupy SMBus (set to busy)
  85   1      	SMB0CN = 0x44;										// SMBus enabled, ACK on acknowledge cycle
  86   1      	BYTE_NUMBER = 0;									// 2 address bytes.
  87   1      	COMMAND = (CLOCK3530_ADDRESS_RESET | READ);		// Chip select + READ
  88   1      	STA = 1;											// Start transfer
  89   1      	while (SM_BUSY);									// Wait for transfer to finish
  90   1      }
  91          
  92          //======================写S-3530A内部实时数据寄存器程序=====================
  93          //功能:将设定年、月、日、星期、时、分、秒数据写入S-3530A                  |
  94          //入口:发送数据放在年、月、日、星期、时、分、秒各寄存器                   |
  95          //出口:NONE                                                               |
  96          //==========================================================================
  97          void SetRealClock(void)
  98          {
  99   1      	while (SM_BUSY);									// Wait for SMBus to be free.
 100   1      	SM_BUSY = 1;										// Occupy SMBus (set to busy)
 101   1      	SMB0CN = 0x44;										// SMBus enabled, ACK on acknowledge cycle
 102   1      	BYTE_NUMBER = 7;									// 2 address bytes.
 103   1      	COMMAND = (CLOCK3530_ADDRESS_DATEHOUR | WRITE);		// Chip select + WRITE
 104   1      	I2CDataBuff = &RealTime.ClockString[0];				// Data to be writen
 105   1      	STA = 1;											// Start transfer
 106   1      }
 107          
 108          //==================读S-3530A实时数据寄存器子程序===========================
 109          //功能:从S-3530A读入当前时间数据                                          |
 110          //入口:NONE                                                               |
 111          //出口:接收数据放在年、月、日、星期、时、分、秒各寄存器                   |
 112          //==========================================================================
 113          void GetRealClock(void)
 114          {
 115   1      	while (SM_BUSY);									// Wait for SMBus to be free.
 116   1      	SM_BUSY = 1;										// Occupy SMBus (set to busy)
 117   1      	SMB0CN = 0x44;										// SMBus enabled, ACK on acknowledge cycle
C51 COMPILER V6.12  I2C                                                                    05/24/2006 15:23:18 PAGE 3   

 118   1      	BYTE_NUMBER = 7;									// 2 address bytes.
 119   1      	COMMAND = (CLOCK3530_ADDRESS_DATEHOUR | READ);		// Chip select + READ
 120   1      	I2CDataBuff = &RealTime.ClockString[0];				// Data to be writen
 121   1      	STA = 1;											// Start transfer
 122   1      	while (SM_BUSY);									// Wait for transfer to finish
 123   1      }
 124          
 125          //============================写状态寄存器程序==============================
 126          //功能:读/写S-3530A状态寄存器,对S-3530A进行设置                          |
 127          //入口:NONE           出口:NONE                                          |
 128          //==========================================================================
 129          unsigned char  GetRealClockStatus(void)
 130          {
 131   1      	unsigned char result; 
 132   1      	while (SM_BUSY);									// Wait for SMBus to be free.
 133   1      	SM_BUSY = 1;										// Occupy SMBus (set to busy)
 134   1      	SMB0CN = 0x44;										// SMBus enabled, ACK on acknowledge cycle
 135   1      	BYTE_NUMBER = 1;									
 136   1      	COMMAND = (CLOCK3530_ADDRESS_STATUS | READ);		
 137   1      	I2CDataBuff = &result;								
 138   1      	STA = 1;											// Start transfer
 139   1      	while (SM_BUSY);									// Wait for transfer to finish
 140   1      	return result;
 141   1      }
 142          void SetRealClockStatus(unsigned char status)
 143          {
 144   1      	while (SM_BUSY);									// Wait for SMBus to be free.
 145   1      	SM_BUSY = 1;										// Occupy SMBus (set to busy)
 146   1      	SMB0CN = 0x44;										// SMBus enabled, ACK on acknowledge cycle
 147   1      	BYTE_NUMBER = 1;									
 148   1      	COMMAND = (CLOCK3530_ADDRESS_STATUS | WRITE);		
 149   1      	I2CDataBuff = &status;								
 150   1      	STA = 1;											// Start transfer
 151   1      }
 152          
 153          
 154          unsigned char revolve(unsigned char val)
 155          {
 156   1      char i;
 157   1      unsigned char val1=0;
 158   1      	for (i=0;i<8;i++)
 159   1      	{
 160   2      		if (val&0x1)
 161   2      			val1++;
 162   2      		val1=_crol_(val1,1);
 163   2      		val=_cror_(val,1);
 164   2      	}

⌨️ 快捷键说明

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