Code Examples Forum Discussions
Hi,
I've included this link to a post from MarkH_61 . I thought this forum would be a good place for referencing this custom component.
It is his latest version of the SysTick Component.
The SysTick functions can be called using CyLib.c. His component makes it more visual if you prefer.
Enjoy.
Len
Show LessHello,
This sample project is the analog front end for sensor.
The inversion amplification with offset in single power supply drive is used to match the sensor signal to the dynamic range of the PSoC AD converter.
Please see the schematics and board.
KitProg3 of CY8CKIT-145-40XX is required for I2C communication with Bridge Panel Control.
1. Create a sine wave output using VDAC with sampling rate of 1ms timer interrupt.
2. It is amplified twice by inverting amplification and 1.2V offset is added.
3. Amplify 4x with PGA and add 1.2V offset too.
Unfortunately, due to the wiring impedance inside PSoC, the gain is about 3 times, not 4 times.
4. The result of AD conversion is displayed on the Bridge Panel Control. BCP script is also attached.
If you actually use it, you should use it with a larger gain.
I hope it helps you.
Best regards,
Yocchi
Show LessHi all,
Previously, I introduced the device that generates sound using the piezoelectric buzzer with Creator. This time, I introduce an example of realizing this with Modus Toolbox 2.1.
The environment used is as follows:
・Modus Toolbox2.1
・CY8CKIT-062-BLE
The buzzer sounds if you simply set PWM to the audible range and output it, but I added an additional timer to express it so that it would be beep sound like pi, pi, pi...
In the Creator version, I expressed using PWM, but in Modus, the input signal from another PWM cannot be used for the PWM input. Also, since the circuit image is not represented in the tool in Modus, I will try to explain it as much as possible here.
Timer_1 creates a sound output period (5Hz), and PWM_2 creates a sound frequency of 2KHz. The start and kill/stop inputs of PWM_2 are used to control whether or not the sound is output. The circuit is follows.
The piezoelectric buzzer is connected to Buzzer 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.
I will explain how to create a project. For Modus projects, click New Application from the Modus Quick panel, select "PSoC 6 BSPs>CY8CKIT-062-BLE", and click the Next button.
Then select “Empty PSoC6 App”, click the Create button, Close to create a new empty project on the Modus IDE.
After creating an empty project, click “Device Configuration” from the Quick panel to configure your component. And, open “Peripheral Clocks” tab and set the clock that is the clock input for PWM and Timer. Check "16 bit Divider1" and set "Divider" to 100 and output to 1MHz. The source for this Clock is CLK_PERI, which supplies 100MHz by default.
Go to "Peripherals" tab to configure the components to use.
The output pin Buzzer will use P12[0], but the only 16bit PWM that can be used at that time is TCPWM[1] 16-bit Counter 4pwm. Therefore, name TCPWM[1] 16-bit Counter 4 as "PWM_2", select PWM-1.0, and set it to output PWM with Period=499 (frequency 2KHz) and Compare=25 (duty 50%). Select Clock Source = “16bit Divider 1 clk” as the clock source for PWM_2. Input Start and Kill (Stop) to control PWM. Modus cannot use the output from another PWM as input, but you can specify the output of the Timer. Here, I decided to use the timer output of the adjacent TCPWM[1] 16-bit Counter 5. As the PWM input, select Start Input = "TCPWM[1] 16-bit Counter 5 overflow" and Kill Signal =" TCPWM[1] 16-bit Counter 5 compare". Kill Mode is "Stop on Kill". The settings of TCPWM[1] 16-bit Counter 4 are shown below.
The Timer uses "TCPWM[1] 16-bit Counter 5 ". Enter the name as "Timer_1" and select "Timer-counter". For the Clock Signal, select "16 bit Divider 1 clk" which is also used for PWM_1. For Clock Prescaler, select "Divide by 64", and set Period = "3124 "(frequency 5Hz) and Compare = "1500"(time when sound is emitted). As outputs, we have confirmed that Overflow and Compare are given to the input of PWM_1. The settings are as follows.
Pin settings open "Pins" tab. Pin "Buzzer" selected Drive Mode = "Strong Drive, Input Buffer off" as P12[0]. Select the PWM output "TCPWM[1] 16-bit Counter 4 pwm (PWM_2)" as Digital Output. The settings are as follows.
The program just starts setting up the PWM and Timer as shown below.
#include "cy_pdl.h"
#include "cyhal.h"
#include "cybsp.h"
int main(void)
{
cy_rslt_t result;
/* Initialize the device and board peripherals */
result = cybsp_init() ;
if (result != CY_RSLT_SUCCESS)
{
CY_ASSERT(0);
}
Cy_TCPWM_Counter_Init(Timer_1_HW, Timer_1_NUM, &Timer_1_config);
Cy_TCPWM_PWM_Init(PWM_2_HW, PWM_2_NUM, &PWM_2_config);
Cy_TCPWM_Counter_Enable(Timer_1_HW, Timer_1_NUM);
Cy_TCPWM_PWM_Enable(PWM_2_HW, PWM_2_NUM);
__enable_irq();
Cy_TCPWM_TriggerStart(Timer_1_HW, Timer_1_MASK);
Cy_TCPWM_TriggerStart(PWM_2_HW, PWM_2_MASK);
for (;;)
{
}
}
This time, the point that Modus is inferior to Creator has appeared remarkably. If you are a Creator user or have done the previous Creator examples before, you may spend a hard time with Modus. The disadvantages of Modus will be largely eliminated if component settings can be expressed by a circuit like Creator instead of using the "Device Configuration" tool and an API that matches the component can be generated.
We are looking forward to an update to Modus Toolbox that addresses these issues.
Thanks,
Kenshow
Show Less
Hello,
This sample project is for testing WDT.
I assume three types of runaway.
1. Hard fault occurs because of Undefined instruction on RAM address 0x20000a00 then Execution of an Undefined instruction
2. Infinite loop on RAM address 0x20000a00
3. Jump to the area padded to 0x0000 of FLASH and execute NOP to the end of FLASH.
NOP execution from FLASH address 0x6800 to 0x7fff then Execution of an instruction from an XN memory address
The hardware fault handler CY_ISR(IntDefaultHandler) is in Cm0plusStart.c.
The hardware configuration is as follows.
LED11 blinks when WDT is cleared. When a reset occurs in WDT, LED9 is turned on for 10ms only.
When a reset occurs, the last byte of the array uint8 InitParameter[128] of raw unit located at address 0x1000 of FLASH memory is incremented.
To place the array uint8 InitParameter[128] at address 0x1000 in FLASH, "-Wl,--section-start=.FlashData=0x00001000" must be described in the Commander of Linker.
Please enable one of the following defines.
#define UndefInst // Undefined instruction on RAM address 0x20000a00 then Execution of an Undefined instruction
#define InfiLoop // Infinite loop on RAM address 0x20000a00
#define NopExecut // NOP execution from FLASH address 0x6800 to 0x7fff then Execution of an instruction from an XN memory
address
Best regards,
Yocchi
Show LessHello,
This test sample code is for bidirectional GPIO communication.
When actually using it, you should prepare the Direction signal and decide the side that controls the signal.
GPIO outputs are Open Drain.
PC terminal software receives and displays P0[3:0] and P1[6:3].
Best regards,
Yocchi
Show LessHi,
こんばんは、
As I have written some CLI programs, I wished that I want to enter a number as dec, hex, bin.
And if I can change the width(size) of a word it would be even better.
CLI タイプのサンプルを書いてきましたが、数字を入力するときに2進、10進、16進で入れることができたらと思っていました。
更に、ワードのサイズも変更できたら便利だろなと。
So I gave it a try using CY8CKIT-044.
Until I started debugging the program, I was thinking that this was going to be an easy project.
But PSoC 4 taught me that I was wrong.
I cheated by using my tty utils sample, but most of the newly written functions in the main.c should work with other MCUs, I hope.
Re: tty_utils a utility sample for CLI type program
というわけで、CY8CKIT-044 を使って試してみました。
デバッグを始めるまでは、これは簡単なサンプルになるだろうな~と、軽く構えていたのですが。
自分が間違っていたことを PSoC 4 に教えられました。
UART 入出力の分部は以前に書いた tty utils サンプルを使って楽をしましたが、今回 main.c 内で書いた新しい関数は
恐らく他の MCU でも機能するのではないかと思います。
Re: tty_utils a utility sample for CLI type program
Since this program was newly written, there may (must) be bugs.
In case you find one (or more), please notify me using the reply to this discussion.
(Although I can not guarantee how fast I will be able to fix it...)
如何せん書きたてのプログラムですので、バグがあるかも知れません (いや、きっとあるでしょう)。
もし、見つけられた方はレスで知らせていただけると幸いです。
(直ぐに治せるかについては保証できないのですが・・・)
Tera Term log
Schematic
Pins
main.c
==================
#include "project.h"
#include "stdio.h"
#include "tty_utils.h"
int word_width = 32 ;
void init_hardware(void)
{
tty_init() ;
CyGlobalIntEnable; /* Enable global interrupts. */
}
void usage(void)
{
print("=== usage ===\n\r") ;
print("Enter number as\n\r") ;
print("hex: Start with 0x or 0X \"0xABCD\"\n\r") ;
print("bin: Start with 0b or 0B \"0b1010\"\n\r") ;
print("dec: Start with 0d or 0D or number \"0d123\" or \"123\"\n\r") ;
print("width <word_width> : set word witdh\n\r") ;
print("help : show this message\n\r") ;
snprintf(str, STR_BUF_LEN, "Current word width = %d\n", word_width) ;
print(str) ;
print("=============\n\r") ;
}
void syntax_error(char *str)
{
print("Syntax Error: ") ;
print(str) ;
print("\n\r") ;
}
int32_t read_bin(char *str)
{
int32_t number = 0 ;
while(*str) {
number <<= 1 ;
switch(*str) {
case '0':
break ;
case '1':
number |= 1u ;
break ;
default:
syntax_error(str) ;
break ;
}
str++ ;
}
return( number ) ;
}
void get_word_width(char *str)
{
int tmp_width ;
sscanf(str, "%*s %d", &tmp_width) ;
if ((32 >= tmp_width) && (tmp_width >= 1)) {
print("Word Width is set to: ") ;
snprintf(str, STR_BUF_LEN, "%d\n\r", tmp_width) ;
print(str) ;
word_width = tmp_width ;
} else {
syntax_error(str) ;
}
return ;
}
void sign_extend(uint32_t *number, int word_width)
{
uint32_t sign ;
int i, up_to ;
sign = *number & (1u << (word_width - 1)) ;
if (8 >= word_width) {
up_to = 8 ;
} else if ((16 >= word_width)&&(word_width > 8)) {
up_to = 16 ;
} else {
up_to = 32 - word_width ;
}
for (i = 0 ; i < up_to ; i++ ) {
sign <<= 1 ;
*number |= sign ;
}
}
int32_t get_number(char *str)
{
int32_t number = 0 ;
if ((str[0] == '0') && (strlen(str) >= 3)) {
switch(str[1]) {
case 'b': // binary
case 'B':
number = read_bin(&str[2]) ;
break ;
case 'd': // decimal
case 'D':
sscanf(str, "%d", &number) ;
break ;
case 'x': // hexadecimal
case 'X':
sscanf(str, "%x", &number) ;
break ;
default:
syntax_error(str) ;
break ;
}
} else { /* default, trying as a decimal */
sscanf(str, "%d", &number) ;
}
return(number) ;
}
void print_as_bin(uint32_t unum)
{
uint32_t mask ;
mask = 1u << (word_width - 1) ;
while(mask) {
if (unum & mask) {
print("1") ;
} else {
print("0") ;
}
mask >>= 1 ;
}
}
void print_a_number(int32_t number)
{
uint32_t ext_num ;
char hex_format[32] ;
int hex_digit = 0 ;
int i ;
uint32_t mask = 0 ;
ext_num = number ;
print("Hex: ") ;
hex_digit = (word_width + 3) / 5 ;
snprintf(hex_format, 31, "0x%%0%dX ", hex_digit) ;
for (i = 0 ; i < word_width ; i++ ) {
mask <<= 1 ;
mask |= 1 ;
}
if (word_width > 16) {
snprintf(str, STR_BUF_LEN, hex_format, (uint32_t)(number & mask)) ;
} else if ((16 >= word_width)&&(word_width > 8)) {
snprintf(str, STR_BUF_LEN, hex_format, (uint16_t)(number & mask)) ;
} else if (8 >= word_width) {
snprintf(str, STR_BUF_LEN, hex_format, (uint8_t)(number & mask)) ;
}
print(str) ;
print("Unsigned: ") ;
if (word_width > 16) {
snprintf(str, STR_BUF_LEN, "%u ", (uint32_t)(number & mask)) ;
} else if ((16 >= word_width)&&(word_width > 8)) {
snprintf(str, STR_BUF_LEN, "%u ", (uint16_t)(number & mask)) ;
} else if (8 >= word_width) {
snprintf(str, STR_BUF_LEN, "%u ", (uint8_t)(number & mask)) ;
}
print(str) ;
print("Signed: ") ;
sign_extend(&ext_num, word_width) ;
if (word_width > 16) {
snprintf(str, STR_BUF_LEN, "%d ", (int32_t)ext_num) ;
} else if ((16 >= word_width)&&(word_width > 8)) {
snprintf(str, STR_BUF_LEN, "%d ", (int16_t)ext_num) ;
} else if (8 >= word_width) {
snprintf(str, STR_BUF_LEN, "%d ", (int8_t)ext_num) ;
}
print(str) ;
print("Bin: ") ;
print_as_bin(number) ;
print("\n\r") ;
}
int main(void)
{
int32_t number ;
init_hardware() ;
splash("tty get number test") ;
usage() ;
prompt() ;
for(;;) {
if (get_line()) { // got a line
switch(str[0]) {
case 'w': case 'W':
get_word_width(str) ;
break ;
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case '-':
number = get_number(str) ;
print_a_number(number) ;
break ;
case 'h': case 'H':
default:
usage() ;
break ;
}
prompt() ;
}
}
}
==================
21-Jul-2020
moto
Show LessHi Team,
I am trying duplex communication between Tera Term and (CY8CKIT-050)PSOC 5LP. I design PSoc Creater as below and written the below c program. Trying to communicate with tera term.
- Let me know whether the attached steps are correct or not?
- Why the serial port radio button is disabled?
I have attached all my design and code.
Thanks,
Abinash
Show LessHi all,
I used the piezoelectric buzzer on PSoC 6 before, so I made a similar one on PSoC 4.
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:
PSoC 4's TCPWM has a slightly different display of component settings than PSoC 6, but you can do the same thing. The LED pin is output to check the period. The piezoelectric buzzer is connected to Buzzer 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 creates period of buzzer with 1MHz of Clock_1 as the clock input. PWM_1 is a PWM (check "PMW" in Configuration" tab in the component), and it is divided into 10Hz (100mS) by Prescale=”x2” and Period=49999 in “PWM” tab. The settings are as follows.
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.
In order to control the output of PWM_2, a Start/Stop input is provided, and the edge of the input signal of line coming from PWM_1 is used as a trigger to start and stop with a trigger. The setting of PWM_2 is as follows.
Pin settings are output only. LED is set to P3[6] (green LED) and Buzzer pin is set to P3[0]. 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 settings are shown below.
The program in main.c is simple. Since the PWM is only started, the CPU will be released when the setting is completed. The program is below.
#include "project.h"
int main(void)
{
CyGlobalIntEnable; /* Enable global interrupts. */
/* Place your initialization/startup code here (e.g. MyInst_Start()) */
PWM_2_Start();
PWM_1_Start();
for(;;)
{
/* Place your application code here. */
}
}
Thanks,
Kenshow
Show LessI planned this program to show the minimum usage of the basic components of PSoC
and to help testing a new board or peripheral when application is yet to be written.
このプログラムは PSoC を初めて使用する方に基本的なコンポーネントの使い方をお見せするとともに
新しく作った基板や周辺をアプリケーションが出来る前に試せるようにと考えて作ってみました。
So I made versions of this program for PSoC 4 (CY8CKIT-044) and PSoC 6 (CY8CKIT-062-BLE).
MCU Tester, a Swiss Army Knife for PSoC (CY8CKIT-044 version)
MCU Tester, a Swiss Army Knife for PSoC (CY8CKIT-062-BLE version)
And finally, here is the version for PSoC 5LP!
このプログラムの PSoC 4 版と PSoC 6 版を先に公開していましたが
MCU Tester, a Swiss Army Knife for PSoC (CY8CKIT-044 version)
MCU Tester, a Swiss Army Knife for PSoC (CY8CKIT-062-BLE version)
やっと、PSoC 5LP 版にたどり着きました!
Initially I was planning to do PSoC4, PSoC 5LP and PSoC 6, but somehow I could not make the I2C of PSoC 5LP work this time.
So I assumed that my old CY8CKIT-059 is finally broken after all the abuse(s) and ordered a couple of new ones to DigiKey.
And alas, once new one arried, the old one decided that it's time to wake up !?
当初、PSoC 4, PSoC 5LP, そして PSoC 6 版と進めようと考えていたのですが、何故か今回は PSoC 5LP の I2C を動かすの苦労しました。
これは、今までだいぶいじめてきたので、CY8CKIT-059 が逝ったかなと思い DigiKey にお替りをお願いしました。
しかし、例によって新米が届いたとたんに古い方が、こりゃ仕事をしないとまずい!と思ったようで・・・
So the schematic(s)
tty
pwm
Note: Since LED was used for GPIO, I connected an external LED to P2[0] to test PWM.
May be you can change the pin to P2[1] to utilize the on board LED.
注: 基板上の LED は GPIO に割り当ててしまったので、外付けの LED でテストをしました。
基板上の LED を使用するのであれば、P2[1] をこちらに割り当てても良いかも知れません。
adc
i2c
gpio
spi
Pins
Tera Term logs
Opening
Mem (Memory)
i2c
"scan" command scans devices on the bus.
Here it found a device at 0x48, which was LM75B.
So I set the slave address to 0x48 using command "slave 0x48"
Unconditional reading a byte is "read", although I entered "read 0", 0 was igonared.
Register read is "readreg <reg address>"
Multiple registers read is "readregs <starting reg address> <number of registers to read>"
Since there is/are some sensor/devices which require some delay after receiving the address
and before preparing the data to be read, "delay" commands set delay in ms between them.
For devices such as temperature sensor and acceleration sensor etc,
utilizing "loop" and "interval" command will be used as below.
"scan" コマンドはバス上のデバイスをスキャンします。
ここでは接続してあった LM75B を見つけています。
“slave 0x48” コマンドでスレーブアドレスを 0x48 (LM75B) に設定しました。
I2C バスからの無条件読み込みは“read”コマンドで行えます。間違って“read 0” と入力しましたが 0 は無視されていました。
特定のレジスタ読みは“readreg <レジスタアドレス>”
複数レジスタの連続読出しは “readregs <開始レジスタアドレス> <読出しレジスタ数>”
デバイスによってはアドレスを受け取ってからデータを用意するのに時間が必要なものがありますので
“delay” コマンドで ms 単位の遅延を指定できます。
また、温度や加速度センサのように繰り返して同じレジスタを読みたいデバイスの場合には
“loop” と “interval” コマンドを使用することで下記のような動作をさせることが可能です。
adc
"measure" triggers a single shot measurement.
"repeat" triggers a "loop" number of measurements with "interval" ms.
I connected an external POT to test this.
"measure" コマンドで単発の測定を行います。
"repeat" コマンドでは "loop" で設定された回数の測定を"interval" で設定された間隔をあけて行います。
この実験は外部に POT を接続して行いました。
gpio
A digital input is assigned to the SW (P2[2]), and a digital output is assigned to the LED (P2[1]).
"write 1" turns the LED on, "write 0" turns it off.
"read" reads status of both input and output.
In the following example, I pushed and released the SW during the "repeat" loop.
SW (P2[2]) をデジタル入力に、LED (P2[1]) をデジタル出力に設定してあります。
"write 1" とすると LED が点灯し、"write 0" とすると消灯します。
"read" コマンドでは、入力、出力の両方を値を読み込んで表示します。
下記の例では、'repeat" によるループの間に SW を押したり離したりしています。
pwm
I set the PWM clock to 10kHz in the schematic.
So if period is 1000 and compare is 500, we see about 10Hz blinking.
With the period of 10000 and compare of 5000, we see about 1Hz blinking.
Then setting period to 100 and compare to 50 by using "config 100 50",
we get about 100 Hz blinking, which is intensity of the LED to my eyes.
So if I change compare to "0", LED turns of, then changing this value to 10, 50, 100,
the intensity gets brighter and brighter.
"stop" stops the PWM and the LED turns off.
"start" starts the PWM again.
回路図で PWM のクロックを 10kHz に設定しました。
周期を 1000 比較値を 500 に設定すると 10 Hz 程度の点滅になります。
周期を 10000 比較値を 5000 にすると 1Hz 程度の点滅となります。
また、"config 100 50" コマンドを使用して周期 100 比較値 50 とすると
だいたい 100 Hz の点滅になり、私の目には点灯が続いているようにしか見えなくなります。
ここで比較値を 0 にすると消灯になり、10, 50, 100 と上げていくと
徐々に明るさが増すように見えます。
"stop" コマンドは PWM を停止しますので、周期、比較値に関わらず LED は消灯します。
"start" コマンドで PWM は動作を開始します。
spi
At first I connected MISO to GND, "write 22" command read "0x00 (0)"
Then I connected MISO to VDD, "write 22" command read "0xFF (255)"
Finally I connected MISO to MOSI, "write 22" command read "0x16 (22)"
Then I set loop = 5, interval = 100, and "repeat 32" commands show the result below.
I expected to use this function to view the waveform with an oscilloscope.
Why the input value is in decimal format? Sorry I don't remember.
Maybe, in the future version I will change it but for the time being, it is as it is.
最初に MISO を GND に接続して、"write 22" コマンドを送ると RX は "0x00 (0)" でした。
次に MISO を VDD に接続して、"write 22" コマンドを送ると RX は "0xFF (255)" でした。
最後に MISO を MOSI に接続して、"write 22" コマンドを送ると RX は "0x16 (22)" でした。
loop = 5, interval = 100 の設定で、"repeat 32" コマンドを送ると下記の画面のような結果になりました。
この機能はオシロスコープで波形を観測することを想定して入れてみました。
入力値の書式が 十進になっている理由は? 失念しました。
将来、入力値の扱いを変えるかもしれませんが、これが現状の設定です。
27-May-2020
moto
P.S. On 16-Jul-2020 A bug found in I2C / readreg command, attached version is the fixed on (mcu_tester_059_200716)
Show LessHi,
Provided is a custom component, Sync32 v0.0, which can synchronize 1 to 32 digital signals to the rising edge of the clock. It can directly substitute standard Sync component while occupying much smaller footprint on the schematic. Component can can be applied to a single line, multiple lines or bus. Component can use internal clock (BUS_CLK or HFCLK), eliminating the need for external clock. Component is compatible with PSoC4 and PSoC5.
Attached archive contains the component library, a Datasheet and installation instructions. Upon installation it will show up in the folder /Community/Digital/Utilities/Sync/
The component provided as-is, no liabilities. It is free to use, modify and distribute.
/odissey1
Figure 1. Size comparison of the components: Left - standard Sync, Right - custom Sync32.
Typical application ideas of the Sync32 component:
Figure 2. Left - original schematic using standard Sync, Right - schematic modified using Sync32.
Figure 2. Left - original schematic using standard Sync, Right - schematic modified using Sync32.
Show Less