Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need help for an EEPROM code

Status
Not open for further replies.

ADGAN

Full Member level 5
Joined
Oct 9, 2013
Messages
295
Helped
4
Reputation
8
Reaction score
4
Trophy points
18
Activity points
1,837
Hi! My project is a Energy meter. I'm getting pulses from an energy measurement IC. I want to save the total no of pulses on the EEPROM. I know how to read & write from the EEPROM using Mikroc PRO. I'm using the timer1 to count the pulses. I'm bit confused in which order to do it. This is what I thought.

First TMR1L = 0 , TMR1H = 0

while(1){
Then amount of pulses that are saved in the EEPROM and timer1 are moved to a variable called 'Pulses'.
Write pulses variable to the EEPROM
}
But very large values are displayed on the LCD when I do this. This is the code:

Code:
#define Highest(param) ((char *)&param)[3]
#define Higher(param) ((char *)&param)[2]
#define Hi(param) ((char *)&param)[1]
#define Lo(param) ((char *)&param)[0]

unsigned short lowByte, highByte, higherByte, highestByte, p_address;
unsigned long pulses;
unsigned short lowByte, highByte, higherByte, highestByte, p_address;
unsigned char txt[14],txt6[14];
unsigned long count;
unsigned long pulses;
unsigned long total_consumption;
unsigned long current_consumption;
unsigned long monthly_consumption;
double Meter_reading;
double Meter_Constant = 0.001;

void EEPROMWriteInt(unsigned short p_address, unsigned long int p_value) {
        lowByte = Lo(p_value); //((p_value >> 0) & 0xFF);
        highByte = Hi(p_value); //((p_value >> 8) & 0xFF);
        higherByte = Higher(p_value);
        highestByte = Highest(p_value);

        EEPROM_write(p_address, lowByte);
        delay_ms(20);
        EEPROM_write(p_address + 1, highByte);
        delay_ms(20);
        EEPROM_write(p_address + 2, higherByte);
        delay_ms(20);
        EEPROM_write(p_address + 3, highestByte);
        delay_ms(20);

        lowByte = 0x00;
        highByte = 0x00;
        higherByte = 0x00;
        highestByte = 0x00;

}

unsigned long int EEPROMReadInt(unsigned short p_address)
        {
        Lo(pulses) = EEPROM_read(p_address);
        delay_ms(20);
        Hi(pulses) = EEPROM_read(p_address + 1);
        delay_ms(20);
        Higher(pulses) = EEPROM_read(p_address + 2);
        delay_ms(20);
        Highest(pulses) = EEPROM_read(p_address + 3);
        delay_ms(20);

        return pulses;

}

void main()
{
     ANSEL = 0;
     ANSELH = 0;
     TRISA = 0;
     PORTA.RA0 = 1;
     C1ON_bit = 0;                      // Disable comparators
     C2ON_bit = 0;
     TRISB = 0;
     PORTB = 0;
     PORTC = 0;
     TRISC = 0x99;
     INTCON = 0xC0;

     PIE1.TMR1IE = 1;
     PIR1.TMR1IF = 0;

     T1CON = 0x03;
     TMR1L = 0;
     TMR1H = 0;

     Lcd_Init();
     Delay_ms(100);                  // Delay 100ms
     Lcd_Cmd(_LCD_CURSOR_OFF);
     Lcd_Cmd(_LCD_CLEAR);
    pulses = (count*65536)+ (TMR1H<<8 | TMR1L);
       pulses += EEPROMReadInt(0);

      while(1){


       pulses = (count*65536)+ (TMR1H<<8 | TMR1L);
       
       Delay_ms(20);
       EEPROMWriteInt(0x00, pulses);
       

      LongToStr(pulses,txt);
     Lcd_out(1,9,txt);

    
          }
   }
 
Last edited:

Where on the code above you had defined p_address value ?


+++
 

sorry about that made the adjustment.

- - - Updated - - -

I made a change to the code ( before the while loop) pls take a look at it.
 

Now, cont variable were not previously defined, and may exhibit anything.



+++
 

Sorry my bad I made the adjustments, really sorry about that.
 

Instead you debug program with all desired features, I could suggest you to perform a step-by-step experiments on each functionality, e.g LCD, Input, type casting, etc...


+++
 

Done but will it make difference to EEPROM

- - - Updated - - -

Its just showing negative values now.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top