实现NST1002 传感器读取温度

This commit is contained in:
helloyifa 2025-04-10 11:15:25 +08:00
parent 3fceda8bf1
commit b79043acf2
4 changed files with 70 additions and 24 deletions

View File

@ -39,7 +39,7 @@ void ble_send_data(u8 *sdata,u8 len)
{
//先查明当前连接的conn_handle
u16 connection_handle = ble_comm_dev_get_handle(0, GATT_ROLE_SERVER);
printf("connection_handle: %04x\n", connection_handle);
//printf("connection_handle: %04x\n", connection_handle);
if(connection_handle == 0)//无连接
{
return;
@ -54,7 +54,16 @@ void ble_send_data(u8 *sdata,u8 len)
}
void ble_receive_callback(u8* data,u16 len){
printf("ble_receive_callback: %s\n", data);
printf("ble_receive_callback (rx:%d): %s\n",len, data);
if(strstr(data,"AT+GETTEMP=?") != NULL){
printf("111\n");
int detect_temp = 365;
u8 str_get_temp[24] = {0};
sprintf(str_get_temp,"AT+GETTEMP=%d",detect_temp);
ble_send_data(&str_get_temp,strlen(str_get_temp));
}else{
printf("222\n");
}
}
@ -200,21 +209,30 @@ static uint8_t reversal(uint8_t data)
delay_ms(15);
//--------------send start convert -----------------------------
gpio_direction_output(DQ_PIN, 0);//pin_write(DQ_PIN, PIN_LOW); // DQ low
delay_us(300);
//printf("send start convert out 0\r\n");
delay_ms(5);
gpio_direction_output(DQ_PIN, 1);//pin_write(DQ_PIN, PIN_HIGH);
delay_us(10);
//printf("send start convert out 1\r\n");
udelay(20);
//-----------------check DQ pin status should be high---------------------------------------
gpio_set_direction(DQ_PIN,1);//pin_mode(DQ_PIN, PIN_MODE_INPUT);
delay_us(100);
gpio_set_direction(DQ_PIN,1);
gpio_set_die(DQ_PIN,1);
//gpio_set_direction(DQ_PIN,1);//pin_mode(DQ_PIN, PIN_MODE_INPUT);
//gpio_set_pull_up(DQ_PIN, 1);
//printf("check DQ pin status should be high cur dq:%d:\r\n" ,pin_read(DQ_PIN));
//delay_ms(30);
if (pin_read(DQ_PIN) == 0)
{
printf("check DQ pin == 0 should be high!!\r\n");
*pcal = (uint16_t)(ERR_NC * 128);
return -1;
}
delay_ms(20);
//printf("check DQ pin status pin_read(DQ_PIN) == 1 \r\n");
//delay_ms(20);
//--------------wait for done pulse---------------
printf("wait for done pulse ... \r\n");
do
{
bit0 = pin_read(DQ_PIN);
@ -223,22 +241,25 @@ static uint8_t reversal(uint8_t data)
if (retry > 20000) // time out 40ms
{
*pcal = (uint16_t)(ERR_DONE * 128);
printf("wait for done pulse fail \r\n");
return -2;
}
} while (bit0);
printf("wait for done pulse succ \r\n");
delay_us(10);
//--------------read 24bit data---------------
gpio_set_direction(DQ_PIN,0);
gpio_direction_output(DQ_PIN, 1);//pin_write(DQ_PIN, PIN_HIGH);
//pin_mode(DQ_PIN, PIN_MODE_OUTPUT_OD);
delay_us(200);
//delay_us(200);
for (i = 0; i < 24; i++)
{
gpio_direction_output(DQ_PIN, 0);//pin_write(DQ_PIN, PIN_LOW); // Falling edge
delay_us(1);// Adjust according to the mcu clock cycle,It's not necessary
gpio_set_direction(DQ_PIN,1);//pin_mode(DQ_PIN, PIN_MODE_INPUT);
gpio_set_die(DQ_PIN,1);
//gpio_set_pull_up(DQ_PIN, 1);
// If the output bit is 0, the DQ will remain low for about 14us
for (j = 0; j < 4; j++)
// It's not necessary to read it 4 timers , adjust according to the mcuclock cycle
@ -277,11 +298,16 @@ static uint8_t reversal(uint8_t data)
}
}
char err_code[4][20] = {"NOT Connected", "No DONE signal", "DAT Recv Err", "CRC Err"};
void read_temp(void)
void nst1002_read_temperatura(void)
{
int err, ei;
double dtmp;
float dtmp;
int16_t cal;
// NTC VDD
gpio_set_direction(IO_PORTB_05,0);
gpio_direction_output(IO_PORTB_05, 1);
err = nst1002_read_cal((uint16_t *)&cal);
if (err < 0)
{
@ -290,15 +316,19 @@ static uint8_t reversal(uint8_t data)
printf("NST1002 err %s\r\n", err_code[ei]);
return;
}
dtmp = cal / 128.0;
printf("Temp %3.7f\r\n", dtmp);
//printf("cal %d\r\n", cal);
dtmp = cal / 128.0f;
int temp_value = dtmp*100;
//printf("Temp %3.7f\r\n", dtmp);
printf("Temp: %d\r\n\r\n", temp_value);
gpio_direction_output(IO_PORTB_05, 0);
}
void temperature_detect(void)
{
u8 str_data [24] ={0};
sprintf(str_data,"%s","hello");
ble_send_data(&str_data,5);
read_temp();
nst1002_read_temperatura();
}
#endif
@ -336,11 +366,26 @@ void temperature_init(void)
printf("vbat_trim:%d...vbg_trim:%d", get_vbat_trim(), get_vbg_trim());
// 添加以上代码看打印值如果为15跟63那就要用烧录器先烧录一遍。
// NTC VDD
gpio_direction_output(IO_PORTB_05, 1);
sys_s_hi_timer_add(NULL, temperature_blink_led, 200);
sys_s_hi_timer_add(NULL, temperature_detect, 1000);
//sys_s_hi_timer_add(NULL, temperature_detect, 2000);
sys_timer_add(NULL, temperature_detect, 200);
// NTC VDD
gpio_set_direction(IO_PORTB_05,0);
gpio_direction_output(IO_PORTB_05, 1);
/*
delay_ms(100);
gpio_set_direction(IO_PORTB_04,1);
gpio_set_die(IO_PORTB_04,1);
delay_ms(100);
printf("check DQ pin status cur dq:%d:\r\n" ,pin_read(IO_PORTB_04));
gpio_set_pull_up(IO_PORTB_04, 1);
delay_ms(100);
printf("check DQ pin status cur dq:%d:\r\n" ,pin_read(IO_PORTB_04));
*/
}
void temperature_deinit(void)

View File

@ -467,6 +467,7 @@ void _adc_init(u32 sys_lvd_en)
//helloyifa
//以下初始化代码添加到void adc_init()里
#if 0
#define CH_SAMPLE_FREQ 1000 //ms
u32 gpio = IO_PORTB_04;//指定IO要和ADC通道对应上
gpio_set_pull_down(gpio, 0);//看需求是否需要开内部下拉会有20%的误差,不同芯片理论电阻值不一样,看规格书说明
@ -490,7 +491,7 @@ void _adc_init(u32 sys_lvd_en)
adc_add_sample_ch(ch_adc_power);//添加检测通道
//adc_set_sample_freq(ch, CH_SAMPLE_FREQ);//设置固定的采样频率,一般不设置,就是最高的轮询时间
#endif
request_irq(IRQ_SARADC_IDX, 0, adc_isr, 0);

Binary file not shown.