hello all,
i am a begineer to nordic . i am trying to read adc PCF8591 value through TWI and i want to print the value in UART TERMINAL.
i have written the code. but it is not fetching the values properly. i attached my code also . please say the solution for this.
Thanks in advance.
POGRAM:
#define SCL_PIN 2
#define SDA_PIN 3
#define SLAVE_ADDRESS 0x48
#define TWI_READ_BIT (0x01)
/* TWI instance. */
static const nrf_drv_twi_t twi_instace_pcf8591 = NRF_DRV_TWI_INSTANCE(0);
/*UART buffer size. */
#define UART_TX_BUF_SIZE 256
#define UART_RX_BUF_SIZE 1
/* Indicates if reading operation from PCF8591 has ended. */
static volatile bool m_xfer_done = true;
/* Indicates if setting mode operation has ended. */
static volatile bool m_set_mode_done = false;
typedef struct
{
uint32_t LDR;
uint32_t THERMISTER;
uint32_t TRIMPOD;
} sensors;
/*UART EVENT HANDLER*/
static 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;
default:
break;
}
}
//void PCF8591set_mode(void)
//{
// ret_code_t err_code;
// /* Writing to MMA7660_REG_MODE "1" enables the accelerometer. */
// uint8_t reg[2] = {0x01,0x41};
// err_code = nrf_drv_twi_tx(&twi_instace_pcf8591,0x48, reg, sizeof(reg), false);
// APP_ERROR_CHECK(err_code);
//
// while(m_set_mode_done == false);
//}
/*UART CONFIGURATION*/
static 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_Baud115200
};
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);
printf("\r\n UART CONFIGURATION DONE\n\r");
}
///**
// * @brief TWI events handler.
// */
void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
printf("\r\n IN TWI_HANDLER\r\n");
uint32_t err_code,count;
static sensors value;
printf("\r\n %d\r\n",p_event->xfer_desc.type);
switch(p_event->type)
{
case NRF_DRV_TWI_EVT_DONE:
printf("\r\n IN SWICH CASE\r\n");
if ((p_event->type == NRF_DRV_TWI_EVT_DONE) &&
(p_event->xfer_desc.type == NRF_DRV_TWI_XFER_TX))
{
if(m_set_mode_done != true)
{
m_set_mode_done = true;
return;
}
m_xfer_done = false;
/* Read 4 bytes from the specified address. */
err_code = nrf_drv_twi_rx(&twi_instace_pcf8591,0x48, (uint8_t*)&value, sizeof(value));
APP_ERROR_CHECK(err_code);
//printf("\r\n 1st time done\r\n");
}
else
{
//read_data(&m_sample);
err_code = nrf_drv_twi_rx(&twi_instace_pcf8591,0x42, (uint8_t*)&value, sizeof(value));
//nrf_twi_rxd_get (twi_instace_pcf8591);
printf("\r\n LDR VALUE=%d\r\n",value.LDR);
printf("\r\n THERMISTER VALUE=%d\r\n",value.THERMISTER);
printf("\r\n TRIMPOD VALUE=%d\r\n",(uint32_t)value.TRIMPOD);
nrf_delay_ms(1000);
m_xfer_done = true;
}
break;
default:
break;
}
}
/*TWI INITIALISATION*/
void twi_init (void)
{
uint32_t err_code;
uint8_t * pdata;
const nrf_drv_twi_config_t twi_adc_pcf8591_config = {
.scl = SCL_PIN,
.sda = SDA_PIN,
.frequency = NRF_TWI_FREQ_100K,
.interrupt_priority = APP_IRQ_PRIORITY_HIGH
};
err_code = nrf_drv_twi_init(&twi_instace_pcf8591,&twi_adc_pcf8591_config,twi_handler, NULL);
APP_ERROR_CHECK(err_code);
nrf_drv_twi_enable(&twi_instace_pcf8591);
printf("\r\n TWI IS INITIALIZED\r\n");
}
int main()
{
uart_config();
twi_init();
//PCF8591set_mode();
uint8_t reg=0;
ret_code_t err_code;
while(true)
{
nrf_delay_ms(100);
/* Start transaction with a slave with the specified address. */
do
{
__WFE();
}while(m_xfer_done == false);
err_code = nrf_drv_twi_tx(&twi_instace_pcf8591,0x48, ®, sizeof(reg), true);
APP_ERROR_CHECK(err_code);
m_xfer_done = true;
}
}