Page 1 of 1

EEPROM issue?

Posted: Wed Mar 14, 2012 4:15 am
by GTF
Hi,
I'm using a 18F14K50 in a PWM position control application with a 0-5V analog sensor. I am writing the learned max, neutral and min values to EEPROM in a learn procedure. The values are read at startup and my routine loops until various variables go out range. The loop and hardware appears to function normally if I do not test for the position being within range. The problem is that when I do, the position values are always out of range on startup. Have I done something wrong here that could cause this? The attached abbreviated program behaves in the same manner in my application.

Thanks,

Grant

Re: EEPROM issue?

Posted: Wed Mar 14, 2012 9:51 am
by JonnyW
Hello. I am not sure what values you are getting out, but your code:

Code: Select all

max_limit = learn_max + 10
min_limit = learn_min - 10
Might be the problem. Because these are byte values then if you have a reading below 10 or above 245 these will wrap (5 - 10 = 251). Try changing your limit values to signed integers and this may behave more correctly.

Cheers,

Jonny

Re: EEPROM issue?

Posted: Wed Mar 14, 2012 2:41 pm
by GTF
Hello Jonny,

I have tried simply using the learned values as the limits as well as the signed integers. The result is the same. I have 2 separate sensors in the application and the same thing is happening with both of them. At times it has been OK in the forward direction(decreasing V), but a little into reverse(increasing V) the loop is ended. My meter shows that I am far from the limit. Min is 0.63V Max is 4.45V Neutral is 3.53V (asymmetrical)

Grant

edit: added info re sensor output vs direction- full forward is 4.45V, full reverse is 0.63V, VDD is 4.88V

Re: EEPROM issue?

Posted: Wed Mar 14, 2012 5:06 pm
by JonnyW
Hello. I have just re-read your code. Did you mean for the code:

Code: Select all

  loop
    A = ADC()
  while A > min_limit AND A < max_limit
As this will wait until 'A' is outside the range. To wait until it is inside the range, use:

Code: Select all

  loop
    A = ADC()
  while A < min_limit AND A > max_limit
I would also bracket your inequalities and use && rather than AND:

Code: Select all

  loop
    A = ADC()
  while (A < min_limit) && (A > max_limit)
Cheers,

Jonny

Re: EEPROM issue?

Posted: Thu Mar 15, 2012 4:44 am
by GTF
The loop is to end when A is outside the limits, so that code appears to be correct. I have tried brackets/no brackets before. My actual code has brackets. The signed integers may have helped get this example working on my hardware. I also had to initially increase the max_limit by 50 over the learned_max, but was then able to drop it back down to 10 with it still working??

Looks like it is going to take some more effort though to get this to work in my actual code. At least the loop now runs until I get into the reverse direction (moving toward learned_max).

Thanks.