A simple UART sample without using interrupt for CY8CKIT-044

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

こんにちは、

This morning, I wrote a simple uart sample without using interrupt for CY8CKIT-059 in the discussion below.

今朝、下記のスレッドで CY8CKIT-059 用に割込みを使わない簡単な UART のサンプルを書いてみました。

Re: UART Mapping with SWD/JTAG in CY8CKIT-059 PSoC® 5LP Prototyping Kit

Somehow, I liked the code as a sample, I also ported it to CY8CKIT-044.

そのコードがなんとなく気に入ったので、CY8CKIT-044 にも移植してみました。

As this sample is not using a user-level interrupt for UART, it would be easier to read.

But I wold think that for a real applications, using an interrupt will be better.

このサンプルは割込みを使用していないので、読みやすいと思います。

しかし、本格的なアプリケーションではやはり割込みを使用した方が良いとも思います。

When you compile and run the program, it will look like the picture below.

Only when you type "on", the Green LED on the board lights.

A the string entered will be displayed so that you can be sure that the program is receiving the input.

プログラムをコンパイルして実行すると、下記の様になります。

入力に on と打ち込んだ時だけ、基板上の緑色LEDが点灯します。

そして入力された文字列も表示されますので、ちゃんとプログラムが文字列を受け取っていることが確認できます。

001-TeraTerm-log.JPG

schematic / 回路図

002-schematic.JPG

pin / ピンアサイン

003-pins.JPG

main.c

==========================

#include "project.h"

#include "stdio.h"

#define STR_LEN 32

#define BS      '\b'

#define CR      '\r'

#define NL      '\n'

#define LED_ON  0u

#define LED_OFF 1u

void print(char *str)

{

    UART_UartPutString(str) ;

}

void printc(char c)

{

    UART_UartPutChar(c) ;

}

int main(void)

{

    int     str_received = 0 ;

    uint8_t c ;

    char    str[STR_LEN+1] ; /* 1 for NULL */

    int     index = 0 ;

   

    CyGlobalIntEnable; /* Enable global interrupts. */

    LED_Write(LED_OFF) ;

   

    UART_Start() ;

    print("\x1b[2J\x1b[;H") ; /* clear screen */

    print("A Simple UART Test on CY8CKIT-044 ") ;   

    snprintf(str, STR_LEN, "(%s %s)\n\r", __DATE__, __TIME__) ;

    print(str) ;

    print("> ") ;

    for(;;)

    {

        if ((str_received == 0)&&(UART_SpiUartGetRxBufferSize() > 0)) { /* received some lstters */

            c = UART_UartGetByte() ;

            switch(c) {

            case BS: /* backspace */

                if (index > 0) {

                    index-- ;

                    str[index] = 0 ;

                    print("\b \b") ; /* BS, space, BS */

                }

                break ;

            case CR:

            case NL:

                str[index] = 0 ; /* terminate the string */

                index = 0 ;

                str_received = 1 ;

                printc(c) ;

                break ;

            default:

                str[index] = c ;

                index++ ;

                if (index >= STR_LEN) { /* buffer overflow */

                    str[STR_LEN] = 0 ;

                    index = 0 ;

                    str_received = -1 ;

                }

                printc(c) ;

                break ;

            }

        }

       

        if (str_received) { /* a string is received (or overflow) */

            if (strcmp(str, "on") == 0) {

                LED_Write(LED_ON) ;

            } else {

                LED_Write(LED_OFF) ;

            }

            print("String Received: ") ;

            print(str) ;

            print("\n\r") ;

           

            str_received = 0 ;

            print("> ") ;

        }

    }

}

/* [] END OF FILE */

==========================

moto

0 Likes
1 Reply
BragadeeshV
Moderator
Moderator
Moderator
First question asked 1000 replies posted 750 replies posted

Hi MoTa_728816​,

Thank you for your contribution

Regards,

Bragadeesh

Regards,
Bragadeesh