| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| tonkostz 
 
 
 Joined: 07 May 2011
 Posts: 58
 Location: Bulgaria
 
 
			    
 
 | 
			
				| Help with internal eeprom programming algorithm |  
				|  Posted: Thu Sep 06, 2012 10:59 am |   |  
				| 
 |  
				| 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: 1074
 Location: Panama
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Thu Sep 06, 2012 11:19 am |   |  
				| 
 |  
				| IF  i understood correctly, then: 
 
 
 where you declare "i":
 
 //<--- 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: 58
 Location: Bulgaria
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Thu Sep 06, 2012 11:30 am |   |  
				| 
 |  
				| 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
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Sep 07, 2012 2:20 am |   |  
				| 
 |  
				| 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: 58
 Location: Bulgaria
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Sep 07, 2012 6:27 am |   |  
				| 
 |  
				|  	  | 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: 1074
 Location: Panama
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Sep 07, 2012 6:31 am |   |  
				| 
 |  
				| 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: 58
 Location: Bulgaria
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Sep 07, 2012 6:52 am |   |  
				| 
 |  
				|  	  | 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: 1074
 Location: Panama
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Sep 07, 2012 9:33 am |   |  
				| 
 |  
				| 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: 58
 Location: Bulgaria
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Sep 07, 2012 10:24 am |   |  
				| 
 |  
				| 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: 58
 Location: Bulgaria
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Sep 07, 2012 12:57 pm |   |  
				| 
 |  
				| 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);
 
 }
 }
 | 
 |  | 
	
		|  | 
	
		|  |