⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 一些简单的c51程序
💻 C
字号:
/*-----------------------------------------------------------------------------
-----------------------------------------------------------------------------*/
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -