You are on page 1of 6

Name Surname: SOLUTIONS

Atılım Un. EE Dept. Signature :


EE 208, 2011-12 Spring
rd
Final Examination Duration: 120 min 24 May 2012

1. (20pt) Assume that the TTL signal shown in the figure is connected to CCP1 pin. The microcontroller
is using a crystal oscillator running at frequency of 4 MHz. The C18 program which is given below,
starts running on the microcontroller at t = 0. Find the value of variable Y at
a. t = 25 ms answer: Y = 24000 – 5000 = 19000

b. t = 73 ms answer: Y = 62000 – 40000 = 22000

c. t = 85 ms answer: Y = 13465– 4465 = 9000

int X, X_old, Y ;
void main(void) {
CCP1CON = 0b00000101;
T3CONbits.T3CCP1 = 0; //Timer1 is the source for capture
T3CONbits.T3CCP2 = 0; //Timer1 is the source for capture
TRISCbits.TRISC2 = 1;
T1CON = 0b10000001;
TMR1H = 0x00 ;
TMR1L = 0x00 ;
INTCONbits.GIE = 1;
INTCONbits.PEIE = 1;
PIE1bits.CCP1IE = 1;
while(1);
}
#pragma interrupt ccp_int
void ccp_int(){ // Interrupt Service Routine
X = 256*CCPR1H + CCPR1L;
if( X > X_old) Y = X - X_old ;
X_old = X ;
PIR1bits.CCP1IF = 0 ;
}
#pragma code int_vect = 0x0008

www.atilimforum.com
void My_Int_Vector_Code(void){
_asm GOTO ccp_int _endasm }

0 t(ms)
5 11 24 29 40 56 62 64 70 75 79 90

Timer1 = 5000 Timer1 = 40000 Timer1 = 4465

Timer1 = 24000 Timer1 = 62000 Timer1 = 13465


Name Surname: SOLUTIONS

2. (20pt)
a. Plot the voltage waveform that is generated at the CCP1 pin for the below configuration of CCP
module and Timer1. Calculate the period of this waveform. Assume that the oscillator frequency is
20 MHz.
CCPR1 (CCP1 compare register) = 0016
CCP1CON ( control register of CCP1 module) = 000000102
T1CON (Timer 1 control register) = 000101012
b. Repeat part (a) for CCP1CON = 000010012
Answer:
a.
CCP1CON = 0000000102  Compare mode, toggle output on match (CCPxIF bit is set)
T1CON = 000101012  Timer 1 is Enabled,
Timer 1 uses internal clock (FOSC/4),
Timer 1 prescaler value is 1:2
Each time Timer1 takes the value 0 and becomes equal to CCPR1 register, a match event occurs and
CCP1 pin is toggled. Timer1 increments every . The duration between
each toggles is 65536 x 0.4µs  26 ms. So the waveform is:
V
T = 26 x 2 = 52 ms
5V

0V t

b. CCP1CON = 000010012  Compare mode, Initialize CCP pin High, on compare match force CCP
pin Low
So the waveform is:
V

5V

0V t
www.atilimforum.com
Name Surname: SOLUTIONS

3. (20 pt) The simplified block diagram for the PWM module of PIC18F452 microcontroller is given in the
following figure. Find the period and duty cycle (%) of the waveform generated at CCP1 pin.

Answer
Timer 2 operates with a clock frequency of and therefore increments every
. So the period of the generated waveform is 0.2µs x PR2 = 0.2µs x (250+1) = 50.2µs.

The extended 10-bit timer (Timer2 + 2bits) operates with a clock frequency of 10MHz and therefore
increments every . So CCP1 pin remains high for 0.05 µs x (Duty cycle register) =
0.05µs x 98 = 4.9 µs.
Duty Cycle = 4.9 / 50.2 = 9.8%

4. (20 pt) Write a program is assembly language which takes exactly 755µs to execute. Your code should
not exceed 20 instructions. Assume that the crystal frequency is 4 MHz.
Answer:
MOVLW D'250'
MOVWF COUNT1
LOOP1 DECF COUNT1
BNZ LOOP1
NOP
NOP
NOP

www.atilimforum.com
NOP

5. (30pt) Write a program, either in C18 or in assembly language (both are acceptable), which utilizes
Timer1 to generate a TTL waveform with a period of 70ms on PORTB.RB1 pin. Assume that the
crystal frequency is 4MHz. (Timer1 roll-over flag bit: PIR1bits.TMR0IF). You are free to use interrupt
or polling.
Name Surname: SOLUTIONS

6. (20pt)

a. Write a program in assembly language that adds the numbers in the file registers between addresses
0x123 and 0x1E5 and stores the sum in the working register. Your code should not exceed 25 lines.
You may assume that the sum is less than 256. (Hint: Use File Select Register)

b. Modify the program that you have written in part (a) such that the sum is allowed to be larger than
255.
Answer:
a. There are 203 register between addresses 0x123 and 0x1E5
CNT EQU 0X02
LFSR 0, 0x123
MOVLW 0xC2 ; 203 in decimal
MOVWF CNT, ACCESS
MOVLW 0
RPT ADDWF POSTINC0, W ; WREG holds the low byte of the sum
DECFSZ CNT, F, ACCESS
BRA RPT

b.
CNT EQU 0X02
SUM_H EQU 0X05 ; Stores the high byte of the sum
SUM_L EQU 0X06 ; Stores the low byte of the sum
LFSR 0, 0x123
MOVLW 0xC2 ; 203 in decimal
MOVWF CNT, ACCESS
MOVLW 0
RPT ADDWF POSTINC0, W ; WREG holds the low byte of the sum
BNC SKP ; If carry occurred
INCF SUM_H, F, ACCESS ; increment the high byte of sum
SKP DECFSZ CNT, F, ACCESS
BRA RPT
MOVWF SUM_L, ACCESS

www.atilimforum.com
Name Surname: SOLUTIONS

HINTS
INTCONbits.GIE Global interrupt enable bit
INTCONbits.PEIE Peripheral interrupt enable bit
PIE1bits.CCP1IE CCP1 interrupt enable bit
Interrupt Vector: 0x0008

www.atilimforum.com
Name Surname: SOLUTIONS

www.atilimforum.com
INDIRECT ADDRESSING

You might also like