main.c

来自「本光盘的所有代码均在Keil C51 7.0以上版本编译通过。读者的电脑只需要能」· C语言 代码 · 共 109 行

C
109
字号
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
#include <ADUC812.H>
#include <stdio.h>
#include "flash.h"

char code msgbuf [] = "This is a test to see if this thing really works.";
char xdata strbuf [101];

/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
long test_eeprom (void)
{
unsigned long i;
unsigned char dbuf;
unsigned char rbuf;

/*-----------------------------------------------
Erase the EEPROM
-----------------------------------------------*/
flash_erase_all ();

/*-----------------------------------------------
Write values into all of the EEPROM
-----------------------------------------------*/
for (dbuf=0, i=flash_size (); i>0; i--, dbuf++)
  {
  flash_write (&dbuf, sizeof (dbuf), i-1);
  }

/*-----------------------------------------------
Read values from EEPROM and compare them to the
original value that was written.  If they don't
match, return the failing address.
-----------------------------------------------*/
for (dbuf=0, i=flash_size (); i>0; i--, dbuf++)
  {
  flash_read (&rbuf, sizeof (dbuf), i-1);
  if (rbuf != dbuf)
    {
    return (i-1);
    }
  }

/*-----------------------------------------------
-1 indicates that everything matched
-----------------------------------------------*/
return (-1);
}

/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
void main (void)
{
long addr;

/*-----------------------------------------------
If we not talking to the monitor, then program
the on-chip UART.
-----------------------------------------------*/
#ifndef MON51
SCON  = 0x50;   // SCON: mode 1, 8-bit UART, enable rcvr
TMOD |= 0x20;   // TMOD: timer 1, mode 2, 8-bit reload
TH1   = 0xF4;   // TH1:  reload value for 2400 baud @ 11.0592 MHz
TR1   = 1;      // TR1:  timer 1 run
TI    = 1;      // TI:   set TI to send first char of UART
#endif

/*-----------------------------------------------
Test the EEPROM and print the results.
-----------------------------------------------*/
printf ("Testing the ERPROM\n");
addr = test_eeprom ();

if (addr == -1)
  {
  printf ("ERPROM test passed!\n");
  }
else
  {
  printf ("ERPROM test failed at %lu\n", addr);
  }

/*-----------------------------------------------
-----------------------------------------------*/
printf ("Erasing the EEPROM\n");
flash_erase_all ();

printf ("EEPROM Size = %lu bytes\n", (unsigned long) flash_size ());

printf ("Writing \"%s\" to the EEPROM\n", msgbuf);
flash_write (msgbuf, sizeof (msgbuf), 0);

flash_read  (strbuf, sizeof (msgbuf), 0);
printf ("Read    \"%s\" from the EEPROM\n", strbuf);

printf ("Erasing the EEPROM\n");
flash_erase_all ();

/*-----------------------------------------------
-----------------------------------------------*/
while (1);
}


/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/

⌨️ 快捷键说明

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