Digital Input

This project demonstrates how to read a digital input pin on a microcntroller and control an Light Emitting Diode (LED) based on the state of a momentary switch connected to the digital input.

proj2.png

Step by Step Instructions

Using the MPLAB® Code Configurator (MCC) simplifies the setup for the various registers within the device to create a digital output. To demonstrate how to get started, this project monitors a momentary switch connected to pin RA4 that is enabled as a digital input using an internal pull-up. The code will lights an LED connected to RA2, enabled as a digital output, when the switch is pressed.

proj2pic.png

The project uses:

PICDEM™ Lab II Connections:

  • Connect 5 V and Gnd to the power bus of the protoboard.
  • Connect 5 V to the Vdd pin of the PIC16F1769
  • Connect Gnd to the Vss pin of the PIC16F1769
  • Connect a wire from the RA2 pin (pin 17) to the breadboard Row 23
  • Connect a wire from the ground bus to breadboard Row 27
  • Connect the LED Anode to breadboard Row 23
  • Connect the LED Cathode to breadboard Row 24
  • Connect a 330 ohm resistor between Row 24 and Row 27
  • Connect a wire from the RA4 pin (pin 3) to the breadboard Row 12
  • Connect the momentary switch between Row 12 and Row14
  • Connect a wire between Row 14 and the Breadboard ground bus

To follow along with these steps, the Integrated Development Environment (IDE) should be open and the Programmer/Debugger connected to both the computer and the development board. The setup was described in the Setup and Installation section of this training module. You should see a screen similar to the one below to move on to step 1:

mplabx.png

1

Create a new "Stand-Alone" project in the IDE for a PIC16F1769. Instructions are below if this is your first project.

2

Open the MPLAB Code Configurator under the Tools -> Embedded menu of MPLAB X IDE.

mcclaunch.png

3

Select the peripherals for your project.
In this first project these peripherals need to be selected:

  • System Module
  • Interrupt Module
  • Pin Module

The System Module, Interrupt Module and the Pin Module will all be automatically included when you launch the MCC. The result should look like the picture below:

proj1per.png

4

Open the Pin Manager and then click on the PORTA - 4 blue lock symbol in the Input row to change it to locked, it will turn green in color. Also click on PORTA - 2 blue lock symbol in the Output row to change it to green and locked. Also make sure the Reset is blue and unlocked. Click on it if set to green locked. This adds the RA4 and RA2 I/O pins to the project. It should look like the picture below when completed:

proj2io2.png

5

Close the Pin Manager and then click on the Pin Module in the project resources area. The center section should show RA2 and RA4 listed on the I/O chart.

gpio.png
  1. RA4 will be the input that will read the switch so no boxes need to be checked.
  2. RA2 will be an output and control the LED. Click on the output box to make the pin an output.

You can change the names of the pins to LED for RA2 and Switch for RA4.

proj2ioset.png

6

The System needs to be setup next. Click on the System name in the Project Resources list. It should be highlighted in blue when selected.

system.png

The System section will appear. In this section the oscillator settings and the configuration settings are selected.

Oscillator

  1. Select the INTOSC from the drop down menu for the System Clock Select.
  2. Select the 1 MHz_HF selection from the Internal Clock drop down menu.

This will enable the internal 1MHz internal oscillator as the system clock.

oscillator.png

Configuration

Each configuration setting can be changed under the Register tab of the System window. Match the selections shown here.

config1.png
config2.png

7

Click on the Generate Code button to have the MCC create the software libraries for this project.

generate.png

8

The project will now have both generated Header Files and Source Files. It should also have a generated a main.c file.
Note: MCC may offer to generate a main.c file. Click YES to allow it to complete that task.

proj2files.png

Double click on the main.c file to open it up in the editor window.

mainfile.png

9

Add the following code to the end of the main.c file:

while (1)
    {
        // Add your application code

        if (Switch_GetValue() == 0)
            {
            LED_SetHigh();
            }
        else
            {
            LED_SetLow();
            }

    }
maincode2.png

Note: The Switch_GetValue(), LED_SetHigh() and, LED_SetLow() functions are macros that were generated by the MCC. Their definitions are located in the pin_manager.h file.

#define Switch_GetValue()         RA4
#define LED_SetHigh()    do { LATA2 = 1; } while(0)
#define LED_SetLow()   do { LATA2 = 0; } while(0)

This is a simple example of how the MCC generates a library of useful functions to make creating project code much easier and quicker.

10

Click on the Build Project Icon (the Hammer) to compile the code. You should see a BUILD SUCCESSFUL message in the output window.

Main_Build_Project.png
BUILD SUCCESSFUL

11

Make sure your project has the programming tool selected (Part of Step 1 above) and connect power to your development board.

The PICkit 3 has limited power capability so we recommend you power the board separately.
The ICD 3 can power a development board, but for simplicity we recommend you power the board separately.
The RealICE cannot power the development board so powering the board separately is required.

Click on the Make and Program Device icon. This will build the project again and launch the programmer. In the output window you should see a series of messages and, if successful, it will end with a Programming and Verify Successful message. When the switch is pressed the LED should light.

Main_Program_Target_Project.png

Output Window:

Connecting to MPLAB PICkit 3...
Firmware Suite Version.....01.34.11
Firmware type..............Enhanced Midrange

Target detected
Device ID Revision = 6

The following memory area(s) will be programmed:
program memory: start address = 0x0, end address = 0x7ff
configuration memory
Device Erased...

Programming...
Programming/Verify complete
finalpic.png

When the project is loaded you will be able to press the switch and see the LED light up.

If it's the first time the programmer is connected to the board, the programming tool may need to download the proper operating firmware for the exact device. You may see a series of processes if this occurs. This should only happen once:

Downloading Firmware…
Downloading bootloader
Bootloader download complete
Programming download…
Downloading RS…
RS download complete
Programming download…
Downloading AP…
AP download complete
Programming download…
Firmware Suite Version…..01.34.11
Firmware type…………..Enhanced Midrange

12

The project can be closed in the MPLAB X IDE. The project is saved automatically when it is built, but any changes to files or configuration may ask to be saved before the project is closed.
The project can be closed under the File Menu > Close Project

closeproject.png

Download

If you have any problems with your project, the completed MPLAB X project file can be downloaded from the link below:

File Download
Installation
Instructions
Windows Linux Mac OSX
Project 2 Files
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.