home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / microcrn / issue_46.arc / EEPROM.C < prev    next >
Text File  |  1988-05-11  |  2KB  |  62 lines

  1. /* Figure 3 from ``Turning a PC into an embedded control system,''
  2.    Micro Cornucopia Magazine Issue #46 */
  3.  
  4. /* Turbo C program to read and write data on the 2816 EEPROM
  5.    by Bruce Eckel, EISYS Consulting */
  6.  
  7. #define BASE 0x220  /* address set on DIP switch */
  8.  
  9. void EEPROM_write(int address, unsigned char value) {
  10.   /* See figures 1 and 2 to understand this function */
  11.   outportb(BASE, address & 0xff);  /* low 8 bits */
  12.   /* High 3 bits and CE forced low */
  13.   outportb(BASE+1, (address >> 8) & 0x07);
  14.   /* Write the data: */
  15.   outportb(BASE + 2, value & 0xff);
  16.   /* 'delay()' is a function from Turbo C 1.5
  17.   to wait for a number of milliseconds.  If you
  18.   have version 1.0, write a simple "for" loop. */
  19.   delay(10); /* Wait 10 mS for EEPROM to finish writing */
  20. }
  21.  
  22. int EEPROM_read(int address) {
  23.   /* Place the address at the 2816 pins and force CE low: */
  24.   outportb(BASE, address & 0xff)
  25. ;
  26.   outportb(BASE+1, (address >> 8) & 0x07);
  27.   /* Read the data: */
  28.   return inportb(BASE + 2);
  29. }
  30.  
  31. main() {
  32.   int i, address, value;
  33.   /* First, dump the contents of the EEPROM: */
  34.   for (i = 0; i < 2048; i++) {
  35.     /* Generate breaks & line numbers: */
  36.     if ( i % 20 == 0) printf("\n%d: ",i);
  37.     printf("%d ",EEPROM_read(i-1));
  38.   }
  39.   while(1) {
  40.     printf("\nRead, Write, or Quit?\n");
  41.     switch(getch()) {
  42.       case 'R' : case 'r' :
  43.         printf("address to read? ");
  44.         scanf("%d",&address);
  45.         printf("%d\n", EEPROM_read(address));
  46.         break;
  47.       case 'W' : case 'w' :
  48.         printf("address to write? ");
  49.         scanf("%d",&address);
  50.         printf("\n");
  51.         printf("value to write? ");
  52.         scanf("%d", &value);
  53.         printf("\n");
  54.         EEPROM_write(address,value);
  55.         break;
  56.       case 'Q' : case 'q' :
  57.         exit(0);
  58.       default:
  59.     }
  60.   }
  61. }
  62.