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

Value overflow in combination with get_timerX()

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







Value overflow in combination with get_timerX()
PostPosted: Sun Jun 08, 2003 9:51 am     Reply with quote

Incremental encoder

I have a "strange" problem regarding value overflow in combination with get_timerX()
I use the compiler Version: 3.130 PCH

My programm (abstract):

#include <18F242.h>
#fuses HS,NOWDT,PUT,NOPROTECT,BROWNOUT,NOLVP
#use delay(clock=40000000)

............................
............................

static signed int32 upcount, dncount, position, velocity;
// up counter is timer 0
// down counter is timer 1

velocity = -upcount;
velocity += dncount;

upcount = get_timer0();
// read timer0 again if overflow
if (get_timer0() < upcount) {upcount = get_timer0();}

dncount = get_timer1();
// read timer1 again if overflow
if (get_timer1() < dncount) {dncount = get_timer1();}

velocity += upcount;
velocity -= dncount;

position += velocity;

if (position > 100000) {output_high(PIN_C4);}
else output_low(PIN_C4);

if (position < -100000) {output_high(PIN_C6);}
else output_low(PIN_C6);

.......................
.......................

Problem: maximum possible value of variable position is only +65535 or -65535 !

This problem occurs in combination with get_timerX() only.

It would be very helpful if somebody can give me a hint how I can solve the problem.

Gepard
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515095
R.J.Hamlett
Guest







Re: Value overflow in combination with get_timerX()
PostPosted: Sun Jun 08, 2003 12:24 pm     Reply with quote

:=Incremental encoder
:=
:=I have a "strange" problem regarding value overflow in combination with get_timerX()
:=I use the compiler Version: 3.130 PCH
:=
:=My programm (abstract):
:=
:=#include <18F242.h>
:=#fuses HS,NOWDT,PUT,NOPROTECT,BROWNOUT,NOLVP
:=#use delay(clock=40000000)
:=
:=............................
:=............................
:=
:=static signed int32 upcount, dncount, position, velocity;
:=// up counter is timer 0
:=// down counter is timer 1
:=
:= velocity = -upcount;
:= velocity += dncount;
:=
:= upcount = get_timer0();
:=// read timer0 again if overflow
:= if (get_timer0() < upcount) {upcount = get_timer0();}
:=
:= dncount = get_timer1();
:=// read timer1 again if overflow
:= if (get_timer1() < dncount) {dncount = get_timer1();}
:=
:= velocity += upcount;
:= velocity -= dncount;
:=
:= position += velocity;
:=
:= if (position > 100000) {output_high(PIN_C4);}
:= else output_low(PIN_C4);
:=
:= if (position < -100000) {output_high(PIN_C6);}
:= else output_low(PIN_C6);
:=
:=.......................
:=.......................
:=
:=Problem: maximum possible value of variable position is only +65535 or -65535 !
:=
:=This problem occurs in combination with get_timerX() only.
:=
:=It would be very helpful if somebody can give me a hint how I can solve the problem.
:=
:=Gepard

I can't see how you are handling the overflow. I'd have expected to see something like this:

signed int32 position, velocity;
union {
signed int32 long;
int16 word[2];
} upcount,dncount;
int16 temp;

temp = get_timer0();
//increment if overflow
if (temp < upcount.word[0]) upcount.word[1] +=1;
upcount.word[0]=temp;

temp = get_timer1();
//increment if overflow
if (temp < dncount.word[0]) upcount.word[1] +=l;
dncount.word[0]=temp;

You appear to just re-read the counter, if it has dropped, but make no correction to add the '1' to the next digit in the counter. What I show, reads the counter at just one place (reading it in multiple locations asks for errors, if it has changed between one read and the next), then if the value has dropped from the last time, stores the counter value as the low 16bits, and increments the high 16bits. Otherwise the counter is just transferred to the low 16bits.

Obviously in the final addition, you would have to use 'upcount.long', and 'dncount.long', to refer to the whole 32bits.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515096
Gepard
Guest







Re: Value overflow in combination with get_timerX()
PostPosted: Sun Jun 08, 2003 2:43 pm     Reply with quote

Thanks a lot for the hints.

I implemented as you wrote.


signed int32 position, velocity;
union {
signed int32 long;
int16 word[2];
} upcount,dncount;
int16 temp;

................
................


Unfortunately the compiler has an error message on line:

signed int32 long;

Deleting intermediary files... done.
Executing: "C:\Programme\PICC\ccsc.exe" "servo1.c" +FH
Error[38] servo1.c 33 : This type can not be qualified with this qualifier
Halting build on first failure as requested.
BUILD FAILED

Can you help?
Thanks in advance.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515097
Kenny



Joined: 07 Sep 2003
Posts: 173
Location: Australia

View user's profile Send private message

Re: Generalising quadrature
PostPosted: Sun Jun 08, 2003 5:32 pm     Reply with quote

<font face="Courier New" size=-1>:=Incremental encoder
:=
:=I have a "strange" problem regarding value overflow in combination with get_timerX()
:=I use the compiler Version: 3.130 PCH
:=
:=My programm (abstract):
:=
:=#include <18F242.h>
:=#fuses HS,NOWDT,PUT,NOPROTECT,BROWNOUT,NOLVP
:=#use delay(clock=40000000)
:=
:=............................
:=............................
:=
:=static signed int32 upcount, dncount, position, velocity;
:=// up counter is timer 0
:=// down counter is timer 1
:=
:= velocity = -upcount;
:= velocity += dncount;
:=
:= upcount = get_timer0();
:=// read timer0 again if overflow
:= if (get_timer0() < upcount) {upcount = get_timer0();}
:=
:= dncount = get_timer1();
:=// read timer1 again if overflow
:= if (get_timer1() < dncount) {dncount = get_timer1();}
:=
:= velocity += upcount;
:= velocity -= dncount;
:=
:= position += velocity;
:=
:= if (position > 100000) {output_high(PIN_C4);}
:= else output_low(PIN_C4);
:=
:= if (position < -100000) {output_high(PIN_C6);}
:= else output_low(PIN_C6);
:=
:=.......................
:=.......................
:=
:=Problem: maximum possible value of variable position is only +65535 or -65535 !
:=
:=This problem occurs in combination with get_timerX() only.
:=
:=It would be very helpful if somebody can give me a hint how I can solve the problem.
:=
:=Gepard

This may not be relevant in your case, but I'll pass it on anyway.
I had a problem - how to take 16 bit data from an external up/down quadrature IC, the HCTL-2016 from Agilent into a signed 32 bit counter in the PIC.
I asked my computer science specialist friend, Nathan Hurst, and by the end of the day he sent me this URL:

Regards
Kenny</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515098
R.J.Hamlett
Guest







Re: Value overflow in combination with get_timerX()
PostPosted: Mon Jun 09, 2003 4:54 am     Reply with quote

Hi Gepard,
You may find out soon, that 'private messages' on this board don't really work. It tells me that you have sent one, but unfortunately never lets me see it.... This is a common problem, hence in general, unless it is 'rude', let's keep it out here on the board.
Repost in public, and I'll be able to reply.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515105
Tomi
Guest







Re: Value overflow in combination with get_timerX()
PostPosted: Mon Jun 09, 2003 11:00 am     Reply with quote

Hi R.J.,
I was able to read that message (don't ask what "private" means Smile anymore). You can see the post if you open the top of thread and scroll down to the list of answers. The last post with a smiley is the private post. Here is a copy:

Thanks a lot for the hints.
I implemented as you wrote.


signed int32 position, velocity;
union {
signed int32 long;
int16 word[2];
} upcount,dncount;
int16 temp;

................
................


Unfortunately the compiler has an error message on line:

signed int32 long;

Deleting intermediary files... done.
Executing: "C:\Programme\PICC\ccsc.exe" "servo1.c" +FH
Error[38] servo1.c 33 : This type can not be qualified with this qualifier
Halting build on first failure as requested.
BUILD FAILED

Can you help?
Thanks in advance.


Think the problem is that long is a typename.
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515119
R.J.Hamlett
Guest







Re: Value overflow in combination with get_timerX()
PostPosted: Mon Jun 09, 2003 2:33 pm     Reply with quote

:=Hi R.J.,
:=I was able to read that message (don't ask what "private" means <img src="http://www.ccsinfo.com/pix/forum/smile.gif" border="0"> anymore). You can see the post if you open the top of thread and scroll down to the list of answers. The last post with a smiley is the private post. Here is a copy:
:=
:=Thanks a lot for the hints.
:=I implemented as you wrote.
:=
:=
:=signed int32 position, velocity;
:=union {
:=signed int32 long;
:=int16 word[2];
:=} upcount,dncount;
:=int16 temp;
:=
:=................
:=................
:=
:=
:=Unfortunately the compiler has an error message on line:
:=
:=signed int32 long;
:=
:=Deleting intermediary files... done.
:=Executing: "C:\Programme\PICC\ccsc.exe" "servo1.c" +FH
:=Error[38] servo1.c 33 : This type can not be qualified with this qualifier
:=Halting build on first failure as requested.
:=BUILD FAILED
:=
:=Can you help?
:=Thanks in advance.
:=
:=
:=Think the problem is that long is a typename.
Aargh.
The stupid thing is that I changed the name, because I normally use 'b' to access the bytes, 'w' to access the words, and 'l' to access the long. I just typed 'long' as a nice 'significant' name, and forgot it was a type name....
I also used 'upcount' in the second bit of arithmetic, when it should be dncount there.
Yes, the 'private reply' feature of the board, doesn't really work at all.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515126
Gepard
Guest







Re: Value overflow in combination with get_timerX()
PostPosted: Mon Jun 09, 2003 3:25 pm     Reply with quote

:=Hi Gepard,
:=You may find out soon, that 'private messages' on this board don't really work. It tells me that you have sent one, but unfortunately never lets me see it.... This is a common problem, hence in general, unless it is 'rude', let's keep it out here on the board.
:=Repost in public, and I'll be able to reply.
:=
:=Best Wishes


Hi R.J.Hamlet

As a new member not all rules are clear in the beginning ...


No I have implemented your proposal.
I have replaced long by a1 (because an error message of the compiler).

union {
signed int32 a1;
.........
.........

and I tried it out on the target hardware.
It is a miracle, it works really great !!

Thanks a lot for your help.

Best regards
Gepard
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515127
R.J.Hamlett
Guest







Re: Value overflow in combination with get_timerX()
PostPosted: Tue Jun 10, 2003 2:12 am     Reply with quote

:=:=Hi Gepard,
:=:=You may find out soon, that 'private messages' on this board don't really work. It tells me that you have sent one, but unfortunately never lets me see it.... This is a common problem, hence in general, unless it is 'rude', let's keep it out here on the board.
:=:=Repost in public, and I'll be able to reply.
:=:=
:=:=Best Wishes
:=
:=
:=Hi R.J.Hamlet
:=
:=As a new member not all rules are clear in the beginning ...
:=
:=
:=No I have implemented your proposal.
:=I have replaced long by a1 (because an error message of the compiler).
:=
:=union {
:= signed int32 a1;
:=.........
:=.........
:=
:=and I tried it out on the target hardware.
:=It is a miracle, it works really great !!
:=
:=Thanks a lot for your help.
:=
:=Best regards
:=Gepard
Good news.
Sorry I typed the code 'off my head' (I usually am...), and missed the obvious mistakes, but at least the 'end result' is good. :-)
It is not a case of 'rules', but the board software, has some lovely bugs, and really (as with usenet posts), it is easier/simpler to keep things in the main thread.

Best Wishes
___________________________
This message was ported from CCS's old forum
Original Post ID: 144515137
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