这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » 国产MCU » c函数中读取cpu温度OK527N-C

共3条 1/1 1 跳转至

c函数中读取cpu温度OK527N-C

工程师
2025-02-20 11:02:03     打赏

有了之前的经验,要读取cpu温度就简单很多了,用户手册中命令行功能测试中有说明怎么测试cpu温度。

image.png

下面将其改到c函数中

int read_CpuTemp(void)
{
    int fd;  
    char buffer[1024];  
    int bytesRead;

    fd = open("/sys/class/thermal/thermal_zone0/temp", O_RDONLY);  
    if (fd == -1) {  
        perror("Error opening file");  
        return EXIT_FAILURE;  
    }  

    // 读取文件内容  
    bytesRead = read(fd, buffer, sizeof(buffer) - 1);  
    if (bytesRead == -1) {  
        perror("Error reading file");  
        close(fd);  
        return EXIT_FAILURE;  
    }
    printf("%d bytes read)\\n", bytesRead);
    buffer[bytesRead] = '\\0';  
    int temp = atoi(buffer);
    return temp;
}

编译成app后传到开发板并运行

chmod +x app
./app

执行结果如下:
image.png

至此完成cpu温度的读取,可惜板子上没有温湿度传感器,不能读取环境温度。





关键词: 函数     温度     OK527N-C    

专家
2025-02-20 20:32:20     打赏
2楼

感谢分享


专家
2025-02-20 20:36:58     打赏
3楼

感谢分享


共3条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]