View previous topic :: View next topic |
Author |
Message |
Guest
|
milliseconds counter |
Posted: Sat Jun 17, 2006 9:40 am |
|
|
Hi,
I want to count the elapsed time (in milliseconds), but If I use an interrupt counter each 1 millisecond the PIC is full time doing this task and I need control other external events.
Could you tell me how can i solve this, may be using any counter or timer without PIC overload ?.
Thank you |
|
![](templates/subSilver/images/spacer.gif) |
Fro_Guy
Joined: 26 Apr 2006 Posts: 12
|
|
Posted: Sat Jun 17, 2006 9:51 am |
|
|
What are you doing in your intrupts? If there is alot going on when you overflow your timer then that can cause an issue. Also what other intrups are you using? Also what clock spped are you using on the pic and what series is it. If you are going alot of things in your other intrupts this will cause the timing to go off, becaus esay you so something in the serial isr, and it takes 15ms, then you loose all thoes counts. Id say the intrupts are they way to go, but it is hard to say without seeing some of your code.
Good luck
Fro Guy |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Sat Jun 17, 2006 9:52 am |
|
|
You don't give several critical factors. What PIC?. What osillator are you running?. What do you actually want to 'do' with the counter (time events, or just have a local value to 'read')?.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
epideath
Joined: 07 Jun 2006 Posts: 47
|
|
Posted: Sat Jun 17, 2006 10:51 am |
|
|
Well even a PIC running at 4MHz the instruction cycle is about 1us (microsecond)
If you are timing miliseconds then the processor will have about 1000 instructions before it is interrupted. and to increment a time value should only take a few instructions.
I'm not sure why you think that the processor is always busy with the interrupt. I see it as about 20 instructions 20us on interrupt and 980us instructions for other stuff. for evey 1ms (millisecond)
Maybe I'm missing soem point here.
regards,
Gary |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Sat Jun 17, 2006 11:05 am |
|
|
I'm thinking to use a PIC18F452 at 20 mhz. I need to make a chonometer, read an 8 channel high speed AD every 10 millisecond and write it in a 70 ns SRAM memory. Then I would export all to my PC and grath an exact (millisecond) time-line.
thank you |
|
![](templates/subSilver/images/spacer.gif) |
Fro_Guy
Joined: 26 Apr 2006 Posts: 12
|
|
Posted: Sat Jun 17, 2006 11:23 am |
|
|
Well what i would do is have the intrupt and have a counter to 10 in it, and when it dose get to 10 then set a flag it indicate it is done and reset the counter. Have you main loop on your program look for this flag and once it is set off do what you need to do and then clear the flag. I dont know how long your other routines take but if they take more than 10 ms, the timing will be off and you may need a fater PIC.
good luck
Fro Guy |
|
![](templates/subSilver/images/spacer.gif) |
Guest
|
|
Posted: Sat Jun 17, 2006 2:43 pm |
|
|
Anonymous wrote: | I'm thinking to use a PIC18F452 at 20 mhz. I need to make a chonometer, read an 8 channel high speed AD every 10 millisecond and write it in a 70 ns SRAM memory. Then I would export all to my PC and grath an exact (millisecond) time-line.
thank you |
There are many ways of doing this, here is how I would.
First decide how the ram is interfaced. At 10ms intervals a spi flash ram chip wouldn't even take 1ms to write a few bytes. You can get 8 megabits for a few dollars (M25P80).
Decide how many samples and what type of averaging/software filtering you are going to use. 10 ms is enough time to use almost any algorithm.
You could use an external A/D + multiplexer for higher res (AD7705). Depending on the application, (you mentioned 70ns ram) you may need the instantanious voltage at 10ms intervals in whch case your going to need a faster converter $$$.
Set up a timer to compare and interrupt at an interval of an exact fraction of 10ms. Use this in the background as your 10ms trigger to save the values.
Use a 232 level translator or FTDI usb chip and interface with a PC application.
Add some fluff and features. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Sat Jun 17, 2006 2:43 pm |
|
|
Key thing here is that there are three parts to 'reading the adc'. You have to select the ADC channel (if only one channel is being read, then this can be done just once), and then wait for the internal capacitor in the ADC, to charge to nearly the incoming voltage (this takes typically about 10uSec, but again if only one channel is being read, does not matter). You then have to start a conversion, and _wait_ for the conversion, then take the reading. Unfortunately this takes time. Now the PIC allows this to be split up. So:
Code: |
int16 adval;
#int_timer2 //prbably the easiest tmer to set to 1mSec
void ticker(void) {
static int8 state=0;
switch (state) {
case 0:
//Here send your output, _but do this using interrupt driven output_
//I, and others have posted examples of interrupt driven RS232 TX
//in the past.
++state;
break;
case 8:
read_adc(ADC_START_ONLY);
//Start the ADC conversion, _but do not wait for it_
++state;
break;
case 9:
adval=read_adc(ADC_READ_ONLY);
//Retrieve the value;
state=0;
break;
default:
++state;
break;
}
}
|
Now the code will spend about ten instructions executing the switch, a couple for the increment,and a couple for each adc operation in each of these states. You then have to add the overhead of the interrupt (about 60 instructions for the 18 chips), but in total then it'll spend perhaps 80 instructions doing this. The _worst case state_, will be the output routine, which will depend totally on how you send the data. However as has been pointed out by another poster, even running the processor at 4MHz, you have 1000 instruction times between successive interrupt 'ticks'.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
|