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

Help with internal eeprom programming algorithm

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



Joined: 07 May 2011
Posts: 40
Location: Bulgaria

View user's profile Send private message

Help with internal eeprom programming algorithm
PostPosted: Thu Sep 06, 2012 10:59 am     Reply with quote

I'm trying to make a program to count button pushes and depending on the result, to program the internal eeprom with different values.
For example:4 pushes of the button - value=1
5 pushes of the same button - value=2
6 pushes of the same button - value=3.
I made this program i don't know how to reset the i variable (i=0) when i=4. I mean that if i continue pushing the button next time i=5 and next i=6.
I want to make the program to count to 4 (for 4 button pushes), write eeprom. Next time i want to push the button 5 times write the other value and next, another 6 times to write the last value to the eeprom.
I'm not sure i managed to explain what i want. I hope you understand.
Code:

if(input(PIN_A5)==1)
 {
   i++;
   if(i>6)
   i=6;
   
   while(input(PIN_A5)) //loop here until button released.
   {
    delay_ms(20);
   }
   
   if(i==4)
   {
    write_int16_eeprom(u4,0);
   }
   
   if(i==5)
   {
    write_int16_eeprom(u4,1);
   }
   
   if(i==6)
   {
    write_int16_eeprom(u4,2);
   }
 
 }
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Thu Sep 06, 2012 11:19 am     Reply with quote

IF i understood correctly, then:



where you declare "i":

Code:
Int i=3;
//<--- start at 3 since you do i++ as soon as you enter your button function.


and then...

Code:
if(input(PIN_A5)==1)
 {
   i++;
   if(i>6)
   i=4;    //<---------- effectively now your allowed values will be 4,5,6 only.
   
   while(input(PIN_A5)) //loop here until button released.
   {
    delay_ms(20);
   }
   
   if(i==4)
   {
    write_int16_eeprom(u4,0);
   }
   
   if(i==5)
   {
    write_int16_eeprom(u4,1);
   }
   
   if(i==6)
   {
    write_int16_eeprom(u4,2);
   }
 
 }

_________________
CCS PCM 5.078 & CCS PCH 5.093
tonkostz



Joined: 07 May 2011
Posts: 40
Location: Bulgaria

View user's profile Send private message

PostPosted: Thu Sep 06, 2012 11:30 am     Reply with quote

It seems like i did not manage to explain correctly what i want. I'll try again.
I push the button 4 times and 0 is written to the eeprom.
Next time i want to push the button 5 times to write 1 and 6 times to write 2. This is what i want.

My current program is doing something else:I push 4 times the button and i=4. Next pushes start from 4, so only 1 push is needed to reach i=5 and 2 pushes to reach 6.
I want after the first 4 pushes i variable to be reset to 0 (i=0).
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Fri Sep 07, 2012 2:20 am     Reply with quote

I think you need a time-out on your button function.

Start the button function with i=0.

If a button has been pressed within the time out, i++.

If a button has not been pressed within the time limit you've finished.

When finished, store required value to eeprom.

Mike

EDIT Beware of wearing out your eeprom!
tonkostz



Joined: 07 May 2011
Posts: 40
Location: Bulgaria

View user's profile Send private message

PostPosted: Fri Sep 07, 2012 6:27 am     Reply with quote

Mike Walne wrote:
I think you need a time-out on your button function.

Start the button function with i=0.

If a button has been pressed within the time out, i++.

If a button has not been pressed within the time limit you've finished.

When finished, store required value to eeprom.

Mike

EDIT Beware of wearing out your eeprom!

Do i need to use interrupts or some button interrupt code for that?
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Fri Sep 07, 2012 6:31 am     Reply with quote

Ah!
then this maybe...
(i didnt test it)

Code:
if(input(PIN_A5)==1)
 {
   i++;
   if(i>6)
   i=6;
   
   while(input(PIN_A5)) //loop here until button released.
   {
    delay_ms(20);
   }
   
   if(i==4)
   {
    write_int16_eeprom(u4,0);
    i=0;
   }
   
   if(i==5)
   {
    write_int16_eeprom(u4,1);
    i=0;
   }
   
   if(i==6)
   {
    write_int16_eeprom(u4,2);
    i=0;
   }
 
 }


...

G
_________________
CCS PCM 5.078 & CCS PCH 5.093
tonkostz



Joined: 07 May 2011
Posts: 40
Location: Bulgaria

View user's profile Send private message

PostPosted: Fri Sep 07, 2012 6:52 am     Reply with quote

Gabriel wrote:
Ah!
then this maybe...
(i didnt test it)

Code:
if(input(PIN_A5)==1)
 {
   i++;
   if(i>6)
   i=6;
   
   while(input(PIN_A5)) //loop here until button released.
   {
    delay_ms(20);
   }
   
   if(i==4)
   {
    write_int16_eeprom(u4,0);
    i=0;
   }
   
   if(i==5)
   {
    write_int16_eeprom(u4,1);
    i=0;
   }
   
   if(i==6)
   {
    write_int16_eeprom(u4,2);
    i=0;
   }
 
 }


...

G

I already have tested it and nothing happened. I mean that nothing was written to the eeprom. Reason for that i think is this confusing statement:
if(i==4)
i=0;
or
if(i==5)
i=0;
or
if(i==6)
i=0;

EDIT:That was my first idea:to count to 4 for example, write eeprom and after some time to reset i(i=0). I don't know how to do it.
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Fri Sep 07, 2012 9:33 am     Reply with quote

post your entire test code... should be a small program.

are you sure your eeprom writing function works?
or your reading eeprom function?

PIC number, Compiler number... more details...

im not really trusting this:
Code:
write_int16_eeprom(u4,0);


"u4" is not a hex number thus not an address.... im assuming thats the address since id does not change in your statements...
i might be missing somthing...


proper debounce needs to be implemented....

what is your input circuit....



as for software logic... i think the last one i posted for you should work....
_________________
CCS PCM 5.078 & CCS PCH 5.093
tonkostz



Joined: 07 May 2011
Posts: 40
Location: Bulgaria

View user's profile Send private message

PostPosted: Fri Sep 07, 2012 10:24 am     Reply with quote

This is my program for testing. Input pin A5 is has a pull down resistor an the button is connected between the pin and +5V.
Code:
#include <12f683.h>
#fuses INTRC_IO,NOWDT,NOMCLR,NOBROWNOUT
#use delay(int=4000000)

#define U4 15

int8 k=0;
int16 data;

void write_int16_eeprom(address, int16 data)
{
   int8 i;

   for(i = 0; i < 2; ++i)
   {
     write_eeprom(address + i, *((int8 *)(&data) + i));
   }
}

int16 read_int16_eeprom(address)
{
   int8  i;
   int16 data;

   for(i = 0; i < 2; ++i)
   {
     *((int8 *)(&data) + i) = read_eeprom(address + i);
   }

   return(data);
}


void main()
{


while(true)
{

if(input(PIN_A5)==1)
 {
   k++;
   if(k>6)
   k=6;
   
   while(input(PIN_A5)) //loop here until button released.
   {
    delay_ms(20);
   }
   
   if(k==4)
   {
    write_int16_eeprom(U4,0);
    k=0; 
   }
   
   if(k==5)
   {
    write_int16_eeprom(U4,1);
    k=0; 
   }
   
   if(k==6)
   {
    write_int16_eeprom(U4,2);
    k=0;
   }
 
 }

 
       
}

}
tonkostz



Joined: 07 May 2011
Posts: 40
Location: Bulgaria

View user's profile Send private message

PostPosted: Fri Sep 07, 2012 12:57 pm     Reply with quote

Finally i managed to make exactly what i wanted.
This is the full code:
Code:
#include <12f683.h>
#fuses INTRC_IO,NOMCLR,NOBROWNOUT
#use delay(int=4000000)

#define DEBOUNCE_PERIOD_IN_MS 100
#define TIMEOUT 20

#define U4 15

int8 k=0;
int16 data;

void write_int16_eeprom(address, int16 data)
{
   int8 i;

   for(i = 0; i < 2; ++i)
   {
     write_eeprom(address + i, *((int8 *)(&data) + i));
   }
}

int16 read_int16_eeprom(address)
{
   int8  i;
   int16 data;

   for(i = 0; i < 2; ++i)
   {
     *((int8 *)(&data) + i) = read_eeprom(address + i);
   }

   return(data);
}

void main()
{
int8 count=0;

while(true)
{             
   if(input(PIN_A5)==1)
   {
    count=0;
    k++;
    if(k>6)
    k=0;
 
   while(input(PIN_A5)) //loop here until button released.
   {
    delay_ms(15);
   }
   
   }
   
   else
   {
    count++;
   }
   
   if(count == TIMEOUT)
   {
    if(k==4)
    {
     write_int16_eeprom(U4,1);
     k=0;
    }
    if(k==5)
    {
     write_int16_eeprom(U4,2);
     k=0;
    }
    if(k==6)
    {
     write_int16_eeprom(U4,3);
     k=0;
    }
     
   }
   
   data=read_int16_eeprom(U4);
   if(data==1)
   {
    output_high(PIN_A2);
   }
   
   if(data==2)
   {
   output_high(PIN_A2);
   delay_ms(50);
   output_low(PIN_A2);
   delay_ms(50);
   } 
   
   if(data==3)
   {
   output_high(PIN_A2);
   delay_ms(200);
   output_low(PIN_A2);
   delay_ms(200);
   }

   
 
   delay_ms(DEBOUNCE_PERIOD_IN_MS);
         
}   
}
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