Spring 13

advertisement
EE 200
Final Exam
30 April 2013
Last Name (Print):
First Name (Print):
ID number (Last 4 digits):
Section:
DO NOT TURN THIS PAGE UNTIL YOU ARE TOLD TO DO SO
Problem Weight Score
1
25
2
25
3
25
4
25
Total
100
INSTRUCTIONS
1. You have 2 hours to complete this exam.
2. This is a closed book exam. You may use one 8.5”× 11” note sheet.
3. Calculators are allowed.
4. Solve each part of the problem in the space following the question. If you need more space, continue your solution
on the reverse side labeling the page with the question number; for example, Problem 1.2 Continued. NO
credit will be given to solutions that do not meet this requirement.
5. DO NOT REMOVE ANY PAGES FROM THIS EXAM. Loose papers will not be accepted and a
grade of ZERO will be assigned.
6. The quality of your analysis and evaluation is as important as your answers. Your reasoning must be precise
and clear; your complete English sentences should convey what you are doing. To receive credit, you must
show your work.
1
Problem 1: (25 Points)
1. (13 points) Complete the function in Figure 1 to assign the peripherals pins of a dsPIC33EP64MC502 microcontroller as follows:
• Assign pin 2 and pin 26 for digital input.
• Assign pin 6 and pin 7 for digital output.
For each line a code, include a short comment describing its purpose.
void initialize_DIO_ports(void) {
}
Figure 1: Function for initializing DIO ports.
2
2. (12 points) Consider the microcontroller code in Figure 2 on page 4 that calls the C function in Figure 1.
(a) (2 points) Determine the value of int x in the expression
int x = PORTAbits.RA0 + 2*PORTBbits.RB15;
if pin 2 is connected to VSS and pin 26 is connected to VDD .
(b) (4 points) If the value of int x is three, approximate the time, in units of seconds, it will take for the inner
while loop to complete execution each time it is called.
(c) (6 points) If the voltage at the inputs pins 2 and 26 are fixed so that int x is two, what is the approximate
period and duty cycle of the square-wave signal generated at pin 6?
3
int main(void) {
int int_x;
initialize_DIO_ports();
while (1) {
int_x = PORTAbits.RA0 + 2*PORTBbits.RB15;
while (int_x > -1) {
switch(int_x){
case 0: PORTBbits.RB2 = 0;
case 1: PORTBbits.RB2 = 1;
case 2: PORTBbits.RB2 = 0;
case 3: PORTBbits.RB2 = 1;
} // end of switch statement
PORTBbits.RB3
PORTBbits.RB3
PORTBbits.RB3
PORTBbits.RB3
int_x = int_x - 1;
__delay_ms(1000);
} // end of inner while loop
} // end of outer while loop
} // end of main function
Figure 2: Microcontroller C-code.
4
=
=
=
=
0;
0;
1;
1;
break;
break;
break;
break;
Problem 2: (25 Points)
Figure 3 on page on page 6 shows C-code for realizing a Moore finite state machine.
1. (2 points) What are the input and and output signals to the finite state machine in terms of RAx and RBx.
2. (3 points) How many states does this Moore machine use?
3. (10 points) Write out the state table.
4. (10 points) Sketch the state diagram.
5
typedef enum {
S0, S1, S2, S3
} S;
S
s = S1;
int main(void) {
int z;
initialize_DIO_ports();
while (1) {
z = PORTAbits.RA0;
switch (s) {
case S0:
switch (z) {
case 0: s
case 1: s
}; break;
case S1:
switch (z) {
case 0: s
case 1: s
}; break;
case S2:
switch (z) {
case 0: s
case 1: s
}; break;
case S3:
switch (z) {
case 0: s
case 1: s
}; break;}
switch (s) {
case S0:
case S1:
case S2:
case S3:
= S0; break;
= S1; break;
= S2; break;
= S3; break;
= S0; break;
= S1; break;
= S2; break;
= S3; break;
LATBbits.LATB0
LATBbits.LATB0
LATBbits.LATB0
LATBbits.LATB0
=
=
=
=
0;
1;
0;
0;
break;
break;
break;
break;}
__delay_ms(1000);}
}
Figure 3: Microcontroller C-code for implementing a Moore finite state machine.
6
Problem 3: (25 Points)
In this problem you will modify the code in Problem 2 to include an asynchronous reset input connected to pin 26.
When the logic level transitions from high to low on pin 26, the finite state machine must be immediately placed in
state S1.
1. (13 points) The function in Figure 4 initializes the interrupt controller so that a falling edge on pin 26 generates
an interrupt using external interrupt 2 (INT2). Complete the C-code by filling in the dashed lines with
appropriate numerical constants and comments.
void initialize_Interrupt_Controller(void)
{
RPINR1bits.INT2R = _______; // ___________________________
INTCON2bits.GIE
=
1; // set global interrupt enable
INTCON2bits.INT2EP = _______; // ___________________________
IFS1bits.INT2IF
=
0; // clear interrupt flag
IEC1bits.INT2IE
=
1; // enable interrupt
}
Figure 4: Function for initializing INT2.
2. (12 points) Complete the interrupt service routine (ISR) in Figure 5. The ISR must place the system in state
S1 and appropriately set the finite state machine output.
void __attribute__((__interrupt__,auto_psv)) _INT2Interrupt (void)
{
}
Figure 5: Interrupt service routine.
7
Problem 4: (25 Points)
Figure 6 on page 9 shows code similar to that used in Laboratory #25 to implement an LED dimmer. In this
experiment, a mechanical encoder generates quadrature signals QEA and QEB that are read by the microcontroller
to determine the percent duty cycle of a PWM output signal. Complete the code in Figure 6 by completing the
following tasks.
1. (12 points) Determine the value of the register RPINR14, in hexadecimal format, so that the quadrature inputs
signals QEA and QEB can be connected to pins 6 and 7, respectively.
RPINR14 = 0 ×
2. (13 points) The original code is written so that a 360◦ rotation of the mechanical encoder shaft sweeps the
duty cycle from 0% to %100. Modify the code so that a 720◦ rotation of the mechanical encoder shaft sweeps
the duty cycle from 0% to %100.
(a) (3 points) Circle the two lines of code in Figure 6 that must be modified.
(b) (4 points) In two or three sentences, justify why the circled lines require modification.
(c) (6 points) In the space below, write and comment the modified code lines.
8
int main(void) {
/**** Setup PWM Module ****/
PTCONbits.PTEN = 0; /* disable the PWM module */
PTPER = 7370; /* period as a multiple of clock cycles, */
IOCON1bits.PMOD = 1; /* PWM1 I/O pin pair is in redundant mode */
IOCON1bits.PENH = 1; /* PWM module controls pin 25, PWM1H */
IOCON1bits.POLH = 0; /* PWM1H is active high */
PWMCON1bits.ITB = 0;
PWMCON1bits.MDCS = 0;
PWMCON1bits.MTBS = 0;
PWMCON1bits.CAM = 0;
/*
/*
/*
/*
PTPER register specifies period */
PDC1 register specifies duty cycle */
PWM uses primaster time base as the clock source*/
Enable edge-aligned mode */
FCLCON1bits.FLTMOD = 3; /* fault input disabled
*/
PTCON2 = 0x0000; /* PWM input clock prescaler: divide by 1 */
PTCONbits.PTEN = 1; /* enable the PWM module */
/* Setup pins 6 and 7 for digital input */
set_IO_ports();
/**** Setup QEI Module ****/
RPINR14 = ______;
QEI1CONbits.QEIEN = 0;
QEI1CONbits.PIMOD = 6;
/* disable QEI counters */
/* modulo count mode, max count = QEI1GEC register*/
QEI1GECH = 0;
QEI1GECL = 95;
/* high (bits 16:32) of QEI1GEC register */
/* low (bits 0:15) of QEI1GEC register */
QEI1CONbits.QEIEN = 1;
/* enable QEI counters */
/* infinite loop */
while (1) {
/* Set duty cycle to QEI counter value */
PDC1 = floor(7370 * (double) POS1CNTL / 95);
/* delay */
__delay_ms(100);
}
}
Figure 6: LED dimmer code similar to that used in Laboratory #25.
9
Download