element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Connected Cloud Challenge
  • Challenges & Projects
  • Design Challenges
  • Connected Cloud Challenge
  • More
  • Cancel
Connected Cloud Challenge
Blog #4 Controlling a servo motor
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: wanfp97
  • Date Created: 7 May 2020 4:22 AM Date Created
  • Views 1800 views
  • Likes 3 likes
  • Comments 2 comments
Related
Recommended

#4 Controlling a servo motor

wanfp97
wanfp97
7 May 2020

Previously I have learnt how to operate a TCPWM using Device Configurator. In this blog, I will try to control a servo motor using a TCPWM for PSoC6.

 

BOM: PSoC6 WiFi-BT Pioneer Kit, a servo motor, and some jumper wires.

 

As previous, I created an Empty_PSoC6_App from the code example and opened up the Device Manager:

image

Setting clock divider to 10000 to get a 10kHz clock.

 

image

Setting up tcpwm1:

I noticed that there is a Enable Compare Swap option available in the Device Manager and I decided to enable it to test out what will happen.

After enabling it, I was able to enter a second compare value Compare 1 for my tcpwm1.

In this case, I set my period to 200 counts to represent 20ms under a 10kHz clock and 4 and 24 to represent 0.4ms and 2.4ms for the Compare 0 and Compare 1 values respectively.

Notice that the 0.4ms and 2.4ms are the minimum and maximum pulse width for my servo to position it at 0 and 180 degree respectively. I got these values by referring the data sheet of my servo and different servo may have different minimum and maximum pulse width required and it is advisable for you to check the datasheet of your servo and change the value if needed.

 

image

Next, I enable the Invert PWM_n Output as the PWM initially starts with and low and switches to high when the count reaches the compare value.

 

image

And lastly, assign the tcpwm1 to a digital output pin and save the configurations.

 

The next thing to do is finding out the function that can be use to trigger the swap between the Compare 0 and Compare 1 values.

image

As previous, I clicked on the Documentation link and get directed to PSoC6 PDL API Reference page.

Since it is something related to TCPWM, I started searching under PDL API Reference > TCPWM (Timer Counter PWM) > Timer/Counter PWM >Functions

I noticed that there is a function that might be related to what I'm searching. The Cy_TCPWM_Counter_EnableCompareSwap function.

So I clicked on that and check out what's inside.

 

image

Fortunately, I found that the last line of the Function Usage state that the Compare 0 and Compare 1 values can be swapped using Cy_TCPWM_TriggerCaptureOrSwap function and this is what I was looking for.

 

image

But I still have no idea of how the syntax of this function should be, so I copy and paste the function into the search box at the top right corner of the documentation page.

 

This is what I have got:

image

I don't really understand what should be the second input argument for the function, but judging from the example in Function Usage, I will try to input it with 0UL and 1UL and see what is going to happen.

 

After trial and error, I found out that Cy_TCPWM_TriggerCaptureOrSwap(tcpwm1_HW, 1UL); will swap the compare value for the tcpwm1 between Compare 0 and Compare 1.

 

Below is the final code that I used to turn the servo right and left every 3 seconds.

#include "cy_pdl.h"
#include "cycfg.h"

int main(void)
{
    /* Initialize the device and board peripherals */
    __enable_irq();
    init_cycfg_all();
    Cy_TCPWM_PWM_Init(tcpwm1_HW, tcpwm1_NUM, &tcpwm1_config); // initialize tcpwm
    Cy_TCPWM_PWM_Enable(tcpwm1_HW, tcpwm1_NUM); // enable tcpwm1
    Cy_TCPWM_TriggerStart(tcpwm1_HW, tcpwm1_MASK); //Start the tcpwm1 (initially 0.4ms high per cycle) (servo positioned at right)
    for (;;)
    {
          Cy_SysLib_Delay(3000); // delay for 3000ms
          Cy_TCPWM_TriggerCaptureOrSwap(tcpwm1_HW, 1UL); // tcpwm1 swap between 2.4ms(left) and 0.4ms(right) high per cycle
    }
}

 

GitHub link: https://github.com/wanfp97/ServoControl

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

After programming and running the code on PSoC6, the servo turned left and right every 3 seconds as it is expected.

  • Sign in to reply
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube