CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

eeprom writing problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
deepakomanna



Joined: 06 Mar 2007
Posts: 92
Location: Pune,India

View user's profile Send private message AIM Address Yahoo Messenger

eeprom writing problem
PostPosted: Thu Aug 30, 2007 3:23 am     Reply with quote

Dear Sir,
here i am using 16f913, MPLAB 7.5 Ver. & CCS PCM C Compiler, Version 3.249, 34534.Also i am testing this using ICD 2.
Actually here i am facing problem for eeprom writing.
In timer0 interrupt routine i am storing data to eeprom every 6 sec(only for testing later it will be in houres).
Here the number should have to increment after 3 sec.And the incremented number should have to display on glass LCD.
if i declared the following statement in timer0 interrupt routine,the number is incremented after several minutes.
Code:
#INT_TIMER0
void timer0_isr(void)
{

// interrupt routine for 25 msec
sec_count++;
if(sec_count == 240)
   {
      //write data every 6 sec to eeprom.
      ee_write32(eeprom_address, number); //write data to EEPROM loc 0x00
      sec_count = 0;   
   }   
   
}

Another way i tried this,if i declare the following statement after the number++; it works as per logic.
Code:
if(min_count == 3)      // if frequency is 337 Hz, min_count == 3, becomes after 3 sec.
         {               // so number is incremented after 3 sec.
            number++;
// =======  if eeprom write declared here,itworks as per specification ===============
//            ee_write32(eeprom_address, number); //write data to EEPROM loc 0x00
//=============================================================
            if(number == 999999)
               number = 0;
            min_count = 0;
         }

so help me to sort out this problem. here is my full code..if any quries let me know
Code:
#include<16F913.h>
#fuses INTRC_IO,NOWDT,PUT,/*****/MCLR,/****/NOPROTECT,NOCPD,NOBROWNOUT,NOIESO,NOFCMEN   //external MCLR bcz of MPLAB ICD2
#use delay(clock=8000000)
/////////////////////////////////////////////////////////////////////////////////////////
//                                 LCD Configuration                                   //
/////////////////////////////////////////////////////////////////////////////////////////
// Digit segments    A              B              C              D              E              F              G           DP
//                         b7             b6             b5             b4             b3             b2            b1             b0
#define DIGIT1  COM3+6,   COM2+6,   COM0+6,   COM1+6,   COM0+5,   COM3+5,   COM2+5,   COM1+5      // DISPALYS FROM RIGHT SIDE
#define DIGIT2  COM3+7,   COM2+7,   COM0+7,   COM1+7,   COM0+4,   COM3+4,   COM2+4               // DISPALYS FROM RIGHT SIDE
#define DIGIT3  COM3+8,   COM2+8,   COM0+8,   COM1+8,   COM0+3,   COM3+3,   COM2+3               // DISPALYS FROM RIGHT SIDE
#define DIGIT4  COM3+9,   COM2+9,   COM0+9,   COM1+9,   COM0+2,   COM3+2,   COM2+2                  // DISPALYS FROM RIGHT SIDE
#define DIGIT5 COM3+11,   COM2+11,   COM0+11,   COM1+11,   COM0+1,   COM3+1,   COM2+1                  // DISPALYS FROM RIGHT SIDE
#define DIGIT6 COM3+12,   COM2+12,   COM0+12,   COM1+12,   COM0+0,   COM3+0,   COM2+0                  // DISPALYS FROM RIGHT SIDE
#define VLCD_ENABLE 0x10
#define BLANK 0
#define DOT 1
#define eeprom_address 0

//character                     0         1        2         3        4        5        6         7       8        9      
byte const Digit_Map[10] = {0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xF6};
byte lcd_pos;
byte segments;

void init_CPU();
int32 ee_read32(int16);
void ee_write32(int16,int32);
int16 sec_counter = 0;
int16 min_count = 0;
int32 number = 0;
int1 flag = 1;
int1 flag2 = 0;
int8 i = 0;
int16 sec_count;

void lcd_putc(char c)
{

 if(c=='\f')
     lcd_pos=0;
else {
           if((c>='0')&&(c<='9'))
               {
                  segments=Digit_Map[c-'0'];
               }
            else
               segments=BLANK;
            
      switch(lcd_pos)
       {
          case 1:  lcd_symbol(segments,DIGIT6); break;     // fill 1s place
         case 2:  lcd_symbol(segments,DIGIT5); break;      // fill 10s place
              case 3:  lcd_symbol(segments,DIGIT4); break;     // fill 100s place
             case 4 : lcd_symbol(segments,DIGIT3); break;     // fill  1000s place
              case 5 :if(flag2 == 1)
                        lcd_symbol(segments,DIGIT2); break;      // fill  10000splace
                case 6 :
                      
                    if((flag == 1))
                        {
                           lcd_symbol((DOT |segments),DIGIT1);    // fill  100000s place
                        }
                     else
                        {
                           lcd_symbol(segments,DIGIT1);
                        }
                         if(number <10)   
                           {      
                               flag2 = 0;  // flag2 here is used to display 0.0. If we not used this data displayed is only 0 till it reaches to 10.
                               lcd_symbol(Digit_Map[i],DIGIT2);
                            }
                            
                         else
                            flag2 = 1;
                                     
                      break;
              
            }
   }
 lcd_pos++;
}

#int_ccp1
void ccp1_isr(void)
{
   // ccp1 interrupt is used to measure the count between two rising pulses. Here we
   //are connected frequency generator to pin no 16 of 16f913

sec_counter++;
if(sec_counter >= 337)
   {
      flag =~flag;      // compliment flag for decimal point.
      sec_counter = 0;
      min_count++;
      if(min_count == 3)      // if frequency is 337 Hz, min_count == 3, becomes after 3 sec.
         {               // so number is incremented once after 3 sec.
            number++;
// =======  if eeprom write declared here,itworks as per specification ===============
//            ee_write32(eeprom_address, number); //write data to EEPROM loc 0x00
//=============================================================
            if(number == 999999)
               number = 0;
            min_count = 0;
         }
   }
//set_timer1(0);           

}

#INT_TIMER0
void timer0_isr(void)
{

// interrupt routine for 25 msec
sec_count++;
if(sec_count == 240)
   {
      //write data every 6 sec to eeprom.
// Declaration of eeprom write here will take more time to increment the number.....
      ee_write32(eeprom_address, number); //write data to EEPROM loc 0x00
      sec_count = 0;   
   }   
   
}

void main()
{
init_CPU();

while(TRUE)
     {      
            number = ee_read32(eeprom_address); //read data from EEPROM loc 0x00
            printf(lcd_putc,"\f%6lu",number);   
            
   }
}

int32 ee_read32(int16 base_address)
{
int8 j;
int32 data_read;
for(j=0; j<4; j++)
   *(&data_read+j) = read_eeprom(base_address+j);
return (data_read);

}
void ee_write32(int16 base_address ,int32 data)
{
int8 j;
for(j=0; j<4; j++)
   write_eeprom(base_address+j,*(&data+j));

}

void  init_CPU()
{

/**************  PORT SETTINGS ****************/
   PORT_B_PULLUPS(0XC0);      // RB7 & RB6 i/p for programming devices.
   SET_TRIS_A(0X80);         //
   SET_TRIS_B(0XC0);
   SET_TRIS_C(0X27);         //VLCD 1,2,3 i/p
   SET_TRIS_E(0X08);         // RE3 i/p for programming the device.
/*************** LCD SETTINGS ********************/
   SETUP_LCD( LCD_MUX14 |LCD_INTRC|VLCD_ENABLE, 2);   
/****************  COMPARATOR SETTINGS  ***************/
   SETUP_COMPARATOR(NC_NC_NC_NC);
/****************  INTERRUPT SETTINGS  *****************/
   ENABLE_INTERRUPTS(GLOBAL);
   ENABLE_INTERRUPTS(INT_TIMER0);      //enable timer1 interrupt
   SET_TIMER0(61);                     // timer0 interrupt for 25 msec.
   SETUP_TIMER_0(RTCC_INTERNAL|RTCC_DIV_256);
/***************************************************************/
      
set_timer1(0);           
 setup_ccp1(CCP_CAPTURE_RE);       //Capture on rising edge
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
}

_________________
Thank You,
With Best Regards,
Deepak.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Thu Aug 30, 2007 8:26 am     Reply with quote

I only looked at your code quickly but I noticed one thing.

Code:
void ee_write32(int16 base_address ,int32 data)
{
int8 j;
for(j=0; j<4; j++)
   write_eeprom(base_address+j,*(&data+j));

}


This part only has 256 bytes of eeprom inside of it. You have declared your addressing as a 16 bit value. This can only be an 8 bit value. You can only write one byte at a time, also. You have declared this as a 32 bit value. Things aren't going to be compatible this way. When your address goes above 255 it will start over-writing the eeprom at address 0 again. Your 32 bit variable will only be saving the lowest 8 bits of data.

Ronald
deepakomanna



Joined: 06 Mar 2007
Posts: 92
Location: Pune,India

View user's profile Send private message AIM Address Yahoo Messenger

eeprom writing problem
PostPosted: Thu Aug 30, 2007 9:51 pm     Reply with quote

Thanks for notifieng this,
After this i have changed
int16 base_address to int8 base_address
And i want to store 32 bit data so i choosed
int32 data
still the problem is not solved.
any help appriciated
_________________
Thank You,
With Best Regards,
Deepak.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Fri Aug 31, 2007 8:51 am     Reply with quote

You will need to write one byte at a time. You could pass the 32bit variable to ee_write32() and then shift/mask the data, inside of the function, so you can save each 8bit portion of it. You will need to re-construct the variable when you read it from the eeprom.

Keep in mind that you will only have enough room to store 64 - 32bit variables this way since each one will take 4 bytes of space.

Ronald
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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