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

📄 10.3.c

📁 51单片机开发之 i2c中线的编程设计
💻 C
字号:

#include <REG51F0X0.H> 						// Header file for the Cygnal 8051F0X0
#include <STDIO.H> 							// Header file for standard I/O
#define  PCF8563DEVICEADDR       0x1
unsigned char g8563_Time[6];
unsigned char g8563_LastMinuteTime[6];                  //前一分钟时间备份

#define 	TRUE			0x01			// Value representing TRUE
#define		FALSE			0x00			// Value representing FALSE
#define 	ON				0x01			// Value representing ON
#define		OFF				0x00			// Value representing OFF
#define 	HIGH			0x01			// Value representing ON
#define		LOW				0x00			// Value representing OFF

#define  	DELAY_WRITE		5000			// approx. 5 ms delay write time (about 1000 cycles / ms)
#define  	DELAY_BLINK		50000			// Value for delay time - blink 


#define high_byte(x)		((x & 0xFF00) >> 8)



sbit  		P1_6 		= 	0x96;			// Define the individual bit (P1.6)
#define 	LED				P1_6			// The eval board has an LED on P1.6

sbit		BUS_BUSY	=	0xC7;			// SM Bus Busy (bit 7)
sbit		BUS_EN		=	0xC6;			// SM Bus Enable (bit 6)
sbit		BUS_START	=	0xC5;			// SM Bus Start (bit 5)
sbit		BUS_STOP	=	0xC4;			// SM Bus Stop (bit 4)
sbit		BUS_INT		=	0xC3;			// SM Bus Interrupt (bit 3)
sbit		BUS_AA		=	0xC2;			// SM Bus ACK (bit 2)
sbit		BUS_FTE		=	0xC1;			// SM Bus Clock timeout - high (bit 1)
sbit		BUS_TOE		=	0xC0;			// SM Bus Clock timeout - low (bit 0)


//中断矢量
unsigned char code iv_table [0xB0] _at_ 0x0003;


void write_byte (unsigned char data_out, unsigned int address);
unsigned char read_byte (unsigned int address);
void i2c_write (unsigned char output_data);
unsigned char i2c_read (void);
void delay_time (unsigned int time_end);
void i2c_start (void);
unsigned char  i2c_stop_and_read (void);
void repeated_i2c_start_and_write (unsigned char output_data);
void i2c_stop_and_write (unsigned char output_data);
//void P8563_CpyTime(unsigned char *p);

const unsigned char cTimeDefault[]={0x00,0x00,0x12,0x01,0x01,0x03};
enum Time{
    SECOND,
    MINUTE,
    HOUR,
    DAY,
    MONTH,
    YEAR
};

void main (void)
{
	unsigned int eeprom_address,MONTH,DAY,HOUR,MINUTE;
	unsigned char eeprom_data;	

	// Disable the WDT (page 93 of data sheet)
	WDTCN = 0xDE;
	WDTCN = 0xAD;

	// Set internal oscilator to 16 MHz - Startup is 2 MHz (page 98 of data sheet)
	OSCICN = 0x07;


	// On the Cygnal processor there is a "Crossover" network that must	
	// be initialized to establish the port pin assignements
	// (see page 101 of the data sheet)
	XBR0 = 0x05;							// Set UART and SMBus to be enabled
	XBR1 = 0x00;							// No functions routed in this register
	XBR2 = 0x40;							// Pull-ups enabled, XBAR enabled, no ADC

	PRT1CF = 0x40;							// Set port 1.6 to push/pull 
											// (i.e the LED on the Eval board)
MONTH=g8563_Time[MONTH];
DAY=g8563_Time[DAY];
HOUR=g8563_Time[HOUR];
MINUTE=g8563_Time[MINUTE];
	

}

//发送I2C信号

void write_byte (unsigned char data_out, unsigned int address)
{
	i2c_start();							// Send start signal
   	i2c_write(0xA0);              			// Send identifier I2C address
   	i2c_write(high_byte(address)); 			// Send address to EEPROM
   	i2c_write((unsigned char)address);  	// Send address to EEPROM
   	i2c_stop_and_write(data_out);      		// Send low byte to EEPROM
   	delay_time(DELAY_WRITE);       			// Delay a period of time to write
}

//从EEPROM读一个字节
unsigned char read_byte (unsigned int address)
{
   	unsigned char data_in;

	i2c_start();							// Send start signal
  	i2c_write(0xA0);              			// Send identifer I2C address
   	i2c_write(high_byte(address));   		// Send address to EEPROM
											// Send address to EEPROM
											// Send repeated start signal
	repeated_i2c_start_and_write((unsigned char)address);					

   	i2c_write(0xA1);              			// Send identifer I2C address
   	data_in = i2c_stop_and_read();			// Read byte, send stop signal

   	return data_in;                 
}


//I2C开始
void i2c_start (void)
{
	while (BUS_BUSY);						// Wait until we are clear to write
	BUS_START = TRUE;						// Perform I2C start
	while (!BUS_INT);						// Wait until start sent
	BUS_START = FALSE;						// Reset I2C start
	BUS_INT = 0;							// Clear SI
}

//重复开始
void repeated_i2c_start_and_write (unsigned char output_data)
{
	BUS_START = TRUE;						// Perform I2C start
	SMB0DAT = output_data;					// Put data into buffer
	while (!BUS_INT);						// Wait unitl we are done with send
	BUS_INT = 0;							// Clear SI
	BUS_START = FALSE;						// Reset I2C start
	while (!BUS_INT);						// Wait unitl we are done with reset
	BUS_INT = 0;							// Clear SI
}

//停止
void i2c_stop_and_write (unsigned char output_data)
{
	BUS_STOP = TRUE;						// Perform I2C stop
	SMB0DAT = output_data;					// Put data into buffer
	while (!BUS_INT);						// Wait unitl we are done with send
	BUS_INT = 0;							// Clear SI
}

//读
unsigned char i2c_stop_and_read (void)
{
	unsigned char input_data;

	BUS_STOP = TRUE;						// Perform I2C stop
	while (!BUS_INT);						// Wait until we have data to read
	input_data = SMB0DAT;					// Read the data
	BUS_INT = 0;							// Clear SI

	return input_data;
}

//写数据
void i2c_write (unsigned char output_data)
{
	SMB0DAT = output_data;					// Put data into buffer
	while (!BUS_INT);						// Wait unitl we are done with send
	BUS_INT = 0;							// Clear SI
}

//读数据
unsigned char i2c_read (void)
{
	unsigned char input_data;

	while (!BUS_INT);						// Wait until we have data to read
	input_data = SMB0DAT;					// Read the data
	BUS_INT = 0;							// Clear SI

	return input_data;
}


//延时
void delay_time (unsigned int time_end)
{
	unsigned int index;
	for (index = 0; index < time_end; index++);
}


⌨️ 快捷键说明

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