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

Setting Sampling Rate of analog signal pic16f877A

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



Joined: 23 Jul 2007
Posts: 103

View user's profile Send private message

Setting Sampling Rate of analog signal pic16f877A
PostPosted: Sat Apr 12, 2008 8:23 am     Reply with quote

Hi Guys,
I'm sampling an analog signal whose cut off frequency,Fc is 400HZ.
I've chosen a sampling frequency Fs of 1000HZ (1/FS = 1msec) based on Nyqueist criteria.Fs =2*Fc.

When a button is pressed the interrupt is supposed to be enabled and signal sampled at the rate of 1000HZ and the adc values saved in an external EEProm with a counter incremented. if count is equal to Max length of EEMprom, the interrupt is disabled and the processor waits for the next button pressed.

The question is how do I implement this using timers interrupt?
Kind regards,
S.Kein Confused
Ttelmah
Guest







PostPosted: Sat Apr 12, 2008 10:08 am     Reply with quote

For this, the best solution, is one of the CCP's.
One option for these, is to _automatically_ trigger an ADC conversion. It triggers, when the 'compare' value in the CCP, matches the count in timer1. Now, you don't give what clock rate you have, so I'll just guess. Assuming (say) an 16MHz crystal, Timer1, can be set to the /4 prescaler. It will then count in Fosc/4/4 steps (1MHz - keep it simple...). If the compare value is then set to '1000', the CCP will trigger every mSec.
Now, you ignore the CCP event completely, instead enabling an interrupt on INT_AD. This is one of the few situations, where this is useful, since it will trigger when the conversion completes. In this routing, you _just_ read the ADC (read_adc(ADC_READ_ONLY)), and set a flag to say this has been done.
At this point the 'main' code takes over.
In the main code, when the flag is set, you sve the data, then clear the flag.
At this point everything is fine. However you now run into your big problem. 'External EEPROM'. This _will not_ be fast enough. EEPROM, has a 'write time', that is typically at least 2mSec (most cheper designs are slower than this 4mSec). You want to be writing a value every mSec, so cannot afford this time. You need to think again about the memory selection. Look at FRAM memories. These are pin compatible, and nearly software compatible for some of the EEPROM variants, but write almost instantaneously.
This should then allow the idea to work.
Obviously, enabling this with a button, and stopping when a count gets to a limit value, is just down to a test in front of the main wait loop, and a test in this loop to disable the interrupt and tidy up.

Best Wishes
kein



Joined: 23 Jul 2007
Posts: 103

View user's profile Send private message

Sampling rate setting with interrupt
PostPosted: Sun Apr 13, 2008 3:40 am     Reply with quote

Thanks Ttelmah,
My clock frequency is 20Mhz. FRAM are hard to find around here. So what I could do is lower the sampling frequency to 333.3HZ so that an interrupt occurs every 3ms. In this case the data will take 2ms to get saved into EEPROM and the other 1ms idle. My signal cut of frequency doesn't have to be 500HZ. A cutoff frequency Fc of 100HZ will just be fine with me. Will this work?
Best regards,
Ttelmah
Guest







PostPosted: Sun Apr 13, 2008 4:35 am     Reply with quote

You will need to check the spec of your EEPROM. However on most of the more readily available I2C parts, the time needed is 4mSec, or even slightly more (for example,the MicroChip 24LC16, is specified as 5mSec). However, remember this is _per write_. On most, you can transfer multiple bytes as a block and perform just one write for the whole block. Depending on the page size for this, you might buffer a whole page, and write in one go, while the interrupt keeps on handling the reading.
Double check on the FRAM, these may be much more available than you think. They have become a very common part in recent months, being substituted for EEPROM, in a lot of products. Not only faster writes, but much greater life as well.

Best Wishes
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