EEEN20011 Microcontroller Engineering II


Hello, if you have any need, please feel free to consult us, this is my wechat: wx91due


EEEN20011 Microcontroller Engineering II

Dr L A Marsh
Lab Task 2 (70 Marks / 14% of module weighting)

In this lab task you are expected to use the speaker, joystick, LCD screen, both potentiometers and the RGB LED on the Application shield. These are shown in the diagram below:

This task is designed to integrate many of the peripherals and programming techniques that you have learned about so far on the course. It also requires to implement a state machine in C++, for which there is an example program available that you may wish to study.

In this task you are required to make use of the LED, and Potentiometer Classes provided in “Example Program 5: Creating a finite state machine and using TimeOut APIs”. You should not modify the Potentiometer class. The LED class should be used unchanged.

You should create a class called SamplingPotentiometer which inherits from the class Potentiometer. This class should make use of a Ticker object to regularly execute the member function sample() in the class Potentiometer. The constructor for this class should accept the pin name, the VDD for the analog input and the sampling frequency as parameters, as shown in Listing 1. You should not add any member functions or data members to SamplingPotentiometer other than those provided in Listing 1. The constructor should configure and attach the Ticker callback to the relevant member function of Potentiometer to trigger the repeated sampling process at the specified frequency.class SamplingPotentiometer : public Potentiometer {

private:
float samplingFrequency, samplingPeriod;
Ticker sampler;
public:
SamplingPotentiometer(PinName p, float v, float fs);
};
Listing 1: SamplingPotentiometer Class declaration for Task 4

You are required to implement a simple state machine using an enum as a means of defining a program state. There are examples of this type of operation in “Example Program 4: Example of a Finite State Machine to control program states”, and “Example Program 5: Creating a finite state machine and using TimeOut APIs”. For this task you should implement the state machine within the main() function, as shown in Example Program 4. The format should be similar to that shown below in Listing 2.

typedef enum {state1, state2, state3, … staten} ProgramState;
//Definition of the enum that refers to the states of the program
ProgramState state;
int main()
{
while(1) {
switch (state) {
case (state1) :
//Code for this state
break;
case(state2) :
//Code for this state
break;
default :
//Default if not matching other state
}
}
}
Listing 2: Architecture of a simple state machine using an enum

In this type of operation your program can do different tasks according to the current state of the program. You can, and should, use more memorable names for each of your states. Some suggestions that you might wish to use for your program are: initialisation, set_duration, start_timer, timer_running, timer_paused, time_elapsed, restart_timer

You are likely to need more states than this, but the above list should give you some idea as to how you can implement a state machine to fulfil the requirements of the task. Bear in mind that you will want to loop in some states, but may want others to only do something once, change state and move on in the next iteration of the loop.

You will need to implement timers and callbacks (ISRs) to produce an audible tone using the speaker, and interrupts to enable you use any required buttons on the joystick. Your program must have interrupt functionality as defined in the ‘Marking Requirements’. Your ISRs should all be simple functions that refrain from calls to computationally intensive operations, or blocking functions such as wait(). Your ISRs should be used to switch the program on to the next state, or to update simple variables. You should create a simple class for the Speaker which has similar functionality to the LED class.

Some variables or objects may need to be declared outside the main function as global variables. This should be avoided unless strictly necessary, and the use of all global variables should be carefully justified. Marks will be deducted for inefficient ISRs or unnecessary use of global variables.

You should use the LCD screen to display different data depending on which state the program is in.

For example, when setting the time you should be able to see the current value of minutes and seconds, when the timer is active you should display the initial time period and the time remaining, and if the timer is paused or if the time period has elapsed then this information should be displayed. You are required to implement a simple graphical GUI showing the countdown time state, as per the video. Please note that this does not have to be identical to the video, but you should show an ability to use shapes to appropriate effect in terms of simple menus and illustration of the remaining time relative to the initial time period. You are marked on the overall quality of the GUI.

You should also use the RGB LED as a means of indicating the state of the program. It should show the following:

Red:
Setting the time period
Flashing Green (0.5 Hz):
Timer running
Orange:
Timer paused
Blue:
Time elapsed
You should be able to use the potentiometers to set the timer period, with the left potentiometer used to specify minutes (0-9), and the right potentiometer to specify seconds (0-59). It should not be possible for the minutes to reach 10, nor the seconds to reach 60.
Where you are required to use the centre (fire) pin on the joystick you should use the InterruptIn API and attach appropriate callbacks. It is entirely likely that you will need to attach different callbacks depending on the required functionality of the button, which will change depending on the program state.

When the time is paused you should implement InterruptIn APIs for the centre, up and down pins, to implement the ability to resume the timer, reset the timer, and quit from the current countdown respectively.

Your final program should display the same functionality as that shown in the example video on Blackboard. Marks will be awarded for smooth operation of the program. So please ensure that updates to the LCD screen are free from flickering, LEDs illuminate correctly, and the speaker outputs the correct signals at the correct points.

Marking Requirements

The program required for this task demonstrates a significant amount of functionality, including user input and a variety of outputs including LEDs, the LCD screen and the Buzzer. As such the marking requirements dictate that in order for full marks the final program should show all of the functionality described in the video, as well as the essential criteria documented below:

The program should contain:

  • The Class SamplingPotentiometer which should inherit from the Potentiometer class. This class should automatically sample the analog inputs at periodic intervals and store sampled values as data members of the class. The latest samples should be accessed by member functions, as described above.
  • Objects of type InterruptIn for buttons of the joystick
  • An object of type C12832. This will need to be imported – please see the guide on Blackboard for using the LCD screen.
  • Objects of type LED to allow for the output of red, green and blue LEDs. The illumination of LEDs should match those described in this document. You should not use DigitalOut directly.
  • Appropriate use of both Ticker and TimeOut APIs for the purposes of:
    • The main countdown period
    • Counting the number of elapsed seconds
    • Generating the output tone from the speaker
  • A number of ISRs/Callbacks which are called when your interrupt events occur (e.g. button presses or timer events). These ISRs should be as efficient as possible and should be used to change program state, or update global variables.
  • You will need to declare some of the above objects outside of the main() function so that they are in scope for your ISRs. You will need to carefully think about which of these will be needed, and to appropriately justify all global variables that are used in your program.
  • Implementation of a state machine that is able to represent the various states that your program is in e.g. Timer Running, Timer Paused, Waiting for User Input etc.
  • An object of type Ticker for generating the speaker tone, where the toggle member function of the Speaker class should be called as a callback (ISR). You should not use wait functions to generate the tone.
Your main function, int main(), should contain code capable of:
  • Declaring all necessary objects and variables which are only needed in the main function
  • Implementing a state machine which can be used to control the flow of the program
  • Setting the program in a suitable initial state
  • Recovering from undefined states that might occur through the implementation of a ‘default’ case e.g. returning to the default state
Important notes (continued on the next page):

You will be expected to fully explain how your program works in order for marks to be awarded.

There will be marks available for your ability to answer questions relating to your program’s implementation.

Your program must compile to be assessed. If you cannot implement the entire functionality without compilation errors then consider if you can implement some of it in a stable version of the program so that you can gain some marks.

Important Notes (continued):

The implementation of this program is expected to differ significantly between students. As such all students must upload their source code via Turnitin for plagiarism checking. Lab marks will be provisional pending this plagiarism check, and any suspected cases of academic malpractice will be referred to the Department for investigation. There is a guide available on Blackboard which details this process; please ensure that you are familiar with it before the lab session, and check with staff in the assessment labs if there are any problems.

This is an in-lab assessment, and no late submission will be permitted. If you do not submit your work for assessment in your scheduled lab, AND correctly upload your code for plagiarism checking before the end of the session you will receive a mark of zero for this task.

You are suggested to undertake the following tasks in working towards your solution:


  1. Implement the class for a SamplingPotentiometer and verify that this works in its own program.
  2. Practice drawing simple shapes to the LCD screen to better understand what is possible in terms of a simple GUI.
  3. Experiment with a simple state machine e.g. that uses a button to transit between different states with a clear output. For example, illuminating a different LED for each state or displaying a different message on the screen.
  4. At this point you should write a brief plan of what your program needs to do. Usually for state machines a flowchart is appropriate, but any alternative method would be suitable.
  5. This plan will make it much easier to implement the program in stages and test more gradually. This is a complicated program, and you are strongly recommended to build it up in stages and debug each stage separately. You are likely to find this task very hard to accomplish if you develop large parts of it at once and don’t test in between, as it will be hard to track down where errors are occurring.


This task considerably more difficult than Task 1. The main advice is to take things in stages, and to remember that you can get marks for achieving some of the criteria, without having to achieve them all. If you can’t do the full program perfectly, then do as much of it as you can, as well as you can.

发表评论

电子邮件地址不会被公开。 必填项已用*标注