View previous topic :: View next topic |
Author |
Message |
theteaman
Joined: 04 Aug 2006 Posts: 98
|
Differences in timers |
Posted: Tue Sep 12, 2006 6:19 am |
|
|
Hello
My PIC18LF2525 has 4 timers. I am using a 20MHz crystal for the PIC.
Looking at the datasheet, it is possible to have a prescale value of 1:2 for TIMER0. Is it correct to say this would mean I can get 20MHz/(4*256*2) = 9765 interrupts per second?
Secondly, TIMER3 defines the prescaler as a whole number, not a ratio. With a prescaler of 4 does this mean I can get 20MHz/(4*256*0.25) = 78,125 interrupts per second? Basically I need as fast a count as possible...
Finally, I'm assuming these speeds are affected when I run the PIC at 3.3V as opposed to 5V, but I cant find in the datasheet how they are affected....
thanks |
|
|
Ttelmah Guest
|
|
Posted: Tue Sep 12, 2006 6:44 am |
|
|
Lets ignore the timer limits for a moment, and look at how fast an interrupt can be, and possibly still work.
On a PIC at 20MHz, the processor is executing 4MIPS. The standard interrupt overhead, is typically about 60 instructions, plus the handler itself. If the handler is 'simple', say 20 instructions. This potentially allows maximum useable, of 4000000/80 interrupts per second (with the chip doing nothing else...). Say about 50000 interrupts per second. Now it is possible to reduce the overhead, by careful programming, but this requires _you_ to write the interrupt handler. So your first 9765 IPS, should be useable, but your second 78125 IPS, almost certainly would not work.
Now going back to the timings. The fastest mode for timer0, is if you disable the prescaler entirely. It is then simply fed off the master clock/4. You also need to set it to be running in 8bit mode (otherwise it'll count t 65536, instead of 256). Set like this, it gives 19531 IPS, which is probably still just about useable. Timer3, is a _16bit_ timer, so will actually give only 1/256th the speed you are calculating.
However remember that you can use the CCP module(s), in combination with either timer3, or timer1, to reset the timer before it reaches the end of the count, which allows counts to give nice time values (such as using an /2 prescaler, and then resetting at a count of 250, to give 10000 IPS).
However also remember, that if you want the chip to do anything much else, you should instead be considering using hardware to handle events at this sort of frequency.
Best Wishes |
|
|
theteaman
Joined: 04 Aug 2006 Posts: 98
|
|
Posted: Tue Sep 12, 2006 7:22 am |
|
|
Thank you ttelmah, you are always easy to understand.
Unlike the datasheet... which is why I need to ask two more questions that should hopefully make me be able to find my own way from now on.
I can choose an internal or external rtcc clock.. which one is the PIC crystal I connect? Whats the other one?
Does CCS C see timers and counters as the same thing? Or what is the difference between the two, in terms of how they trigger interrupt handlers?
thanks! |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Sep 12, 2006 7:37 am |
|
|
A good explanation from Ttelmah to which I want to add a few things.
I don't know what it is that attracts people to using interrupts, but often in this forum I see people asking for how to solve a problem using interrupts when there are specialized hardware modules like CCP and / or PWM in the PIC that can handle many of these high frequency issues much better.
Another technique besides using interrupts is to disable the interrupt and check the interrupt request flag from within your main loop. For high frequency interrupts this is more efficient because you will save the relative large interrupt handler overhead. Disadvantage is that you will have to check the interrupt request flag with short intervals or you are running the risk of missing an interrupt. |
|
|
Ttelmah Guest
|
|
Posted: Tue Sep 12, 2006 7:44 am |
|
|
Yes. The modules are 'timer/counter' modules, and trigger the same interrupts. The only difference, is that in 'counter' mode, the module uses an external clock.
Most of the timers can be fed off the internal clock, or an external 'pin' (being fed with an external clock). In general,T1, can also be fed off an external _crystal_ (it has two pins, and the extra amplifier gate, to form it's own oscillator. Hence this is the one normally used to form a RTCC. On your chip (with four timers), Timer3, also has this ability.
You can use whatever crystal you want for an RTCC, but normally a nice value, that easily divides by a binary ratio, to give one tick/second, or one tick per tenth/hundredth of a second is chosen (32768Hz, or 3.2768Mhz) .
There is no seperate limit on the timer modules at 3v, just the one on how fast the chip itself can run.
Best Wishes |
|
|
theteaman
Joined: 04 Aug 2006 Posts: 98
|
|
Posted: Tue Sep 12, 2006 5:35 pm |
|
|
ckielstra mentioned the large overhead with interrupts and suggested CCP as an alternative. I have looked in to CCP, but do not see how it could help with my problem - could someone explain? Essentially what I want to do is clock some data serially out of the PIC at a set frequency (say, 8000Hz). Or would a clock be my best option?
thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 12, 2006 5:42 pm |
|
|
Quote: | Essentially what I want to do is clock some data serially out of the
PIC at a set frequency (say, 8000Hz) |
Look at the SSP module:
SSP is an abreviation for Synchronous Serial Port. It can clock out
data at a set frequency. Use the dedicated pins SCK and SDO for
clock out and data out, respectively. Use the setup_spi() function to
setup the port parameters. Use spi_write() to send out one byte. |
|
|
theteaman
Joined: 04 Aug 2006 Posts: 98
|
|
Posted: Tue Sep 12, 2006 6:05 pm |
|
|
PCM programmer wrote: | Quote: | Essentially what I want to do is clock some data serially out of the
PIC at a set frequency (say, 8000Hz) |
Look at the SSP module:
SSP is an abreviation for Synchronous Serial Port. It can clock out
data at a set frequency. Use the dedicated pins SCK and SDO for
clock out and data out, respectively. Use the setup_spi() function to
setup the port parameters. Use spi_write() to send out one byte. |
ofcourse! this is embarrasing, in fact, because I already use SPI to interface with some other components. I'll need to share the SPI port with another device but I guess, I can multiplex them?
thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 12, 2006 6:15 pm |
|
|
Use digital i/o pins as Chip Select signals -- one for each device.
For example, pin C0 could be the \CS for the first device and
pin C1 could be the \CS for the 2nd device. These \CS pins
have to be manually set low and high with output_low()
and output_high() functions. The SSP module doesn't handle
chip selects. You have to do it in code. |
|
|
theteaman
Joined: 04 Aug 2006 Posts: 98
|
|
Posted: Tue Sep 12, 2006 6:24 pm |
|
|
PCM programmer wrote: | Use digital i/o pins as Chip Select signals -- one for each device.
For example, pin C0 could be the \CS for the first device and
pin C1 could be the \CS for the 2nd device. These \CS pins
have to be manually set low and high with output_low()
and output_high() functions. The SSP module doesn't handle
chip selects. You have to do it in code. |
Yep thats what I was thinking.. thank you for your help!
thank you all! |
|
|
theteaman
Joined: 04 Aug 2006 Posts: 98
|
|
Posted: Tue Sep 12, 2006 6:59 pm |
|
|
theteaman wrote: | PCM programmer wrote: | Quote: | Essentially what I want to do is clock some data serially out of the
PIC at a set frequency (say, 8000Hz) |
Look at the SSP module:
SSP is an abreviation for Synchronous Serial Port. It can clock out
data at a set frequency. Use the dedicated pins SCK and SDO for
clock out and data out, respectively. Use the setup_spi() function to
setup the port parameters. Use spi_write() to send out one byte. |
ofcourse! this is embarrasing, in fact, because I already use SPI to interface with some other components. I'll need to share the SPI port with another device but I guess, I can multiplex them?
thanks |
Hi PCM Programmer
I confused myself, but I know realise that in the end this still causes my original problem. SPI will send out 8 bits at a set frequency, but how would I send out more than 8 bits at this frequency? Like a kilobyte? I would need to use interrupts, I'd think.... what do you suggest? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 12, 2006 7:12 pm |
|
|
Can you give some details about the device that's receiving the data,
and also about the bitstream ? For example, is it OK if the bitstream
has a short gap between each byte ? |
|
|
theteaman
Joined: 04 Aug 2006 Posts: 98
|
|
Posted: Tue Sep 12, 2006 7:17 pm |
|
|
PCM programmer wrote: | Can you give some details about the device that's receiving the data,
and also about the bitstream ? For example, is it OK if the bitstream
has a short gap between each byte ? |
Its an audio DAC, I dont need a high quality sample rate or anything, but basically I'm retreiving data from a flash module using SPI. I need to retrieve data from the flash every so often as there is not enough RAM to store all the data at once. I was hoping there was a way I could have a while loop that looked after the memory retrieval, while at the same time the DAC is constantly being fed data at an acceptable rate.. my memory code is done, but I'm having trouble determining the best (and fastest/efficient) way to sent the data out (serially)..
thanks |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Sep 12, 2006 7:32 pm |
|
|
Does this DAC require raw data or must it be in i2s format ?
Can you post the manufacturer and part number of the DAC ? |
|
|
theteaman
Joined: 04 Aug 2006 Posts: 98
|
|
Posted: Tue Sep 12, 2006 7:34 pm |
|
|
PCM programmer wrote: | Does this DAC require raw data or must it be in i2s format ?
Can you post the manufacturer and part number of the DAC ? |
I plan on just sending raw PCM data, its the cs4340 which runs at a low voltage for me.. it does not need to be in i2s format (although it supports it). |
|
|
|