您的位置 首页 芯闻

C8051F单片机读写串行EEPROM程序

C8051F单片机读写串行EEPROM程序,查询方式————————————————————————–

//C8051F单片机读写串行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.
//——————————————————————————
//——————————————————————————
// Header files
//——————————————————————————
#include // Header file for the Cygnal 8051F0X0
#include // Header file for standard I/O
//——————————————————————————
// 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
#define DELAY_WRITE 5000 // approx. 5 ms delay write time (about 1000 cycles / ms)
#define DELAY_BLINK 50000 // Value for delay time – blink
//——————————————————————————
// Macros
//——————————————————————————
// Get high byte macro
#define high_byte(x) ((x & 0xFF00) >> 8)
//——————————————————————————
// 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
//——————————————————————————
// 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)
//——————————————————————————
// 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);
//——————————————————————————
// MAIN FUNCTION
//——————————————————————————
void main (void)
{
unsigned int eeprom_address;
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)
// Initialize the serial port (9600, 8, N, 1)
PCON &= 0x7F; // Clear bit 7 of the PCON register (SMOD1 = 0)
SCON = 0x50; // 0101,0000 (Mode 1 and RxD enable)
CKCON = 0x10; // Make T1M 1 (i.e. SysClk for Timer 1 not / by 12)
// (see page 141 of the data sheet)
TMOD |= 0x20; // Timer #1 in autoreload 8 bit mode
TCON |= 0x40; // Set Timer #1 to run mode (TR = 1)
TH1 = 0xCC; // Baud rate is determined by
// Timer #1 overflow rate
// Baud Rate = (Fcpu / 32) / (256 – TH1)
// Fcpu = 16.00 MHz (see above setting of osc.)
// TH1 = 252
// (see page 130 of the data sheet)
SCON |= 0x02; // Set UART to send first char (TI = 1)
// Initialize the I2C Bus (SMBus)
// (see page 111)
SMB0CR = 0x60; // Set the clock to approx. 10 uS TH, TL (50 kHz)
// (see page 117 of the data sheet)
BUS_EN = TRUE; // Enable the bus
printf(“Keil Software, Inc.”); // Display starup message
printf(“8051F0X0 MCU I睠 Example Test Program”);
printf(“Version 1.0.0”);
printf(“Copyright 2000 – Keil Software, Inc.”);
printf(“All rights reserved.”);
printf(“Writing data to EEPROM….”);
for (eeprom_address = 0; eeprom_address < 75; eeprom_address++)
write_byte((unsigned char)eeprom_address + 0x30, eeprom_address);
printf(“Done!”);
while (TRUE)
{
for (eeprom_address = 0; eeprom_address < 75; eeprom_address++)
{
// Read data from eeprom and display it
eeprom_data = read_byte(eeprom_address);
printf(“Address: %3u Character: %c”, eeprom_address, eeprom_data);
LED = HIGH; // Blink LED with delay
delay_time(DELAY_BLINK);
LED = LOW;
delay_time(DELAY_BLINK);
}
}
}
//——————————————————————————
// 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: 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;
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;
}
//——————————————————————————
// Routine: i2c_start
// Inputs: none
// Outputs: none
// Purpose: Sends I2C Start Trasfer – State “B”
//——————————————————————————
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
}
//——————————————————————————
// 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
}
//——————————————————————————
// 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
}
//——————————————————————————
// 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;
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;
}
//——————————————————————————
// 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
}
//——————————————————————————
// Routine: i2c_read
// Inputs: none
// Outputs: input byte
// Purpose: Reads data from the I2C bus
//——————————————————————————
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;
}
////////////////////////////////////////////////////////////////////////////////
// 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++);
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/news/xinwen/260428.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部