Home > Avr, Microcontroller > AVR SHT 11 (Temperature & Humidity Sensor)

AVR SHT 11 (Temperature & Humidity Sensor)

February 19th, 2010 nashrul Leave a comment Go to comments

/***************************************************************************

File : sht-11.c

Description : Main module

Project : SHT 11 Temperature & Humidity Sensor

Version : 2.0.1

Author : nashrul@nashrul.co.cc

Date : 2010

***************************************************************************/

#include

#include

#include

#include

#asm

.equ __lcd_port=0×15 ;PORTC

#endasm

#define io DDRB.1

#define sda_out PORTB.1

#define sda_in PINB.1

#define sck PORTB.0

#define temp 0b00000011

#define humi 0b00000101

#define read_reg 0b00000111

#define write_reg 0b00000110

union data_int{unsigned char c[sizeof unsigned int];unsigned int t;}sht;

unsigned char lcd[20],msbt,lsbt,msbh,lsbh,value;

unsigned int temperatur,humidity;

void initial()

{

io=1;

sda_out=1; //Initial state

sck=0;

delay_us(2);

sck=1;

delay_us(2);

sda_out=0;

delay_us(3);

sck=0;

delay_us(5);

sck=1;

delay_us(2);

sda_out=1;

delay_us(3);

sck=0;

}

//———————————————————————————-

void start()

//———————————————————————————-

// generates a transmission start

// _____ ________

// DATA: |_______|

// ___ ___

// SCK : ___| |___| |______

{unsigned char i;

io=1;

sda_out=1;

for (i = 0 ; i < 9 ; i++)

{

sck=1;

delay_us(1);

sck=0;

delay_us(1);

}

initial();

}

void kirim_perintah(unsigned char dat)

{

unsigned char x;

for ( x = 128 ;  x > 0 ;  x /= 2)

{

sck=0;

delay_us(1);

if  (x & dat)  sda_out=1;

else  sda_out=0;

sck=1;

delay_us(1);

};

}

unsigned char terima_data()

{ unsigned char x;

value = 0;

for (x = 128 ; x > 0 ; x /= 2)

{

sck = 0;

delay_ms(1);

if  (sda_in)  value=(value | x);

sck=1;

delay_ms(1);

};

sck=0;

delay_ms(2);

io=1;

sck=1;

sda_out = 0;

delay_ms(1);

io = 0;

return value;

}

void baca_sht(unsigned char xxx)

{

start();

kirim_perintah(xxx);

io=0;

sck=0;

delay_us(1);

sck=1;

if  (sda_in == 1) {goto end;};

delay_us(1);

sck=0;

delay_us(11);

while (sda_in==1);

sht.c[1]=terima_data();

sht.c[0]=terima_data();

end:

io=1;

sda_out=1;

}

void main(void)

{

PORTA=0×00;

DDRA=0×00;

PORTB=0×02;

DDRB=0×01;

PORTC=0×08;

DDRC=0×08;

PORTD=0×00;

DDRD=0×00;

ACSR=0×80;

SFIOR=0×00;

lcd_init(20);

while (1)

{

baca_sht(temp);

msbt=sht.c[1];

lsbt=sht.c[0];

temperatur=sht.t;

baca_sht(humi);

msbh=sht.c[1];

lsbh=sht.c[0];

humidity=sht.t;

// status_reg();

sprintf(lcd,”%3d %3d%8d”,msbt,lsbt,temperatur);

lcd_gotoxy(0,0); lcd_puts(lcd);

sprintf(lcd,”%3d %3d%8d”,msbh,lsbh,humidity);

lcd_gotoxy(0,1); lcd_puts(lcd);

// sprintf(lcd,”%3d %3d%8d”,value);

// lcd_gotoxy(0,2); lcd_puts(lcd);

};

}

Related posts:

  1. AVR Keypad 4×4 Auto Scan (Interrupt Timer) /*************************************************************************** File : keypad-4x4-int-timer.c Description : Main module Project : Keypad Matrix 4x4 Auto Scan (Interrupt Timer) Version : 1.1.5 Author : nashrul@nashrul.co.cc Date : 2006 - 2008 ***************************************************************************/ #include <mega16.h> #include <lcd.h> #include <delay.h> #include <stdio.h> #define kolom1  PORTA.0              // out      1 #define kolom2    PORTA.1 #define kolom3    PORTA.2 #define baris1    PINA.4               // in       Pull #define baris2    PINA.5 #define baris3    PINA.6 #define baris4    PINA.7 #asm .equ __lcd_port=0×18 ;PORTB #endasm char buf[17]; unsigned char sss, ss, mm, tt, key, [...]...
  2. AVR KeyPad 3×4 #include <mega324.h> #include <delay.h> #include <stdio.h> //mulai inisialisai PIN Key Pad #define kolom1    PORTA.0              // out      1 #define kolom2    PORTA.1 #define kolom3    PORTA.2 #define baris1    PINA.4               // in       Pull #define baris2    PINA.5 #define baris3    PINA.6 #define baris4    PINA.7 unsigned char tombol,kolom,x;        // variabel tombol u/ simpan hasil yg ditekan (dalam kode Ascii) void cek_baris()                     // 48 adalah konversi tampilan charakter { if  (baris1 == 0) { tombol = kolom + 48; x=1; } else [...]...
  3. AVR Half Duplex Software UART (9600 bps, 8N1) /********************************************* This program was produced using CodeVision AVR V1.23.6b Standard © Copyright 1998-2002 HP InfoTech s.r.l. http://www.hpinfotech.ro e-mail:office@hpinfotech.ro , hpinfotech@xnet.ro Project : UART3: Half Duplex Software UART (9600 bps, 8N1) Version : 1.3 Date : October 2002 Author : Radu Hristea (radu.hristea@aptrans-group.com; transeast@programmer.net) Company : TransEast s.r.l. Comments: Total size = 321 words (including the demo program) The receive line is buffered (the [...]...
  4. Free Download CodeVisionAVR V2.03.4 CodeVisionAVR V2 Standard Features Application that runs under Windows® 2000, XP, Vista and Windows 7, 32bit and 64bit Easy to use Integrated Development Environment and ANSI C compatible Compiler Editor with auto indentation, syntax highlighting for both C and AVR assembler, function parameters and structure/union members autocomplete Supported data types: bit, bool, char, int, short, long, float Fast floating point [...]...
  5. Serial Communication RS232 Description With serial communication you can exchange data with the AVR-microcontroller and your PC. Allmost all AVR-microcontroller have a UART (Universal Asynchronous Reciever/Transmitter) on board of the chip (accept for the AT1200 and some ATTiny microcontrollers). The data transmission between the PC and the microcontroller is serial and asynchronous, serial means that the bits are send [...]...
  6. Multiple Serial Communication on Avr Mcu Multiple Serial Communication on Avr Mcu?????????? This question comes up from time to time, and I want to give a simple solution in this problem. My friend needs MCU with 4 UART (@ 38.4 KBaud each respectively) to their robot projects in Battle Division KRCI (Kontes Robot Cerdas Indonesia) 2010. 4 UART is a problem, because the [...]...
  1. April 28th, 2010 at 03:50 | #1

    :razz:

  2. June 22nd, 2010 at 03:11 | #2

    terima kasih atas program di atas, tapi kok saya pakai hasil yang ditampilkan dlam LCD cuma angka 0 0 0 saja ya, kok g da nilainya?tolong ya…

    • July 1st, 2010 at 22:23 | #3

      Konfigurasi Hardwerx dah benar pa blum?. pull-up n pull-down d pin scl sda…………..

  3. qiqi
    August 9th, 2010 at 09:26 | #4

    type data ini>>”union data_int{unsigned char c[sizeof unsigned int];unsigned int t;}sht;”
    selalu eror ketika di make

    is there a solution?

    • August 14th, 2010 at 01:15 | #5

      [sizeof unsigned int] ganti dengan [2]. monggo dijoba.
      [sizeof unsigned int] support d code vision AVR versi 1.

  1. No trackbacks yet.
icon_wink.gif icon_neutral.gif icon_mad.gif icon_twisted.gif icon_smile.gif icon_eek.gif icon_sad.gif icon_rolleyes.gif icon_razz.gif icon_redface.gif icon_surprised.gif icon_mrgreen.gif icon_lol.gif icon_idea.gif icon_biggrin.gif icon_evil.gif icon_cry.gif icon_cool.gif icon_arrow.gif icon_confused.gif icon_question.gif icon_exclaim.gif 

Switch to our mobile site