By the end of this lab you should be able to:
- Compile a C program in MPLAB X and download and run on the PIC chip
- Know the basics of version control with Git (cloning a repository, committing changes, pushing commits to a GitHub remote repository)
- Understand how to control the output of individual pins to control LEDs
- Understand how to poll the state of an input pin connected to a button
If you have not already done so, download and install the following:
MPLAB X IDE - this is the main development environment (scroll down to the Downloads tab and select the download for your system)
MPLAB XC8 Compiler - needed to compile code for the microcontroller (scroll halfway down the page and there is a set of tabs, select Compiler Downloads tab and choose the XC8 compiler for your system)
Github desktop - graphical interface for common Git tasks - much better than MPLAB Git integration!
Git - software tools for Git version control (optional but recommended)
- Open up Gitbub desktop applicaiton and sign in
- Select File -> Clone repository...
- On the URL tab copy your repository URL i.e. https://github.com/ME3-HECM/labname-gitusername
- Set the local path for where you store your git repository and add .X to the end of the folder name. Then hit Clone
- Check that there is now a folder on your hard drive that contains a local copy of the git repo and that the folder name ends with .X
- Select Microchip Embedded and Stand-alone Project
- Select Family Advanced 8bit MCUs (PIC18) and Device PIC18F67K40, if you have connected the PICKIT select it here, if not just leave it as No Tool
- Select XC8 Compiler
- The Project Folder box needs to match the folder name of the Git clone. Select directory and enter project name. MPLAB automatically adds .X to the project name
⚠️ Double check the Project Folder matches the Git folder you created previously and hit finish
- Right click the source files icon in the projects tab and select add existing item - select main.c
- Click on the main.c file under the source files icon and open to view/edit the code
- Hit Build Main Project or Clean and build, the code should compile and you'll see a BUILD SUCCESSFUL message in the output tab
- Connect the PICKIT to the board and hit Make and Program Device (ensure board is connected to USB and powered on)
- You should see the LED light up when you press the button
- Well done 👍, that's the difficult bit over!
Now is a good time to commit your first changes to your local git repository and then push them to the remote repository on GitHub.com
- In Github Desktop you should see some files automatically created by MPLAB when the project was created. Git automatically identifies any files that have changed in the directory managed by git
- Write a useful message to go with your commit (i.e "MPLABX Project creation" or similar)
- Hit Commit - you're changes are now committed to your local git repository - each commit forms a historical record of changes made to the code
- To push the changes (commits) to the remote repo on GitHub.com using the Push Origin button
- Check on GitHub to see your commits online - that's the basic git workflow for source code management. We'll expand on this in more detail later in the module
After you have got the basic "Hello world!" example working, complete the below exercises. Remember to Commit changes using Git after each exercise and Push the changes to GitHub when complete.
- Microcontroller "Hello world!" working ✅
- Make the second LED (pin RH3) come on when the other LED (RD7) is not
- Enable input on the second button (RF3) and require both buttons to be pressed to change the state of LEDs
- Change the code so that the each button controls its respective LED
- Complete the comments in main.c to explain the purpose of each section of code