自制室内温度仪-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 4848|回复: 2

自制室内温度仪

[复制链接]
发表于 2013-8-25 18:36 | 显示全部楼层 |阅读模式
一、      准备工作
1、        Arduino UNO

2、        LM35线性温度传感模块

3、        12864LCD模块

二、      搭线
总体图:

1) CS——8                  //片选 低有效
2) RST——9          //给芯片复位,低有效,如果不需软件给芯片复位,可不连接
3) A0——10             //数据和命令选择,L 命令 H  数据
4) CLK——13          //时钟
5) MOSI——11        //数据
6) VCC——5V
7) GND——GND         //电源接线
8) LED——背光选择     //低有效
          LM35连接示意图:

效果图:

三、      代码调试

[mw_shl_code=c,true]#include "U8glib.h"
U8GLIB_MINI12864 u8g(13, 11, 8, 10, 9); //SPI Com: SCK = 13, MOSI = 11, CS = 8, A0 = 10 ,RST=9;
int potPin = 4;
float temperature = 0;
long val=0;

void draw(char * text) {
  u8g.setFont(u8g_font_unifont);
  u8g.drawStr( 0, 33, text);
}

void setup(void) {
  if ( u8g.getMode() == U8G_MODE_R3G3B2 )
    u8g.setColorIndex(255);
  else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
    u8g.setColorIndex(3);
  else if ( u8g.getMode() == U8G_MODE_BW )
    u8g.setColorIndex(1);
  pinMode(4,OUTPUT);
  Serial.begin(9600);
}

char * floatToString(char * outstr, float value, int places, int minwidth=0, bool rightjustify=false) {
    // this is used to write a float value to string, outstr.  oustr is also the return value.
    int digit;
    float tens = 0.1;
    int tenscount = 0;
    int i;
    float tempfloat = value;
    int c = 0;
    int charcount = 1;
    int extra = 0;
    // make sure we round properly. this could use pow from <math.h>, but doesn't seem worth the import
    // if this rounding step isn't here, the value  54.321 prints as 54.3209

    // calculate rounding term d:   0.5/pow(10,places)  
    float d = 0.5;
    if (value < 0)
        d *= -1.0;
    // divide by ten for each decimal place
    for (i = 0; i < places; i++)
        d/= 10.0;   
    // this small addition, combined with truncation will round our values properly
    tempfloat +=  d;

    // first get value tens to be the large power of ten less than value   
    if (value < 0)
        tempfloat *= -1.0;
    while ((tens * 10.0) <= tempfloat) {
        tens *= 10.0;
        tenscount += 1;
    }

    if (tenscount > 0)
        charcount += tenscount;
    else
        charcount += 1;

    if (value < 0)
        charcount += 1;
    charcount += 1 + places;

    minwidth += 1; // both count the null final character
    if (minwidth > charcount){        
        extra = minwidth - charcount;
        charcount = minwidth;
    }

    if (extra > 0 and rightjustify) {
        for (int i = 0; i< extra; i++) {
            outstr[c++] = ' ';
        }
    }

    // write out the negative if needed
    if (value < 0)
        outstr[c++] = '-';

    if (tenscount == 0)
        outstr[c++] = '0';

    for (i=0; i< tenscount; i++) {
        digit = (int) (tempfloat/tens);
        itoa(digit, &outstr[c++], 10);
        tempfloat = tempfloat - ((float)digit * tens);
        tens /= 10.0;
    }

    // if no places after decimal, stop now and return

    // otherwise, write the point and continue on
    if (places > 0)
    outstr[c++] = '.';


    // now write out each decimal place by shifting digits one by one into the ones place and writing the truncated value
    for (i = 0; i < places; i++) {
        tempfloat *= 10.0;
        digit = (int) tempfloat;
        itoa(digit, &outstr[c++], 10);
        // once written, subtract off that digit
        tempfloat = tempfloat - (float) digit;
    }
    if (extra > 0 and not rightjustify) {
        for (int i = 0; i< extra; i++) {
            outstr[c++] = ' ';
        }
    }


    outstr[c++] = '\0';
    return outstr;
}

void loop(void) {
  val=analogRead(potPin);
  temperature = (val*0.0048828125*100);
  Serial.print("Tep=  ");
  Serial.print(temperature);
  Serial.println(" C");
  char buffer[25];
  digitalWrite(4,LOW);
  u8g.firstPage();  
  do {
    draw(floatToString(buffer, temperature , 2));
  } while( u8g.nextPage() );
  delay(500);
}[/mw_shl_code]


Arduino uno

Arduino uno
2.jpg

12864LCD模块

12864LCD模块
S}8RI_M2M]1SK%U32T5_9]2.jpg

LM35连接示意图

LM35连接示意图

最终效果图

最终效果图
发表于 2013-10-5 23:34 | 显示全部楼层
谢谢楼主,终于把我的那个12864驱动起来了
 楼主| 发表于 2013-10-8 16:13 | 显示全部楼层
,继续努力,我好久不摸arduino了,我会回来的。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-12-29 23:18 , Processed in 0.116785 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表