Want to put UART receiving data to array

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

cross mob
AbPa_4654881
Level 3
Level 3
10 replies posted 10 questions asked 10 likes given

Hi all,

I am new to cypress community,I am currently working with CY8Ckit-059 psoc kit. The task is I want to send a .txt file that contain 1 and 0 using the uart. that I am able to send by UART_Getchar function and by using tera term application.

As my .txt file contain Example: 1000100101 so according to this stream

1st letter is : 1 so i want to drive led_1 on some pin and

2nd letter is : 0 so I want to drive led_2 on some pin

similarly for 3rd bit : i want to drive led_1 and for 4th bit : i want to drive led_2 and so on

so I am thinking like If I can save 2 bytes(1st letter,2nd letter) of data to an array and then read from array to drive the led_1 and led_2 according to values.can anyone please help me regarding this? 

I have attached the code that i m trying to do but that thing is not working properly

#include "project.h"

char datarecive[2];

CY_ISR(InterruptRX)

  {

    int i =0;

    for (i=0;i<2;i++)

    {

        datarecive=UART_GetChar();

    }

}

int main()

{

    CyGlobalIntEnable;

    UART_Start();

   isrRX_StartEx(InterruptRX);

for(;;)

{

/////////////////////////////////////////////////////// FOR LED_1 drive ///////////////////////////////////////////////////////////

       if (datarecive[0]=='1')

        {

            LED_1_Write(1);

         }

       

     if (datarecive[0]=='0')/* Place your application code here. */

        {

            LED_1_Write(0);

         }

/////////////////////////////////////////////////////// FOR LED_2 drive ///////////////////////////////////////////////////////////

       if (datarecive[1]=='1')

        {

            LED_2_Write(1);

         }

       

     if (datarecive[1]=='0')/* Place your application code here. */

        {

            LED_2_Write(0);

         }

}

0 Likes
1 Solution
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,

To make it a little more robust, I'd like to use my tty_utility.

Re: tty_utils a utility sample for CLI type program

I'm using a ring-buffer style read and write data from UART.

Hopefully this should be better than the previous one, but because of the nature of UART,

probably we may not be able to expect perfect.

For even more robust/reliable communication you must introduce the concept of protocol

as well as error correction. But let's make it outside the scope of this discussion.

(I need to go to bed soon)

schematic

031-schematic.JPG

pins

032-pins.JPG

main.c

NOTE: This time I'm using my utility tty_utils.h and tty_utils.c

so main.c is much simpler now.

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

#include "project.h"

#include "tty_utils.h"

#define LED_ON  (0u)

#define LED_OFF (1u)

#define printc(c) UART_PutChar(c)

void init_hardware(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */ 

    tty_init() ;

}

void doLEDs(uint8_t data)

{

    if (data & 0x01) {

        LED_1_Write(LED_ON) ;

        printc('A') ;

    } else {

        LED_1_Write(LED_OFF) ;

        printc('B') ;

    }

    if (data & 0x02) {

        LED_2_Write(LED_ON) ;

        printc('C') ;

    } else {

        LED_2_Write(LED_OFF) ;

        printc('D') ;

    }

}

int main(void)

{

    int led_no = 0 ;

    int data ;

  

    init_hardware() ;

    splash("UART to 2 LEDs test ver.3") ;

    for(;;)

    {

        if (get_string()) { /* get_string receives a word in str[] */

            switch(str[0]) { /* str[0] is the first letter */

            case '0': doLEDs(0) ; break ;

            case '1': doLEDs(1) ; break ;

            case '2': doLEDs(2) ; break ;

            case '3': doLEDs(3) ; break ;

            default:

                print("Syntax Error: ") ;

                print(str) ;

                print("\n\r") ;

                break ;

            }

        }

    }

}

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

Tera Term log

030-TeraTerm-log.JPG

moto

View solution in original post

4 Replies
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,

To specify the number of LED only by the location does not sound safe,

as once the sequence gets disturbed, you can not tell which is the LED1 and LED2.

So at least we would like to have some kind of start mark and/or end mark

or at least make it easier to distinguish the LED number.

Having said that, I tried to implement your data format "01001101111..." as version 1

And for the version 2, I introduced a quick-dirty format "0 1 2 3 "

schematic

001-schematic.JPG

pins

NOTE: Since CY8CKIT-059 has only 1 on board LED, I set LED_1 for the on board LED

   and I just assigned LED_2 to P3[7]. You can change LED_1 and LED_2 where-ever you like

   as far as the pins are GPIOs.

002-pins.JPG

main.c

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

#include "project.h"

#include "stdio.h"

#define LED_ON  (0u)

#define LED_OFF (1u)

#define STR_LEN 32

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

void    print(char *str)

{

    UART_PutString(str) ;

}

void cls(void)

{

    print("\033c") ; /* reset */

    CyDelay(20) ;

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

    CyDelay(20) ;

}

void splash(char *prog_name)

{

    cls() ;

    if (prog_name && *prog_name) {

        print(prog_name) ;

    }

    print(" (") ;

    print(__DATE__) ;

    print(" ") ;

    print(__TIME__) ;

    print(")\n") ;

}

volatile int num_received = 0 ;

volatile unsigned char datareceive[2] ;

CY_ISR(InterruptRX)

{

    isr_1_ClearPending() ;

    if (num_received < 2) {

        datareceive[num_received] = UART_GetChar() ;

        num_received++ ;

    }

}

void init_hardware(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */  

   

    isr_1_ClearPending() ;

    isr_1_StartEx(InterruptRX) ;

   

    UART_Start() ;

}

void doLEDs(uint8_t data[])

{

    print(" LED1 : ") ;

    if (data[0] == '1') {

        LED_1_Write(LED_ON) ;

        print("ON  ") ;

    } else {

        LED_1_Write(LED_OFF) ;

        print("OFF ") ;

    }

    print("LED2 : ") ;

    if (data[1] == '1') {

        LED_2_Write(LED_ON) ;

        print("ON  ") ;

    } else {

        LED_2_Write(LED_OFF) ;

        print("OFF ") ;

    }

    print("\r\n") ;

}

int main(void)

{

    int led_no = 0 ;

   

    init_hardware() ;

    splash("UART to 2 LEDs test ver.1") ;

    for(;;)

    {

        if (num_received == 2) {

            doLEDs(datareceive) ;

            num_received = 0 ;

        }

    }

}

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

Tera Term output

NOTE: If you don't want to see "LED1: ON..", comment out print()s in do_LEDs() function.

000-TeraTerm-log.JPG

Now, since you are using 2 bytes for 2 LEDs, I came up with following protocol.

Letter1 { 0 | 1 | 2 | 3 }, Letter2 delimiter

So the input will look like

0 1 2 3

When "0 ", which is '0' + ' ', both LEDs are off

When "1 ", LED1 is ON, LED2 is OFF

When "2 ", LED1 is OFF, LED2 is ON

When "3 ", LED1 is ON , LED2 is OFF

schematic and pins are same with above.

main.c

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

#include "project.h"

#include "stdio.h"

#define LED_ON  (0u)

#define LED_OFF (1u)

#define STR_LEN 32

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

void    print(char *str)

{

    UART_PutString(str) ;

}

void cls(void)

{

    print("\033c") ; /* reset */

    CyDelay(20) ;

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

    CyDelay(20) ;

}

void splash(char *prog_name)

{

    cls() ;

    if (prog_name && *prog_name) {

        print(prog_name) ;

    }

    print(" (") ;

    print(__DATE__) ;

    print(" ") ;

    print(__TIME__) ;

    print(")\n") ;

}

volatile int num_received = 0 ;

volatile unsigned char datareceive[2] ;

CY_ISR(InterruptRX)

{

    isr_1_ClearPending() ;

    if (num_received < 2) {

        datareceive[num_received] = UART_GetChar() ;

        num_received++ ;

    }

}

void init_hardware(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */  

   

    isr_1_ClearPending() ;

    isr_1_StartEx(InterruptRX) ;

   

    UART_Start() ;

}

void doLEDs(uint8_t data)

{

    print(" LED1 : ") ;

    if (data & 0x01) {

        LED_1_Write(LED_ON) ;

        print("ON  ") ;

    } else {

        LED_1_Write(LED_OFF) ;

        print("OFF ") ;

    }

    print("LED2 : ") ;

    if (data & 0x02) {

        LED_2_Write(LED_ON) ;

        print("ON  ") ;

    } else {

        LED_2_Write(LED_OFF) ;

        print("OFF ") ;

    }

    print("\r\n") ;

}

int main(void)

{

    int led_no = 0 ;

    int data ;

   

    init_hardware() ;

    splash("UART to 2 LEDs test ver.2") ;

    for(;;)

    {

        if (num_received == 2) {

            sscanf(datareceive, "%d", &data) ;

            doLEDs(data) ;

            num_received = 0 ;

        }

    }

}

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

Tera Term log

NOTE: If you don't want to see "LED1: ON..", comment out print()s in do_LEDs() function.

010-TeraTerm-log.JPG

moto

0 Likes

Thanku very much MoTa_728816...U saved my day I m using 2nd approach that is much better way...actully my .txt file is something big around 1232KB. as I m sending this file over UART so I am putting UART_PutChar function to verify my complete file is transfered or not.. but after transmission of complete file also I m getting some loss in data at receiver. can U please help me How I can avoid loss of Data over UART  by this?

as I m putting UART_PutChar putting Like this then I am counting this UART_Putchar  thing by copying from teraterm screen.

void doLEDs(uint8_t data)

{ings by tA

//    print(" LED1 : ") ;

    if (data & 0x01) {

        LED_1_Write(LED_ON) ;

       UART_Putchar("A");

//      print("ON  ") ;

    } else {

        LED_1_Write(LED_OFF) ;

        UART_Putchar("B");

//       print("OFF ") ;

    }

//    print("LED2 : ") ;

    if (data & 0x02) {

        LED_2_Write(LED_ON) ;

       UART_Putchar("C");

//        print("ON  ") ;

    } else {

        LED_2_Write(LED_OFF) ;

       UART_Putchar("D");

//       print("OFF ") ;

    }

}

0 Likes
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,

To make it a little more robust, I'd like to use my tty_utility.

Re: tty_utils a utility sample for CLI type program

I'm using a ring-buffer style read and write data from UART.

Hopefully this should be better than the previous one, but because of the nature of UART,

probably we may not be able to expect perfect.

For even more robust/reliable communication you must introduce the concept of protocol

as well as error correction. But let's make it outside the scope of this discussion.

(I need to go to bed soon)

schematic

031-schematic.JPG

pins

032-pins.JPG

main.c

NOTE: This time I'm using my utility tty_utils.h and tty_utils.c

so main.c is much simpler now.

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

#include "project.h"

#include "tty_utils.h"

#define LED_ON  (0u)

#define LED_OFF (1u)

#define printc(c) UART_PutChar(c)

void init_hardware(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */ 

    tty_init() ;

}

void doLEDs(uint8_t data)

{

    if (data & 0x01) {

        LED_1_Write(LED_ON) ;

        printc('A') ;

    } else {

        LED_1_Write(LED_OFF) ;

        printc('B') ;

    }

    if (data & 0x02) {

        LED_2_Write(LED_ON) ;

        printc('C') ;

    } else {

        LED_2_Write(LED_OFF) ;

        printc('D') ;

    }

}

int main(void)

{

    int led_no = 0 ;

    int data ;

  

    init_hardware() ;

    splash("UART to 2 LEDs test ver.3") ;

    for(;;)

    {

        if (get_string()) { /* get_string receives a word in str[] */

            switch(str[0]) { /* str[0] is the first letter */

            case '0': doLEDs(0) ; break ;

            case '1': doLEDs(1) ; break ;

            case '2': doLEDs(2) ; break ;

            case '3': doLEDs(3) ; break ;

            default:

                print("Syntax Error: ") ;

                print(str) ;

                print("\n\r") ;

                break ;

            }

        }

    }

}

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

Tera Term log

030-TeraTerm-log.JPG

moto

Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

AbPa,

Here is a link to a similar discussion and example code included.  Create a function to light different LEDs

Len

Len
"Engineering is an Art. The Art of Compromise."