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

PROPORTIONAL PRESCALER
Goto page 1, 2, 3, 4  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PROPORTIONAL PRESCALER
PostPosted: Sun Feb 24, 2008 4:21 pm     Reply with quote

Please could anybody help me with or suggest the best solution to a counting/timer problem I have.

Using: PIC18F4525
xtal: 4MHz

what I need to do is to count up to 65535, over a variable time period, from 5 seconds to 300 seconds.

so clock pulse = 4MHz/4 = 1us
and; 65536 x 1us = 65.536ms (minimum/fastest time taken for 65536 counts)

now I will have a variable 't' equating to (5s-300s). this value must be used in some easy way to set the prescaler to count at this speed/time.

e.g for 10s:

10s/65.536ms = gain of 152.58
and 1us x 152.58 = 152.58us.


therefore the prescaler would have to be configured to give an output every 152.58us.

and my question is, is there such a way to set the prescaler for a variable which has nearly 300 possibilities - or am I going up the wrong track.
Regards
Carl
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 24, 2008 5:06 pm     Reply with quote

Quote:

what I need to do is to count up to 65535, over a variable time period,
from 5 seconds to 300 seconds.

Tell us why you want to do this. What are you trying to count ?
What is the source of the input ?
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PROPORTIONAL PRESCALER
PostPosted: Sun Feb 24, 2008 5:25 pm     Reply with quote

thanks for replying PCM Programmer,

I want to output(on 2 ports) the value 's' from 0 to 65535 - this is then converted into a voltage later on with a D/A converter. BUT THIS IS DONE OVER A VARIABLE TIME PERIOD.

There is no input, just the xtal. So i need to manipulate and increment the time period/counters/timers/prescalers. from this I can then do one of two options (I think?):

1) for every count change, output (write) a proportionally timed signal to one pin. then read this pin and for every h_L transition - increment 's', and for every L_H transition, write 's' to the 2 ports.

2)just do it in software with timers so that for every count change the value is written to the 2 ports.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 24, 2008 5:30 pm     Reply with quote

What is the overall purpose of doing this ?
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PROPORTIONAL PRESCALER
PostPosted: Sun Feb 24, 2008 5:34 pm     Reply with quote

this is being used for a 4_20mA Loop Calibrator.
particular modes of these devices work by ramping up and down from 4_20mA and back again OVER A PARTICULAR TIME PERIOD.

therefore the starting point is to get the output from the pic to write the 16bit value over a variable time period.

THE TIME PERIOD IS SET BY THE USER ANYWHERE FROM 5s to 300s.
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon Feb 25, 2008 2:51 am     Reply with quote

Quote:

this is being used for a 4_20mA Loop Calibrator.
particular modes of these devices work by ramping up and down from 4_20mA and
back again OVER A PARTICULAR TIME PERIOD.

I already made a Current Calibrator for a Loop Converter with 14 bit resolution for industrial
PLC's. It has a fixed ramping time (12 sec) imposed by the custom previous requirement.
I have in developing stage a new version with 16 bit resolution and variable ramping time.
If this is what you are asking for, it doesn't need to change the prescaler in such way.


Humberto
libor



Joined: 14 Dec 2004
Posts: 288
Location: Hungary

View user's profile Send private message

PostPosted: Mon Feb 25, 2008 3:59 am     Reply with quote

What do you, guys, use for 4-20mA output ? I was looking for schematics but only could find solutions with custom ICs like the XTR-1xx series. Do you use them or do you have your own circuits ?
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Mon Feb 25, 2008 5:36 am     Reply with quote

Humberto,

This is exactly what i am after. I am designing a 16 bit 4_20mA converter with variable ramp time up/down times. I know that the calibrators on the market at present only have a few standard ramp times - that is why I want a variable one. You say it can be done without changing the prescaler - I would be very much in your debt if you could help.

Libor,
The best idea is to use a dedicated IC for the job like your xtr115 or xtr110 - I am using something different because of availability problems. Bear in mind that there are two types - those that simulate and those that source. the difference being that one needs an external power supply in the loop and the other one doesn't. also please bear in mind that it is the variable input (voltage or current) going into the transmitter that is proportional to the 4_20mA output.
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Re: PROPORTIONAL PRESCALER
PostPosted: Mon Feb 25, 2008 6:25 am     Reply with quote

carl wrote:
...from this I can then do one of two options (I think?):

1)....

2)just do it in software with timers so that for every count change the value is written to the 2 ports.


Use option #2. If you want a ramp from 5 to 300 seconds and each ramp has 65535 steps, then each step must take from 76 to 4578 usec. You must make the calculation by dividing the ramp time by 65535. Then use a CCP module in Output Compare Mode to get an interrupt every 76 to 4578 microseconds. Your interrupt service routine must be very short, especially when the ramp time is 5 seconds, because you need to enter the ISR, do the output to the two ports that go to the A/D, acknowledge the interrupt, and set up for the next one and exit, all in 76 usec. You can simplify the setup for the next interrupt by using Special Event Mode in the CCP module to reset Timer 1 automatically. Note that if the interrupt time period (76 usec in this case) is fixed, then your resolution on setting the shortest ramp time is only 1 part in 76 (more than 1%). That means you can set a ramp time of 5.00 sec. or 5.05 sec, but not 5.025 sec. Is that good enough for you? Because if it is not, then you will have to use a more complicated dithering of the interrupt time period.

Robert Scott
Real-Time Specialties
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Mon Feb 25, 2008 7:30 am     Reply with quote

libor wrote:
Quote:
What do you, guys, use for 4-20mA output ?

carl wrote:
Quote:

You say it can be done without changing the prescaler - I would be very much in your debt if you could help.

watch yours PM.

Humberto
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PROPORTIONAL PRESCALER
PostPosted: Mon Feb 25, 2008 8:54 am     Reply with quote

Dear RL Scott, Thankyou so much for your response. I read quite a bit on your method and lots of replies stated that this was not a good way to use an interrupt because the counter is still counting whilst the ISR is being serviced. Although it looks as though I am going to have to go down this route because setting the prescaler is limited - (not enough divide's by to make it accurate).

As for the steps they are in 1 second increments so the accuracy should be able to be done as you stated. I did not state all the parameters needed when I first posted this question because I did not want to baffle anyone but the 76us would actually be 38us because I need to count upto 65535 and then back down again within the time period - 36us per step up and 36us per step down. THIS IS FOR THE 5s CHOICE.

Also I need to poll the value every 10ms and display on lcd. Do you think I have enough time to play with to perform all these tasks - or if not I suppose getting a fatser clock would help.

This is all secondary to the fact that I am new to C and the above seems tricky. I am getting the hang of it but find examples and books really usefull sources - any examples of the above sistuation?

Your response was very much appreciated - thankyou Robert Scott and Humberto
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Re: PROPORTIONAL PRESCALER
PostPosted: Mon Feb 25, 2008 8:08 pm     Reply with quote

carl wrote:
...36us per step up and 36us per step down. THIS IS FOR THE 5s CHOICE...


Then you will have a hard time making this work in an ISR. Your only hope may be by counting instruction times and ignoring the counter.

Quote:

Also I need to poll the value every 10ms and display on lcd. Do you think I have enough time to play with to perform all these tasks - or if not I suppose getting a fatser clock would help.


You can't see the LCD every 10 msec, so why bother updating it that fast? In any case, when you update the LCD, the ramp will be delayed and performance will suffer. You can do longer times easily, but the 5 second ramp is going to be very hard if you have to do other things at the same time. Maybe you should use two PICs.

Robert Scott
Real-Time Specialties
Embedded Systems Consulting
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

proportional prescaler
PostPosted: Tue Feb 26, 2008 1:53 am     Reply with quote

Thanks Robert for getting back,
Yep I knew the 10ms refresh was rubbish - made an error there - I will make it 500ms or something like that.

Quote:
In any case, when you update the LCD, the ramp will be delayed and performance will suffer


Is this because I wouldn't have enough time to do it??

I could get a faster crystal and increase the minimum time from 5s to 10s - therefore increasing the amount of time I have in the ISR. would this be OK??
RLScott



Joined: 10 Jul 2007
Posts: 465

View user's profile Send private message

Re: proportional prescaler
PostPosted: Tue Feb 26, 2008 6:08 am     Reply with quote

carl wrote:

Quote:
In any case, when you update the LCD, the ramp will be delayed and performance will suffer


Is this because I wouldn't have enough time to do it??

I could get a faster crystal and increase the minimum time from 5s to 10s - therefore increasing the amount of time I have in the ISR. would this be OK??


If you update the LCD in a main program loop and drive the ramp in an ISR, then they can co-exist quite nicely. But if you are forced to update the ramp in your main loop, then anything else you do in your main loop will delay and corrupt the ramp (unless it is very very quick - on the order to 5 or 6 instructions. I'm sure you can't update your LCD in 5 or 6 instructions). So, if you get a faster crystal so that you can afford the overhead of an ISR on every step of the ramp, then you can do other things in your main program loop.

Robert Scott
Real-Time Specialties
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

proportional prescaler
PostPosted: Tue Feb 26, 2008 9:25 am     Reply with quote

Hi Robert,

I'm going to get a faster xtal and give it a go. First I will absorb everything already discussed and learn how to use ISR's. Thanks for your help and I will keep you updated.

Have you got any good examples to look at?
Carl
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2, 3, 4  Next
Page 1 of 4

 
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