Code Examples Forum Discussions
Hi all,
I wanted to make a device that uses a piezoelectric buzzer to generate sound, so I expressed it using PWM.
The environment used is as follows:
・PSoC Creator 4.3
・CY8CKIT-062-BLE
The buzzer sounds if you simply set the PWM to the audible range and output it, but I expressed it using another PWM so that it would be beep sound like pi, pi, pi... The image of the output waveform is as follows:
PWM_1 creates the period of sound output, and PWM_2 creates the frequency of the sound. The kill input of PWM_2 is used to control whether or not the sound output. The circuit is as follows:
The LED pin is output to check the period. The piezoelectric buzzer is connected to Pin_1 pin and via 1kΩ to GND. If you want a louder sound, connect it to the output of pwm_n in PWM_2 instead of GND.
PWM_1 uses the clock input of 1KHz of Clock_1 to create the period for buzzer sound. Divide it into 200 counts (Period=199) to obtain 5Hz (200mS). Of the 200 counts, the duty is set to 180:20 (90%) using Compare0. 20 counts is the time when the sound is generated by PWM_2. The settings are as follows. Please change the duty ratio according to your preference.
PWM_2 is where you set the frequency of the sound. The frequency of the sound is decided by inputting the clock of 1MHz and dividing this. This time, 2KHz (Period=499) and 50% duty (Compare0=250). If you want to change the frequency of the sound, change the Period value here. We recommend a duty of 50%. Otherwise, the sound will be a bit dirty. The setting of PWM_2 is shown below:
In order to control the output of PWM_2, it is possible to provide a Start/Stop (kill) input and use it as an edge trigger, but this time, Kill mode is asynchronous mode and the kill input is set to Level. The settings of Advanced tab are as follows:
Pin settings are output only. The LED is set to P1[5] (orange LED) and Pin_1 is set to P12[1]. The parameter setting of the pin is the same for both, check the Digital output and HW connection, and set the Drive mode to Strong Drive. The setting of Pin_1 is shown.
The program in main_cm4.c is simple. Since the PWM is only started, the CPU will be released when the setting is completed. The program looks like this:
#include "project.h"
int main(void)
{__enable_irq(); /* Enable global interrupts. */
/* Start the PWM component */
PWM_2_Start();
PWM_1_Start();for(;;)
{
/* Place your application code here. */
}
}
Thanks,
Kenshow
Show LessI have just posted this code for your reference or as a test tool.
[Overview]
This code is based on a "UART2_TTY_EXAMPLE_TLE984x" sample code.
I modified the code as a base software for any function test which can control via UART2.
If you will test some function repeatedly, this code may help you.
This code has three test code as the below.
Key=1: VDDEXT_LED = On
Key=2: VDDEXT_LED = Off
Key=3: 1[sec] timer
Each test code can execute by key input via terminal software on your PC.
For example, If you type [1] key then VDDEXT_LED will be turn on.
You can change the each test code for your test purpose.
This code is including the following functions. These code may also help you.
1, A sample test subroutine which prints 1~9 every second via UART2.
2, put_HexByte(uint8 data) : Convert one hexadecimal data into one ASCII data and then send it via UAR2.
3, put_HexWord(uint16 data) : Convert two hexadecimal data into two ASCII data and then send it via UAR2.
4, Delay_100us(uint32_t data) : 100[us] delay function which is using a Delay_us() function
[Environment]
Confirmed evaluation boards :
1) TLE984x Evalboard ( J16 = JP27 = JP28 = Short)
2) TLE9844-2QX Application Kit
IDE :
1)uVision V5.36.0.0
Software :
1) UART2_TTY_EXAMPLE_TLE984x_FuncTEST.zip
[Instructions]
1) Program this software into your evaluation board via uVision
2) Execute any terminal software (ex. TeraTerm) and connect with evaluation board at 115200bps.
3) Push RESET-SW on the evaluation board.
4) Follow the menu.
[Execution image]
Hiro.
Show LessHello all
We have uploaded the sample code to wake up from DeepSleep with the read / write of I2C slave below, but this time, We will upload the sample code to process the read / write operation in SCB_I2C_SlaveCompleteCallback.
The callback function ensures that the read / write is done before the process of entering DeepSleep.
Show Less
Hi,
In a discussion Matrix Key Sample using interrupt and debouncing was hoped.
So I modified my old 4x4 Matrix Keypad Sample
https://community.infineon.com/t5/Code-Examples/4x4-Matrix-Keypad-Sample/m-p/96977#M370
trying to fulfill the requirement. (As usual, there should be infinite approaches to do that though).
schematic
Pins
main.c
#include "project.h"
#include "stdio.h"
#define NUM_ROW 4
#define NUM_COL 4
#define DEBOUNCE_COUNT 4
volatile uint8_t key[NUM_ROW][NUM_COL] = { 0u } ;
uint8_t letter[NUM_ROW][NUM_COL] = {
{ '1', '2', '3', 'A' },
{ '4', '5', '6', 'B' },
{ '7', '8', '9', 'C' },
{ '*', '0', '#', 'D' }
} ;
volatile int row_no = 0 ;
volatile int col_no = 0 ;
volatile int matrix_key_pushed = 0 ;
volatile int matrix_key_released = 0 ;
CY_ISR(key_pushed_isr)
{
uint8_t tmp ;
tmp = Control_Reg_Read() ^ 0x01 ;
Control_Reg_Write( tmp ) ;
key_pushed_int_ClearPending() ;
matrix_key_pushed = 1 ;
}
CY_ISR(key_released_isr)
{
uint8_t tmp ;
tmp = Control_Reg_Read() ^ 0x02 ;
Control_Reg_Write( tmp ) ;
key_released_int_ClearPending() ;
matrix_key_released = 1 ;
}
void scan_key_matrix(void)
{
int key_pushed = 0 ;
switch(row_no) {
case 0: ROW0_Write(1) ; ROW1_Write(0) ; ROW2_Write(0) ; ROW3_Write(0) ; break ;
case 1: ROW0_Write(0) ; ROW1_Write(1) ; ROW2_Write(0) ; ROW3_Write(0) ; break ;
case 2: ROW0_Write(0) ; ROW1_Write(0) ; ROW2_Write(1) ; ROW3_Write(0) ; break ;
case 3: ROW0_Write(0) ; ROW1_Write(0) ; ROW2_Write(0) ; ROW3_Write(1) ; break ;
default: ROW0_Write(0) ; ROW1_Write(0) ; ROW2_Write(0) ; ROW3_Write(0) ; break ;
}
switch(col_no) {
case 0: key_pushed = COL0_Read() ; break ;
case 1: key_pushed = COL1_Read() ; break ;
case 2: key_pushed = COL2_Read() ; break ;
case 3: key_pushed = COL3_Read() ; break ;
}
if (key_pushed) {
if (key[row_no][col_no] == (DEBOUNCE_COUNT - 1)) {
Control_Reg_Write(0x01) ; // matrix_key_pushed = 1 ;
}
if (++key[row_no][col_no] >= DEBOUNCE_COUNT) {
key[row_no][col_no] = DEBOUNCE_COUNT ;
}
} else {
if (key[row_no][col_no]) {
Control_Reg_Write(0x2) ; // matrix_key_released = 1 ;
}
key[row_no][col_no] = 0 ;
}
col_no++ ;
if (col_no >= NUM_COL) {
col_no = 0 ;
row_no = (row_no + 1) % NUM_ROW ;
}
ROW0_Write(0) ;
ROW1_Write(0) ;
ROW2_Write(0) ;
ROW3_Write(0) ;
}
CY_ISR(my_tick_isr)
{
scan_key_matrix() ;
}
#define STR_LEN 64
char str[STR_LEN+1] ;
void print(char *str)
{
UART_PutString(str) ;
}
void printc(char c)
{
UART_PutChar(c) ;
}
void cls(void)
{
print("\033c") ; /* reset */
CyDelay(20) ;
print("\033[2J") ; /* clear screen */
CyDelay(20) ;
}
void splash(void)
{
cls() ;
print("5LP 4x4 Matrix Key Test") ;
snprintf(str, STR_LEN, "(%s %s)\n", __DATE__, __TIME__) ;
print(str) ;
}
void init_hardware(void)
{
CyGlobalIntEnable; /* Enable global interrupts. */
UART_Start() ;
splash() ;
CySysTickStart() ;
CySysTickSetCallback(0, my_tick_isr) ;
Control_Reg_Write(0x00) ;
key_pushed_int_ClearPending() ;
key_released_int_ClearPending() ;
key_pushed_int_StartEx(key_pushed_isr) ;
key_released_int_StartEx(key_released_isr) ;
}
void dump_key_matrix(void)
{
int col, row ;
for (row = 0 ; row < NUM_ROW ; row++) {
for (col = 0 ; col < NUM_COL ; col++) {
if (key[row][col]) {
snprintf(str, STR_LEN, "%c ", letter[row][col]) ;
print(str) ;
} else {
print("- ") ;
}
}
print("\n\r") ;
}
}
int main(void)
{
init_hardware() ;
dump_key_matrix() ;
for(;;)
{
if (matrix_key_pushed) {
matrix_key_pushed = 0 ;
cls() ;
dump_key_matrix() ;
}
if (matrix_key_released) {
matrix_key_released = 0 ;
cls() ;
dump_key_matrix() ;
}
}
}
moto
Show LessHi,
I saw a discussion about using LCD with PSoC 4
https://community.infineon.com/t5/PSoC-4/LCD-interfacing/td-p/352401
And when I ran search in Code Examples, the one I uploaded before was for PSoC 6.
So I hacked a one for PSoC 4. (Assuming HD44780 Char LCD Module)
The discussion asked for CY8C4045AZI445, which I could not find the datasheet,
right now the (seems to be) closest one I have was CY8CKIT-149 (CY8C4147AZI-S475).
So I made a sample project for this board.
Hopefully it will work with CY8C4045AZI by changing the target device and pin assign.
The top level schematic
Pins
In the datasheet of Character LCD
When build and programmed to the device, the LCD shows "LCD_TEST"
And Tera Term connected to the com port shows a splash line and a prompt "> "
Typing a string and hit enter key sends the string to the LCD
Note: the string "hello world" often gets corrupted,
may be we need to add some delay between letter and letter.
moto
Show Less
Hi All,
This is sample code that uses SmartIO to eliminate switch input chattering with CY8CKIT-145-40XX.
The SmartIO clock is LFCLK. Since SmartIO can only count up to 6.4ms, two SmartIOs are needed to eliminate chattering in 100ms.
We generate a 5ms carry with Count up Wrap on SmartIO Port3.
After that, we use SmartIO Port2's Count up Wrap to count up the 5ms carry and create a 100ms carry.
In SmartIO Port2, Count up Wrap is reset when the Switch input is low level.
By inputting the P2_5 output to the Pin_INT input, chattering of 100 ms or less can be eliminated and an interrupt can be generated.
The blue parts and wiring are not included in the CY8CKIT-145-40XX. So you must prepare them.
Thanks and regards,
Show LessHello,
This is a sample code for wakeup from DeepSleep using I2C slave communication for CY8CKIT-145-40XX.
In case of polling contrtol, the following code will be written as the main function.
************************************************************************************************************
for(;;)
{
/* Write buffer(master -> slave) */
if (0u != (I2CS_I2CSlaveStatus() & I2CS_I2C_SSTAT_WR_CMPLT)) ....... Conditional statement (1)
{
Processing the data, Clearing the buffer and status
}
/* Read buffer(master <- slave) */
if (0u != (I2CS_I2CSlaveStatus() & I2CS_I2C_SSTAT_RD_CMPLT)) ....... Conditional statement (2)
{
Clearing the buffer and status
}
/* Low power modes */
/* Enter critical section to lock the slave state */
uint8 intState = CyEnterCriticalSection();
/* Check if slave is busy */
status = (SCB_I2CSlaveStatus() & (SCB_I2C_SSTAT_RD_BUSY | SCB_I2C_SSTAT_WR_BUSY));
if (0u == status) ....... Conditional statement (3)
{
/* Slave is not busy: enter Deep Sleep */
SCB_Sleep(); /* Configure the slave to be wakeup source */
CySysPmDeepSleep();
/* Exit critical section to continue slave operation */
CyExitCriticalSection(intState);
SCB_Wakeup(); /* Configure the slave to active mode operation */
}
else
{
/* Slave is busy. Do not enter Deep Sleep. */
/* Exit critical section to continue slave operation */
CyExitCriticalSection(intState);
}
}
************************************************************************************************************
Conditional statement(3) may be processed before processing either conditional statement(1) or (2).
The read data for wakeup may be corrupted.
Then, the "if" statement of Conditional statement (3) is as follows.
************************************************************************************************************
status = (I2CS_I2CSlaveStatus() & (I2CS_I2C_SSTAT_RD_BUSY | I2CS_I2C_SSTAT_WR_BUSY));
if (0u == status && i2cs_deepsleep_flag == CLEAR)
{
}
************************************************************************************************************
The i2cs_deepsleep_flag is set in the void I2CS_I2C_SlaveCompleteCallback (void) API.
************************************************************************************************************
void I2CS_I2C_SlaveCompleteCallback(void)
{
if (0u != (I2CS_I2CSlaveStatus() & I2CS_I2C_SSTAT_WR_CMPLT) ||
0u != (I2CS_I2CSlaveStatus() & I2CS_I2C_SSTAT_RD_CMPLT))
{
i2cs_deepsleep_flag = SET;
}
}
************************************************************************************************************
As a result, data can be reliably written and read in I2C slave communication for wakeup.
Comment out "#define FLAG_CONTROL" in main.c to disable i2cs_deepsleep_flag.
************************************************************************************************************
/* I2C slave flag control for low power mode in case of WR & RD completion polling */
//#define FLAG_CONTROL
************************************************************************************************************
Best regards,
Yocchi
Show Less
Hello,
I'm using the Modus Toolbox and CY8CKIT-062-WIFI-BT. When I create and run the individual examples for "Bluetooth_LE_CAPSENSE_Buttons_and_Slider" and "I2C_Master", they work fine separately. I'm trying to merge them together manually, by using the Bluetooth example as the baseline. When I do this, my i2c code doesn't work. I get this error(CY_SCB_I2C_MASTER_MANUAL_ADDR_NAK) when calling cyhal_i2c_master_write(). I've already called the cyhal_i2c_init() and the cyhal_i2c_configure() like in the working example....Can someone provide some clues on why this wouldn't work??
Appreciate any help and sug
Show LessNow that DCmp V2.0 is released, I can now consider work on possible future enhancements.
Feature: Hysteresis
It has been suggested by another PSoC5LP enthusiast to add hysteresis to the threshold comparisons.
This would potentially allow for slow and noisy input signals to not cause as much comparison output chatter. 'Cleaner' outputs might allow for better HW and SW state machine control.
For example, you could use DCmp to create a PID controller. This control could make decisions to control a heater element or a cooling element (like an air conditioning unit).
Since the input would be a temperature sensor going through an ADC conversion, the input can be slow but also a bit noisy. Too much comparison chatter on the threshold might cause the heating or cooling to rapidly get signals to turn on and off many times in a short duration. This could cause the temperature control device to prematurely fail.
Adding sufficient hysteresis by updating the threshold after crossing it to a value above the input noise level should eliminate this effect.
Implementation Ideas
Method #1: Dual Thresholds
I could implement hysteresis by using the dual-threshold ability of the DCmp component.
For example, I could load the upper threshold into Thresh1 and the lower threshold into Thresh2. The comparison outputs would start with the upper thresholds.
As the signal crosses the upper threshold (Thresh1), DCmp would then use the lower threshold (Thresh2) for its comparison outputs. If the signal goes below the lower threshold, the comparison outputs would now use the upper threshold.
Pros: This is the fastest hysteresis switching.
Cons: The design would lose the dual threshold dual-output set of comparisons. Also, this forces DCmp into unsigned comparisons.
Method #2: DMA Threshold Reloading
Once the input signal crosses the upper threshold, a HW signals the DMA to load the lower threshold into DCmp threshold register. If the lower threshold is crossed, the upper threshold is loaded into the same threshold register via DMA.
This can be done in a TD loop with the new values.
Pros: This method allows the DCmp to be used in all modes currently available since the threshold registers are reloaded. This allows for signed comparisons.
Cons: A DMA operation can take 8 to 10 BUS_CLK cycles to complete. This would add latency to the hysteresis switching. However in most cases this latency might not be a problem. For example, if the input is being supplied by an ADC, it is highly possible that the hysteresis switching can occur before the next ADC conversion can finish.
I welcome your feedback as well as further enhancement suggestions.
Show Less
I've created a new component for the PSoC5 that can be used in place of the Infineon Digital Comparator component in many cases.
My new component DCmp has many advantages over the Infineon component and when used with DMA as the data loading, can be very fast (~ 4 Msamples/sec)
Although the Infineon Digital Comparator is the fastest solution it comes at significant costs. The DCmp component can save on PSoC resources as well as provide additional features.
The table below compares the two components side-by-side. I've highlighted in RED what I believe to be the better advantage.
Method |
Infineon Digital Comparator |
CONSULTRON DCmp |
Input load |
HW |
DMA or CPU |
Threshold load |
HW |
DMA or CPU |
Number of simultaneous inputs |
1 |
1 or 2 |
Number of simultaneous comparisons |
1 |
1 or 2 |
Simultaneous comparison outputs |
1 HW |
1 to 6 HW types |
Data Widths |
1- to 32- bits |
1- to 32- bits |
Comparison output glitch filter |
No (async) |
Yes (load signal sync) |
Relative PSoC resource allocation* |
>=(1/3)x@8-bits >=(2/9)x@16-bits >=(2.5/13)x@24-bits >=(3/17)x@32-bits |
(1/1)x @ 1- to 8-bits + 1 Datapath |
Relative Comparison Speed |
Fastest (async) |
Very fast using DMA (>4M cmps/sec) |
Signed comparisons |
No |
Yes |
* “(mm/uu)x” represents macrocells/unique pterms. |
I've also provided a Demo project with 3 variations that demonstrate the abilities of DCmp.
The project was created for the CY8CKIT-059 but can be ported to other evaluation/kits or your own design.
Requirements:
- PSoC Creator 4.2 or higher
- PSoC5LP.
Show Less