Skip to content

This repository provides an MPLAB® X project for interfacing the Configurable Logic Block (CLB) with Timer0 (TMR0) and Timer1 (TMR1) peripherals to control a Switec stepper motor as tachometer.

License

Notifications You must be signed in to change notification settings

microchip-pic-avr-examples/pic16f13276-tachometer-mplab-mcc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Microchip Technologies Inc.

Tachometer — Use Case for CLB Using the PIC16F13276 Microcontroller With MCC Melody

This repository provides an MPLAB® X project for interfacing the Configurable Logic Block (CLB) with Timer0 (TMR0) and Timer1 (TMR1) peripherals to control a Switec stepper motor. By the end of this example, you will be able to provide a degree of rotation (0°-315°) and have the stepper motor move to that angle.

The CLB peripheral is a collection of logic elements that can be programmed to perform a wide variety of digital logic functions. The logic function may be completely combinatorial, sequential, or a combination of the two, enabling users to incorporate hardware-based custom logic into their applications.

Related Documentation

More details and code examples on the PIC16F13276 can be found at the following links:

Software Used

Hardware Used

Operation

To program the Curiosity Nano board with this MPLAB X project, follow the steps provided in the How to Program the Curiosity Nano Board chapter.

Concept

This example demonstrates the capabilities of the CLB, a Core Independent Peripheral (CIP), that can control and manipulate the Switec stepper motor. The figure below shows the implemented solution.


Two bits of the CLB Software Input (CLBSWIN) register are used as synchronized inputs to enable the CLB circuit (CLBSWIN0) and to select the direction (CLBSWIN1), clockwise if it is set as 0 in software, or counterclockwise if it is 1.

The TMR0 peripheral is configured as an 8-bit timer to create a 2-millisecond periodic signal. This signal is used to control the partial steps rate according to the specifications of the Switec stepper motor.

For one full step, three partial steps are needed, as shown in the figure below, from the Switec data sheet. The TMR1 peripheral is used to control the number of partial steps (as TMR0 overflow pulses), configured by the software application.


The positive edge of the TMR1 overflow is used to stop the waveform sequence applied to the Switec stepper motor when the number of partial steps is reached. The edge detector introduces a 2-CLB clock delay (of four milliseconds in this configuration). To avoid missing a step, a CLB interrupt request (CLB_IRQ0) is generated when the step sequence is complete (on the edge of TMR1 overflow).

When the application starts, the shaft of the motor is set to an initial position, so that the motor always starts from a fixed position and is defined to the left of the dial. Using the Enhanced Universal Synchronous Asynchronous Receiver Transmitter (EUSART) peripheral, a position can be sent via the PC terminal and observed on the external hardware.

The UART peripheral is used to control the shaft position of the motor. The user must send a value between 0 and 315 in order to set the desired position and the specific " \n " character at the end of the line for a right interpretation in the software application. Specific messages are also displayed in the terminal as displayed in the picture below.

Setup

The following peripheral and clock configurations are set up using MPLAB® Code Configurator (MCC) Melody for the PIC16F13276:

  1. Configurations Bits:

    • CONFIG1:
      • External Oscillator Selection bits: Oscillator not enabled
      • Reset Oscillator Selection bits: HFINTOSC (1 MHz)
    • CONFIG2:
      • Brown-out Reset Enable bits: Brown-out Reset disabled
    • CONFIG3:
      • WDT Operating Mode bits: WDT Disabled, SEN is ignored
  2. Clock Control:

    • Clock Source: HFINTOSC
    • HF Internal Clock: 16_MHz
    • Clock Divider: 1
  3. CLB Synthesizer Library

    • Clock Divider: Divide clock source by 1
    • Clock Selection: TMR0_Overflow
  4. CLB1:

    • Enable CLB: Enabled
  5. CRC:

    • Auto-configured by the CLB
  6. TMR0:

    • Timer Enable: Enabled
    • Timer Mode: 8-bit
    • Clock Source: LFINTOSC
    • Prescaler: 1:1
    • Requested Period: 2 ms
  7. TMR1:

    • Timer Enable: Enabled
    • 16-Bit Read/Write Mode Enable: Enabled
    • Clock Source: Auto-configured by the CLB Synthesizer Library (CLB_BLE_OUT_TRIG[0] in this case)
    • Prescaler: 1:1
    • Timer Count Editor Enable: Enabled
  8. UART2:

    • Receive Enable: Enabled
    • Serial Port Enable: Enabled
    • Transmit Enable: Enabled
    • Request Baudrate: 9600
    • Redirect Printf to UART: Enabled
  9. Pin Grid View:

    • CLBPPSOUT0: RB5 (Contact 1)
    • CLBPPSOUT1: RB4 (Contact 4)
    • CLBPPSOUT2: RB3 (Contact 2,3)
    • EUSART RX2: RD7 (TX CDC)
    • EUSART TX2: RD6 (RX CDC)

Demo

Different shift positions are sent via the PC terminal and can be observed in the next demonstration. Each transmitted number is described by the LF newline character. The actual position and a Help message are also shown in the terminal.

Note: The demo is at 2x speed.


Summary

This example demonstrates the capabilities of the CLB, a CIP that can control and manipulate a Switec Stepper Motor to drive in any direction to a received set point from the PC terminal.

How to Program the Curiosity Nano Board

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

  1. Connect the 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 click 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 PICxxxxx Curiosity Nano in the Connected Hardware Tool section of the project settings.
    a. Right click the project and click Properties.
    b. Click the arrow under the Connected Hardware Tool.
    c. Select PICxxxxx Curiosity Nano (click the SN), click Apply and then click OK.

  6. Program the project to the board.
    Right click the project and click Make and Program Device.



Menu

About

This repository provides an MPLAB® X project for interfacing the Configurable Logic Block (CLB) with Timer0 (TMR0) and Timer1 (TMR1) peripherals to control a Switec stepper motor as tachometer.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published