Suhu dan Kelembaban Ruang dapat diukur dengan sensor SHT11. Sensor ini sudah dilengkapi modul dengan keluaran data digital. Komunikasinya adalah dengan TWI, SCK dan DATA. Sensor ini dihubungkan langsung dengan 2 pin mikrokontroler untuk pembangkit pulsa clock dan datanya. Ini gambar komunikasinya
Dan gambar di bawah ini adalah timing diagram komunikasinya untuk ambil data RH.
Kalo pengen ambil data suhu tinggal ganti command seperti di datasheet. Nah ini dia program mikronya. Saya pake ATMEGA16.
/*****************************************************
This program was produced by the
CodeWizardAVR V1.25.3 Standard
Automatic Program Generator
© Copyright 1998-2007 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 3/24/2010
Author : dlogog
Company : ITS
Comments:
Chip type : ATmega16
Program type : Application
Clock frequency : 11.059200 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
#include
#include
#include
#define SDAout PORTB.0
#define SDAin PINB.0
#define SCLK PORTB.1
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include
// Declare your global variables here
unsigned char timeout,ackbit;
unsigned int dataread,suhuSHT,RHSHT;
void start (void)
{ DDRB=0x03;
SDAout=1;
SCLK=0;
SCLK=1;
SDAout=0;
SCLK=0;
SCLK=1;
SDAout=1;
SCLK=0;
DDRB=0x03;
}
void reset (void)
{ unsigned char i;
DDRB=0x03;
SDAout=1;
SCLK=0;
for (i=0;i<=8;i++) { SCLK=1; SCLK=0; } start(); DDRB=0x03; } void tunggu (void) { unsigned char i; DDRB=0x02; SDAout=1; for (i=0;i<255;i++) { timeout=SDAin; if (timeout==0) break; delay_ms(1); } DDRB=0x02; } void perintah (unsigned char data) { unsigned char i; DDRB=0x03; for (i=0;i<8;i++) { if ((data>>7)==1) SDAout=1;
else SDAout=0;
SCLK=1;
SCLK=0;
data<<=1;
}
SDAout=1;
SCLK=1;
DDRB=0x02;
ackbit=SDAin;
SCLK=0;
DDRB=0x03;
}
void baca (void)
{ unsigned char i;
dataread=0x00;
DDRB=0x02;
for (i=0;i<8;i++)
{ dataread<<=1;
SCLK=1;
dataread|=SDAin;
SCLK=0;
}
DDRB=0x03;
if (ackbit==1) SDAout=1;
else SDAout=0;
SCLK=1;
SCLK=0;
SDAout=1;
DDRB=0x03;
}
void bacasuhu (void)
{
start();
perintah(0x03);
if (ackbit==0)
{
tunggu();
if (timeout==0)
{ ackbit=0;
baca();
suhuSHT=dataread;
suhuSHT<<=8;
ackbit=1;
baca();
suhuSHT|=dataread;
}
}
}
void bacaRH (void)
{
start();
perintah(0x05);
if (ackbit==0)
{
tunggu();
if (timeout==0)
{ ackbit=0;
baca();
RHSHT=dataread;
RHSHT<<=8;
ackbit=1;
baca();
RHSHT|=dataread;
}
}
}
void main(void)
{
unsigned char suhu[16];
unsigned char lembab[16];
float RHlin,RHtrue,temp;
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// LCD module initialization
lcd_init(16);
lcd_gotoxy(0,0);
lcd_putsf("coba");
delay_ms(300);
while (1)
{
reset();
bacasuhu();
temp=(-40)+(0.01*(float)suhuSHT);
sprintf(suhu,"suhu = %0.02f",temp);
lcd_gotoxy(0,0);
lcd_puts(suhu);
delay_ms(100);
reset();
bacaRH();
RHlin=((float)RHSHT*(float)RHSHT*(-0.0000028))+(0.0405*(float)RHSHT)-4;
RHtrue=(temp-25)*(0.01+(0.00008*(float)RHSHT))+RHlin;
sprintf(lembab,"RH = %0.02f",RHtrue);
lcd_gotoxy(0,1);
lcd_puts(lembab);
delay_ms(100);
reset();
// Place your code here
};
}
Yaa..ini cuma secarik cerita yang pernah tak jalani..smoga bermanfaat..
Dan gambar di bawah ini adalah timing diagram komunikasinya untuk ambil data RH.
Kalo pengen ambil data suhu tinggal ganti command seperti di datasheet. Nah ini dia program mikronya. Saya pake ATMEGA16.
/*****************************************************
This program was produced by the
CodeWizardAVR V1.25.3 Standard
Automatic Program Generator
© Copyright 1998-2007 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 3/24/2010
Author : dlogog
Company : ITS
Comments:
Chip type : ATmega16
Program type : Application
Clock frequency : 11.059200 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
#include
#include
#include
#define SDAout PORTB.0
#define SDAin PINB.0
#define SCLK PORTB.1
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include
// Declare your global variables here
unsigned char timeout,ackbit;
unsigned int dataread,suhuSHT,RHSHT;
void start (void)
{ DDRB=0x03;
SDAout=1;
SCLK=0;
SCLK=1;
SDAout=0;
SCLK=0;
SCLK=1;
SDAout=1;
SCLK=0;
DDRB=0x03;
}
void reset (void)
{ unsigned char i;
DDRB=0x03;
SDAout=1;
SCLK=0;
for (i=0;i<=8;i++) { SCLK=1; SCLK=0; } start(); DDRB=0x03; } void tunggu (void) { unsigned char i; DDRB=0x02; SDAout=1; for (i=0;i<255;i++) { timeout=SDAin; if (timeout==0) break; delay_ms(1); } DDRB=0x02; } void perintah (unsigned char data) { unsigned char i; DDRB=0x03; for (i=0;i<8;i++) { if ((data>>7)==1) SDAout=1;
else SDAout=0;
SCLK=1;
SCLK=0;
data<<=1;
}
SDAout=1;
SCLK=1;
DDRB=0x02;
ackbit=SDAin;
SCLK=0;
DDRB=0x03;
}
void baca (void)
{ unsigned char i;
dataread=0x00;
DDRB=0x02;
for (i=0;i<8;i++)
{ dataread<<=1;
SCLK=1;
dataread|=SDAin;
SCLK=0;
}
DDRB=0x03;
if (ackbit==1) SDAout=1;
else SDAout=0;
SCLK=1;
SCLK=0;
SDAout=1;
DDRB=0x03;
}
void bacasuhu (void)
{
start();
perintah(0x03);
if (ackbit==0)
{
tunggu();
if (timeout==0)
{ ackbit=0;
baca();
suhuSHT=dataread;
suhuSHT<<=8;
ackbit=1;
baca();
suhuSHT|=dataread;
}
}
}
void bacaRH (void)
{
start();
perintah(0x05);
if (ackbit==0)
{
tunggu();
if (timeout==0)
{ ackbit=0;
baca();
RHSHT=dataread;
RHSHT<<=8;
ackbit=1;
baca();
RHSHT|=dataread;
}
}
}
void main(void)
{
unsigned char suhu[16];
unsigned char lembab[16];
float RHlin,RHtrue,temp;
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// LCD module initialization
lcd_init(16);
lcd_gotoxy(0,0);
lcd_putsf("coba");
delay_ms(300);
while (1)
{
reset();
bacasuhu();
temp=(-40)+(0.01*(float)suhuSHT);
sprintf(suhu,"suhu = %0.02f",temp);
lcd_gotoxy(0,0);
lcd_puts(suhu);
delay_ms(100);
reset();
bacaRH();
RHlin=((float)RHSHT*(float)RHSHT*(-0.0000028))+(0.0405*(float)RHSHT)-4;
RHtrue=(temp-25)*(0.01+(0.00008*(float)RHSHT))+RHlin;
sprintf(lembab,"RH = %0.02f",RHtrue);
lcd_gotoxy(0,1);
lcd_puts(lembab);
delay_ms(100);
reset();
// Place your code here
};
}
Yaa..ini cuma secarik cerita yang pernah tak jalani..smoga bermanfaat..