Вы находитесь на странице: 1из 18

ECE1011 Introduction to Programming and Programmable Devices

TUTORIAL 1 DIGITAL INPUT / OUTPUT


In this tutorial you will learn how to write code in C to interface with an I/O port of a microcontroller. The tutorial will also reinforce your experience of debugging code using the ICD2 debugger. You will also become familiar with some of the peripherals found on the development board. And last but by no means least you will be presented with the concept of a structured programming approach to code development. Note: Each section of the tutorial will build on the previous task. There is no need to create a new project for each section.

THE TOOLS
To carry out this Tutorial we will need the following tools MPLAB IDE ICD2 Debugger Microchip C18 Compiler (MCC18) PICDEM 2 PLUS development board installed with a pic18F452 Device. The PICDEM 2 PLUS development board has many components for the user to examine and assess. For this tutorial we will be making use of the four LEDs connected to the lower four bits of PORTB, and the button connected to pin 4 of PORTA (RA4).

CREATING THE PROJECT


Open MPLAB and start the Project Wizard. Project > ProjectWizard These are the settings you need to use: Step1 Device Step2 Active Toolsuite Step3 CreateNewProjectFile Step4 Add Existing Files To Project Step5 Summary PIC18F452 Microchip C18 Toolsuite M:\MPLAB\Tutorial1DigitalIO\DigitalIO Click Next Click Finish

If you wish, consult Walkthrough 1 to refresh yourself on how to create a project with the wizard.

PART 1 SIMPLE LED ON/OFF ROUTINE


In this section of the tutorial we will create code to allow us to turn on and off one of the LEDs on the PICDEM 2 Plus Development board. The LED you will be driving is LED0 which is connected to pin RB0 (PORTB pin 0) on the microcontroller.

CREATE LED.C AND LED.H


Our first code segment will be to drive the LED. The LEDs are connected to the lower four pins on PORTB. Firstly we need to configure the pins connected to the LEDs as outputs. We do this by writing a 0 to bit positions of the TRIS register related to the pins we wish to configure as an output. The code to do this is: TRISB = 0xF0; //In binary 1111 0000b

This will write 1s to the upper four bits of the port and a 0 to the lower four bits, making the high four bits of the port inputs, and the lower four bits of the port as outputs. We will create a function to contain this instruction called InitialiseLED() in our new source file LED.c. You will now create a file LED.c in your project folder called LED.c and add the code shown. Click File > New or click on the New File icon. Enter the code shown below Click File > SaveAs and use the file name LED.c

You will now add the new file LED.c to the project. Click Project > AddFilesToProject or right click on SourceFiles in the Project Window and Select AddFiles. Select your new LED.c file and select Open

You will now create a header file for your new source file. By including this in other files the function InitialiseLED() will be able to be called. We will see this later. 2

Click File > New or click on the New File icon. Enter the code shown below Click File > SaveAs and use the file name LED.h

You will now add functionality to your new LED.c file which when called will turn the LED on or off. This is done by writing a 1 to the PORTB register to turn an LED on and a 0 to turn the LED off. The two instructions are shown below. Refer to the tutorial on Number Systems and Bitwise Operators if you do not understand these statements. PORTB |= 0x01; PORTB &= (~0x01); //Turn LED0 on //Turn LED0 off

These two instructions will now be included in two functions LED_Off, and LED_On in the LED.c file. Add the following code to your LED.c file and Save.

New Code

Now Add the following code to your LED.h file and Save.

New Code

We now have three functions in our LED.c file to interface to our LED. 1. 2. 3. InitialiseLED() - which when called will configure the pins connected to the LEDs as outputs. LED_Off() - which when called will turn off the LED LED_On() - which when called will turn on the LED

CREATING YOUR MAIN TEST CODE MAIN.C


We now need to test our LED functions. You will now create a file Main.c in which we will call our LED functions, and then add this file to your project. To allow access to the LED functions you need to include the LED.h file as shown in the code below. Click File > New or click on the New File icon. Enter the code shown below Click File > SaveAs and use the file name Main.c

Click Project > AddFilesToProject or right click on SourceFiles in the Project Window and Select AddFiles . Select your new Main.c file and select Open

BUILD THE APPLICATION


You will now test your code. But first you need to build the application and then correct any errors that may exist. Click Project > BuildAll or Click the BuildAll icon in the toolbar. If there are errors then you will need to inspect the compiler output and correct your code. The most common errors are syntax errors. Check for typo errors, missing semicolons etc.

PREPARING FOR THE TEST


You will now add a breakpoint so that execution will be paused when the program is run so that you can step through the rest of the code. Double Click on the Line InitialiseLED(); in Main.c. A red breakpoint icon should appear to the left of that line.

You will now add a watch window to inspect PORTBs registers. Click view > Watch. An empty watch window should now appear. Under the SFR drop down list select TRISB and ADD SFR Under the SFR drop down list select PORTB and ADD SFR Your watch window should now be the same as below.

ARRANGE THE WINDOWS READY FOR TESTING


Arrange the window in MPLAB as shown to allow easy viewing of files and registers as you conduct your code test.

Completed MPLAB Project for Simple LED ON/OFF Routine

RUNNING THE CODE IN THE SIMULATOR


We will first test the application using the MPLAB simulator. Click Debugger > SelectTool > MPLAB SIM The simulator toolbar should then appear.

You will now run your code. Click Debugger > Run or Click the Run icon in the Simulator toolbar The code should now be paused at the breakpoint you created earlier. You will now use the Debugger > StepInto function to watch the program flow and observe the changes the code makes on the PORTB registers. Click Debugger > StepInto or Click the StepInto icon in the Simulator toolbar and observe how each line of code modifies the two registers. Continue SteppingInto until the end of the code is reached.

TESTING ON THE HARDWARE


You will now debug the code on the development hardware, the PICDEM 2 Plus board. To do this you will need to change the Debugger tool from the MPLAB SIM simulator tool to the ICD2 debugger. See the earlier Walkthrough Embedded Development and Debugging - ICD, for details on this if you have forgotten. Click Debugger > SelectTool > ICD2 Click Debugger > Connect Click Debugger > Run or Click the Run icon in the ICD2 toolbar The code should again now be paused at the breakpoint you created earlier. Click Debugger > StepInto or Click the StepInto icon in the ICD2 toolbar and observe how each line of code modifies the two registers as well as the status of the LED. Continue SteppingInto until the end of the code is reached.

PART2 TOGGLE THE LED FUNCTION


This part of the Tutorial adds another function to your LED interface which will make the LED toggle each time the function is called.

ADDING A NEW FUNCTION TO YOUR LED.C AND LED.H FILE


The toggle command can be executed by using the following function. This toggles the state of pin 0 on the microcontroller each time the instruction is executed. PORTB ^= 0x01; This is the bitwise exclusive OR instruction we looked at during an earlier lecture. Revise the notes if you need a refresh. You will now add this instruction to a new function, LEDToggle(), in the LED.c file. Add the following code to your LED.c file and Save.

New Code

You will now modify the LED.h file to expose this function to other files. Add the following code to your LED.h file and Save.

//New Code

ADD A TOGGLE CALLING FUNCTION TO MAIN.C


We now need to add code in our main.c which will call our new LEDToggle() function in LED.c. The LEDToggle() function is within a while(1) loop, sometimes called a forever loop. This means this function will continuously be called. Add the following code to your Main.c file and Save.

BUILD THE APPLICATION


You will now rebuild your code. Click Project > BuildAll or Click the BuildAll icon in the toolbar. Again if there are errors then you will need to inspect the compiler output and correct your code.

ARRANGE THE WINDOWS READY FOR TESTING


Arrange the window in MPLAB as shown to allow easy viewing of files and registers as you conduct your code test.

RUNNING THE CODE IN THE SIMULATOR


Again we will first test the application using the MPLAB simulator. Click Debugger > SelectTool > MPLAB SIM You will now run your code. Click Debugger > Run or Click the Run icon in the Simulator toolbar The code should now be paused at the breakpoint you created earlier. You will now use the Debugger > StepInto function to watch the program flow and observe the changes the code makes on the PORTB registers. Click Debugger > StepInto or Click the StepInto icon in the Simulator toolbar and observe how each line of code modifies the two registers. 10

TESTING ON THE HARDWARE


You will now debug the code on the development hardware, the PICDEM 2 Plus board. To do this you will again need to change the Debugger tool from the MPLAB SIM simulator tool to the ICD2 debugger. Click Debugger > SelectTool > ICD2 Click Debugger > Connect Click Debugger > Run or Click the Run icon in the ICD2 toolbar The code should again now be paused at the breakpoint you created earlier. Click Debugger > Run or Click the Run icon in the ICD2 toolbar You will notice that due to the speed at which the application is executing it is not clear that the LED on the development board is actually toggling. We therefore need to add a delay to reduce the rate at which the LED is toggling so that we can see this happening. This is the purpose of the next section but before we start this we need to halt the execution of our current code. Click Debugger > Halt or Click the Halt icon in theICD2 toolbar

PART 3 - LED TOGGLE WITH DELAY


In this section we will add a Delay() function to our code calling the LEDToggle() routine.

CREATE DELAY.C AND D ELAY.H


The delay routine works by entering a for loop with a specified number of passes. Each pass will require a certain number of processor clock cycles to execute and hence will cause a certain time delay between entering and exiting the function depending on the number of passes entered in the loop. The delay time will also depend on the clock speed applied to the processor. Click File > New or click on the New File icon. Enter the code shown below Click File > SaveAs and use the file name Delay.c

11

You will now add the new file Delay.c to the project. Click Project > AddFilesToProject or right click on SourceFiles in the Project Window a nd Select AddFiles. Select your new Delay.c file and select Open

You will now create a header file for your new source file. Click File > New or click on the New File icon. Enter the code shown below Click File > SaveAs and use the file name Delay.h

ADD A DELAY CALLING FUNCTION TO MAIN.C


We now need to add code in our main.c which will call our new Delay() function defined in Delay.c. Modify the code in your Main.c file as shown and Save.(Dont forget the new #include statement)

12

BUILD THE APPLICATION


You will now need to rebuild your code. Click Project > BuildAll or Click the BuildAll icon in the toolbar. Again if there are errors then you will need to inspect the compiler output and correct your code.

ARRANGE THE WINDOWS READY FOR TESTING


Arrange the window in MPLAB as shown to allow easy viewing of files and registers as you conduct your code test.

13

TESTING ON THE HARDWARE


You will now debug the code on the development hardware, the PICDEM 2 Plus board. Make sure you still have the ICD2 selected as your debugging tool. If not then: Click Debugger > SelectTool > ICD2 Click Debugger > Connect Click Debugger > Run or Click the Run icon in the ICD2 toolbar The code should again now be paused at the breakpoint you created earlier. Repeatedly Click Debugger > StepOver or Click the SetOver icon in the ICD2 toolbar You should see the PORTB register toggle on each pass of the LEDToggle() instruction, and you should also see the LED Toggle on the development board. Now we will run the code in real-time. Click Debugger > Run or Click the Run icon in the ICD2 toolbar You should see the LED Toggle at a rate of about 0.5s.

For you to try Try different values in the delay routine then rebuild and run your code. Observe the change in flashing rate on the LED.

14

PART4 STOP START BUTTON


In this section we will add a start/stop function to our application. This will be implemented using one of the push buttons on the development board. The button we will use is S2 which is wired to pin 4 on PORTA of the microcontroller. Pressing and holding the button will cause the LED to stop flashing. We will create code which will read the status of button S2 in a new source file Button.c.

CREATE BUTTON.C AND BUTTON.H


You will create a new source file called Button.c. This will hold two functions, one to initialize the port to allow the status of pin 4 on PORTA to be read (make pin 4 an input), and a second to read the status of pin4 on PORTA. Click File > New or click on the New File icon. Enter the code shown below Click File > SaveAs and use the file name Button.c

You will now add the new file Button.c to the project. Click Project > AddFilesToProject or right click on SourceFiles in the Project Window and Select AddFiles. Select your new Button.c file and select Open

15

You will now create a header file for your new source file. Click File > New or click on the New File icon. Enter the code shown below Click File > SaveAs and use the file name Button.h

ADD THE BUTTON CALLING ROUTINES TO MAIN.C


We now need to add code in our main.c which will call our new Button functions defined in Button.c. Modify the code in your Main.c file as shown and Save.

16

BUILD THE APPLICATION


You will now need to rebuild your code. Click Project > BuildAll or Click the BuildAll icon in the toolbar. Again if there are errors then you will need to inspect the compiler output and correct your code.

ARRANGE THE WINDOWS READY FOR TESTING


Arrange the window in MPLAB as shown to allow easy viewing of files and registers as you conduct your code test.

17

RUNNING THE BUTTON TEST ON THE HARDWARE


You will now debug the code on the development hardware, the PICDEM 2 Plus board. Make sure you still have the ICD2 selected as your debugging tool. If not then: Click Debugger > SelectTool > ICD2 Click Debugger > Connect Click Debugger > Run or Click the Run icon in the ICD2 toolbar The code should again now be paused at the breakpoint you created earlier. Now we will run the code in real-time. Click Debugger > Run or Click the Run icon in the ICD2 toolbar Again you should see the LED Toggle at a rate of about 0.5s. Now we will see what happen if we press the S2 button. Press the S2 button on the development board. When you press and hold you should see the toggling of the LED stop. If you release the button then the LED should begin to flash again. Release the S2 button on the development board.

For you to try 1. 2. Change the code in Main.c so that the LED toggling action starts when the button is pressed and stops when it is released. Change the code in LED.c so that instead of the functions operating on LED0 they operate on a) LED 1 b) LED2 c) LED3 Add a new function in LED.c called LEDCount() which when called increments a count on the LEDs from 0 to 15 and then repeats. Add a new function in LED.c called LEDWrite() which when called sets a binary value of 0 to 15 on the LEDs.

3. 4.

18

Вам также может понравиться