You are on page 1of 3

4.

Design Approach
To implement a reaction timer, the setup code for the seven segment displays was first
included. After setting up the seven segment displays, a real time interrupt was created to be
activated every 2.048 ms. In the RTI, the result of the reaction timer was displayed on the seven
segment displays. It is activated every 2 ms so it is visible to human eyes. After setting up the
RTI, three input capture pins were set up. The input capture pins used were pins 0-2 and were all
set up as inputs and were used to capture falling edges and was activated by a button. In the
interrupt for the pin 0, the time on TC0 was captured and saved to time_1. The interrupt on pin 0
was disabled as well as enabling the interrupt for pin 2. For pin 2s interrupt, the second time was
captured from TC2 and saved in time_2. After the time was captured, a global variable called
done was set to 1 allowing the main program to calculate the time between the events. The time
in milliseconds was calculated by subtraction the time_2 from time_1 and multiplying by 1000
times the prescaled clock. The result in milliseconds was then converted to bcd code by using the
hex2bcd() command and then displayed on the seven segment displays. To reset the system, a
button was pushed which activated the input capture on pin 1. In pin 1s interrupt, the times were
reset and the interrupt on pin 0 was re-enabled. The reaction timer was then wired up by
connecting the ports.
A 10 Hz square wave was generated on Port T4 by using an output compare function.
This was done by turning on the timer subsystem and setting the prescaler to 0x04 for .375 MHz.
After doing this, the period was calculated and found to be 37500. Pin 4 of Port T was then made
an output pin and set up to toggle on compare as well as the flag being cleared. The interrupt was
then enabled in the TIOS register. Inside the interrupt, a half period was added to TC4 to toggle
the output and the flag was cleared.
To implement a variable speed motor control system, pulse width modulation was used
with multiple duty cycles. To generate a 5000 Hz cycle, a period of 4800 cycles was required. To
obtain the period of 4800 cycles, clock mode 1 was chosen. Values of 96 for PWMPER4, 0 for
PCKA, and 25 for PWMSCLA were chosen to generate a period of 4800 cycles. Once the values
to generate the signal were determined, the PWM signal was set up to be in 8 bit mode, left
aligned, high polarity, and in clock mode 1. PWM was then enabled on channel 4 and an original
duty cycle of 50% was set. After setting up the PWM subsystem, the setup code for the seven
segment displays and DIP switches. An RTI was then included to be activated every 4.096 ms.
The RTI was used to display the result onto the seven segment displays. Bits 0 and 1 of Port A
were then made as outputs and bit 0 of Port A was set and bit 1 was cleared. After this setup
code, an input capture interrupt was setup on channel 0 of Port T. This input capture was set up
to capture rising edges. When the first rising edge was detected, the time on TC0 was saved in
time_1 and a count was set to 1 telling the TIC to save the next time in time_2. When the second
rising edge came, the time was saved in time_2 and the count was reset to 0. These times were
used to determine the speed that the motor was spinning by converting the number of cycles that
were counted between rising edges to RPM. In the main part of the program, the state of the DIP
switches were checked. The lower four DIP switches were used to determine the duty cycle. The
duty cycle was set up to increment by 6 every time the DIP switches were moved up one. DIP
switch 4 was used to determine what was to be displayed on the seven segment displays. If it was
a 1, the bcd representation of the cycles was displayed. If it was a 0, the bcd representation of the
RPM was displayed. The circuit shown in Figure 5 was then built to implement the motor
controller using PWM.
Figure 5: Using a PWM signal to adjust the speed of a motor

An A/D converter was implemented to convert the output from a potentiometer and from
a photodiode into a digital representation. This was done by first powering up the A/D converter
and setting up to be in 10 bit mode and to do 8 conversions. The A/D converter was then set up
to be right justified, unsigned, to convert continuously, and on only one channel. The channel to
be converted was channel 4 for the photodiode and channel 7 for the potentiometer. After setting
up the A/D converter, a TOI was set up to generate an interrupt every 174 ms. Inside of the TOI
interrupt, the values of the conversion from the A/D converter were stored in a variable called res
and the screen of the LCD was cleared. After setting up the TOI, an RTI was set up to be
generated every 4 ms. Inside of the RTI interrupt, the values stored in res were averaged and
converted to millivolts. After setting up the RTI, the lcd setup command, openlcd(), was used
and then a loop was created to run continuously. In the continuous loop, the average value of the
conversions and the value in millivolts was converted to ASCII to be displayed on the LCD
using the sprintf() command. The ASCII converted values were then displayed on the LCD. The
first line was the average value of the conversions and the second line was the value in millivolts.
To implement a PID controller, an A/D converter was first set up to do eight conversions,
be in eight bit mode, right justified, unsigned, do continuous conversions, and to only convert on
Port 7. After setting up the A/D converter, a PWM signal was set up to be in 8 bit mode, left
aligned, high polarity, and to be in clock mode 0 on channel 4. To generate a 50000 Hz square
wave PWM signal, PCKA was set to 1 and PWMPER was set to 255. PWM was then enabled on
channel four and the duty cycle was set to 50 percent to start. After setting up the PWM signal, a
TOF was set up to generate an interrupt every 174 ms. Inside of this interrupt, the A/D value
from the potentiometer was read and the lcd screen was cleared. An RTI was then set up to
generate an interrupt every 174 ms. Inside of the RTI interrupt, the PID controller was
implemented by taking the value from the potentiometer to calculate the desired speed. After
calculating the desired speed, both integral and proportional controls were implemented to adjust
the duty cycle so the actual speed would go to the desired speed. To prevent overflow of the duty
cycle, several if statements were included to keep the value within the range of 0 to 255. The raw
A/D value, number of cycles, desired speed, and actual speed were then displayed on the LCD in
the RTI. After the RTI was setup, a TOC was set up to capture rising edges on channel 0 of Port
T. This TOC was used to determine the time between the rising edges coming from the optical
encoder of the motor. This value was used to determine the actual speed of the motor. After all of
the setup code, the main program had a command to initialize the LCD screen and had some
setup code to make the LEDs outputs and to activate the seven segment displays. There was also
a continuous loop that waited for interrupts in the main program.

You might also like