In my previous blog post, #5 GPIO read and write , I faced an issue where my TCPWM keep swapping its compare value and causing the servo to act weirdly.
After some troubleshooting, I have found that the issue occur due to I place the Cy_TCPWM_TriggerCaptureOrSwap(tcpwm1_HW, 1UL); at the part where I poll the logical status of the GPIO and thus causing the TCPWM to swap its compare value each time the GPIO logical status is checked.
To resolve this issue, the easiest solution would be not using the polling method and use interrupt method instead.
I have checked how to implement the interrupt method in PSoC6 in my previous blog post, #6 GPIO interrupt .
In this blog post, I will utilize the GPIO interrupt and try to resolve the issue that I have faced in my previous blog post#5 GPIO read and write.
BOM: PSoC6 WiFi-BT Pioneer Kit, a red LED, a green LED, a servo motor, a push button, jumper wires, and 2 potentiometer with resistance adjusted to be around 300 ohm.
GitHub: https://github.com/wanfp97/PSoC6-ServoControl-using-GPIO-interrupt
As previous, I have create a new Empty_PSoC6_App.
configuring my TCPWM with a period of 20ms and pulse width of 0.4ms and 2.4ms.
You can refer to my previous blog post on #3 Familiarize with Device Configurator to know the details on how to configure it using Device Configurator.
Setting the pins the same way as in #5 GPIO read and write.
In the main.c:
#include "cy_pdl.h" to include Peripheral Driver Library.
#include "cycfg.h" to include the library for Device Configurator.
#include "cyhal.h" as the GPIO interrupt function that I'm going to use come from this library.
declare the function prototype for the interrupt service routine (ISR) and also a Boolean type variable to handle the interrupt flag.
in the int main(), enable the interrupt, initialize all the configuration done in Device Configurator.
Initialize, enable, and trigger start the tcpwm1.
assign the ISR handler to the button pin using cy_hal_register_callback() function.
enable the interrupt event for button pin. Note that CYHAL_GPIO_IRQ_BOTH is used since I need to interrupt on both rising and falling edge of the button pin to interrupt when the button is pressed or released.
You can change according to this table for different interrupt requirements.
In the for(;;) loop:
If button is pressed:
clear the interrupt flag to prepare it for the next interrupt
then read the logical level of the button pin using the Cy_GPIO_Read() function
and then add the task to be done according to the logical level of the button pin.
The final product works like this:
Note that I'm using active high connection for both the LEDs and the button.
Recall block diagram designed in #5 GPIO read and write:
The system works exactly like the block diagram, therefore, it can be say that the issue is resolved.