|
|
View previous topic :: View next topic |
Author |
Message |
Eeprom_programmer
Joined: 07 Aug 2007 Posts: 24
|
How to file in a Eeprom |
Posted: Tue Aug 07, 2007 12:19 pm |
|
|
I am trying to log the date on a EEPROM, but my code doesn't want to work. Please help me for some reason my rs232 communication doesn't want to work either.
Thanks in advance
Code: |
#include "16F883.h"
//#include "time.h"
#use DELAY(clock=4000000)
#fuses HS,NOWDT,NOPROTECT,PUT,NOLVP,NOBROWNOUT,NOWRT
#define RS232_XMIT PIN_C6 // (output) RS232 serial transmit
#define RS232_RCV PIN_C7 // (input) RS232 serial receive
#define MAX_TICKS 4
#define OPAMP_GAIN 7.0
#define ANALOG_REF 5.0
#ifndef DAL_SCL
#define DAL_SCL PIN_C3
#define DAL_SDA PIN_C4
#define EE_BLOCK 7
#endif
#case
#use rs232(baud=9600, xmit=RS232_XMIT, rcv=RS232_RCV)
#use i2c(master,sda=DAL_SDA, scl=DAL_SCL)
#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
//unsigned int16 ee_address = 0;
//unsigned int16 sensor = 0; // holds ADC value of sensor
//unsigned int16 sensitivity=0 ; // holds ADC value of sensitivity
//unsigned char ticks = 0;
//unsigned char ms = 0;
//unsigned char second = 0;
//unsigned char minute = 0;
//unsigned char Cublice_num = 1;
// Writes to the slave board from the master.
void write_ext_eeprom(int16 address, unsigned char data)
{
i2c_start();
i2c_write(0xAE);
i2c_write((int8)(address>>8));
i2c_write((int8)address);
i2c_write(data);
i2c_stop();
delay_ms(5);
}
// Read from the slave board and display the data.
unsigned char read_ext_eeprom(int16 address )
{
unsigned char data_read;
i2c_start();
i2c_write(0xAE);
i2c_write( (int8)(address>>8));
i2c_write( (int8)address);
i2c_start();
i2c_write(0xAF);
data_read = i2c_read(0);
i2c_stop();
delay_ms(5);
return data_read;
}
// Data structures
// Data structure for logging
typedef struct
{
unsigned char dd, // Day
mt, // Month
hh, // Hour
mm, // Minute
ss; // Seconds
unsigned int16 lev; // Fluid Level
} lvlData;
// Level Data structure
lvlData levelData; // Stores info to be logged to EEPROM
// This function will log a single lvlData structure to EEPROM
//===================================================================================================================================
void logtoEE(unsigned int ee_address, lvlData *currentEntry)
{
// Move to correct block
ee_address = ee_address * EE_BLOCK;
// Write EEPROM
write_ext_eeprom(ee_address, currentEntry->dd); // Day
write_ext_eeprom(ee_address+1, currentEntry->mt); // Month
write_ext_eeprom(ee_address+2, currentEntry->hh); // Hour
write_ext_eeprom(ee_address+3, currentEntry->mm); // Minute
write_ext_eeprom(ee_address+4, currentEntry->ss); // Second
write_ext_eeprom(ee_address+5, currentEntry->lev >> 8); // Level (High byte)
write_ext_eeprom(ee_address+6, (currentEntry->lev)); // Level (Low byte)
}
//===================================================================================================================================
//===================================================================================================================================
// This function will read a single log from the EEPROM and store it in a lvlData structure
void readEE(unsigned int ee_address, lvlData *currentEntry)
{
// Move to correct block
ee_address = ee_address * EE_BLOCK;
// Read EEPROM
currentEntry->dd = read_ext_eeprom(ee_address); // Day
currentEntry->mt = read_ext_eeprom(ee_address+1); // Month
currentEntry->hh = read_ext_eeprom(ee_address+2); // Hour
currentEntry->mm = read_ext_eeprom(ee_address+3); // Minute
currentEntry->ss = read_ext_eeprom(ee_address+4); // Second
currentEntry->lev = read_ext_eeprom(ee_address+5); // Level (High Byte)
currentEntry->lev = (currentEntry->lev << 8) | (read_ext_eeprom(ee_address+6)); // Level (Low byte)
}
void setup()
{
set_tris_a(0x0B); // Port A Direction
// RA0 : Sensor Input (1)
// RA1 : Sensitivity Input (1)
// RA2 : Not used (0)
// RA3 : Not used (1)
// RA4 : Not used (0)
// RA5 : Not used (0)
set_tris_b(0x00); // Port B Direction
// RB0 : Not used (0)
// RB1 : RS-485 Direction Output (0)
// RB2 : LED1 Output (0)
// RB3 : LED2 Output (0)
// RB4 : LED3 Output (0)
// RB5 : RUN LED Output (0)
// RB6 : Not used (0)
// RB7 : Not used (0)
set_tris_c(0x80); // Port C Direction
// RC0 : Not used (0)
// RC1 : Not used (0)
// RC2 : Relay Output (0)
// RC3 : SCLK (0)
// RC4 : SDAT (0)
// RC5 : Not used (0)
// RC6 : Serial Transmit (0)
// RC7 : Serial Receive (1)
// ADC Setup
//enable all interrupts (else timer2 wont happen)
}
void main()
{
struct lvlData *currentEntry;
unsigned char i;
setup();
while(1)
{
for(i = 1 ; i <10; i++)
{
levelData.dd = 1;// Day
levelData.mt = 2; // Month
levelData.hh = 3; // Hour
levelData.mm = 4; // Minute;
levelData.ss = 5;
levelData.lev= 6;
logtoEE(i , i);
readEE (i , i);
printf( " \nDag %d" , currentEntry->dd); // Day
printf( " \nMaand %d" , currentEntry->mt); // Month
printf( " \nUur%d" , currentEntry->hh); // Hour
printf( " \nMinute%d" , currentEntry->mm); // Minute
printf( " \nSekondes%d" ,currentEntry->ss); // Second
delay_ms(500);
//printf( " \nLev1 %d" , currentEntry->lev); // Level (High Byte)
//printf( " \nLev2 %d" , currentEntry->lev); // Level (Low byte)
}
}
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Eeprom_programmer
Joined: 07 Aug 2007 Posts: 24
|
|
Posted: Tue Aug 07, 2007 2:48 pm |
|
|
Thx the RS232 works but the eeprom doesn't any ideas?
Code: |
#include "16F883.h"
//#include "time.h"
#use DELAY(clock=4000000)
#fuses HS,NOWDT,NOPROTECT,PUT,NOLVP,NOBROWNOUT,NOWRT
#define RS232_XMIT PIN_C6 // (output) RS232 serial transmit
#define RS232_RCV PIN_C7 // (input) RS232 serial receive
#define MAX_TICKS 4
#define OPAMP_GAIN 7.0
#define ANALOG_REF 5.0
#ifndef DAL_SCL
#define DAL_SCL PIN_C3
#define DAL_SDA PIN_C4
#define EE_BLOCK 7
#endif
#case
#use rs232(baud=9600, xmit=RS232_XMIT, rcv=RS232_RCV)
#use i2c(master,sda=DAL_SDA, scl=DAL_SCL)
#use fast_io(a)
#use fast_io(b)
#use fast_io(c)
//unsigned int16 ee_address = 0;
//unsigned int16 sensor = 0; // holds ADC value of sensor
//unsigned int16 sensitivity=0 ; // holds ADC value of sensitivity
//unsigned char ticks = 0;
//unsigned char ms = 0;
//unsigned char second = 0;
//unsigned char minute = 0;
//unsigned char Cublice_num = 1;
// Writes to the slave board from the master.
void write_ext_eeprom(int16 address, unsigned char data)
{
i2c_start();
i2c_write(0xAE);
i2c_write((int8)(address>>8));
i2c_write((int8)address);
i2c_write(data);
i2c_stop();
delay_ms(5);
}
// Read from the slave board and display the data.
unsigned char read_ext_eeprom(int16 address )
{
unsigned char data_read;
i2c_start();
i2c_write(0xAE);
i2c_write( (int8)(address>>8));
i2c_write( (int8)address);
i2c_start();
i2c_write(0xAF);
data_read = i2c_read(0);
i2c_stop();
delay_ms(5);
return data_read;
}
// Data structures
// Data structure for logging
typedef struct
{
unsigned char dd, // Day
mt, // Month
hh, // Hour
mm, // Minute
ss; // Seconds
unsigned int16 lev; // Fluid Level
} lvlData;
// Level Data structure
lvlData levelData; // Stores info to be logged to EEPROM
// This function will log a single lvlData structure to EEPROM
//===================================================================================================================================
void logtoEE(unsigned int ee_address, lvlData *currentEntry)
{
// Move to correct block
ee_address = ee_address * EE_BLOCK;
// Write EEPROM
write_ext_eeprom(ee_address, currentEntry->dd); // Day
write_ext_eeprom(ee_address+1, currentEntry->mt); // Month
write_ext_eeprom(ee_address+2, currentEntry->hh); // Hour
write_ext_eeprom(ee_address+3, currentEntry->mm); // Minute
write_ext_eeprom(ee_address+4, currentEntry->ss); // Second
write_ext_eeprom(ee_address+5, currentEntry->lev >> 8); // Level (High byte)
write_ext_eeprom(ee_address+6, (currentEntry->lev)); // Level (Low byte)
}
//===================================================================================================================================
//===================================================================================================================================
// This function will read a single log from the EEPROM and store it in a lvlData structure
void readEE(unsigned int ee_address, lvlData *currentEntry)
{
// Move to correct block
ee_address = ee_address * EE_BLOCK;
// Read EEPROM
currentEntry->dd = read_ext_eeprom(ee_address); // Day
currentEntry->mt = read_ext_eeprom(ee_address+1); // Month
currentEntry->hh = read_ext_eeprom(ee_address+2); // Hour
currentEntry->mm = read_ext_eeprom(ee_address+3); // Minute
currentEntry->ss = read_ext_eeprom(ee_address+4); // Second
currentEntry->lev = read_ext_eeprom(ee_address+5); // Level (High Byte)
currentEntry->lev = (currentEntry->lev << 8) | (read_ext_eeprom(ee_address+6)); // Level (Low byte)
}
void setup()
{
set_tris_a(0x0B); // Port A Direction
// RA0 : Sensor Input (1)
// RA1 : Sensitivity Input (1)
// RA2 : Not used (0)
// RA3 : Not used (1)
// RA4 : Not used (0)
// RA5 : Not used (0)
set_tris_b(0x00); // Port B Direction
// RB0 : Not used (0)
// RB1 : RS-485 Direction Output (0)
// RB2 : LED1 Output (0)
// RB3 : LED2 Output (0)
// RB4 : LED3 Output (0)
// RB5 : RUN LED Output (0)
// RB6 : Not used (0)
// RB7 : Not used (0)
set_tris_c(0x80); // Port C Direction
// RC0 : Not used (0)
// RC1 : Not used (0)
// RC2 : Relay Output (0)
// RC3 : SCLK (0)
// RC4 : SDAT (0)
// RC5 : Not used (0)
// RC6 : Serial Transmit (0)
// RC7 : Serial Receive (1)
// ADC Setup
//enable all interrupts (else timer2 wont happen)
}
void main()
{
while(1)
{
struct lvlData *currentEntry;
unsigned char i = 0;
setup();
printf( " \n Die datum " );
delay_ms(50);
levelData.dd = 1;// Day
levelData.mt = 2; // Month
levelData.hh = 3; // Hour
levelData.mm = 4; // Minute;
levelData.ss = 5;
levelData.lev= 6;
logtoEE(i , i);
readEE (i , i);
printf( " \nDag %d" , currentEntry->dd); // Day
printf( " \nMaand %d" , currentEntry->mt); // Month
printf( " \nUur%d" , currentEntry->hh); // Hour
printf( " \nMinute%d" , currentEntry->mm); // Minute
printf( " \nSekondes%d" ,currentEntry->ss); // Second
}
} |
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Aug 07, 2007 3:03 pm |
|
|
Code: | logtoEE(i , i);
readEE (i , i); | The second parameter is wrong in both calls. |
|
|
Eeprom_programmer
Joined: 07 Aug 2007 Posts: 24
|
|
Posted: Tue Aug 07, 2007 3:05 pm |
|
|
Thx, but can you maybe give me a hint to what it must be becaues i am trying to understand this, but it is hard. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Aug 07, 2007 3:19 pm |
|
|
Try the changes below (not tested)
Code: | void main()
{
int8 RecordNr; // Moved the variable declarations out of the loop.
// And I like int8 better as it is easier to port to other compilers.
struct lvlData currentEntry; // removed the '*' because you do not want a pointer but have to reserve memory.
setup(); // moved out of loop for optimization.
while(1)
{
RecordNr = 0;
printf( " \n Die datum " );
// delay_ms(50); no delay required here
levelData.dd = 1; // Day
levelData.mt = 2; // Month
levelData.hh = 3; // Hour
levelData.mm = 4; // Minute;
levelData.ss = 5;
levelData.lev= 6;
logtoEE(RecordNr, &levelData); // A '&' indicates the compiler we want to pass the address of the struct
readEE (RecordNr, ¤tEntry);
// Because we changed the declaration of currentEntry, we had to change the data access below from '->' to '.'
printf( "\nDag %d", currentEntry.dd); // Day
printf( "\nMaand %d", currentEntry.mt); // Month
printf( "\nUur %d", currentEntry.hh); // Hour
printf( "\nMinuut %d", currentEntry.mm); // Minute
printf( "\nSeconde %d", currentEntry.ss); // Second
}
} |
|
|
|
Eeprom_programmer
Joined: 07 Aug 2007 Posts: 24
|
|
Posted: Tue Aug 07, 2007 4:19 pm |
|
|
Thanx very much for the help, i understand a lot better now, still i get this errors
--- Info 300 "P:\main1.c" Line 83(2,9): More info: First Declaration of lvlData
*** Error 31 "P:\main1.c" Line 175(18,25): Identifier is already used in this scope
it has something to do with
Code: |
struct lvldata currentEntry
|
when i change it to
Code: |
struct lvldata *currentEntry
|
it works but returns the wrong values.
Sorry to bother |
|
|
SET
Joined: 15 Nov 2005 Posts: 161 Location: Glasgow, UK
|
|
Posted: Tue Aug 07, 2007 4:39 pm |
|
|
'levelData' declaration is missing:
Code: | struct lvlData currentEntry;
struct lvlData levelData; |
|
|
|
Eeprom_programmer
Joined: 07 Aug 2007 Posts: 24
|
|
Posted: Wed Aug 08, 2007 5:06 am |
|
|
Thx, but that only seems to create more errors... |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Aug 08, 2007 6:09 am |
|
|
My mistake (I said I didn't test it)
Forget the well intended suggestion from SET. In my posted main(), change Code: | struct lvlData currentEntry; | to Code: | lvlData currentEntry; |
|
|
|
SET
Joined: 15 Nov 2005 Posts: 161 Location: Glasgow, UK
|
|
Posted: Wed Aug 08, 2007 6:17 am |
|
|
Oops, I didnt see levelData declared further up, I only looked at the snippet in 'main' |
|
|
Eeprom_programmer
Joined: 07 Aug 2007 Posts: 24
|
|
Posted: Thu Aug 09, 2007 3:13 am |
|
|
So now i have both
Code: |
lvlData levelData
lvlData currentEntry
|
No error anymore - Thank you very much!
But the only value that is returned is -1
Any futher ideas would be greatly apreciated
Thank for all the help up to here! |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Aug 09, 2007 3:55 am |
|
|
What is your compiler version?
Where do you see the -1 values? |
|
|
eeprom_programmer* Guest
|
|
Posted: Thu Aug 09, 2007 8:26 am |
|
|
when i printf dd,hh,mt,ss etc.
My compiler version is V4.033
dd = -1
hh = -1
etc |
|
|
Eeprom_programmer
Joined: 07 Aug 2007 Posts: 24
|
|
Posted: Thu Aug 09, 2007 8:27 am |
|
|
Sorry i wasn't logged in.
Code: |
printf( " \nDag %d" , currentEntry->dd); // Day
printf( " \nMaand %d" , currentEntry->mt); // Month
printf( " \nUur%d" , currentEntry->hh); // Hour
printf( " \nMinute%d" , currentEntry->mm); // Minute
printf( " \nSekondes%d" ,currentEntry->ss); // Second
delay_ms(500);
|
The values being displayed here returns -1 values |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|