timed sampling

I have a feeling i’m going to feel like an idiot asking this question, but…
I need to get a sample from a sensor every minute how would I do it in MPLAB? I know in easy c there’s just a wait command but what are my choices in MPLAb? thanks muchly for your responses. :smiley:

When using MPLAB and PIC18 C you can configure one of the timers to generate an interrupt at a specified interval, say 100 milliseconds, using timer interrupts and an Interrupt Service Routine (ISR). In the ISR, you read the ADC to sample and store your analog data using a short routine. The data can then be processed in the mainline PIC18 C application.

sorry I wasn’t too specific on the sensor. it’s actually a serial device so i’ll be calling my data reading routine if the serial interrupt is true around the 1 minute mark.
How would you get the timer to generate an interrupt? is there a pre written function for this or do I have to write one?

Microchip provides a peripheral library that configures the timer peripheral called timer.h, which does not require an external 32.768 kHz crystal or extra capacitors, but is not as accurate as using a clock crystal for time keeping. Timer interrupts will also need to be configured as part of a timer based Interrupt Service Routine (ISR). You can find documentation on the available function calls in the PIC18 C Peripheral library Reference Manual found on the Microchip WEB site. It has examples on how to call each function, although it is a bit confusing. Tutorials and application notes regarding timer0 and timer1 may be found at this location:

Another option is to use the Real Time Clock (RTC), which requires an external 32.768 kHz crystal and two 12 Pf capacitors to generate very accurate clock interrupts every minute, and call your serial routine from inside the RTC Interrupt Service Routine (ISR). This option can be configured directly from MPLAB using the Visual Initializer tool available on the MPLAB IDE although it still may require an ISR. More information on the RTC can be found at this link:

IFI has a white paper that provides a nice primer to using timers and interrupts in MPLAB.


  • Dean