View previous topic :: View next topic |
Author |
Message |
emazzu630
Joined: 14 Aug 2018 Posts: 20 Location: Ouro Verde - SC
|
Write and read in EEPROM of PIC 16F877A [Solved] |
Posted: Fri Feb 08, 2019 11:14 pm |
|
|
Via the "S0" and "S1" button I change the "up" "down" addresses of the
eeprom memory, and "S2" and "S3" to "write" and "read" in the
addresses and it works very well. I can store values of "8 bits" correctly. I
have tried several examples in "16 bits", but it did not work.
Has anyone taken an equal situation and is willing to help me?
Code: |
#include <16F877a.h>
#fuses HS, NOWDT, NOPROTECT, NOBROWNOUT, NOLVP, NOCPD, PUT
#use delay(clock=4000000)
#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
#use fast_io(d)
#byte porta = 0x05
#byte portb = 0x06
#byte portc = 0x07
#byte portd = 0x08
#byte trisa = 0x85
#byte trisb = 0x86
#byte trisc = 0x87
#byte trisd = 0x88
///////////////////
// (code here) //
///////////////////
//**********************************************************
void mem_eeprom() {
///////////////////
// (codigo aqui) //
///////////////////
if (S3 == 0)
write_eeprom(adress, data); // write data in eeprom
if (S2 == 0) {
data = read_eeprom(adress); // writes data on 74hc595
while (S2 == 0); // wait to release S2
}
}
//**********************************************************
void main() {
///////////////////
// (codigo aqui) //
///////////////////
trisa = 0b11111111;
trisb = 0b00000000; // set the ports
trisc = 0b01011111;
trisd = 0b00000000;
delay_ms(200);
while (true){
mem_eeprom();
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Feb 09, 2019 3:23 am |
|
|
Look at the CCS driver file internal_eeprom.c.
It has these routines to write to 16-bit integers:
Quote: |
void write_int16_eeprom(address, int16 data)
void write_int16_eeprom(address, int16 data) |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Sat Feb 09, 2019 3:36 am |
|
|
There is though an 'issue' with the posted code. If your switch is held on for
even a short time, a lot of EEPROM 'lives' will be used.
Every time you call the write, it uses up a life. Hold the switch down for half
a second, and a hundred lives will have been used....
Do this at all often, and the EEPROM will be killed.
Code: |
if (S3 == 0)
{
int temp2;
temp2=read_eeprom(adress);
if (temp2!=data)
write_eeprom(adress, data); // write data in eeprom
}
|
This way it'll read what is already in the EEPROM, and only write if the new
'data' is different. Result a life is only used when the data changes. Hold
the button down, and only one write will occur. |
|
|
emazzu630
Joined: 14 Aug 2018 Posts: 20 Location: Ouro Verde - SC
|
|
Posted: Sat Feb 09, 2019 7:54 am |
|
|
Obrigado PCM programmer, Ttelmah... A ajuda deverá ser importante.
Last edited by emazzu630 on Sun Feb 24, 2019 5:41 pm; edited 5 times in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19553
|
|
Posted: Sat Feb 09, 2019 8:54 am |
|
|
PCM_programmer has already pointed you to the code for the int16.
Just include "internal_eeprom.c". It gives you functions that work exactly
like the single byte write, but with versions that support int1, int16, int32,
int48, float, int64 etc..
All of the codes you post look as if they should also work.
Remember the address will have to increment in 2's if you are writing
int16's. So you can't write an int16 to address 0, and then another to
address 1. You would need to write the second to address 2 etc.. |
|
|
emazzu630
Joined: 14 Aug 2018 Posts: 20 Location: Ouro Verde - SC
|
|
Posted: Sun Feb 24, 2019 5:23 pm |
|
|
After adding the "internal_eeprom.c" library and writing the code below,
with the counter for eeprom from 2 to 256, everything is working correctly
with write and read int16.
Code: |
if (S5 == 0) {
int16 temp2;
temp2 = read_int16_eeprom (adress);
if (temp2 != data) {
write_int16_eeprom( adress, data); // write data to eeprom
}
if (S4 == 0) {
data = read_int16_eeprom(adress); // read data in eeprom and writes it to the 74HC595 and display
while (S4 == 0); // wait while "S4" is low
|
|
|
|
|