I am a beginner for ARM microcontroller, I am unable to generate the PWM wave and ADC simultaneously in the nRF51822, but both the things happening separately. I need the support for my issues and after then it should also work with the Bluetooth as well.
I am attaching the code:
#include <stdbool.h>
#include <stdint.h>
#include "nrf.h"
#include "app_error.h"
#include "bsp.h"
#include "nrf_delay.h"
#include "app_pwm.h"
#include "nrf_adc.h"
#include "boards.h"
#include "nrf_adc.h"
#include "app_uart.h"
volatile int32_t adc_sample;
#define UART_TX_BUF_SIZE 256 /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 1 /**< UART RX buffer size. */
#ifndef NRF_APP_PRIORITY_HIGH
#define NRF_APP_PRIORITY_HIGH 1
#endif
/**
* @brief UART events handler.
*/
void uart_events_handler(app_uart_evt_t * p_event)
{
switch (p_event->evt_type)
{
case APP_UART_COMMUNICATION_ERROR: APP_ERROR_HANDLER(p_event->data.error_communication);
break;
case APP_UART_FIFO_ERROR: APP_ERROR_HANDLER(p_event->data.error_code);
break;
case APP_UART_TX_EMPTY:
break;
default: break;
}
}
void ADC_IRQHandler(void)
{
nrf_adc_conversion_event_clean();
adc_sample = nrf_adc_result_get();
}
/**
* @brief UART initialization.
*/
void uart_config(void)
{
uint32_t err_code;
const app_uart_comm_params_t comm_params =
{
RX_PIN_NUMBER,
TX_PIN_NUMBER,
RTS_PIN_NUMBER,
CTS_PIN_NUMBER,
APP_UART_FLOW_CONTROL_DISABLED,
false,
UART_BAUDRATE_BAUDRATE_Baud38400
};
APP_UART_FIFO_INIT(&comm_params,
UART_RX_BUF_SIZE,
UART_TX_BUF_SIZE,
uart_events_handler,
APP_IRQ_PRIORITY_LOW,
err_code);
APP_ERROR_CHECK(err_code);
}
/**
* @brief ADC initialization.
*/
void adc_config(void)
{
const nrf_adc_config_t nrf_adc_config = NRF_ADC_CONFIG_DEFAULT;
// Initialize and configure ADC
nrf_adc_configure( (nrf_adc_config_t *)&nrf_adc_config);
nrf_adc_input_select(NRF_ADC_CONFIG_INPUT_2);
nrf_adc_int_enable(ADC_INTENSET_END_Enabled << ADC_INTENSET_END_Pos);
NVIC_SetPriority(ADC_IRQn, NRF_APP_PRIORITY_HIGH);
NVIC_EnableIRQ(ADC_IRQn);
}
APP_PWM_INSTANCE(PWM2,1); // Create the instance "PWM1" using TIMER1.
static volatile bool ready_flag; // A flag indicating PWM status.
void pwm_ready_callback(uint32_t pwm_id) // PWM callback function
{
ready_flag = true;
}
int main(void)
{
ret_code_t err_code;
/* 2-channel PWM, 200Hz, output on DK LED pins. */
app_pwm_config_t pwm1_cfg = APP_PWM_DEFAULT_CONFIG_2CH(10L, BSP_LED_0, BSP_LED_1);
/* Switch the polarity of the second channel. */
pwm1_cfg.pin_polarity[1] = APP_PWM_POLARITY_ACTIVE_HIGH;
/* Initialize and enable PWM. */
err_code = app_pwm_init(&PWM2,&pwm1_cfg,pwm_ready_callback);
APP_ERROR_CHECK(err_code);
app_pwm_enable(&PWM2);
uint32_t value;
adc_config();
uart_config();
printf("\n\rADC HAL simple example\r\n");
printf("Current sample value:\r\n");
while(true)
{
value = 60; // Duty cycle range 0-100.
ready_flag = false;
/* Set the duty cycle - keep trying until PWM is ready... */
while (app_pwm_channel_duty_set(&PWM2, 0, value) == NRF_ERROR_BUSY);
/* ... or wait for callback. */
while(!ready_flag);
APP_ERROR_CHECK(app_pwm_channel_duty_set(&PWM2, 1, value));
// trigger next ADC conversion
nrf_adc_start();
// enter into sleep mode
__SEV();
__WFE();
__WFE();
nrf_delay_ms(100);
printf("%d\r\n", (int)adc_sample); // out ADC result
}
}
/** @} */