Академический Документы
Профессиональный Документы
Культура Документы
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).
If you wish, consult Walkthrough 1 to refresh yourself on how to create a project with the wizard.
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
Click Project > AddFilesToProject or right click on SourceFiles in the Project Window and Select AddFiles . Select your new Main.c file and select Open
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.
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.
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
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
12
13
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
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
16
17
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