งานปฏิบัติ 14
งานปฏิบัติ 14
การอ่านค่าอุณหภูมิด้วยDS18820
Code 1
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 11
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float Tfloat;
LiquidCrystal_I2C lcd(0x27,16,2);
void setup(void)
{
sensors.begin();
lcd.begin();
Serial.begin(9600);
}
void loop(void)
{
sensors.requestTemperatures();
Tfloat = sensors.getTempCByIndex(0);
Serial.print(Tfloat);
lcd.setCursor(0,0);
lcd.print("T = ");
lcd.print(Tfloat,4);
delay(250);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Code 2
#include <LedControl.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 11
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float Tfloat;
long Tint;
LedControl lc=LedControl(8,10,9,1);
// Pin 8->DIN, 10->CLK, 9->CS(LOAD), 1 = No.of devices
void show6digit(long num)
{
int seg1,seg2,seg3,seg4,seg5,seg6;
seg6 = (num/100000);
seg5 = ((num%100000)/10000);
seg4 = (((num%100000)%10000)/1000);
seg3 = ((((num%100000)%10000)%1000)/100);
seg2 = (((((num%100000)%10000)%1000)%100)/10);
seg1 = (((((num%100000)%10000)%1000)%100)%10);
lc.setDigit(0,0,seg1,false);
lc.setDigit(0,1,seg2,false);
lc.setDigit(0,2,seg3,false);
lc.setDigit(0,3,seg4,false);
lc.setDigit(0,4,seg5,true);
if (num>=100000)
lc.setDigit(0,5,seg6,false);
delay(300);
}
void setup(void)
{
sensors.begin();
lc.shutdown(0,false);
lc.setIntensity(0,5);
lc.clearDisplay(0);
}
void loop(void)
{
sensors.requestTemperatures();
Tfloat = sensors.getTempCByIndex(0);
Tint = long(Tfloat*10000);
lc.clearDisplay(0);
show6digit(Tint);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Code 3
#include <LedControl.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 11
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
float Tfloat;
long Tint;
LedControl lc=LedControl(8,10,9,1);
// Pin 8->DIN, 10->CLK, 9->CS(LOAD), 1 = No.of devices
void show6digit(int num)
{
int seg1,seg2;
seg2 = (((((num%100000)%10000)%1000)%100)/10);
seg1 = (((((num%100000)%10000)%1000)%100)%10);
lc.setDigit(0,0,seg1,false);
if (num>=10)
lc.setDigit(0,1,seg2,false);
delay(300);
}
void setup(void)
{
sensors.begin();
lc.shutdown(0,false);
lc.setIntensity(0,5);
lc.clearDisplay(0);
}
void loop(void)
{
sensors.requestTemperatures();
Tfloat = sensors.getTempCByIndex(0);
Tint = int(Tfloat);
lc.clearDisplay(0);
show6digit(Tint);
}
}
ความคิดเห็น
แสดงความคิดเห็น