📄 spi_ee_poll1.c
字号:
// P0.4 (MOSI) as push-pull outputs
P1MDOUT |= 0xC0; // enable P1.6 (LED) and P1.7 (EE_CS)
// as push-pull outputs
}
//-----------------------------------------------------------------------------
// SPI0_Init
//-----------------------------------------------------------------------------
//
// Configure SPI0 for 8-bit, 2MHz SCK, Master mode, polled operation, data
// sampled on 1st SCK rising edge.
//
void SPI0_Init (void)
{
SPI0CFG = 0x07; // data sampled on 1st SCK rising edge
// 8-bit data words
SPI0CN = 0x03; // Master mode; SPI enabled; flags
// cleared
SPI0CKR = SYSCLK/2/2000000; // SPI clock <= 2MHz (limited by
// EEPROM spec.)
}
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
//
void UART0_Init (void)
{
SCON0 = 0x50; // SCON0: mode 1, 8-bit UART, enable RX
TMOD = 0x20; // TMOD: timer 1, mode 2, 8-bit reload
TH1 = -(SYSCLK/BAUDRATE/16); // set Timer1 reload value for baudrate
TR1 = 1; // start Timer1
CKCON |= 0x10; // Timer1 uses SYSCLK as time base
PCON |= 0x80; // SMOD00 = 1 (disable baud rate
// divide-by-two)
TI0 = 1; // Indicate TX0 ready
}
//-----------------------------------------------------------------------------
// Timer0_ms
//-----------------------------------------------------------------------------
//
// Configure Timer0 to delay <ms> milliseconds before returning.
//
void Timer0_ms (unsigned ms)
{
unsigned i; // millisecond counter
TCON &= ~0x30; // STOP Timer0 and clear overflow flag
TMOD &= ~0x0f; // configure Timer0 to 16-bit mode
TMOD |= 0x01;
CKCON |= 0x08; // Timer0 counts SYSCLKs
for (i = 0; i < ms; i++) { // count milliseconds
TR0 = 0; // STOP Timer0
TH0 = (-SYSCLK/1000) >> 8; // set Timer0 to overflow in 1ms
TL0 = -SYSCLK/1000;
TR0 = 1; // START Timer0
while (TF0 == 0); // wait for overflow
TF0 = 0; // clear overflow indicator
}
}
//-----------------------------------------------------------------------------
// Timer0_us
//-----------------------------------------------------------------------------
//
// Configure Timer0 to delay <us> microseconds before returning.
//
void Timer0_us (unsigned us)
{
unsigned i; // millisecond counter
TCON &= ~0x30; // STOP Timer0 and clear overflow flag
TMOD &= ~0x0f; // configure Timer0 to 16-bit mode
TMOD |= 0x01;
CKCON |= 0x08; // Timer0 counts SYSCLKs
for (i = 0; i < us; i++) { // count microseconds
TR0 = 0; // STOP Timer0
TH0 = (-SYSCLK/1000000) >> 8; // set Timer0 to overflow in 1us
TL0 = -SYSCLK/1000000;
TR0 = 1; // START Timer0
while (TF0 == 0); // wait for overflow
TF0 = 0; // clear overflow indicator
}
}
//-----------------------------------------------------------------------------
// EE_Read
//-----------------------------------------------------------------------------
//
// This routine reads and returns a single EEPROM byte whose address is
// given in <Addr>.
//
unsigned char EE_Read (unsigned Addr)
{
unsigned char retval; // value to return
EE_CS = 0; // select EEPROM
EE_CS = 0; // find out why Keil compiler is
// optimizing one of these out
Timer0_us (1); // wait at least 250ns (CS setup time)
// transmit READ opcode
SPIF = 0;
SPI0DAT = EE_READ;
while (SPIF == 0);
// transmit Address MSB-first
SPIF = 0; // transmit MSB of address
SPI0DAT = (Addr >> 8);
while (SPIF == 0);
SPIF = 0; // transmit LSB of address
SPI0DAT = Addr;
while (SPIF == 0);
// initiate dummy transmit to read data
SPIF = 0;
SPI0DAT = 0;
while (SPIF == 0);
retval = SPI0DAT; // read data from SPI
Timer0_us (1); // wait at least 250ns (CS hold time)
EE_CS = 1; // de-select EEPROM
Timer0_us (1); // wait at least 500ns (CS disable time)
return retval;
}
//-----------------------------------------------------------------------------
// EE_Write
//-----------------------------------------------------------------------------
//
// This routine writes a single EEPROM byte <value> to address <Addr>. Here
// we implement post-write polling, and return once the write operation has
// completed. This prevents us from having to poll before an EEPROM Read
// or Write operation.
//
void EE_Write (unsigned Addr, unsigned char value)
{
EE_CS = 0; // select EEPROM
Timer0_us (1); // wait at least 250ns (CS setup time)
// transmit WREN (Write Enable) opcode
SPIF = 0;
SPI0DAT = EE_WREN;
while (SPIF == 0);
Timer0_us (1); // wait at least 250ns (CS hold time)
EE_CS = 1; // de-select EEPROM to set WREN latch
Timer0_us (1); // wait at least 500ns (CS disable
// time)
EE_CS = 0; // select EEPROM
Timer0_us (1); // wait at least 250ns (CS setup time)
// transmit WRITE opcode
SPIF = 0;
SPI0DAT = EE_WRITE;
while (SPIF == 0);
// transmit Address MSB-first
SPIF = 0; // transmit MSB of address
SPI0DAT = (Addr >> 8);
while (SPIF == 0);
SPIF = 0; // transmit LSB of address
SPI0DAT = Addr;
while (SPIF == 0);
// transmit data
SPIF = 0;
SPI0DAT = value;
while (SPIF == 0);
Timer0_us (1); // wait at least 250ns (CS hold time)
EE_CS = 1; // deselect EEPROM (initiate EEPROM
// write cycle)
// now poll Read Status Register (RDSR) for Write operation complete
do {
Timer0_us (1); // wait at least 500ns (CS disable
// time)
EE_CS = 0; // select EEPROM to begin polling
Timer0_us (1); // wait at least 250ns (CS setup time)
SPIF = 0;
SPI0DAT = EE_RDSR; // send Read Status register opcode
while (SPIF == 0);
SPIF = 0;
SPI0DAT = 0; // dummy write to read status register
while (SPIF == 0);
Timer0_us (1); // wait at least 250ns (CS hold
// time)
EE_CS = 1; // de-select EEPROM
} while (SPI0DAT & 0x01); // poll until WIP (Write In
// Progress) bit goes to '0'
Timer0_us (1); // wait at least 500ns (CS disable
// time)
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -