ET4047 Autumn 2016 Project 2

Verified

Added on  2019/09/18

|4
|1457
|1080
Project
AI Summary
This document details a project for the ET4047 course, focusing on controlling a DC motor using an ATMega328P microcontroller. Students are tasked with writing a program that reads commands via the serial port to adjust the motor's speed and direction using PWM. The program also incorporates ADC readings for motor current, input capture for measuring the period of a periodic signal from a 555 timer, and various serial commands for reporting different data values (timer period, ADC voltage, motor direction, etc.). The project requires initializing various peripherals (Timer/Counter 1 & 2, ADC, USART) and handling interrupts. An optional extra challenge involves integrating an ultrasonic sensor for distance-based motor control.
Document Page
ET4047 Autumn 2016 Project 2 (Lab Weeks 8-12)
(Version 1.0, first released version)
This project is worth 15% of the total module assessment.
Description:
Write a single ATMega328P program that changes the direction and speed of a DC motor
using PWM, based on date read over the serial port. The program will:
Read a character using the serial port. Depending on the character that has been read, the
program will change the motor speed or direction, or report requested data (suitably
formatted) over the serial port.
Read the ADC0 input
Detect the period of a periodic input signal applied to PortB bit 0 (the Input Capture pin)
Report the time taken by the high and low parts of the input signal applied to PortB bit 0.
Operation of the program:
The program controls the speed of a DC motor using the Arduino Motor Shield, using
PWM and the Timer/Counter 2 output OC2B, where a zero PWM value stops rotation and
a PWM value of 100% corresponds to the maximum motor speed. The Direction is
controlled by the state of PORTB Bit 4, where 1 means Forward and 0 means Reverse.
The program can also start or stop the motor using a Brake signal on PORTB Bit 1, where
1 means Halt or stop motor rotation and 0 means Go or start the motor rotation.
The OC2B signal from the ATMega328P is shared with PORTD Bit 3. The PWM output on
OC2B is controlled by the data value written to the OCR2B register.
The ADC0 input is driven by the motor control IC on the motor shield and is a function of
the motor current. The ADC should be initialised so that it runs in Free Running Mode.
The Timer/Counter2 output OC2B is used as the PWM output, but Timer/Counter2
interrupts are not enabled.
The Timer/Counter1 input, ICP1, is connected to a 555 Timer, which produces a periodic
signal in a frequency range from approximately 2 kHz to 13 kHz, where the frequency is
controlled by a potentiometer on a shield.
The program responds to the following single character commands received over the
serial port:
'F' or 'f': Set Direction of motor spin to Forward.
'R' or 'r': Set Direction of motor spin to Reverse.
‘H’ or ‘h’: Halt motor irrespective of the motor speed previously selected – ie Turn on Brake
‘G’ or ‘g’: Start the motor (depends on the motor speed previously selected) – ie Turn off
Brake
‘0’ to ‘9’: Set the motor speed using the OC2RB register, to value from 0 to 90% of full
speed. You must convert the char value to an integer, and set the PWM based on this
ET4047 Autumn 2016 Project 2. Page 1
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
value.
‘T’ or ‘t’: Report the period of the 555 Timer in microseconds.
‘L’ or ‘t’: Report the time taken by the low pulse of the 555 Timer signal in microseconds
‘J’ or ‘j’: Report the time taken by the high pulse of the 555 Timer signal in microseconds
'C' or 'c': Continuously report the Timer input period in microseconds.
‘E’ or ‘e’: Stop continuous reporting of Timer input.
‘A’ or ‘a’: Report the ADC0 conversion result. This is the ADC value.
‘V’ or ‘v’: Report the ADC0 conversion result in mV. You must convert the ADC value to mV
'M' or 'm': Report motor direction to user.
‘S’ or ‘s’: Report the current value of the OCR2B register to the user.
All other characters are ignored.
Note: The data reported to the user is in ASCII format, and should be formatted with some
text to show what is being displayed. (e.g. “Timer period = 103us”, or “Voltage = 1234mV”).
Code Structure:
Initialisation Section:
Initialise Bit 3 of PORTD to output (needed as PWM output)
Initialise Bits 4 and 1 of PORTB to outputs (motor direction and brake bits)
Initialise Timer/Counter 1 for input capture and rollover.
Initialise Timer/Counter 2 for PWM – Note Timer 2 interrupts are not used.
Initialise USART Serial Port
Initialise ADC
// Use a separate initialisation function for each initialisation task.
Enable global interrupts
while(1)
{
Poll Serial port or use scanf to check for new character and Parse input
Poll New Input Capture data flag to see if new ICP data has been captured.
If yes
{
If the continuous timer value display is selected, report new timer value tothe
user on the USART
Clear the New Input Capture data flag
}
}
//================================================================
ET4047 Autumn 2016 Project 2. Page 2
Document Page
ADC ISR
Read new ADC result into a variable
Set new ADC_value flag.
//================================================================
Timer1 Overflow ISR
Increment the timer1 overflow counter
//================================================================
Timer1 Input Capture ISR
Calculate the period of the input signal in microseconds
Set up any variables for the next interrupt.
Clear the timer 1 overflow counter
Set the New Input Capture data flag
//=================================================================
USART Transmit Complete ISR (if used)
Send new data byte from transmit print queue (see example program)
//=================================================================
Initialisations in more detail
Timer/Counter1 setup:
Timer/Counter1 Clock source: CLKIO/8
All Timer1 outputs disabled
Timer/Counter1 Input Capture set for falling edge with noise control turned OFF
Timer/Counter1 Input Capture and Timer1 Overflow Interrupts enabled.
Timer/Counter2 setup:
All Timer/Counter2 interrupts disabled
Timer/Counter2 Clock Source: CLKIO/1024
Timer/Counter2 OC2B output enabled (Clarification: this means set DDRD Bit 3 as output)
Phase Correct PWM Mode, TOP = 0xFF
Clear OC2B on Compare Match when Upcounting
ADC setup:
ADC Channel 0 used
AVCC selected as the ADC Reference Voltage
ADLAR ADC in 10-bit mode
AutoTrigger enabled for Free Running re-trigger mode
ADC Prescaler: 128
ADC Interrupt Enabled
You need to set the ADSC (Start Conversion) bit in the ADC on initialisation to begin the
first ADC conversion.
ET4047 Autumn 2016 Project 2. Page 3
Document Page
USART setup:
8-bit data
No parity
9600 baud (but try 115200 later)
RX enabled RX interrupt disabled
TX Enabled. TXC interrupt enabled if NOT using printf/scanf for serial I/O
Also initialise any variables you are using
Note on the Serial Port operation:
You can use the serial port in one of two ways:
1. Use scanf and printf to read new data and send reported data to the user. In this
case you will not use the serial TX interrupt. But you will have to work out how to
make the scanf and printf functions work with the serial port.
2. Use the code in the sample code in the lectures and also provided on Sulis to
receive and transmit data bytes. In this case you will have to convert integer data to
ASCII and do your own formatting. In this case, use the serial TX complete
interrupt.
Note on arithmetic: use integer arithmetic throughout. Do not use floating point arithmetic
or formatting unless absolutely necessary.
Marking Scheme:
Initialisations, comments and overall program structure: 3%
ADC reading: 2%
Motor Control using PWM and direction input: 3%
Serial Mode – reporting data in command driven and continuous modes: 3%
Timer clock period reporting: 4%
Timetable:
Demonstrate your program running on Arduino + shields in the appropriate lab session.
Submit the final versions of your programs using Sulis, before 5pm, Fri 30 Nov 2016.
Extra Challenge: For an extra 5% marks in the module assessment, optionally allow the
motor speed to be controlled by the distance from the Arduino boards to an object, as
measured using an HC-SR04 ultrasonic sensor, and also add new commands to allow the
distance to be reported to the user and a flag to allow motor control using the ultrasonic
sensor to be enabled/disabled. More details on this in week 10.
ET4047 Autumn 2016 Project 2. Page 4
chevron_up_icon
1 out of 4
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]