Interfaccia EEPROM    24CL16 e pic 16F876                           

 

Il codice  permette di interfacciare una eeprom tipo 24LC16 ad un pic 16F876.

Il protocolllo di comunicazione utilizzato è l'I2C; il quarzo usato è di 4MHz .

Le istruzioni per scrivere o leggere dalla eeprom, sono inviate tramite porta seriale

collegata al PC  utilizzando un software d'interfacciamento come hyperterminal.

I parametri di settaggio sono quelli visibili nel file.h giù in basso.

 
Il codice è stato scritto con il compilatore CCS che trovate al seguente link:

 

                                SCHEMA ELETTRICO

 
 


CCS compiler

Il codice sorgente:


#include "C:\CODICI PIC\I2c_EEPROM_11_03_2011\primok_eprom.h"
#include "input.c"
void main() {

   BYTE value, cmd;
   EEPROM_ADDRESS address;
  
   init_ext_eeprom();

   do {
      do {
        
         printf("\r\nRead or Write: ");
         cmd=getc();
         cmd=toupper(cmd);
         putc(cmd);
      } while ( (cmd!='R') && (cmd!='W') );

      printf("\n\rLocation: ");

#if sizeof(EEPROM_ADDRESS)==1
      address = gethex();
#else
#if EEPROM_SIZE>0xfff
      address = gethex();
#else
      address = gethex1();
#endif
      address = (address<<8)+gethex();
#endif

      if(cmd=='R'){
    
        
         printf("\r\nValue: %X\r\n",READ_EXT_EEPROM( address ) );
                
      }
      if(cmd=='W') {
         printf("\r\nNew value: ");
         value = gethex();
         printf("\n\r");
         WRITE_EXT_EEPROM( address, value );
         output_B(value);
         
      }
   
   } while (TRUE);
}

Il file.h

#include <16F876.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES XT                      //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES PUT                      //Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection

#use delay(clock=4000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

#define EEPROM_SDA  PIN_C3
#define EEPROM_SCL  PIN_C4

#use i2c(master,fast, sda=EEPROM_SDA, scl=EEPROM_SCL)

#define EEPROM_ADDRESS unsigned int16
#define EEPROM_SIZE   1024

void init_ext_eeprom() {
  output_float(EEPROM_SCL);
 output_float(EEPROM_SDA);
}

int1 ext_eeprom_ready() {
   int1 ack;
   i2c_start();            // If the write command is acknowledged,
   ack = i2c_write(0xA0);  // then the device is ready.
   delay_us(10);
   i2c_stop();
   return !ack;
}

void write_ext_eeprom(EEPROM_ADDRESS address, BYTE data) {
  while(!ext_eeprom_ready());
   
   i2c_start();
   i2c_write((0xa0|(BYTE)(address>>7))&0xfe);
   i2c_write(address);
   i2c_write(data);
   i2c_stop();
     
}

BYTE read_ext_eeprom(EEPROM_ADDRESS address) {
   BYTE data;

  while(!ext_eeprom_ready());
   i2c_start();
   i2c_write((0xa0|(BYTE)(address>>7))&0xfe);
   i2c_write(address);
   i2c_start();
   i2c_write((0xa0|(BYTE)(address>>7))|1);
   data=i2c_read(0);
   i2c_stop();
   return(data);
}

 Qui di seguito una versione di simulazione con proteus
 

Download Firmware

No comments

Leave your comment

In reply to Some User