Microchip | The Embedded Control Solutions Company Register Login
Browse All
Microchip Technology
 369 Code Snippets

How To do the Basics with a PIC18F25K42

Microchip Technology

Check out this project's MPLAB Xpress Webpage to view the complete code example along with a detailed step by step write-up and description, or visit the PIC18F25K42 Device Product Page for more information.

About this Code Example

Introduction

The following labs are designed for Microchip’s Xpress and Curiosity HPC boards. While the Xpress board is specific to the PIC18F25K42 device onboard, the Curiosity HPC Development Board supports most of Microchip's 28 and 40-pin 8-bit PIC® MCUs.This project contains 10 lab exercises that demonstrate the myriad basic capabilities of PIC® devices and can also be used to test the condition of your board. You can progress through each of the labs by simply pressing the S1 button on the Curiosity HPC Board (or as shown in the schematic below).

All the labs are written in C language compatible with the latest XC8 compilers. MPLAB Code Configurator wasn't used to generate any of the code.

Note about using MPLAB Code Configurator

MPLAB Code Configurator (MCC) wasn't used to generate any of the code. To learn how to use MCC with Xpress or Curiosity HPC board, please refer to the development board's user guide.

Schematic for Xpress Board

The Curiosity HPC board comes with all the LEDs and switches on board. For the Xpress boards, you'll require LEDs D2, D3, D4 and D5, potentiometer R1 and switch S1. Refer to the circuit diagram attached to connect them to the Xpress board.

Related Parts

List of Labs

  • Lab 1 : Hello World
  • Lab 2 : Blink
  • Lab 3 : Rotate
  • Lab 4 : Analog to Digital Converter
  • Lab 5 : Variable Speed Rotate
  • Lab 6 : Pulse Width Modulation
  • Lab 7 : Timer1
  • Lab 8 : Interrupt
  • Lab 9 : Sleep/Wake Up using Watchdog Timer
  • Lab 10 : High Endurance Flash

Lab 1: Hello World

Introduction

The first lab shows how to turn on an LED.

Hardware Effects

D2 will light up and stay lit.

Summary

The LEDs are connected to the input-output (I/O) pins. First, the I/O pin must be configured to be an output. In this case, when one of these pins is driven high (D2 = 1), the LED will turn on. These two logic levels are derived from the power pins of the PIC® MCU. Since the PIC®'s power pin (VDD) is connected to 5V and the source (VSS) to ground (0V), a logic level of ‘1’ is equivalent to 5V, and a logic level of ‘0’ is 0V.

Lab 2: Blink

This lab blinks the same LED used in the previous lab (D2).

Hardware Effects

D2 blinks at a rate of approximately 1.5 seconds.

Summary

One way to create a delay is to spend time decrementing a value. In assembly, the timing can be accurately programmed since the user will have direct control on how the code is executed. In C, the compiler takes the code and compiles it into assembly language before creating the file to program to the actual PIC® MCU (HEX file). Because of this, it is hard to predict exactly how many instructions it takes for a line of code to be executed. For a more accurate timing in C, this lab uses the MCU’s TIMER1 module to produce the desired delay. TIMER1 is discussed in detail in Lab 7: TIMER1.

Lab 3: Rotate

Introduction

This lab would build on Labs 1 and 2, which showed how to light up a LED and then make it blink with using loops. This lab incorporates four onboard LEDs (D2, D3, D4 and D5) and the program will light each LED up in turn.

Hardware Effects

Program will each light up D2, D3, D4 and D5 in turn every 500 milliseconds. Once D5 is lit, D2 lights up and the pattern repeats.

Summary

In C, we use Binary Left Shift and Right Shift Operators (<< and >> respectively) to move bits around in the registers. The shift operations are 9-bit operations involving the 8-bit register being manipulated and the Carry bit in the STATUS register as the ninth bit. With the rotate instructions, the register contents are rotated through the Carry bit.

Lab 4: Analog to Digital Converter

Introduction

This lab shows how to configure the ADC, run a conversion, read the analog voltage controlled by the potentiometer (POT1) on the board, and display the high order four bits on the display.

Hardware Effects

The top four MSBs of the ADC are displayed on the LEDs. Rotate the potentiometer to change the display.

Summary

Most PIC® devices have on-board Analog-to-Digital Converters (ADC) with resolution ranging from 8 to 16 bits. The converter can be referenced to the device’s VDD or an external voltage reference. This lesson references it to VDD. The result from the ADC is represented by a ratio of the voltage to the reference.

Lab 5: Variable Speed Rotate

Introduction

This lab combines all of the previous labs to produce a variable speed rotating LED display that is proportional to the ADC value. The ADC value and LED rotate speed are inversely proportional to each other.

Hardware Effects

Rotate POT1 counterclockwise to see the LEDs shift faster.

Summary

A crucial step in this lesson is to check if the delay value is 0. If it does not perform the zero check, and the ADC result is zero, the LEDs will rotate at an incorrect speed. This is an effect of the delay value underflowing from 0 to 255.

Lab 6: Pulse Width Modulation

Introduction

In this lab, the PIC® MCU generates a PWM signal that lights an LED with the POT1 thereby controlling the brightness.

Hardware Effects

Rotating POT1 will adjust the brightness of a single LED D5.

Summary

Pulse-Width Modulation (PWM) is a scheme that provides power to a load by switching quickly between fully on and fully off states. The PWM signal resembles a square wave where the high portion of the signal is considered the on state and the low portion of the signal is considered the off state. The high portion, also known as the pulse width, can vary in time and is defined in steps. A longer on time will illuminate the LED brighter. The frequency or period of the PWM does not change. A larger number of steps applied, which lengthens the pulse width, also supplies more power to the load. Lowering the number of steps applied, shortens the pulse width thus supplying less power. The PWM period is defined as the duration of one cycle or the total amount of on and off time combined.

Lab 7: Timer1

Introduction

This lab will produce the same output as Lab 3: Rotate. The only difference is that this version uses Timer1 to provide the delay routine.

Hardware Effects

LEDs rotate from right to left, similar to Lab 3.

Summary

Timer1 is a counter module which uses two 8-bit paired registers (TMR1H:TMR1L) to implement a 16-bit timer/counter in the processor. It may be used to count instruction cycles or external events that occur at or below the instruction cycle rate. This lab configures Timer1 to count instruction cycles and to set a flag when it rolls over. This frees up the processor to do meaningful work rather than wasting instruction cycles in a timing loop.

Lab 8: Interrupt

Introduction

This lab discusses about interrupts – its purpose, capabilities and how to set them up. Most interrupts are sourced from MCU peripheral modules. Some I/O pins can also be configured to generate interrupts when they change state. Interrupts usually signal events that require servicing by the software’s Interrupt Service Routine (ISR). Once an interrupt occurs, the program counter immediately jumps to the ISR and executed all its instructions before exiting. After an exit from the ISR, the program counter jumps to the instruction right after the one executed before the interrupt occurred. It is a rather more efficient way of watching out for events than continuously polling a bit or register.

Hardware Effects

D2, D3, D4 and D5 rotate from right to left at a constant rate of 500 ms.

Summary

This lab demonstrates the advantage of using interrupts over polling. An interrupt is generated whenever the Timer0 register reaches 0xFF and goes back to reset value. This indicates that 500 ms has passed and it is time to rotate the light. This interrupt is serviced by the TMR0_ISR() function. Note that this is the same for Lab 7 but this time, we are not continuously polling the TMR1IF flag.

Lab 9: Sleep/Wake Up using Watchdog Timer

Introduction

This lab will introduce the Sleep mode. SLEEP() function is used to put the device into a low-power standby mode.

Hardware Effects

Once this lab is set to RUNNING state, the watchdog timer will start counting. D2 and D4 are ON while the MCU is in sleep mode. Pressing the switch during Sleep will have no effect on the program won't go to the next lab since the PIC® is in Sleep mode. After the watchdog timer reached its period, (for this lab is approximately 16 seconds) the PIC® exits sleep mode and the LEDs are toggled.

Summary

The Power-Down mode is entered by executing the SLEEP instruction. Upon entering Sleep mode, there are different conditions that exist such as:

  • WDT will be cleared but will keep on counting, if enabled for operation during Sleep.
  • PD bit of the STATUS register is cleared.
  • TO bit of the STATUS register is set.
  • CPU clock is disabled.

Effects of Sleep may vary per device. It is recommended that the user refer to the specific device datasheet for detailed information on Sleep. The Watchdog Timer (WDT) is a system timer that generates a Reset if the firmware does not issue a CLRWDT instruction within the time-out period. WDT is typically used to recover the system from unexpected events. When the device enters Sleep, the WDT is cleared. If the WDT is enabled during Sleep, the WDT resumes counting. When the device exits Sleep, the WDT is cleared again. When a WDT time-out occurs while the device is in Sleep, no Reset is generated.

Lab 10: High Endurance Flash

Introduction

In this lab, we will discuss High-Endurance Flash (HEF) Memory, an alternative to Data EEPROM memory present in many devices. Most new devices have both types of memory but others have only one or the other. As we progress, we will also discuss the similarities and differences between these two as well as the purpose and set-up procedures to use the available HEF memory block on devices.

Hardware Effects

D2 and D4 will light up as we write ‘5’ into the HEF memory of the device.

Summary

High-Endurance Flash (HEF) Memory is a type of non-volatile memory much like the Data EEPROM. Data stored in this type of memory is retained in spite of power outages. HEF’s advantage over regular Flash Memory lies in its superior Erase-Write cycle endurance. While regular Flash could only sustain around 10,000 E/W cycles before breaking down, HEF can go for around 100,000 E/W cycles, within the range of average EEPROM endurance. Between true EEPROM and HEF, the difference lies in how operations are handled in both types of memory. In HEF, erase and write operations are performed in fixed blocks as opposed to data EEPROMs that are designed to allow byte-by-byte erase and write. Another difference is that writing to HEF stalls the processor for a few milliseconds as the MCU is unable to fetch new instructions form the Flash memory array. This is in contrast to true data EEPROMs which do not stall MCU executions during a write cycle.

Happy coding!

Categories

Release History

  • Dec 4, 2018

    Version: 1.0.0

    Initial Release

Download Contact the Developer

*Required Fields


  • Current Version:1.0.0
  • Created:Dec 4, 2018
  • Updated:Dec 4, 2018
  • Downloads:737
  • MPLAB Version:MPLABX IDE
  • C Compiler:XC8 1.41
  • Development Tools:Xpress Board
  • Supported Devices:PIC18F25K42

Corporate News

Resources

Stay Connected

Stay up to date with the latest news from Microchip Technology.