【国民技术N32G457评测】RTC + 温度计
[复制链接]
评测国民技术N32G457快一个月了,今天把前面几个项目整合了一下,合并成一个显示日历+温度,前面准备做一个电子烟测试系统的,因为手头上的硬件全部发走了,所以就没有能够展示。
温度测量是用一个100K的NTC电阻+100K分压电阻组成的测度电路。LCD屏是1.8寸128*160的TFT LCD屏,9.9元买的,分辨率有点低。
RTC是RT—thread Studio的软RTC实现。
具体配如置如下:
把ADC、ST7735文件夹放到packages下面。
ntc.c
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-02-13 Administrator the first version
*/
#include "ntc.h"
#include <stdio.h>
#include <stdint.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <rtconfig.h>
#include <drv_adc.h>
/*************************************************************
函数: static int16_t get_ntc_temperature(uint16_t ad_data)
功能:
参数: ad_data: 的ADC采集到的NTC电路分压AD值
返回: 根据单位换算成摄氏度/华氏度,返回温度值*10
描述: 100K与NTC串联,接到3V3电源上
**************************************************************/
static int16_t get_ntc_temperature(uint16_t ad_data)
{
float Rntc,t;
uint8_t index;
Rntc = 100.0*ad_data/(4095 - ad_data);//求出Rntc阻值,单位为Komh
for(index = 0;index < (N-1);index++)
{
if(Rntc > ntc[index])
{
break;
}
}
if(index <= 0) //temp lower than -20℃
{
t = -20.0; //rail at -20℃
}
else
if(index >= (N-1)) //temp higher than 170℃
{
t = 170.0; //rail at 170℃
}
else //temp between -20℃ and 170℃
{
t = (-20 + index) - (Rntc-ntc[index])/(ntc[index]-ntc[index-1]);
}
t= t-3;
t = t*100; // 温度扩大10倍
return (int16_t)t; // 类型转换为int16_t
}
char v[6] = {'0','0','.','0','0','C'};
void adc_temp_sample(void)
{
rt_adc_device_t adc_temp_dev;
rt_uint32_t value, vol;
double temperate;
rt_err_t ret = RT_EOK;
adc_temp_dev = (rt_adc_device_t)rt_device_find(ADC_DEV_NAME);
if(adc_temp_dev == RT_NULL)
{
rt_kprintf("adc_temp run failed ! can't find %s device!\n",ADC_DEV_NAME);
//return RT_ERROR;
}
/*使能设备 */
ret = rt_adc_enable(adc_temp_dev, ADC_DEV_CHANNEL);
if(ret == RT_EOK)
{
rt_kprintf("adc sample run success! find %s device!\n", ADC_DEV_NAME);
}
while(1)
{
value = rt_adc_read(adc_temp_dev, ADC_DEV_CHANNEL);
temperate = get_ntc_temperature((rt_uint32_t)value); //得到气温X10
vol = (rt_uint32_t)temperate;
v[0] = vol/1000 + '0';
v[1] = vol/100%10 +'0';
v[3] = vol/10%10 +'0';
v[4] = vol%10 +'0';
//LCD_ShowIntNum(20,110,vol,4,BLACK,WHITE,16);
//LCD_ShowString(20,110,v, BROWN ,WHITE,16,0);
rt_thread_mdelay(500);
}
}
ntc.h
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-02-13 Administrator the first version
*/
#ifndef PACKAGES_ADC_NTC_H_
#define PACKAGES_ADC_NTC_H_
#include <stdio.h>
#define ADC_DEV_NAME "adc1"
#define ADC_DEV_CHANNEL 2 //PA1
#define N 191 //NTC温度对应阻值个数,即ntc[]数组大小
const static float ntc[N]=
{
//-20℃ //-19 //-18 //-17 //-16 //-15 //-14 //-13 //-12 //-11
975.8038,920.5962,868.8615,820.3603,774.8710,732.1889,692.1238,654.4999,619.1540,585.9346,
//-10℃ //-9 //-8 //-7 //-6 //-5 //-4 //-3 //-2 //-1
554.7016,525.3245,497.6821,471.6621,447.1599,424.0781,402.3264,381.8204,362.4818,344.2375,
//0℃ //1 //2 //3 //4 //5 //6 //7 //8 //9
327.0195,310.7640,295.4121,280.9084,267.2014,254.2428,241.9877,230.3940,219.4224,209.0361,
//10℃ //11 //12 //13 //14 //15 //16 //17 //18 //19
199.2007,189.8841,181.0559,172.6881,164.7540,157.2290,150.0898,143.3144,136.8825,130.7749,
//20℃ //21 //22 //23 //24 //25 //26 //27 //28 //29
124.9734,119.4612,114.2223,109.2417,104.5053,100.0000,95.7132, 91.6333, 87.7492, 84.0505,
//30℃ //31 //32 //33 //34 //35 //36 //37 //38 //39
80.5274, 77.1707, 73.9717, 70.9222, 68.0144, 65.2411, 62.5954, 60.0707, 57.6610, 55.3604,
//40℃ //41 //42 //43 //44 //45 //46 //47 //48 //49
53.1635, 51.0651, 49.0602, 47.1443, 45.3130, 43.5621, 41.8878, 40.2862, 38.7539, 37.2876,
//50℃ //51 //52 //53 //54 //55 //56 //57 //58 //59
35.8842, 34.5405, 33.2538, 32.0214, 30.8408, 29.7096, 28.6253, 27.5860, 26.5895, 25.6338,
//60℃ //61 //62 //63 //64 //65 //66 //67 //68 //69
24.7171, 23.8376, 22.9937, 22.1836, 21.4061, 20.6594, 19.9424, 19.2537, 18.5920, 17.9562,
//70℃ //71 //72 //73 //74 //75 //76 //77 //78 //79
17.3452, 16.7578, 16.1930, 15.6499, 15.1276, 14.6251, 14.1417, 13.6764,13.2286, 12.7976,
//80℃ //81 //82 //83 //84 //85 //86 //87 //88 //89
12.3825, 11.9828, 11.5978, 11.2270, 10.8697, 10.5254, 10.1935, 9.8736, 9.5652, 9.2678,
//90℃ //91 //92 //93 //94 //95 //96 //97 //98 //99
8.9809, 8.7042, 8.4373, 8.1797, 7.9312, 7.6912, 7.4596, 7.2360, 7.0201, 6.8115,
//100℃ //101 //102 //103 //104 //105 //106 //107 //108 //109
6.6101, 6.4155, 6.2274, 6.0457, 5.8701, 5.7003, 5.5362, 5.3775, 5.2240, 5.0755,
//110℃ //111 //112 //113 //114 //115 //116 //117 //118 //119
4.9319, 4.7930, 4.6586, 4.5285, 4.4026, 4.2807, 4.1627, 4.0484, 3.9378, 3.8306,
//120℃ //121 //122 //123 //124 //125 //126 //127 //128 //129
3.7268, 3.6263, 3.5289, 3.4345, 3.3430, 3.2543, 3.1683, 3.0850, 3.0042, 2.9258,
//130℃ //131 //132 //133 //134 //135 //136 //137 //138 //139
2.8498, 2.7761, 2.7045, 2.6352, 2.5678, 2.5025, 2.4391, 2.3775, 2.3178, 2.2598,
//140℃ //141 //142 //143 //144 //145 //146 //147 //148 //149
2.2034, 2.1487, 2.0956, 2.0440, 1.9939, 1.9452, 1.8978, 1.8518, 1.8071, 1.7637,
//150℃ //151 //152 //153 //154 //155 //156 //157 //158 //159
1.7215, 1.6804, 1.6405, 1.6017, 1.5640, 1.5273, 1.4915, 1.4568, 1.4230, 1.3901,
//160℃ //161 //162 //163 //164 //165 //166 //167 //168 //169
1.3582, 1.3270, 1.2967, 1.2672, 1.2385, 1.2106, 1.1833, 1.1568, 1.1310, 1.1059,
//170℃
1.0814
};
extern char v[6] ;
void adc_temp_sample();
#endif /* PACKAGES_ADC_NTC_H_ */
然后main.c如下:
#include <stdint.h>
#include <rtthread.h>
#include <rtdevice.h>
#include "lcd_init.h"
#include "lcd.h"
#include "pic.h"
#include "ntc.h"
#include <time.h>
/* defined the LED1 pin: PB5 */
#define LED1_PIN 91
#define THREAD_PRIORITY 25
#define THREAD_STACK_SIZE 512
#define THREAD_TIMESLICE 5
extern char v[6] ;
static void rtc_sample(void)
{
time_t now;
char da_buf[12];
char time_buf[12];
struct tm *t;
while(1)
{
now = time(RT_NULL);
t=localtime(&now);
rt_sprintf(da_buf, "%d-%2d-%2d",(t->tm_year)+1900,(t->tm_mon)+1,t->tm_mday);
rt_sprintf(time_buf,"%2d:%2d:%2d",t->tm_hour,t->tm_min,t->tm_sec);
LCD_ShowString(24,110,da_buf, BLACK ,WHITE,16,0);
LCD_ShowString(30,90,time_buf, BLACK ,WHITE,16,0);
LCD_ShowString(10,56,v,BLACK,WHITE,32,0);
rt_thread_mdelay(100);
}
}
int main(void)
{
uint32_t Speed = 200;
static rt_thread_t tid1 = RT_NULL;
static rt_thread_t tid_adc = RT_NULL;
rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
int day;
LCD_Init();//LCD初始化
LCD_Fill(0,0,LCD_W,LCD_H,WHITE);
LCD_ShowString(10,0,"RTTDEMO",BLUE,WHITE,32,0);
LCD_ShowString(24,36,"N32G45XVL",RED,WHITE,16,0);
//LCD_ShowPicture(74,110,40,40,gImage_1);
/* 创建线程1,名称是thread1,入口是thread1_entry*/
tid1 = rt_thread_create("thread1",
rtc_sample, RT_NULL,
THREAD_STACK_SIZE,
THREAD_PRIORITY, THREAD_TIMESLICE);
/* 如果获得线程控制块,启动这个线程 */
if (tid1 != RT_NULL)
rt_thread_startup(tid1);
tid_adc = rt_thread_create("thread2",
adc_temp_sample, RT_NULL,
THREAD_STACK_SIZE,
THREAD_PRIORITY, THREAD_TIMESLICE);
/* 如果获得线程控制块,启动这个线程 */
if (tid_adc != RT_NULL)
rt_thread_startup(tid_adc);
while (1)
{
rt_pin_write(LED1_PIN, PIN_LOW);
rt_thread_mdelay(Speed);
rt_pin_write(LED1_PIN, PIN_HIGH);
rt_thread_mdelay(Speed);
}
}
效里如下:
|