ARM C++ Programming Reference

Useful coding references for nRF52840 microcontrollers

Variable Declaration

Variable
signed char,   int8_t
unsigned char, uint8_t
signed short,  int16_t
unsigned short,uint16_t
signed int,    int32_t
unsigned int,  uint32_t
signed long,    int64_t
unsigned long,  uint64_t

Useful Basic Function

nrf_delay_ms(500);                 //delay in msec
bsp_board_init(BSP_INIT_LEDS);     //initialising LED
bsp_board_led_invert(0);           //toggle LED
nrf_gpio_pin_set(LED_1); or bsp_board_led_on(0);
nrf_gpio_pin_clear(LED_1); or bsp_board_led_off(0);
LED_1 = NRF_GPIO_PIN_MAP(0,13)

UART

//UART initialisation
const app_uart_comm_params_t comm_params =
      {
          RX_PIN_NUMBER,
          TX_PIN_NUMBER,
          RTS_PIN_NUMBER,
          CTS_PIN_NUMBER,
          UART_HWFC,
          false,
#if defined (UART_PRESENT)
          NRF_UART_BAUDRATE_115200
#else
          NRF_UARTE_BAUDRATE_115200
#endif
      };

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);

uint8_t cr;           //declare byte
app_uart_put(cr);     //send byte
app_uart_get(&cr);    //get byte
printf("\r\nUART example started.\r\n");     //print to UART

nRF5x Project Structure

All the function is written in a manner that is portable and general purpose coding. This may make it very difficult to read and understand. But basically, it works like PIC microcontroller where each pin or peripheral can be individually configure.

The entry point to the program is at “main.h” -> “int main(void)”.

Hardware specific “fixed” configuration are all done in “pca10056.h“. All pins input/output definition are configured in this file which is very specific to this designed pca10056 circuit board.

“boards.c” contains generic codes for managing the LED and Push Buttons codes.

Project (Solution) specific codes will be under the Application folder. Usually consist of “main.c” where the code starts, and “sdk_config.h” where are the configuration of the application will be.

Peripheral useful for projects

More information of the examples project can be found at
https://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk5.v13.0.0%2Fexamples.html

More information about the library used in the example, refer to the SDK Library reference notes,
https://infocenter.nordicsemi.com/index.jsp?topic=%2Fstruct_sdk%2Fstruct%2Fsdk_nrf5_latest.html

  • Digital IO pins (project “blinky“)
  • Pin Change Interrupt (project “pin_change_int“)
  • UART (project “uart“, “serial“)
  • Timer (project “timer“, “gpiote“)
  • Pin Change Interrupt (project “pin_change_int”)
  • Power Management (project “pwr_mgmt”)
  • SPI (project “spi“)
  • PWM (project “led_softblink“, “low_power_pwm“)
  • Watch Dog Timer (project “wdt”)
  • Low Energy Bluetooth BLE
  • USB BLE UART
  • USB HID

Pin maps for the nRF52840-DK board examples demonstration

PeripheralsPin out in the nRF52840 example demo
LED1P0.13
LED2P0.14
LED3P0.15
LED4P0.16
SW1P0.11
SW2P0.12
SW3P0.24
SW4P0.25
UART-RXP0.08
UART-TXP0.06

<- Back to Bluetooth Resources Page