Consider the following code segment that utilizes the watchdog timer in in the i
ID: 3600780 • Letter: C
Question
Consider the following code segment that utilizes the watchdog timer in in the interval mode with period set to 1 s (line 4 of the code).
#include <msp430xG46x.h>
void main(void) {
int p = 0;
WDTCTL = WDT_ADLY_1000; // 1 s interval timer
P2DIR |= BIT2; // Set P2.2 to output direction
P2OUT |= BIT2;
for (;;) {
if ((IFG1 & WDTIFG) == 1) {
p++;
if (p == 5) {P2OUT ^= BIT2; p=0;}
IFG1 &= ~WDTIFG;
}
}
}
A. (5 points) What does the code segment do? Sketch the signal you would observe on P2.2.
B. (5 points) How would you implement the given functionality using an interrupt service routine.
C. (10 points) You would like to generate two pulse-width modulated (PWM) signals P1 (75% of duty cycle) and P2 (50% of duty cycle), with frequency of 1 KHz. Assume that an external high-frequency resonator is used to provide 8MHz clock on ACLK that is used by TimerA. Can you do this using TimerA? If yes, describe a TimerA configuration that will carry out signal generation? Note: use English and waveforms to describe your solution.
P1
P2
0ms
0.25ms
0.5ms
0.75ms
1ms
1.25ms
1.5ms
1.75ms
P1
P2
Explanation / Answer
[cc lang=”Matlab”]clc;
clear all;
close all;
F2=input(‘Message frequency=’);
F1=input(‘Carrier Sawtooth frequency=’);
A=5;
t=0:0.001:1;
c=A.*sawtooth(2*pi*F1*t);%Carrier sawtooth
subplot(3,1,1);
plot(t,c);
xlabel(‘time’);
ylabel(‘Amplitude’);
title(‘Carrier sawtooth wave’);
grid on;
m=0.75*A.*sin(2*pi*F2*t);%Message amplitude must be less than Sawtooth
subplot(3,1,2);
plot(t,m);
xlabel(‘Time’);
ylabel(‘Amplitude’);
title(‘Message Signal’);
grid on;
n=length(c);%Length of carrier sawtooth is stored to ‘n’
for i=1:n%Comparing Message and Sawtooth amplitudes
if (m(i)>=c(i))
pwm(i)=1;
else
pwm(i)=0;
end
end
subplot(3,1,3);
plot(t,pwm);
xlabel(‘Time’);
ylabel(‘Amplitude’);
title(‘plot of PWM’);
axis([0 1 0 2]);%X-Axis varies from 0 to 1 & Y-Axis from 0 to 2
grid on;[/cc]
clc;
clear all;
t = 0:0.001:1;
fc = input('Enter the Frequency of Carrier Signal (Sawtooth) = ');
fm = input('Enter the Frequency of Message Signal (Sinusoidal) = ');
a = input('Enter the Amplitude of Carrier Signal = ');
b = input('Enter the Amplitude of Message Signal(should be < Carrier) = ');
vc = a.*sawtooth(2*pi*fc*t);
vm = b.*sin(2*pi*fm*t);
n = length(vc);
for i = 1:n
if (vm(i)>=vc(i))
pwm(i) = 1;
else
pwm(i) = 0;
end
end
% Representation of the Message Signal
subplot(3,1,1);
plot(t,vm,'black');
xlabel('Time ----->');
ylabel('Amplitude ----->');
title('Message Signal');
legend('Message Signal ---->');
grid on;
% Representation of the Carrier Signal
subplot(3,1,2);
plot(t,vc);
xlabel('Sample ----->');
ylabel('Amplitude ----->');
title('Carrier Signal');
legend('Carrier Signal ---->');
grid on;
% Representation of the PWM Signal
subplot(3,1,3);
plot(t,pwm,'red');
xlabel('Sample ----->');
ylabel('Amplitude ----->');
title('PWM Signal');
legend('PWM Signal ---->');
axis([0 1 0 2]);
grid on;
% Add title to the Overall Plot
ha = axes ('Position',[0 0 1 1],'Xlim',[0 1],'Ylim',[0 1],'Box','off','Visible','off','Units','normalized', 'clipping' , 'off');
text (0.5, 1,'f Pulse Width Modulation ','HorizontalAlignment','center','VerticalAlignment', 'top')
clc;
clear all;
t = 0:0.001:1;
fc = input('Enter the frequency of carrier signal (sawtooth) = ');
fm = input('Enter the frequency of message signal (sine) = ');
a = input('Enter the amplitude of carrier signal = ');
b = input('Enter the amplitude of message signal(should be < Carrier) = ');
vc = a.*sawtooth(2*pi*fc*t);
vm = b.*sin(2*pi*fm*t);
n = length(vc);
for i = 1:n
if (vm(i)>=vc(i))
pwm(i) = 1;
else
pwm(i) = 0;
end
end
s = daq.createSession('ni');
addAnalogInputChannel(s,'cDAQ1Mod1', 0, 'Voltage');
addCounterOutputChannel(s,'cDAQ1Mod5', 0, 'PulseGeneration');
s
subplot(3,1,1);
plot(t,vm);
xlabel('Time ----->');
ylabel('Amplitude ----->');
title('Message Signal');
grid on;
subplot(3,1,2);
plot(t,vc);
xlabel('Time ----->');
ylabel('Amplitude ----->');
title('Carrier Signal');
grid on;
subplot(3,1,3);
plot(t,pwm);
xlabel('Time ----->');
ylabel('Amplitude ----->');
title('PWM Signal');
axis([0 1 0 2]);
grid on;
ch = s.Channels(2);
ch.Frequency = 10;
ch.InitialDelay = 0.5;
ch.DutyCycle = 0.75;
s.Rate = 1000;
s.DurationInSeconds = 1;
% StartForeground returns data for input channels only. The data variable
% will contain one column of data.
[data, time] = s.startForeground();
plot(time, data);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.