Page 247 - Mechatronics with Experiments
P. 247
MICROCONTROLLERS 233
The program can determine whether ADC conversion is complete or not by either checking
the ADCON bit:2 (cleared) or waiting for ADC conversion complete interrupt if it is
enabled. The sampling circuit is automatically connected to the input signal again, and the
charge capacitor tracks the analog input signal. The next conversion is started again by
setting the ADCON0 bit:2.
The ADC can be configured for use either by directly writing to the registers, or
by using C library functions present in the C-18 compiler. The C functions OpenADC(..),
ConvertADC (), BusyADC() and ReadADC(), CloseADC() are used to configure the ADC
module, begin the conversion process, and read the result of conversion, and disable the
ADC converter. Examples of their use are given below:
#include <p18f452.h>
#include <adc.h>
#include <stdlib.h>
#include <delays.h>
OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_8ANA_0REF, ADC_CH0 & ADC_INT_OFF);
/∗ Enable ADC in specified configuration. ∗/
/∗ Select: clock source, format (right justified), reference voltage source,
channel 0 , enable/disable interrupt on ADC-conversion completion ∗/
while(1)
{
ConvertADC(); /∗ Start the ADC conversion process∗/
while ( BusyADC() ) ; /∗ Wait until conversion is complete:
return 1 if busy, 0 if not.∗/
result = ReadADC(); /∗ Read converted voltage; store in variable ’result’.
10-bit conversion result will be stored in the
least or most significant 10-bit portion of
result depending on how ADC was configured
in OpenADC(...) call. ∗/
Delay1KTCYx(1) ; /∗ Delay for 1000 TCY cycle ∗/
}
CloseADC(); /∗ Disable the ADC converter ∗/
Here, the arguments in the OpenADC(..) function are used to configure the ADC
module to use the specified clock sources, reference voltages and select the ADC channel.
A detailed description of the arguments is provided in the C-18 users guide.
Timers and Counters In real-time applications, there are many cases when different
tasks need to be performed at different periodic intervals. For instance, in an industrial
control application, the status of doors in a building may need to be checked every minute,
the parking lot gate status may need to be checked every hour. The best way to generate such
periodic interrupts with different frequencies is to use a programmable timer/counter chip.
A timer/counter chip operates as a timer or as a counter. In the timer mode of operation, it
counts the number of clock cycles, hence it can be used as a time measurement peripheral.
The clock source can be an internal clock or external clock. In counter mode, it counts the
number of signal state transitions at a defined pin, that is, at rising edge or falling edge.
The PIC 18F452 chip supports four timers/counters: TIMER0, TIMER1, TIMER2,
and TIMER3. TIMER0 is an 8-bit or 16-bit software selectable, TIMER1 is a 16-bit,
TIMER2 is an 8-bit timer, and TIMER3 is a 16-bit timer/counter. Timer/counter operation
is controlled by setting up appropriate register bits, reading from and writing to certain
registers. Timer operation setup requires enable/disable, source of signal, type of operation