Page 246 - Mechatronics with Experiments
P. 246

232   MECHATRONICS
                                    PWM  Dutycycle  = CCPR1L : CCP1CON < 5:4 > ×                  (4.5)
                                                  (Clock Period) × (Timer Prescale Value)         (4.6)
                                                         1
                                                = 252 ×      × 1 = 0.063 ms                       (4.7)
                                                       4MHz
                              C library functions used to setup and operate the PWM outputs (PWM1 or PWM2)are
                              shown below,

                              #include <pwm.h>
                              #include <timers.h>

                              OpenTimer2(TIMER_INT_OFF & T2_PS_1_4 & P2_POST_1_8) ;
                                      /∗ Setup TIMER2: disable interrupt, set pre and post scalers to 4 and 8 ∗/

                              OpenPWM1(char period ) ;
                                      /∗ Enable and setup PWM1 module output signal ”period” (8-bit value). ∗/
                                 ...  /∗ PWM period=(period+1)∗4∗Tosc∗(TIMER2 Prescaler)∗/

                              SetDCPWM1(unsigned int duty_cycle) ;
                                      /∗ Set ”duty cycle” of PWM output signal (10-bit value) ∗/
                                      /∗  High Time of PWM  =  ( duty_cycle ∗Tosc)  ∗/
                              ...
                              ClosePWM1() ; /∗ Disable PWM1 output module ∗/

                              Analog to Digital Converter (ADC)      The analog to digital converter (A/D or
                              ADC) allows conversion of an analog input signal to a digital number. The ADC on the
                              PIC 18F452 has a 10-bit range and eight multiplexed input channels. Unused analog input
                              channel pins can be configured as digital I/O pins. The analog voltage reference is software
                              selected to be either the chip positive and negative supply (V DD  and V ) or the voltage
                                                                                       SS
                              levels at RA3/V REF+  and RA2/V REF−  pins.
                              There are two main registers to control the operation of ADC:

                                1. ADCON0 and ADCON1 registers: to configure the ADC and control its operation,
                                   that is:
                                     enable ADC,

                                     select the method to trigger the ADC conversion “START” (timer source or polled-

                                     programming by directly writing to a bit of the control register),
                                     select which channel to sample,

                                     select the conversion rate,

                                      format the conversion result.
                                2. ADRESH and ADRESL registers: to hold the ADC converted data in 10-bit format
                                   in the two 8-bit registers.
                              A 10-bit ADC conversion takes 12 × T  time period. The T  time can be software
                                                              AD                 AD
                              selected to be one of seven possible values: 2 × T  ,4 × T  ,8 × T  ,16 × T  ,
                                                                       OSC      OSC      OSC      OSC
                              32 × T  ,64 × T  , or internal A/D module RC oscillator.
                                   OSC      OSC
                                   ADC must be setup with proper register value settings. Then, when ADCON0 bit:2
                              is set, the ADC conversion process starts. At that point, sampling of the signal is stopped,
                              the charge capacitor holds the sampled voltage, and the ADC conversion process converts
                              the analog signal to a digital number using the successive approximation method. The con-
                              version process takes about 12 × T AD  time periods. When an ADC conversion is complete

                                1. the result is stored in ADRESH and ADRESL registers,
                                2. ADCON0 bit:2 is cleared, indicating ADC conversion is done,
                                3. the ADC interrupt flag bit is set.
   241   242   243   244   245   246   247   248   249   250   251