Skip to content

microchip-pic-avr-examples/pic16f18446-sensor-board-battery-measurement

Repository files navigation

MCHP

Sensor Board Battery Measurement Using the PIC16F18446 Microcontroller

The PIC16F184xx family has a new Analog-to-Digital Converter with Computation (ADCC) with a 12-bit resolution. This project shows how the PIC can internally measure its VDD using the ADCC and Fixed Voltage Reference.

This example showcases the PIC16F18446 Sensor Board. Battery (VDD) level is measured while the display is turned on. The batteries charge percentage is shown as a battery icon with proportional filing in the top-right corner of the display. Additionally, the battery voltage in Volts and percentage graph are displayed.

Related Documentation

Software Used

Hardware Used

Operation

To program the microcontroller (MCU) with this MPLAB® X project, follow the steps provided in the How to Program the Microcontroller chapter.

Setup

The following configurations must be made for this project:

  • Clock Control:

    • Clock Source: HFINTOSC
    • HF Internal Clock: 32 MHz
    • Clock Divider: 1


  • Configuration bits:

    • External Oscillator Selection bits: Oscillator disabled
    • Reset Oscillator Selection bits: HFINTOSC (1 MHz)
    • WDT Operating Mode bits: WDT Disabled, SWDTEN is ignored


  • MSSP1 (SPI):

    • Mode: Host
    • SPI Mode: SPI Mode 0
    • Input data sampled at: Middle
    • Clock source selection: FOSC/4


  • CLC1:

    • Enable CLC: Yes
    • Logic Cell mode: AND-OR


  • CLC2:

    • Enable CLC: Yes
    • Logic Cell mode: AND-OR


  • FVR:

    • Enable FVR: Yes
    • FVR buffer gain to ADC: 1x
    • FVR buffer gain to other peripherals: 1x


  • ADCC:

    • Enable ADC: Yes
    • Operating Mode: Burst Average mode
    • Result Alignment: Right
    • Positive Input Channel: ANA0
    • Positive Reference: VDD
    • Negative Reference: VSS
    • Auto-conversion Trigger: Disabled
    • Acquisition Count: 0
    • Clock Source: FOSC
    • Clock Divider: FOSC/128
    • Repeat: 64



Pin Configuration Description Custom Name
RC1 SCK1 Serial Clock SCK1
RB4 SDI1 Serial Data In SDI1
RC5 CLC2 Analog output IO_RC5
RC6 CLC1 Analog output IO_RC6
RB5 GPIO Digital input S2
RC4 GPIO Digital input S3
RB7 GPIO Digital output LCD_RESET
RC2 GPIO Digital output LCD_BACKLIGHT
RC7 GPIO Digital output LCD_CS



Demo

To get the VDD voltage, ADCC is used to measure the FVR, which is configured to 1.024V, with VDD as positive reference. Then the VDD is calculated using this formula: VDD = 1.024V * 4096 / ADC_Reading.


Summary

This code example shows how to use the Battery level measurement feature on the Sensor Board. MSSP and CLC are used for LCD control. The Battery level is determined using FVR and ADCC.

How to Program the Microcontroller

This chapter demonstrates how to use the MPLAB® X IDE to program a PIC® device with an Example_Project.X. This applies to other projects.

  1. Connect the Sensor board to the PC.

  2. Open the Example_Project.X project in MPLAB X IDE.

  3. Set the Example_Project.X project as main project.
    Right click the project in the Projects tab and then Set as Main Project.

  4. Clean and build the Example_Project.X project.
    Right click the Example_Project.X project and select Clean and Build.

  5. Select Starter Kits (PKOB) in the Connected Hardware Tool section of the project settings:
    Right click the project and Properties.
    Click the arrow under the Connected Hardware Tool, and from the dropdown, select Starter Kits (PKOB) by clicking the SN.
    Click Apply and then OK.

  6. Program the project to the microcontroller.
    Right click the project and then Make and Program Device.