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 an Switec stepper motor as tachometer.

License

Notifications You must be signed in to change notification settings

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

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MCHP

Tachometer — Use Case for CLB Using the PIC16F13145 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 PIC16F13145 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 Register (CLBSWIN) 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 two 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 in the figure below, taken from the Switec data sheet. The TMR1 peripheral is used to control the number of partial steps (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 two CLB clocks delay (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 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 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 in the picture below.

Setup

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

  1. Configurations Bits:

    • CONFIG1:
      • External Oscillator mode selection bits: Oscillator not enabled
      • Power-up default value for COSC bits: HFINTOSC (1MHz)
    • CONFIG2:
      • Brown-out reset enable bits: Brown-out reset disabled
    • CONFIG3:
      • WDT operating mode: WDT Disabled, SEN is ignored
  2. Clock Control:

    • Clock Source: HFINTOSC
    • HF Internal Clock: 16_MHz
    • Clock Divider: 1
  3. CLB1:

    • Enable CLB: Enabled
    • Clock Selection: TMR0_Overflow
    • Clock Divider: Divide clock source by 1
  4. CRC:

    • Auto-configured by CLB
  5. NVM:

    • Auto-configured by CLB
  6. TMR0:

    • Enable Timer: Enabled
    • Clock Selection: LFINTOSC
    • Clock Prescaler: 1:1
    • Timer Mode: 8-bit
    • Requested Period(s): 0.002 (2 ms)
  7. TMR1:

    • Enable Timer: Enabled
    • 16-Bit Read/Write Mode Enable: Enabled
    • Clock Selection: CLBOUTx (where x is written in Notifications tab)
      • For this example, CLBOUT1 must be used
    • Clock Prescaler: 1:1
    • Enable Period Count Editor: Enabled
  8. UART1:

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

    • CLBPPSOUT0: RB5 (Contact 1)
    • CLBPPSOUT1: RB6 (Contact 4)
    • CLBPPSOUT2: RB7 (Contact 2,3)
    • EUSART RX1: RC5 (RX CDC)
    • EUSART TX1: RC4 (TX CDC)

Demo

Different shift position are sent via the PC terminal and can be observed in the next demonstration. Each transmitted number is described by 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 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.

  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:
    Right click the project and click Properties.
    Click the arrow under the Connected Hardware Tool.
    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 an Switec stepper motor as tachometer.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published