Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Problem with 16f876 and external eeprom (i2c)

Status
Not open for further replies.

dibe84

New Member
Hi, I've a problem with a Microchip PIC16F876.
I'm trying to write to an external eeprom (24LC32A) with the following circuit:

https://digilander.libero.it/dibe84/cirORb.JPG

and the following source code (written in C18 and compliled with PICCLITE):

#include "pic1687x.h"
#include "macro.h"
#include "delay.c"
#include "exteprom.c"
#include "i2c.c"

void main() {
BYTE idDevice1;
BYTE data1;
WORD address1;


ADCON0 = 0x00; // Disabilita A/D convertitore
ADCON1 = 0x07; // Configura PortA come digitale
PORTA = 0x00; // Reset ogni linea
PORTB = 0x00; // Reset ogni linea
PORTC = 0x00; // Reset ogni linea
TRISA = 0x00; // Tutte le porte definite come output
TRISB = 0x00; // Tutte le porte definite come output
TRISC = 0x00; // Tutte le porte definite come output

PORTB = 0b00000001; //lampeggio i led per prova
DelayMs(250);
PORTB = 0b00000000;
DelayMs(250);
PORTB = 0b00000001;
DelayMs(250);
PORTB = 0b00000000;
DelayMs(250);
DelayMs(250);

I2cInitMaster(); // Init I2c in modalità master


idDevice1 = 0x00; // Identificativo del device1
address1 = 0x00AA; // Indirizzo della cella eeprom utilizzato
data1 = 0x60; // Valore da scrivere nella eeprom

PORTB = 0x00; // Reset led

DelaySec(1);
ExtEpromWrite(idDevice1, address1, data1); // Scrive dato
ExtEpromWrite(idDevice1, 0x0001, 0x11); // Scrive dato
ExtEpromWrite(idDevice1, 0x0002, 0x12); // Scrive dato
ExtEpromWrite(idDevice1, 0x0003, 0x13); // Scrive dato

ExtEpromWrite(idDevice1, 0x00B2, 0x22); // Scrive dato
ExtEpromWrite(idDevice1, 0x00A0, 0x64); // Scrive dato
ExtEpromWrite(idDevice1, 0x00A1, 0x65); // Scrive dato
ExtEpromWrite(idDevice1, 0x00A0, 0x64); // Scrive dato
ExtEpromWrite(idDevice1, 0x00A2, 0x66); // Scrive dato


//lampeggio il led alla RB0, fine programma END

TRISB = 0;
DelayMs(250);
PORTB = 0b11111111;
DelayMs(250);
PORTB = 0b00000000;
DelayMs(250);
PORTB = 0b11111111;
DelayMs(250);
PORTB = 0b00000000;
DelayMs(250);
PORTB = 0b11111111;
DelayMs(250);
PORTB = 0b00000000;
DelayMs(250);
PORTB = 0b11111111;
DelayMs(250);
PORTB = 0b00000000;
DelayMs(250);
PORTB = 0b11111111;
DelayMs(250);
PORTB = 0b00000000;
DelayMs(250);
PORTB = 0b00000001;
DelayMs(250);
PORTB = 0b00000000;

here, you can find the inclued files
https://digilander.libero.it/dibe84/include.rar
(I think they works, I find them here: www.jofi.it/fiser)

The program write only few values on the external eeprom (for example:
ExtEpromWrite(idDevice1, 0x0001, 0x11); // Scrive dato
ExtEpromWrite(idDevice1, 0x0002, 0x12); // Scrive dato
ExtEpromWrite(idDevice1, 0x0003, 0x13); // Scrive dato

it writes on the last value (0x13)....
)

But it isn't the only problem because the program not always ends: usually restarts before the end of the it (I understand it can't see the sequence of lamping leds by the end of program). In the other hand if I disconnect the SDA or SCL pin the program always ends.

You can help me?!?!?

(sorry for my bad english, but I'm italian)

bye!!
 
buena sera,

I see several things which are suspicious.

First, there is no ending brace of the main function.
Second, what do you think happens when main() returns? There is no operating system to return to! You need to have some kind of "do forever" loop in main(). Something like:

Code:
    while(1)
    { }
or
    for(;;)
    {  }
or
    do
    {  } while(1)

Alternately if you can check the compiled code you can find out what happens when Main() returns. In the best case there should be a "loop: goto loop" instruction there. The alternative is too horrible to contemplate.

Ciao
 
Last edited:
Status
Not open for further replies.

Latest threads

Back
Top