panstenos / ECM-lab-1

Embedded C for Microcontrollers/ Getting started and Digital IO

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Lab 1 - Getting started and Digital IO

Learning outcomes

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

Prerequisites

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)

Your first project - the microcontroller "Hello world!"

Cloning from Github using GitHub Destop:

  1. Open up Gitbub desktop applicaiton and sign in
  2. Select File -> Clone repository...
  3. On the URL tab copy your repository URL i.e. https://github.com/ME3-HECM/labname-gitusername
  4. Set the local path for where you store your git repository and add .X to the end of the folder name. Then hit Clone
  5. 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

Cloning GitHub repo using Github Desktop

Creating new MPLAB project:

  1. Select Microchip Embedded and Stand-alone Project
  2. 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
  3. Select XC8 Compiler
  4. 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
  5. ⚠️ Double check the Project Folder matches the Git folder you created previously and hit finish

Creating new project in MPLAB X

Adding source file and compiling/programming:

  1. Right click the source files icon in the projects tab and select add existing item - select main.c
  2. Click on the main.c file under the source files icon and open to view/edit the code
  3. Hit Build Main Project or Clean and build, the code should compile and you'll see a BUILD SUCCESSFUL message in the output tab
  4. Connect the PICKIT to the board and hit Make and Program Device (ensure board is connected to USB and powered on)
  5. You should see the LED light up when you press the button
  6. Well done 👍, that's the difficult bit over!

Make and program device

Working with git and Github Desktop:

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

  1. 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
  2. Write a useful message to go with your commit (i.e "MPLABX Project creation" or similar)
  3. 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
  4. To push the changes (commits) to the remote repo on GitHub.com using the Push Origin button
  5. 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

Commits and Push to GitHub in Github Desktop

Exercises:

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.

  1. Microcontroller "Hello world!" working ✅
  2. Make the second LED (pin RH3) come on when the other LED (RD7) is not
  3. Enable input on the second button (RF3) and require both buttons to be pressed to change the state of LEDs
  4. Change the code so that the each button controls its respective LED
  5. Complete the comments in main.c to explain the purpose of each section of code

About

Embedded C for Microcontrollers/ Getting started and Digital IO


Languages

Language:C 100.0%