附件是基于Nr51822平台的简单演示代码,读取芯片内部温度,通过串口打印出来。适合初学者。
int main(void)
{
ppi_timer_init();
ppi_init();
start_ppi();
simple_uart_config(0xff,9,0xff,11,false);
uart_exit();
m_temp_init();
while(1)
{
task_temperature();
}
}
void m_temp_init(void)
{
nrf_temp_init();
}
void task_temperature(void)
{
char buf[30];
uint8_t len;
int32_t volatile temp;
NRF_TEMP->TASKS_START = 1; /** Start the temperature measurement. */
/* Busy wait while temperature measurement is not finished, you can skip waiting if you enable interrupt for DATARDY event and read the result in the interrupt. */
/*lint -e{845} // A zero has been given as right argument to operator '|'" */
while (NRF_TEMP->EVENTS_DATARDY == 0)
{
// Do nothing.
}
NRF_TEMP->EVENTS_DATARDY = 0;
/**@note Workaround for PAN_028 rev2.0A anomaly 29 - TEMP: Stop task clears the TEMP register. */
temp = nrf_temp_read();
/**@note Workaround for PAN_028 rev2.0A anomaly 30 - TEMP: Temp module analog front end does not power down when DATARDY event occurs. */
NRF_TEMP->TASKS_STOP = 1; /** Stop the temperature measurement. */
sprintf(buf,"%d.%d C\r\n",temp/4,temp%4*25);
simple_uart_putstring((const uint8_t*)buf);
nrf_delay_ms(1000);
}
效果: