0% found this document useful (0 votes)
32 views15 pages

Micro Unit 4 Question Answers - Watermark

Unit 4

Uploaded by

Tanmay shitole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views15 pages

Micro Unit 4 Question Answers - Watermark

Unit 4

Uploaded by

Tanmay shitole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

UNIT – IV: Peripheral Support in PIC 18FXXXX

Q. 1 Draw Functional Diagram of PIC Timer and Explain concept of prescaler.

• The timers are used to measure the time or generate the accurate time delay.
• The timer takes the internal clock as a reference clock, while the counter counts external
clocks or pulses applied through port pins.

• PIC18F Family has 2 to 5 has independent timers which can be used as timer, Counters
or for PWM generation.
• Below table provides the details of the twoTimers

Timer Size Control Count Register Flag Bit Modes


Register
TIMER0 16-bit T0CON TMR0H,TMR0L TMR0IF 8 Bit and 16 Bit Mode

TIMER1 16-bit T1CON TMR1H,TMR1L TMR1IF 16 Bit Mode only

• 8-bit Mode capable of counting 0-255

• 16-bit Mode – capable of counting 0-65535

Prescalar Concept

Prescalar is a configurable clock-divider circuit.

It can be used to divide the clock frequency input to the timer module.

For example, if the instruction clock is 5MHz and we use a prescaler of 2 to divide it which
effectively makes the clock 2.5MHz.

So each counting time will increase from 0.2 µs to 0.4 µs


Mrs. Trupti Thite, TE ENTC Microcontroller Page 1
Q. 2 Explain T0CON and T1CON Register in PIC 18Fxxx

T0CON is an 8-bit register used to Control the Timer 0 Operations

Explanation of all 8 Bits is Given Below,


TMR0ON (Timer0 on/off control bit)

1 = Enable timer 0
0 = Disable timer 0

T08BIT (Timer0 8-bit/16-bit control bit)

1 = Configured as an 8-bit timer/counter


0 = Configured as an 16-bit timer/counter

T0CS (Timer0 clock source select bit)

1 = Transition on T0CKL pin


0 = Internal instruction cycle clock (CLK0)
T0SE (Timer0 Source Edge select bit)

1 = High to Low transition on T0CKL pin


0 = Low to High transition on T0CKL pin

PSA (Timer0 Prescaler assignment bit)

1 = Timer 0 Prescaler is not assigned


0 = Timer 0 Prescaler is assigned

T0PS2 - T0PS0 (Timer0 prescaler select bit)

111 - 1:256 prescaler value


110 - 1:128 prescaler value
101 - 1:64 prescaler value
100 - 1:32 prescaler value
011 - 1:16 prescaler value
010 - 1:8 prescaler value
001 - 1:4 prescaler value
000 - 1:2 prescaler value

Mrs. Trupti Thite, TE ENTC Microcontroller Page 2


T1CON is an 8-bit register used to Control the Timer 1 Operations

Explanation of all 8 Bits is Given Below,


TMR1ON (Timer1 on/off control bit)

1 = Enable timer 1
0 = Disable/STOP timer 1

TMR1CS (Timer1 clock source select bit)

1 = External Clock
0 = Internal Clock

T1sync (Timer1 synchronization bit)

1 = Counter mode (for external clock only)


0 = otherwise
T1OSCEN (Timer1 Oscillator enable bit)

1 = Timer1 Oscillator enabled


0 = Timer1 Oscillator off

T1CKPS1:T1CKPS0 (Timer1 Prescaler SELECTOR bit)

00 – 1:1 prescaler value


01 - 1:2 prescaler value
10 - 1:4 prescaler value
11 - 1:8 prescaler value

RD16 (16 BIT Enable bit )

1 = One 16 Bit Operation


0 = two 8 bit operation

Mrs. Trupti Thite, TE ENTC Microcontroller Page 3


Q. 3 Write C code to toggle RC0 bit after 1m second, 16-bit operation Timer 0, XTAL
frequency = 10MHz (without Prescaler)

Mrs. Trupti Thite, TE ENTC Microcontroller Page 4


Mrs. Trupti Thite, TE ENTC Microcontroller Page 5
Q. 4 Explain interrupt structure of PIC18F

 Interrupts are mechanisms which enable instant response to events


 In normal mode, microcontroller executes the main program using Polling
 Upon interrupt, microcontroller stops the execution of main program and commences the
special part of the program(ISR) which will analyze and handle the interrupt.

The PIC18F devices have multiple interrupt sources and an interrupt priority feature that allows
each interrupt source to be assigned a high priority level or a low-priority level

PIC18F Interrupts Sources


 Timers 0, 1
 Pins RB0, RB1, RB2 for external hardware interrupts INT0,INT1, INT2
 PORTB-Change interrupt (any one of the upper four Port B pins)
 Serial communication’s USART interrupts: receive and transmit, respectively
 ADC (analog-to-digital converter)
 CCP (compare capture pulse-width-modulation)
 There are ten registers which are used to control interrupt operation.
These registers are:
o RCON
o INTCON
o INTCON2
o INTCON3
o PIR1, PIR2
o PIE1, PIE2
o IPR1, IPR2
 Interrupt vector table for PIC18 (ROM location)

 Power-on reset 0x0000

 High priority interrupt 0x0008

 Low priority interrupt 0x0018

Mrs. Trupti Thite, TE ENTC Microcontroller Page 6


Q. 5 Explain PORTB Change Interrupt of PIC18F

• Four pins of PORTB can cause an interrupt when any changes detected on them, They
are referred as PORTB change interrupt

• An input change on PORTB<7:4> sets flag bit, RBIF (INTCON<0>).

• The interrupt can be enabled/disabled by setting/clearing enable bit, RBIE and it has flag
RBIF Located in INTCON

INTCON Register

Q. 6 Elaborate Timer Interrupts and Steps for Programming PIC18F4550 Timer using
Interrupt.

Timer Interrupt Flag Bits and Associated Registers are given below

Interrupt Flag Bit Register Enable Bit Register

• Timer0 • TMR0IF • INTCON • TMR0IE • INTCON


• Timer1 • TMR1IF • PIR1 • TMR1IE • PIE1

Mrs. Trupti Thite, TE ENTC Microcontroller Page 7


Steps for Programming PIC18F4550 Timer using interrupt

1. Enable GIE, PEIE, TMR1IE.

2. Configure the T1CON register.

3. Clear TMR1IF Timer1 interrupts flag.

4. Load the count in TMR1H (higher byte) andTMR1L (lower byte).

5. Set TMR1ON to start the Timer1 operation.

6. When TMR1IF = 1, code will jump to ISR to execute it, and after execution control
returns to the main program.

Q. 9 Describe CCP module in PIC 18.

• PIC 18 has 0 and 5 CCP modules inside it as CCP1, CCP2 and CCP so on.
• PWM feature of the CCP has been enhanced for better DC motor control. It is called
enhanced CCP [ECCP].
• Each Capture/Compare/PWM module is associated with a control register (generically,
CCPxCON) and a data register (CCPRx).
• The data register, in turn, is comprised of two 8-bit registers: CCPRxL (low byte) and
CCPRxH (high byte).

Mrs. Trupti Thite, TE ENTC Microcontroller Page 8


CCPxCON Register

DCXB1:DCXB0:-Used to select duty cycle in PWM Mode

CCxM[3:0]:- Used to specify the mode of operation of CCPx module


0000 : Capture/Compare/PWM disabled (resets CCPx module)
0010: Compare mode: Toggle output on match (CCPxIF bit is set)
0100: Capture mode: Every falling edge
0101: Capture mode: Every rising edge
0110: Capture mode: Every 4th rising edge
0111: Capture mode: Every 16th rising edge
1000: Compare mode
1001: Compare mode
1010: Compare mode
1011: Compare mode:
11xx: PWM mode

• Capture Mode provides access to the current state of a register which constantly changes
its value. In this case, it is the timer TMR1 register. The Capture Mode is used to time a
particular event
• Compare Mode constantly compares values of two registers. One of them is the timer
TMR1 register. This circuit also allows the user to trigger an external event when a
predetermined amount of time has expired.
• PWM (Pulse Width Modulation) This mode allow us to generate pulses with variable
width through Program. PWM is widely used in DC Motor control



Mrs. Trupti Thite, TE ENTC Microcontroller Page 9
10. Explain Generation of PWM using CCP Module

PIC 18 CCP Module has PWM feature, which allows programmer to create pulses of variable
widths.
Pulse Width Modulation (PWM) is a technique by which the width of a pulse is varied while
keeping the frequency of the wave constant.

In creating pulses with variable widths for PWM two factors are important
1. Setting Period of Pulse
2. Setting Duty Cycle of Pulse

Steps to set two factors are given below


1. Setting Period of PWM
To set period Calculate PR2 Register Value using below formula

Here, Fpwm =Expected PWM frequency


Fosc= Oscillator frequency
N=Prescalar value
2. Setting Duty Cycle of PWM
A. Multiply PR2 Value by expected duty cycle.
B. Load whole number of answer in CCPRL1 Register
C. to set factional part (Decimal Point Part) of answer use DC1B1 and DC1B2 bit as
shown in below table

Mrs. Trupti Thite, TE ENTC Microcontroller Page 10


10. Write Steps to Program PWM Module

Following steps are taken to program PWM Module of CCP,

11. Explain Interfacing of PIC 18 F with DC Motor for speed control using CCP Module
Or
Write code of DC Motor Control Using PWM Module
Or

Write a program for 2.5KHz and 75% duty cycle PWM generation with N=4. Assume
Fosc = 10MHz

Answer:

Mrs. Trupti Thite, TE ENTC Microcontroller Page 11


Following are steps,

1. Find value of PR2


• PR2 = [Fosc/Fpwm*4*N]-1 = [10Mhz/2.5KHz*4*4]-1 = 249
2. Find value of CCPR1L
• CCPR1L = PR2*Duty Cycle = 249 * 0.75 = 186.75 ≈ 186
3. Set the TMR2 pre-scalar value, and Enable TMR2 by writing T2CON
• T2CON = 0x01 (00000001)
4. Configure CCPx module for PWM and set DC1B2 and DC1B1 for decimal portion of duty
cycle 0.75 , S0 11 will be loaded into CCP1CON 5:4

Program:

#include<PIC18F4550.h>

12. Explain PIC 18F ADC

Analog to digital converters are mostly used for data acquisition to convert analog sensor output
into Digital.
PIC 18 F has inbuilt on chip ADC with 10 bit resolution and having 16 Channels
Following table gives details about registers required for ADC programming

Mrs. Trupti Thite, TE ENTC Microcontroller Page 12


A/D Control Register (ADCON0) is used for channel selection as below

Steps for Programming ADC


1. Configure ADCON1 Register to select Reference voltage using VCFG1: VCFG0 bits and
also configure port pins which we require as an analog input using PCFG3: PCFG0 bits.
2. Configure ADCON2 Register to select A/D result format, A/D clock, and acquisition time.

A/D conversion and Read digital values


1. Configure ADCON0 Register to select a channel that we require using CHS3: CHS0.
2. Start A/D conversion by setting ADON bit and Go/done’ bit of ADCON0 Register.
3. Wait for G0/done’ bit which is cleared when the conversion is completed. 4.Then copy
Digital data which is stored in ADRESH and ADRESL Register

Program is as below,

Mrs. Trupti Thite, TE ENTC Microcontroller Page 13


13. Explain LM 35 Interfacing toPIC18F using ADC

• LM35 is a temperature sensor that can measure temperature in the range of -55°C to 150°C.
• It is a 3-terminal device that provides an analog voltage proportional to the temperature.
• The higher the temperature, the higher is the output voltage.
• The output analog voltage can be converted to digital form using ADC so that a
microcontroller can process it.
• Let’s interface the LM35 temperature sensor with PIC18FXX and display the surrounding
temperature on the LCD16x2 display.
• LM35 gives output in the analog form so connect out pin of a sensor to one of the ADC
channels of PIC18FXX.

Interfacing Diagram is as shown below,

Mrs. Trupti Thite, TE ENTC Microcontroller Page 14


Algorithm:

Mrs. Trupti Thite, TE ENTC Microcontroller Page 15

You might also like