微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > C8051F单片机读写串行EEPROM程序

C8051F单片机读写串行EEPROM程序

时间:11-24 来源:互联网 点击:

// 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

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top