C8051F單片機(jī)讀寫(xiě)串行EEPROM程序
//------------------------------------------------------------------------------
// Keil Software, Inc.
//
// Project: Cygnal 8051F000 I2C Example Program
//
// Filename: Cygnal_I2C_Example_Program.c
// Version: 1.0.0
// Description: This file contains example code that will communicate to a
// serial EEPROM using I2C. Data will be printed over the
// serial port.
//
// Copyright 2000 - Keil Software, Inc.
// All rights reserved.
//------------------------------------------------------------------------------
// Keil Software, Inc.
//
// Project:
//
// Filename:
// Version:
// Description: This file contains example code that will communicate to a
//
//
//
// Copyright 2000 - Keil Software, Inc.
// All rights reserved.
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Header files
//------------------------------------------------------------------------------
#include // Header file for the Cygnal 8051F0X0
#include // Header file for standard I/O
// Header files
//------------------------------------------------------------------------------
#include
#include
//------------------------------------------------------------------------------
// Value Definitions
//------------------------------------------------------------------------------
#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
// Value Definitions
//------------------------------------------------------------------------------
#define
#define
#define
#define
#define
#define
#define DELAY_WRITE 5000 // approx. 5 ms delay write time (about 1000 cycles / ms)
#define DELAY_BLINK 50000 // Value for delay time - blink
#define
//------------------------------------------------------------------------------
// Macros
//------------------------------------------------------------------------------
// Get high byte macro
#define high_byte(x) ((x & 0xFF00) >> 8)
// Macros
//------------------------------------------------------------------------------
#define high_byte(x)
//------------------------------------------------------------------------------
// I/O Port Defines
//------------------------------------------------------------------------------
sbit P1_6 = 0x96; // Define the individual bit (P1.6)
#define LED P1_6 // The eval board has an LED on P1.6
// I/O Port Defines
//------------------------------------------------------------------------------
sbit
#define
//------------------------------------------------------------------------------
// I2C Bus (SMBus) register bit definitions
//------------------------------------------------------------------------------
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)
// I2C Bus (SMBus) register bit definitions
//------------------------------------------------------------------------------
sbit
sbit
sbit
sbit
sbit
sbit
sbit
sbit
//------------------------------------------------------------------------------
// Rerserve Interrupt vector space (the 8051F000 has an IV table from 0x03 to 0xAB)
//------------------------------------------------------------------------------
unsigned char code iv_table [0xB0] _at_ 0x0003;
// Rerserve Interrupt vector space (the 8051F000 has an IV table from 0x03 to 0xAB)
//------------------------------------------------------------------------------
unsigned char code iv_table [0xB0] _at_ 0x0003;
//------------------------------------------------------------------------------
// Function Prototypes
//------------------------------------------------------------------------------
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);
// Function Prototypes
//------------------------------------------------------------------------------
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
void repeated_i2c_start_and_write (unsigned char output_data);
void i2c_stop_and_write (unsigned char output_data);
//------------------------------------------------------------------------------
// MAIN FUNCTION
//------------------------------------------------------------------------------
void main (void)
{
unsigned int eeprom_address;
unsigned char eeprom_data;
// MAIN FUNCTION
//------------------------------------------------------------------------------
void main (void)
{
}
//------------------------------------------------------------------------------
// I2C Peripheral Function Prototypes
//------------------------------------------------------------------------------
// I2C Peripheral Function Prototypes
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// Procedure: write_byte
// Inputs: data out, address
// Outputs: none
// Description: Writes a byte to the EEPROM given the address
//------------------------------------------------------------------------------
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
}
// Procedure: write_byte
// Inputs:
// Outputs:
// Description: Writes a byte to the EEPROM given the address
//------------------------------------------------------------------------------
void write_byte (unsigned char data_out, unsigned int address)
{
}
//------------------------------------------------------------------------------
// Procedure: read_byte
// Inputs: address
// Outputs: none
// Description: Reads a byte from the EEPROM given the address
//------------------------------------------------------------------------------
unsigned char read_byte (unsigned int address)
{
unsigned char data_in;
// Procedure: read_byte
// Inputs:
// Outputs:
// Description: Reads a byte from the EEPROM given the address
//------------------------------------------------------------------------------
unsigned char read_byte (unsigned int address)
{
}
//------------------------------------------------------------------------------
//
// Inputs:
// Outputs: none
// Purpose: Sends I2C Start Trasfer - State "B"
//------------------------------------------------------------------------------
void i2c_start (void)
{
}
//------------------------------------------------------------------------------
// Routine: repeated_i2c_start_and_write
// Inputs: none
// Outputs: none
// Purpose: Sends I2C Start Trasfer - State "B"
//------------------------------------------------------------------------------
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
}
//
// Inputs:
// Outputs: none
// Purpose: Sends I2C Start Trasfer - State "B"
//------------------------------------------------------------------------------
void repeated_i2c_start_and_write (unsigned char output_data)
{
}
//------------------------------------------------------------------------------
// Routine: i2c_stop_and_write
// Inputs: output byte
// Outputs: none
// Purpose: Sends I2C Stop Trasfer - State "C" (also sends last byte)
//------------------------------------------------------------------------------
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
}
//
// Inputs:
// Outputs: none
// Purpose: Sends I2C Stop Trasfer - State "C" (also sends last byte)
//------------------------------------------------------------------------------
void i2c_stop_and_write (unsigned char output_data)
{
}
//------------------------------------------------------------------------------
// Routine: i2c_stop_and_read
// Inputs: none
// Outputs: input byte
// Purpose: Sends I2C Stop Trasfer - State "C" (also reads last byte)
//------------------------------------------------------------------------------
unsigned char i2c_stop_and_read (void)
{
unsigned char input_data;
//
// Inputs:
// Outputs: input byte
// Purpose: Sends I2C Stop Trasfer - State "C" (also reads last byte)
//------------------------------------------------------------------------------
unsigned char i2c_stop_and_read (void)
{
}
//------------------------------------------------------------------------------
// Routine: i2c_write
// Inputs: output byte
// Outputs: none
// Purpose: Writes data over the I2C bus
//------------------------------------------------------------------------------
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
}
//
// Inputs:
// Outputs: none
// Purpose: Writes data over the I2C bus
//------------------------------------------------------------------------------
void i2c_write (unsigned char output_data)
{
}
//------------------------------------------------------------------------------
// Routine: i2c_read
// Inputs: none
// Outputs: input byte
// Purpose: Reads data from the I2C bus
//------------------------------------------------------------------------------
unsigned char i2c_read (void)
{
unsigned char input_data;
//
// Inputs:
// Outputs: input byte
// Purpose: Reads data from the I2C bus
//------------------------------------------------------------------------------
unsigned char i2c_read (void)
{
}
////////////////////////////////////////////////////////////////////////////////
// Routine: delay_time
// Inputs: counter value to stop delaying
// Outputs: none
// Purpose: To pause execution for pre-determined time
////////////////////////////////////////////////////////////////////////////////
void delay_time (unsigned int time_end)
{
unsigned int index;
for (index = 0; index < time_end; index++);
}
//
// Inputs:
// Outputs: none
// Purpose: To pause execution for pre-determined time
////////////////////////////////////////////////////////////////////////////////
void delay_time (unsigned int time_end)
{
}
評(píng)論