26
Programming Digital Embedded Systems using the LPC1768 ARM development kit (an introduction)

Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

Embed Size (px)

Citation preview

Page 1: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

Programming Digital Embedded Systems using the LPC1768 ARM development kit (an introduction)

Page 2: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

Contents1. Programming the LPC1768 board (M3 ARM processor). DAY1......................................................3

1.1 Creating an account...............................................................................................................3

1.2 Using the on line compiler.....................................................................................................6

1.3 Setting up the LPC1768 platform...........................................................................................8

1.4 Creating a new program......................................................................................................10

1.5 Downloading programs to the LPC1768 board....................................................................11

1.6 Programming Step 1, displaying a message to the user.......................................................13

1.7 Writing a simple “Hello” program........................................................................................15

2 Session 2. Writing a simple program...........................................................................................17

2.1 Explanation of the code.......................................................................................................18

3 Session 3. A simple thermostat for a home.................................................................................19

Page 3: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

1. Programming the LPC1768 board (M3 ARM processor). DAY1

During the following three sessions you are going to become familiar with the LPC1768 development kit and its application within home automation.

The following three sessions are going to be considered.

1st session, creating an account and simple C “Hello” program

2nd session, develop a simple programme that reads user inputs.

3rd session, develop a simple thermostat for home.

1.1 Creating an accountOpen internet explorer and go to the address: “mbed.org”.

You should see the screen shown in figure 1.

On the top right side click on “login or signup”.

Figure 1"mbed.org" web site.

Page 4: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

The screen shown in figure 2 will appear, click on “signup” to create a new account.

Figure 2 Sign up screen.

In order to create a new account click on “No, I haven’t created an account before” shown in figure 3 below.

Figure 3 Creating and account.

Page 5: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

Fill in your preferred details in the field shown in the figure below (figure 4).

Figure 4 insert your details to create your account.

Make sure you tick the “I agree to the terms and conditions” and once your details are in the fields, click on the “signup” button on the bottom of the website.

Page 6: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

1.2 Using the on line compiler.The LPC1768 platform operate an M3 ARM microprocessor and like any microprocessor there is a need to issue a series of machine code instructions to perform operations. However now days the programmers very seldom issue machine code instructions as high language code instruction interpreters have been developed (compilers). The LPC1768 can be programmed in C or even C++ language. During this section you will set up the compiler environment.

Once you created your account login by following the login process (you may go once again to mbed.org and click on the login or signup button, this time however follow the login option shown in the figure below (figure 5).

Figure 5. Logging on the online compiler.

Page 7: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

Once you successfully log in, click on the “Compiler” button on the top right side of the website as shown in figure 6 below.

Figure 6. Running the on line compiler.

The compiler currently has no active development platforms, hence we have to set it up for the LPC1768 platform used during the laboratory sessions.

Page 8: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

1.3 Setting up the LPC1768 platformOnce you are in the compiler website, find the “no device” icon displayed on the top right corner of the website and click on it.

On the “Select a Platform” window (shown in the figure 7 below) click on the Add a device. Note that for the presented session a LPC1768 platform was already selected, hence ignore the one currently presented.

Figure 7. Adding the LPC1768 platform.

From the platforms presented (Shown in figure 8 below) select the “mbed LPC1768”.

Figure 8 Available development platforms for the online compiler.

Page 9: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

Once the LPC1768 platform has been selected a new webpage will pop up, click on the right side on the “Add to mbed Compiler” (as shown in figure 9 below).

Figure 9. Adding the selected platform to the Compiler.

There is a glitch with the website, there is a need to press “F5” to refresh the internet browser in order for the selected platform to appear on the compiler session.

Once you complete the compiler setup we are ready to develop programs.

Page 10: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

1.4 Creating a new program A new programme can be created by clicking on the New->New Program… as shown in the figure below (figure 10).

Figure 10. Creating a new programme.

Once the New Programme… command is selected the following window will pop up shown in the figure below (figure 11, project name).

Figure 11. Setting up project name.

Enter in the “Program Name” text field the following “Your_Name_Program”

On the left side “Program Workspace” of the compiler locate your newly created program and double click on the main.cpp. The following file will be displayed on the editor field of the compiler.

Figure 12. Program Workspace field.

Page 11: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

1.5 Downloading programs to the LPC1768 board.Every project will need to be compiled (translated to machine binary code) and eventually be downloaded and executed on the microprocessor. The following describe this process.

Connect the lpc 1768 board via USB to the PC and wait for the drivers to be installed (usually this is done automatically, a message pop up to let you know when the driver process is completed).

Memo: Make sure you plug in the usb connection on top of the board and not on the side one.

Locate the “Compile” button on the top of the compiler window and hit it once as shown in the figure 13 below. A default program is created when a new project is created, more on this latter on.

Figure 13. Compiling a program.

A progress window of your compilation will pop up. Provided you have not done any changes to the code, the compilation process should be successful, hence you will need to download your programme to the board, the following window (in figure 14) should pop up.

Figure 14. Compilation process progress.

Upon successful compilation the following window (figure 15) should pop up at the bottom of the compiler.

Figure 15. Compilation completed successfully.

Downloading successfully compiled programs to the board is a task of drag and drop.

Select the drop down arrow on the right side of the “Save” button and click on the “Save as…”

Page 12: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

Locate the mbed board (usually it comes up as E drive, but try to find the drive named “MBED”, click on the “Save button” (see figure 16 below).

Figure 16. Locating the LPC1768 "USB Flash Drive".

You might notice (if you are looking on the lpc1768 board) that the LED next to the USB cable is flashing, this is to indicate the programme is downloading, once this LED stops flashing, reset the board by pressing the push button next to the processor.

You will notice the one of the LEDs will start blinking.

Page 13: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

1.6 Programming Step 1, displaying a message to the user.In order to use the LCD screen on the application board the LCD library should be loaded. In order to load the LCD library click on the “Import” button on the compiler session (figure 17 below).

In the window that will appear locate the “Search” text box and enter the search words :C12832 (figure 17 below).

Figure 17Locating LCD library.

The following will be displayed after the search completes:

Figure 18. LCD library.

Select library by single clicking and click on the “import” button (in the figure 18 above and figure 19 below).

Page 14: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

The following will be displayed, that indicates a successful import of the library.

Once the library is imported it can use it as shown in the following section.

Page 15: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

1.7 Writing a simple “Hello” programGo back to main.cpp (double click on the main.cpp file on the Program Workspace” and add the following lines:

//Original Code:

#include "mbed.h"

DigitalOut myled(LED1);

int main() {

while(1) {

myled = 1;

wait(0.2);

myled = 0;

wait(0.2);

}

}

Modified code that display’s Hello to the user.

#include "mbed.h"

#include "C12832_lcd.h"

DigitalOut myled(LED1);

C12832_LCD myLCD;

int main() {

myLCD.printf("Hello");

while(1) {

myled = 1;

wait(0.2);

myled = 0;

wait(0.2);

}

}

Page 16: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

Follow the steps you did before to compile and download the program to the LPC 1768.

Because this is the second time we download the same program name to the LPC1768, a window will pop up asking you to confirm saving the file (sown below)

Click on “Yes” and the download will be performed. Remember to wait for the fast blinking LED stops blinking. Hit reset and see the hello word on the screen.

Modify the program so that it displays your name.

Page 17: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

2 Session 2. Writing a simple program.Programming Step2, Reading Temperature Set Point and displaying it on the screen.

In order to add the Reading Temperature Set Point we need to add an analogue input, hence modify the code in main as shown in the list below.

#include "mbed.h"

#include "C12832_lcd.h"

DigitalOut myled(LED1);

C12832_LCD myLCD;

AnalogIn TempSP(p19);

float fTempSP;

int main() {

myLCD.cls();

while(1)

{

myLCD.locate(0,0);

fTempSP=TempSP.read();

myLCD.printf("Desired Temp= %.1f ", fTempSP);

myled = 1;

wait(0.2);

myled = 0;

wait(0.2);

}

}

Modify the code to read a temperature set point from 20.0 degrees Celsius up to 40.0 degrees Celsius. Also translate the temperature set point to Fahrenheit degrees. Display them on the top line of the LCD screen.

Page 18: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

2.1 Explanation of the code#include "mbed.h" //This adds all the necessary libraries to operate the LPC1768

#include "C12832_lcd.h" //This is for the LCD screen.

DigitalOut myled(LED1); //This creates a digital out called myled and assigns it to LED1 //LPC1768, it blinks on off to show the program is running.

C12832_LCD myLCD; //This creates a software object for the LCD printf operations.

AnalogIn TempSP(p19); //This is an analogue input that reads the Potentiometer connected //to pin 19 of the LPC1768.

float fTempSP; //This is a variable floating point called fTempSP for the

//Temperature set point.

int main() { //Main function, every C program has to have one.

myLCD.cls(); //Clean the LCD screen.

while(1) //What is enclosed in {} runs for ever.

{

myLCD.locate(0,0); //Go to location 0,0 of the LCD for the following operations.

fTempSP=TempSP.read(); //Read the temperature set point from the user.

myLCD.printf("Desired Temp= %.1f ", fTempSP); //Print a message to the screen.

myled = 1; //Switch on myled

wait(0.2); //Wait for 0.2 seconds.

myled = 0; //Switch off myled

wait(0.2); //Wait for 0.2 seconds.

}

}

Make sure you read and understand what the program above does. For a better understanding the following topics need to be considered, make sure you read the following before the third session.

Writing C programs,

C syntax and statements (for, while, if, variable allocation, simple math operations).

Functions, variables (definition, types).

Page 19: Programming the LPC1768 board (M3 ARM processor). …midas.herts.ac.uk/helpsheets/mbed.docx  · Web viewProgramming Digital Embedded Systems using the LPC1768 ... creating an account

3 Session 3. A simple thermostat for a home.For this session you are required to develop code for reading the temperature from the on board temperature sensor on the LPC1768 board (check the notes on mbed.org and also on the back of the application board) and display a message to the user according to the following:

First Line: Temp Set Point = xx.x C, yy.y F

Second Line: Current Temperature: zz.z C, aa.a F

Third Line: A message depending on the Temperature set point and current temperature according to the following:

If “Temp Set Point”>”Current Temperature”, display: “Heater is ON” switch on LED2 as well.

If “Temp Set Point” is +- 2 C from “Current Temperature”, display: “Room is within desired temperature”.

If Temp Set Point”<”Current Temperature “, display: “Heater is OFF”

Make sure you commend your code.