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.

[SOLVED] saving data perminantly in pic16f877a

Status
Not open for further replies.

Qaisar Azeemi

Full Member level 5
Joined
Feb 11, 2011
Messages
315
Helped
16
Reputation
32
Reaction score
15
Trophy points
1,298
Location
Peshawar, Pakistan, Pakistan
Activity points
3,829
Hi all!

i want to save my data in the eeprom of pic16f877a. here is my code. i don't know where i am going wrong. kindly help me.
here is the part of my code for your ease. please ignore declerations. i am writing the full code related to eeprom.
Code:
void main()
{
//............................EEPROM default data...............................
 if ( EEPROM_Read(0x30)== 0);
 {
 EEPROM_Write(0x30, 0x0A );           // Write data to address 0x30 as default
 gen_wt=  EEPROM_Read(0x30);          // generator warm up time = 10sec
 }
 
 if ( EEPROM_Read(0x38)== 0);
 {
 EEPROM_Write(0x38, 0x02 );           // Write data to address 0x38 as default
 self_ont= EEPROM_Read(0x38);         // Self ON time = 2sec
 }
 
 if ( EEPROM_Read(0x40)== 0);
 {
 EEPROM_Write(0x40, 0x05 );           // Write data to address 0x40 as default
 self_oft= EEPROM_Read(0x40);         // Self OFF time = 5sec
 }
 
//..............................................................................

}


while(1)
  {

                    if ((enter==0) && (reset==1) && (up==1) && (down==1))// (Button(&PORTD,0,ms,0))  //when Enter is pressed
                     {
                          Delay_ms(ms); // wait for switch debounce
                          itrue=1;
                          x=0;
                          Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                          Lcd_Cmd(_LCD_CURSOR_OFF);
                          Lcd_Out(1,1,CopyConst2Ram(msg,ep));   //enter passward
                          delay_ms(t);
                          passward();
                          Delay_ms(1000);
                                   Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                   Lcd_Cmd(_LCD_CURSOR_OFF);
                                   Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                   Lcd_Out(1,1,CopyConst2Ram(msg,pw));   //please wait
                                   Delay_ms(1000);

                                   Lcd_Cmd(_LCD_CLEAR);
                                   Lcd_Out(1,1,CopyConst2Ram(msg,gwt));  // Generator warm up time

                                   inc=0;  //for selection of variable
                                   itrue=1;   // Entring and Exiting main menu
                                   opt=1;    // option b/w selection number or variable

                                             while(itrue==1)
                                             {
                                                  if ((enter==0) && (reset==1) && (up==1) && (down==1))//(Button(&PORTD,0,ms,0))  // pressing enter to enter time
                                                  {
                                                      Delay_ms(ms); // wait for switch debounce
                                                      opt=0;
                                                      Lcd_Out(2,1,CopyConst2Ram(msg,sp));
                                                  }

                                                      Delay_ms(200);

                                                  if ((enter==1) && (reset==0) && (up==1) && (down==1))//(Button(&PORTD,1,ms,0)) //press reset to exit
                                                  {
                                                     Delay_ms(ms); // wait for switch debounce
                                                     itrue=0;
                                                  }
                                                  
                                                      Delay_ms(200);
                                                  
                                                  if ((enter==1) && (reset==1) && (up==0) && (down==1))//(Button(&PORTD,2,ms,0))  //up
                                                  {
                                                      Delay_ms(ms); // wait for switch debounce
                                                      inc=scroll_up(opt,inc);
                                                  }

                                                       Delay_ms(200);

                                                  if ((enter==1) && (reset==1) && (up==1) && (down==0))//(Button(&PORTD,3,ms,0))   // down
                                                   {
                                                      Delay_ms(ms); // wait for switch debounce
                                                      inc=scroll_down(inc);
                                                   }
                                                   
                                                       Delay_ms(200);
                                             }

                                              x++;

                          Delay_ms(500);
                     }

and here is the scroll function that i.ve decleared before main() function.

Code:
// ----------------------- scroll up -------------------------------------------

     scroll_up( int opt ,int inc )
     {
       int j=1;

             if(opt==1)
             {
                  inc++;
                 Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                 Lcd_Cmd(_LCD_CURSOR_OFF);
                 Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);

                           if(inc==0)
                         Lcd_Out(1,1,CopyConst2Ram(msg,gwt));

                          if(inc==1)
                         Lcd_Out(1,1,CopyConst2Ram(msg,sont));

                          if(inc==2)
                          {
                          Lcd_Out(1,1,CopyConst2Ram(msg,soft));
                          inc=-1;
                          }
             } //end  if opt = 1

             if(opt==0)
             {
             
                if (inc < 0)
                   inc=2;
               // Lcd_Cmd(_LCD_CLEAR);                    // Clear display
               // Lcd_Cmd(_LCD_CURSOR_OFF);
                Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);

             //..................inc=0...gen_wt...............................
                        if(inc==0)
                        {
                           x=EEPROM_Read(0x30);
                          // x=gen_wt;
                            Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                            Lcd_Cmd(_LCD_CURSOR_OFF);
                          Lcd_Out(1,1,CopyConst2Ram(msg,gwt));
                          IntToStr(x, string);
                          Lcd_Out(2,10,string);

                           do
                           {
                               if((enter==1) && (reset==1) && (up==0) && (down==1) && j==1)//(Button(&PORTD,2,ms,0)&&j) // up pressed
                                 {   Delay_ms(ms); // wait for switch debounce
                                     x++;
                                 }

                                 if((enter==1) && (reset==1) && (up==1) && (down==0) && j==1)//(Button(&PORTD,2,ms,0)&&j) // up pressed
                                 {   Delay_ms(ms); // wait for switch debounce
                                     x--;
                                 }
                                 
                                     IntToStr(x, string);
                                     Lcd_Out(2,10,string);

                                     Delay_ms(500);
                                // }// end if

                                                   if((enter==0) && (reset==1) && (up==1) && (down==1))//( Button(&PORTD,0,ms,0)) //Enter pressed to save
                                                    {   Delay_ms(ms); // wait for switch debounce
                                                        gen_wt=x;
                                                        Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                                        Lcd_Cmd(_LCD_CURSOR_OFF);
                                                        Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                                        Delay_ms(500);
                                                        Lcd_Out(1,1,CopyConst2Ram(msg,sav));
                                                         EEPROM_Write(0x30, x );
                                                         Delay_ms(1000);
                                                         Lcd_Cmd(_LCD_CLEAR);
                                                         Lcd_Out(1,1,CopyConst2Ram(msg,gwt));
                                                         IntToStr(x, string);
                                                         Lcd_Out(2,10,string);
                                                        j=0; x=0; opt=1;
                                                     } // end saving

                        } while(j);
                  }// if inc=0
                  //.................... inc = 1 ...... self_ont ..............
                   if(inc==1)
                        {
                         x=EEPROM_Read(0x38);
                         // x=self_ont;
                          Lcd_Cmd(_LCD_CLEAR);                // Clear display
                          Lcd_Cmd(_LCD_CURSOR_OFF);
                          Lcd_Out(1,1,CopyConst2Ram(msg,sont));
                          IntToStr(x, string);
                          Lcd_Out(2,10,string);

                           do
                           {
                               if((enter==1) && (reset==1) && (up==0) && (down==1) && j==1)//(Button(&PORTD,2,ms,0)&&j) // up pressed
                                 {     Delay_ms(ms); // wait for switch debounce
                                     x++;
                                  }

                                 if((enter==1) && (reset==1) && (up==1) && (down==0) && j==1)//(Button(&PORTD,2,ms,0)&&j) // down pressed
                                 {   Delay_ms(ms); // wait for switch debounce
                                     x--;
                                 }
                                     IntToStr(x, string);
                                     Lcd_Out(2,10,string);

                                     Delay_ms(500);
                                // }// end if

                                                   if((enter==0) && (reset==1) && (up==1) && (down==1))//( Button(&PORTD,0,ms,0)) //Enter pressed to save
                                                    {    Delay_ms(ms); // wait for switch debounce
                                                        self_ont=x;
                                                        Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                                        Lcd_Cmd(_LCD_CURSOR_OFF);
                                                        Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                                        Delay_ms(500);
                                                        Lcd_Out(1,1,CopyConst2Ram(msg,sav));
                                                          EEPROM_Write(0x38, x );
                                                        Delay_ms(1000);
                                                         Lcd_Cmd(_LCD_CLEAR);
                                                         Lcd_Out(1,1,CopyConst2Ram(msg,sont));
                                                         IntToStr(x, string);
                                                         Lcd_Out(2,10,string);
                                                        j=0;  x=0;  opt=1;
                                                     } // end saving

                        } while(j);
                  }// if inc=1
                  //.....................inc=2................self_oft..................................
                   if(inc==2)
                        {
                           x=EEPROM_Read(0x40);
                           // x=self_oft;
                            Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                            Lcd_Cmd(_LCD_CURSOR_OFF);
                            Lcd_Out(1,1,CopyConst2Ram(msg,soft));
                            IntToStr(x, string);
                            Lcd_Out(2,10,string);

                           do
                           {
                               if((enter==1) && (reset==1) && (up==0) && (down==1) && j==1)//(Button(&PORTD,2,ms,0)&&j) // up pressed
                                 {   Delay_ms(ms); // wait for switch debounce
                                     x++;
                                 }

                                 if((enter==1) && (reset==1) && (up==1) && (down==0) && j==1)//(Button(&PORTD,2,ms,0)&&j) // up pressed
                                 {   Delay_ms(ms); // wait for switch debounce
                                     x--;
                                 }
                                     IntToStr(x, string);
                                     Lcd_Out(2,10,string);
                                     Delay_ms(500);
                                     inc=-1;
                               //  }// end if

                                                   if((enter==0) && (reset==1) && (up==1) && (down==1))//( Button(&PORTD,0,ms,0)) //Enter pressed to save
                                                    {    Delay_ms(ms); // wait for switch debounce
                                                        self_oft=x;
                                                        Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                                                        Lcd_Cmd(_LCD_CURSOR_OFF);
                                                        Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);
                                                        Delay_ms(500);
                                                        Lcd_Out(1,1,CopyConst2Ram(msg,sav));
                                                         EEPROM_Write(0x40, x );
                                                        Delay_ms(1000);
                                                        Lcd_Cmd(_LCD_CLEAR);
                                                         Lcd_Out(1,1,CopyConst2Ram(msg,soft));
                                                         IntToStr(x, string);
                                                         Lcd_Out(2,10,string);
                                                        j=0; x=0;  opt=1;
                                                     } // end saving

                                                    // inc=0;

                        } while(j);
                  }// if inc=2


             } // if opt = 0

              return (inc);

     } // end scroll up

 //-----------------------scroll Down ...........................................

     scroll_down(int inc )
     {
      // int j=1;
       inc--;

           if (inc < 0)
              inc=2;

             if(opt==1)
             {
                 Lcd_Cmd(_LCD_CLEAR);                    // Clear display
                 Lcd_Cmd(_LCD_CURSOR_OFF);
                 Lcd_Cmd(_LCD_MOVE_CURSOR_RIGHT);

                        if(inc==0)
                         Lcd_Out(1,1,CopyConst2Ram(msg,gwt));

                         if(inc==1)
                         Lcd_Out(1,1,CopyConst2Ram(msg,sont));

                          if(inc==2)
                         {
                          Lcd_Out(1,1,CopyConst2Ram(msg,soft));
                         }
             } //end  if opt = 1
         return (inc);
     }
 

hitech c compiler (library function)
----------------------------------
eeprom_write(address,data);
data= eeprom_read(address);
 

hare is exmp. in CCS compiler
**broken link removed**.
 

Data is not saving permanently. on power off the saved values are lost.

Are you sure that the data is being written to the eeprom?

Try this code

Code:
if ( EEPROM_Read(0x30)== 0);
 {
 EEPROM_Write(0x30, 0x0A );           // Write data to address 0x30 as default
}

gen_wt=  EEPROM_Read(0x30);        // generator warm up time = 10sec
 
 if ( EEPROM_Read(0x38)== 0);
 {
 EEPROM_Write(0x38, 0x02 );           // Write data to address 0x38 as default

 }
 
 self_ont= EEPROM_Read(0x38);         // Self ON time = 2sec

 if ( EEPROM_Read(0x40)== 0);
 {
 EEPROM_Write(0x40, 0x05 );           // Write data to address 0x40 as default
 
 }

self_oft= EEPROM_Read(0x40);         // Self OFF time = 5sec

The first time when addresses 0x30, 0x38, and 0x40 are 0 the values 10, 2, and 5 are written. Then the next time addresses 0x30, 0x38, and 0x40 will not be 0.
So, you should read the values into the 3 variables if the read eeprom statement are outside the if statements.
 
Last edited:

Reference: MikroC Pro for PIC User Manual, Section: EEPROM LIBRARY, Page:294
Ensure minimum 20ms delay between successive use of routines
EEPROM_Write and EEPROM_Read. Although PIC will write the correct value,
EEPROM_Read might return an undefined result.

BigDog
 

Thanks *** i did it... :) the problem was in sami colon after the if statment. now it is saving well.... :)

Code:
//............................EEPROM default data...............................
 if ( EEPROM_Read(0x30)== 0xff)
 EEPROM_Write(0x30, 0x0A);
 
 Delay_ms(20);          // Write data to address 0x30 as default
 gen_wt=  EEPROM_Read(0x30);          // generator warm up time = 10sec
 Delay_ms(20);

 
 if ( EEPROM_Read(0x38)== 0xff)
 EEPROM_Write(0x38, 0x02);           // Write data to address 0x38 as default
 
 Delay_ms(20);
 self_ont= EEPROM_Read(0x38);         // Self ON time = 2sec
 Delay_ms(20);

 
 if ( EEPROM_Read(0x40)== 0xff)
 EEPROM_Write(0x40, 0x05 );           // Write data to address 0x40 as default
 
 Delay_ms(20);
 self_oft= EEPROM_Read(0x40);         // Self OFF time = 5sec
 Delay_ms(20);


//..............................................................................    */

i am very thank full to all friends who helped me......
 

Thanks *** i did it... :) the problem was in sami colon after the if statment. now it is saving well.... :)

Excuse me?

The main problem was the missing 20ms delays between routine calls besides the missed placed semicolons.

Before:

Code:
void main()
{
//............................EEPROM default data...............................
 if ( EEPROM_Read(0x30)== 0);
 {
 EEPROM_Write(0x30, 0x0A );           // Write data to address 0x30 as default
 gen_wt=  EEPROM_Read(0x30);          // generator warm up time = 10sec
 }
 
 if ( EEPROM_Read(0x38)== 0);
 {
 EEPROM_Write(0x38, 0x02 );           // Write data to address 0x38 as default
 self_ont= EEPROM_Read(0x38);         // Self ON time = 2sec
 }
 
 if ( EEPROM_Read(0x40)== 0);
 {
 EEPROM_Write(0x40, 0x05 );           // Write data to address 0x40 as default
 self_oft= EEPROM_Read(0x40);         // Self OFF time = 5sec
 }
 
//..............................................................................

}

After:
Code:
//............................EEPROM default data...............................
 if ( EEPROM_Read(0x30)== 0xff)
 EEPROM_Write(0x30, 0x0A);
 
 [COLOR="#FF0000"]Delay_ms(20);[/COLOR]          // Write data to address 0x30 as default
 gen_wt=  EEPROM_Read(0x30);          // generator warm up time = 10sec
[COLOR="#FF0000"] Delay_ms(20);[/COLOR]

 
 if ( EEPROM_Read(0x38)== 0xff)
 EEPROM_Write(0x38, 0x02);           // Write data to address 0x38 as default
 
 [COLOR="#FF0000"]Delay_ms(20);[/COLOR]
 self_ont= EEPROM_Read(0x38);         // Self ON time = 2sec
[COLOR="#FF0000"] Delay_ms(20);[/COLOR]

 
 if ( EEPROM_Read(0x40)== 0xff)
 EEPROM_Write(0x40, 0x05 );           // Write data to address 0x40 as default
 
 [COLOR="#FF0000"]Delay_ms(20);[/COLOR]
 self_oft= EEPROM_Read(0x40);         // Self OFF time = 5sec
 [COLOR="#FF0000"]Delay_ms(20);[/COLOR]


//..............................................................................    */


BigDog
 

Excuse me?

The main problem was the missing 20ms delays between routine calls, not a missing semicolon.

BigDog

Absolutely not.... i have tried with 20ms delay but the result was the same and data was not saving. the old code was that:
Code:
//............................EEPROM default data...............................
 if ( EEPROM_Read(0x30)== 0xff);
{
 EEPROM_Write(0x30, 0x0A);
 
 Delay_ms(20);          // Write data to address 0x30 as default
 gen_wt=  EEPROM_Read(0x30);          // generator warm up time = 10sec
 Delay_ms(20);
}
 
 if ( EEPROM_Read(0x38)== 0xff);
{
 EEPROM_Write(0x38, 0x02);           // Write data to address 0x38 as default
 
 Delay_ms(20);
 self_ont= EEPROM_Read(0x38);         // Self ON time = 2sec
 Delay_ms(20);
}
 
 if ( EEPROM_Read(0x40)== 0xff);
{
 EEPROM_Write(0x40, 0x05 );           // Write data to address 0x40 as default
 
 Delay_ms(20);
 self_oft= EEPROM_Read(0x40);         // Self OFF time = 5sec
 Delay_ms(20);
}

//..............................................................................    */

but when i removed the sami colon it saved the data..... its tested result..... :cool:
 

Actually both the inclusion of 20ms delays and the removal of your missed placed semicolons are required.

According to the MikroC documentation, the routines will not function as expected without a minimum of 20ms delay between calls.

BigDog
 

you should put the read statement out of the if block. It check if the adresses contains 0xff, only then it reads the values into the variables, but on reset, the addresses will not contain 0xff so there will be no read operation.
 

Actually both the inclusion of 20ms delays and the removal of your missed placed semicolons are required.

According to the MikroC documentation, the routines will not function as expected without a minimum of 20ms delay between calls.

BigDog

Thank you very much bigdogguru you are quite right. i tested it. :)

you should put the read statement out of the if block. It check if the adresses contains 0xff, only then it reads the values into the variables, but on reset, the addresses will not contain 0xff so there will be no read operation.

you are right internetuser2k12, i write the routine accordingly. please see the 9th post of this thread.

another question that i wana ask is :

I want to read analog value at pin RA3 and disply it as volts on the lcd screen. where RA0, RA1 and RA2 are digital inputs....

and what should be the value of ADCON1 register .. now i have assign ADCON1=0x07; acording to datasheet to make all pins digital.. is there any technique to make RA3 analog and read the value of voltages? can its ADC read the value on RA3 when i kept ADCON1=0x07 ???
 
Last edited:

before reading ADC make ADC0N1 = 0b10000010; or 0x82; By this RA0 - RA4 will be analog and during this time RA0, RA1, and RA2 will not work. Then after reading the adc make ADCON1 to 0b10000111; or 0x87; then all AN pins will be digital, during this time RA0. RA1, and RA2 will work and RA3 will not work.

------------------Update-------------------

Also you should add

CVRCON.CVREN = 1;
CVRCON.CVROE = 0;

see page 141 of datasheet.
 

before reading ADC make ADC0N1 = 0b10000010; or 0x82; By this RA0 - RA4 will be analog and during this time RA0, RA1, and RA2 will not work. Then after reading the adc make ADCON1 to 0b10000111; or 0x87; then all AN pins will be digital, during this time RA0. RA1, and RA2 will work and RA3 will not work.

------------------Update-------------------

Also you should add

CVRCON.CVREN = 1;
CVRCON.CVROE = 0;

see page 141 of datasheet.

thank you very much internetuser2k12. i follow your instructions and got the result. but it still have problems.
prior to switching ADCON1 values the frequency detection was instantaneous.... that is my code was detecting the incoming pulses smoothly and correctly. but now when i start switching the adcon1 values to get the adc output the frequency is not that much correct. it seems to be delayed. i.e; before switching it was detecting the pulses on k==16 but now it is detecting the pulses on k==160.... which is very strange to me... here is the code:
Code:
void main()
{
ADCON1 = 0x07;
// ..... displaying Frequency..............................................
      
                                 
            for(;j==1;)
               {
                 if(G_freq==1 && redge==0)     // G_freq=porta.f2
                    {
                        G++;
                        redge=1;
                    }
                        else if(G_freq==0)
                        {
                                redge=0;
                        }
               // }

                         if(mains==P)  // jump out of loop when mains present
                         {j=0;}
                         
                         if(frq==1)   //on timer 1 interrupt
                         {
                             k++;

                                if(k==155)    // after 16 interrupts = 1sec
                                  {
                                     // l2=~l2;
                                      shortToStr(G,op);
                                      Lcd_Out(2,2,op);
                                      Lcd_Out_CP("Hz");
                                      Delay_ms(200);
                                      G=0;
                                      k=0;
                                   }
                             T1CON.TMR1ON=1;
                             frq=0;
                             
                         }

             ADCON1 =0x04;
                        //  ADC_Init();
                          tmp = ADC_Read(3);
                          Delay_ms(ms);
                          shortToStr(tmp,op);
                          Lcd_Out(2,10,op);
                          Lcd_Out_CP("V");
                          Delay_ms(ms);
                          ADCON1 = 0x07;
}

interrupt program:
Code:
void interrupt()
{
     if(PIR1.TMR1IF==1)
     { 

       PIR1.TMR1IF=0;  // <------------- clear the timer interrupt flag
     
       TMR1L=0X00;     // <------------- reload the timer
       TMR1H=0X00;

        frq=1;
        
     }
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top