This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Suggest changes in the UART code.

I am working on small UART project. I am begineer coder. 

I want to transmit 0-9 numbers using UART. I am using nRF52832 board and IDE using is Eclipse pca10040. I applied my logic and written code for the same. But still code not working properly. When transmitted data equals to received data then LED should blink if not LED should be OFF.

Here is my code. Please suggest changes. I am on deadline to complete this code. 

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "app_uart.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "nrf.h"
#include "bsp.h"



#define MAX_TEST_DATA_BYTES     (15U)
#define UART_TX_BUF_SIZE 256
#define UART_RX_BUF_SIZE 256



//  UART error handler function.


void uart_error_handle(app_uart_evt_t * p_event)
{
    if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_communication);
    }
    else if (p_event->evt_type == APP_UART_FIFO_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_code);
    }
}




// LED glow function: When data Transmitted is equal to data Received then LED glow up else stays off.


void led_blink()
{

    bsp_board_leds_init();


    while (true)
    {
        for (int i = 0; i < 1; i++)
        {
            bsp_board_led_invert(i);
            nrf_delay_ms(500);
        }
    }
}

//   UART transmit function.


 #if ((Enable_UART_TxDecimalNumber==1) || (Enable_UART_TxFloatNumber==1))


void UART_Tx(uint32_t dec_no, uint8_t nod)
{
		uint8_t i=0;

		printf("\nEnter decimal no to transmit:");
		scanf("\n%d", &dec_no);

		if(dec_no==0)																			//decimal number to transmit
		{
			for(i=0; ((i<nod) && (i<max_dttx)); i++)											//number of digits to transmit
			a[i] = 0x00;																		//maximum number of digits to transmit
		}
		else if
		{
			for(i=0; i<nod; i++)
			{
				if(dec_no!=0)
				{
					a[i] = util_GetMod32(dec_no,10);
					dec_no = dec_no/10;
				}


		else if( (nod == dttx) || (nod > max_dttx))												//total numbers to transmit
			{
				break;
			}
		else
			{
				a[i]=0;
			}
		}
	}

	while(i)
	{
		UART_TxChar(util_Dec2Ascii(a[i-1]));													//Convert decimal to ASCII
		i--;
	}

	void led_blink();
}

#endif



//  main Function.


int main(void)
{
    uint32_t err_code;

    bsp_board_leds_init();

    const app_uart_comm_params_t comm_params =
      {
          RX_PIN_NUMBER,
          TX_PIN_NUMBER,
          RTS_PIN_NUMBER,
          CTS_PIN_NUMBER,
          APP_UART_FLOW_CONTROL_ENABLED,
          false,
          UART_BAUDRATE_BAUDRATE_Baud19200
      };

    APP_UART_FIFO_INIT(&comm_params,
                         UART_RX_BUF_SIZE,
                         UART_TX_BUF_SIZE,
                         uart_error_handle,
                         APP_IRQ_PRIORITY_LOWEST,
                         err_code);

    APP_ERROR_CHECK(err_code);


    UART_Tx();



	return 0;
}






Related