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 support@ccsinfo.com

Storing 10bit ADC value in internal EEPROM

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



Joined: 17 Oct 2005
Posts: 98

View user's profile Send private message

Storing 10bit ADC value in internal EEPROM
PostPosted: Tue Feb 07, 2006 5:21 am     Reply with quote

Hello forum,

I would like to store the 10bit ADC value from the ADC on a PIC12F675 into the internal EEPROM. My problem is my limited C knowledge.

I would like to store 4 10bit readings in 5 EEPROM addresses.

So how do I split the 10bit readings into 8bit segments?

I was thinking:

1. Store 4 10bit readings in 16bit integers.
2. Take the first 10bit reading and put the 8 LSB in the first EEPROM address.
3. Take the 2 MSB in the first 10bit reading and add the 6 LSB of the second 10bit reading and store this in the second EEPROM address.
4. Take the 4 MSB of the second 10bit reading and add the 4 LSB of the third 10bit reading and store this in the third EEPROM address.
5. You get the idea, right?

But I'm having problems contructing a 8bit int from the 16bit int.

How do I "extract" certain bits from an integer, and how do I put them into another integer?

I hope you can help me. Thanks.

Regards,
Futterama
dan king



Joined: 22 Sep 2003
Posts: 119

View user's profile Send private message

PostPosted: Tue Feb 07, 2006 7:19 am     Reply with quote

You can simply store the lsB by copying the 16b var into a 8 bit, the msB will be discarded. Then shift the 16 bit variable 8 places and store that result into the msB. An example follows where temp_val is 16bit and msb and lsb are 8 bit.

Code:
lsb = temp_val;
msb = (temp_val >>8);
write_eeprom(1,lsb);
write_eeprom(2,msb);


As far as packing the data into fewer locations, you'll have to play with manually masking( logical AND) and merging (logical OR).

Hope this helps. Let me know if you need more assistance.

Dan
Futterama



Joined: 17 Oct 2005
Posts: 98

View user's profile Send private message

PostPosted: Tue Feb 07, 2006 7:34 am     Reply with quote

I found a way to do it after much testing.

I made this function that will return the number of LSBs given:

Code:
int8 GetBits(int16 FromLongint, int8 BitsToGet)
{
   int8 result = 0;
   int8 temp, i;

   for(i=0; i<BitsToGet; i++)
   {
      if(FromLongint & 0x01)
         temp = 128;
      else
         temp = 0;
      result = result | temp;
      if(i<BitsToGet-1)
         result >>= 1;
      else
         result >>= 8-BitsToGet;
      FromLongint >>= 1;
   }
   return result;
}


I'll just have to make a simular function that will return a number of MSBs.
Tttelmah
Guest







PostPosted: Tue Feb 07, 2006 7:45 am     Reply with quote

Code:

int8 store[5];

void put10(int16 val,int8 locn_number) {
   //Save a ten bit value to a single 8bit location, plus two
   //bits in  fifth byte.
   int8 bits;
   store[locn_number]=make8(val,0);  //put the LSB into the array
   store[4]&=(0HFC << locn_number); //clear bits to write
   bits=(make8(val,1) & 3) << locn_number;
   store4|=bits;  //insert the extra two bits
}

int16 get10(int8 locn_number) {
   //retrieve a 10bit value from the array
   int8 mask;
   mask=(store[4] >> locn_number) & 3; //retrieve the two bits
   return make16(mask,store[locn_number]);
}

Untested, but the basic idea should work.
You will need to read the whole array out of eeprom, before retrieving a value using 'get10'. This will accept location numbers from 0 to 3, to access the four values. Similarly, once updated with 'put10', you will need to save at least the bottom byte, and the fifth byte to the eeprom.

Best Wishes
Futterama



Joined: 17 Oct 2005
Posts: 98

View user's profile Send private message

PostPosted: Wed Feb 08, 2006 4:53 am     Reply with quote

I'll just post my final code here:

Code:
int8 FourMSBs = 0;
int8 LSBs_address = 0;
int8 MSBs_address = 4;
int8 HelpAddress = 0;

void WriteEEPROM(int8 address, int8 data)
{
   // Write byte
   EEIF = 0; // Clear interrupt flag
   EEADR = address; // Set write address
   EEDATA = data; // Set data to write
   WREN = 1; // Enable EEPROM Write
   EECON2 = 0x55; // Following write sequence
   EECON2 = 0xAA; // Following write sequence
   WR = 1; // Initiate write
   while(!EEIF); // Wait for the write sequence to complete
   WREN = 0; // Disable EEPROM Write
   EEIF = 0; // Clear interrupt flag
}

int8 ReadEEPROM(int8 address)
{
   // Read byte
   EEADR = address; // Set read address
   RD = 1; // Initiate read
   return EEDATA; // Return data
}

void SaveToEEPROM(int16 data)
{
   int LSB, MSB;

   LSB = data;          // Get the 8 LSBs
   MSB = (data >> 8);   // Get the 8 MSBs (there is only 2)

   WriteEEPROM(LSBs_address, LSB);   // Write LSBs to EEPROM
   LSBs_address++;                   // Increment the LSB memory address
   HelpAddress++;                    // Increment the help address

   if(HelpAddress == 1)
      FourMSBs = FourMSBs | MSB;        // Put MSBs into bit 0 and 1
   if(HelpAddress == 2)
      FourMSBs = FourMSBs | (MSB * 4);  // Put MSBs into bit 2 and 3
   if(HelpAddress == 3)
      FourMSBs = FourMSBs | (MSB * 16); // Put MSBs into bit 4 and 5
   if(HelpAddress == 4)
      FourMSBs = FourMSBs | (MSB * 64); // Put MSBs into bit 6 and 7

   WriteEEPROM(MSBs_address, FourMSBs); // Write MSBs to EEPROM

   if(LSBs_address == MSBs_address)  // If the LSB address equals the MSB address (4, 9, 14, 19, 14, 29 ect.)
   {
      LSBs_address++;                   // Increment the LSB memory address
      MSBs_address = MSBs_address + 5;  // Increment the MSB memory address by 5
      FourMSBs = 0;                     // Reset variable
      HelpAddress = 0;                  // Reset variable
   }
}


In the EEPROM the data will be placed like this:
| 8LSBs | 8LSBs | 8LSBs | 8LSBs | 8MSBs from prev. 4 bytes |
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