【国民技术N32G457评测】RT_Thread ADC 之电压表
本帖最后由 lugl4313820 于 2022-1-29 23:33 编辑<p>今天用RT_Thread Studio 做一个电压表。工程是基于原来的SSD1306来建立的,这里主要讲一下ADC的工程如何建立,代码的书写。</p>
<p>1、打开RT_Thread Setttings 打开ADC选项:</p>
<p>打开:DS_N32G457系列数据手册V1.2.1.pdf,找到ADC1 通道2 即PA1。</p>
<p></p>
<p>在main.c中添加adc读取线程:</p>
<p>main.c如下:</p>
<pre>
<code>#include <stdint.h>
#include <rtthread.h>
#include <rtdevice.h>
#include "ssd1306.h"
#include <stdio.h>
/* defined the LED1 pin: PB5 */
#define LED1_PIN 91
#define ADC_DEV_NAME "adc1"
#define ADC_DEV_CHANNEL2//PA1
#define THREAD_PRIORITY 20
#define THREAD_STACK_SIZE 1025
#define THREAD_TIMESLICE 10
static rt_thread_t tid_ssd1306_display = RT_NULL;
void adc_temp_sample(void *parameter)
{
rt_adc_device_t adc_temp_dev;
rt_uint32_t value, vol;
double temperate;
rt_err_t ret = RT_EOK;
char v = {'0','.','0','0','0','V'};
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 = value *(3.30 / 4096.0)*1000;
vol = (rt_uint32_t)temperate;
ssd1306_Fill(Black);
ssd1306_SetCursor(2, 26);
v = vol/1000 + '0';
v = vol/100%10 +'0';
v = vol/10%10 +'0';
v = vol%10 +'0';
ssd1306_WriteString(v, Font_16x26, White);
ssd1306_UpdateScreen();
rt_kprintf("the value is :%s \n\r", v);
rt_thread_mdelay(100);
}
}
int main(void)
{
uint32_t Speed = 200;
/* set LED1 pin mode to output */
rt_pin_mode(LED1_PIN, PIN_MODE_OUTPUT);
ssd1306_Init();
rt_kprintf("start ssd1306\r\n");
ssd1306_Fill(White);
ssd1306_UpdateScreen();
ssd1306_Fill(Black);
ssd1306_UpdateScreen();
ssd1306_Fill(Black);
ssd1306_SetCursor(2,16);
ssd1306_WriteString("START", Font_16x26, White);
ssd1306_UpdateScreen();
tid_ssd1306_display = rt_thread_create("th_ssd",
adc_temp_sample, RT_NULL,
THREAD_STACK_SIZE,
THREAD_PRIORITY, THREAD_TIMESLICE);
/* 如果获得线程控制块,启动这个线程 */
if (tid_ssd1306_display != RT_NULL)
rt_thread_startup(tid_ssd1306_display);
while (1)
{
rt_pin_write(LED1_PIN, PIN_LOW);
rt_thread_mdelay(Speed);
rt_pin_write(LED1_PIN, PIN_HIGH);
rt_thread_mdelay(Speed);
}
}</code></pre>
<p>总的来说用RT—Thread Studio 生成ADC取值,还是很简单的。从视频的检测结果来看,与FLUK 18B+的相比,高了大约0.05V左右 (后面我量了一下参考电压只有3.24V左右,可能是这个原因造成的吧),当然也没有做平均,是单次采样的显示。结果还是令人满意的。</p>
<p> 今天本来是想先用ADC来测量片内的温度(ADC1 16通道),但是测出来的值是零,没有跑成功。用官方提供的KEIL的例程是可以测出来的。还没有找到具体原因,翻了一下STM32的资料,是讲要较对一下,看了rt_thread的n32g45x.adc,drv_adc.c,adc.c也没找到解决方法。如果官方技术上班,还请技术指导一下。</p>
<p>附上视频:</p>
<p><iframe allowfullscreen="true" frameborder="0" height="510" src="https://training.eeworld.com.cn/shareOpenCourseAPI?isauto=true&lessonid=32431" style="background:#eee;margin-bottom:10px;" width="700"></iframe><br />
</p>
<p> </p>
<p>顶一下,整的不错</p>
<p> </p>
Jacktang 发表于 2022-1-30 08:32
顶一下,整的不错
<p>谢谢支持!还需要继续努力呀!生命不息,学习不断!</p>
页:
[1]