Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Message

Bls: Bls: [AVR-Chat] help me interface atmega8535 ds1307 sht11

2009-03-14 by Ardian sandy

this my code jim....


/*****************************************************
This program was produced by the
CodeWizardAVR V1.24.2b Professional
Automatic Program Generator
© Copyright 1998-2004 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.ro
e-mail:office@hpinfotech.ro

Project : Monitoring humidity and temperatur
Version : 0.0
Date    : 3/11/2009
Author  : Ardi                         
Company : 
Comments: 


Chip type           : ATmega8535
Program type        : Application
Clock frequency     : 11.059200 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 128
*****************************************************/

#include <mega8535.h>
#include <stdio.h>
#include <math.h>
#include <delay.h>
#include <stdlib.h>
// I2C Bus functions
#asm
   .equ __i2c_port=0x18 ;PORTB
   .equ __sda_bit=1
   .equ __scl_bit=2
#endasm
#include <i2c.h>

// DS1307 Real Time Clock functions
#include <ds1307.h>

// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>

#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

// Get a character from the USART Receiver
#ifndef _DEBUG_TERMINAL_IO_
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char status,data;
while (1)
      {
      while (((status=UCSRA) & RX_COMPLETE)==0);
      data=UDR;
      if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
         return data;
      };
}
#pragma used-
#endif

// Write a character to the USART Transmitter
#ifndef _DEBUG_TERMINAL_IO_
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while ((UCSRA & DATA_REGISTER_EMPTY)==0);
UDR=c;
}
#pragma used-
#endif

// Declare your global variables here
typedef union
{ unsigned int i; float f;} value; 
 
 
enum {TEMP,HUMI};
 
sfrb PIND = 0x10;
sfrb PORTD = 0x12;
sfrb DDRD = 0x11;
      
#define    SHT_DATA_OUT        DDRD.2
#define    SHT_DATA_IN        PIND.2
#define    SHT_SCK            PORTD.3
#define    HEAT_SW            PIND.4            // Heater On or Off 
#define    noACK 0
#define    ACK 1
                            //adr  command  r/w
#define STATUS_REG_W 0x06   //000   0011    0
#define STATUS_REG_R 0x07   //000   0011    1
#define MEASURE_TEMP 0x03   //000   0001    1
#define MEASURE_HUMI 0x05   //000   0010    1
#define RESET        0x1e   //000   1111    0 

const float C1=-4.0;              // for 12 Bit
const float C2=+0.0405;           // for 12 Bit
const float C3=-0.0000028;        // for 12 Bit
const float T1=+0.01;             // for 14 Bit @ 5V
const float T2=+0.00008;          // for 14 Bit @ 5V                
                         
  
char lcd_buffer[33];
                                          

//SHT Functions
char SHT_WriteByte(unsigned char value);
char SHT_ReadByte(unsigned char ack);
void s_transstart(void);
void s_connectionreset(void);
char s_softreset(void);
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode);
void calc_sth11(float *p_humidity ,float *p_temperature);
float calc_dewpoint(float h,float t); 
char s_write_statusreg(unsigned char *p_value); 
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum);
unsigned char h,m,s;

 
void main(void)
{
// Declare your local variables here
value humi_val, temp_val;
unsigned char error,checksum;
float dew_point;
delay_ms(200);
s_connectionreset();



// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=Out Func1=Out Func0=Out 
// State7=T State6=T State5=T State4=T State3=0 State2=0 State1=0 State0=0 
PORTA=0x00;
DDRA=0x0F;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=0 
PORTB=0x00;
DDRB=0x01;

// 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=Out Func4=Out Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x34;

// 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
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;

// USART initialization
// Communication Parameters: 8 Data, 2 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 19200
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x8E;
UBRRH=0x00;
UBRRL=0x23;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
// Analog Comparator Output: Off
ACSR=0x80;
SFIOR=0x00;

// I2C Bus initialization
i2c_init();
delay_ms(250);
// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: Off
// SQW/OUT pin state: 0
rtc_init(0,0,0);
rtc_set_date(14,03,2009);
rtc_set_time(19,05,01);   

// LCD module initialization
lcd_init(20);

// Setup Sensibus Pins

PORTD.3    = 0;    // ClockLow
DDRD.3 = 1;     // SCK is an output
PORTD.2 = 0;    // Always Zero
              // Toggle DDRD.2 for Data 

/*
while (1)
    {
    s_transstart();                //transmission start
                     
    error+=SHT_WriteByte(MEASURE_TEMP);     //send command to sensor
         
      for (i=0;i<65535;i++) 
          if(SHT_DATA_IN==0) break;     //wait until sensor has finished the measurement
  
      if(SHT_DATA_IN) error+=1;        //or timeout (~2 sec.) is reached
  
      MSB  =SHT_ReadByte(ACK);    //read the first byte (MSB)
      LSB  =SHT_ReadByte(ACK);    //read the second byte (LSB)
      checksum =SHT_ReadByte(noACK);  //read checksum
  
      sprintf(lcd_buffer,"T= %u %u %u",MSB,LSB,checksum);
        lcd_gotoxy(0,1);
        lcd_puts(lcd_buffer);
 
    delay_ms(500);
    }
*/

while (1)
    {
    error=0;
    error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);
    error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);  
    error=0;
    if(error!=0) 
    s_connectionreset();
    else{
        humi_val.f=(float)humi_val.i;                   //converts integer to float
          temp_val.f=(float)temp_val.i;                   //converts integer to float
          calc_sth11(&humi_val.f,&temp_val.f);            //calculate humidity, temperature
          dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
        sprintf(lcd_buffer,"T:%3.1fC RH:%3.1f%%",temp_val.f,humi_val.f);
        lcd_gotoxy(0,0);
    lcd_puts(lcd_buffer);
    
    rtc_get_time(&h,&m,&s) ;
    lcd_gotoxy(0,1);
    sprintf(lcd_buffer,"%i:%i:%i",h,m,s);
    lcd_puts(lcd_buffer);
      }
    // Global enable interrupts
    #asm("sei")     
      delay_ms(1000);
      #asm("cli")
      }
}

//----------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
//----------------------------------------------------------------------------------
char SHT_WriteByte(unsigned char value) 
{ 
  unsigned char i,error=0;  
  for (i=0x80;i>0;i/=2)                 //shift bit for masking
      { 
      if (i & value)     SHT_DATA_OUT=0;        //masking value with i , write to SENSI-BUS
    else SHT_DATA_OUT=1;                        
    SHT_SCK=1;                          //clk for SENSI-BUS
    delay_us(5);                        //pulswith approx. 5 us      
    SHT_SCK=0;
      }
  SHT_DATA_OUT=0;                       //release DATA-line
  SHT_SCK=1;                            //clk #9 for ack 
  error=SHT_DATA_IN;                    //check ack (DATA will be pulled down by SHT11)
  SHT_SCK=0;        
  return error;                         //error=1 in case of no acknowledge
}

//----------------------------------------------------------------------------------
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
//----------------------------------------------------------------------------------
char SHT_ReadByte(unsigned char ack) 
{ 
  unsigned char i,val=0;
  SHT_DATA_OUT=0;                         //release DATA-line
  for (i=0x80;i>0;i/=2)                 //shift bit for masking
    { 
    SHT_SCK=1;                          //clk for SENSI-BUS
    if (SHT_DATA_IN) val=(val | i);       //read bit  
    SHT_SCK=0;                       
      }
  SHT_DATA_OUT=ack;                       //in case of "ack==1" pull down DATA-Line
  SHT_SCK=1;                            //clk #9 for ack
  delay_us(5);                             //pulswith approx. 5 us 
  SHT_SCK=0;                            
  SHT_DATA_OUT=0;                         //release DATA-line
  return val;
}

//----------------------------------------------------------------------------------
// generates a transmission start 
//       _____         ________
// DATA:      |_______|
//           ___     ___
// SCK : ___|   |___|   |______
//----------------------------------------------------------------------------------
void s_transstart(void)
{                                
   SHT_DATA_OUT=0; 
   SHT_SCK=0;                   //Initial state
   delay_us(1);
   SHT_SCK=1;
   delay_us(1);
   SHT_DATA_OUT=1;
   delay_us(1);
   SHT_SCK=0;  
   delay_us(5);
   SHT_SCK=1;
   delay_us(1);
   SHT_DATA_OUT=0;           
   delay_us(1);
   SHT_SCK=0;           
}

//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
//       _____________________________________________________         ________
// DATA:                                                      |_______|
//          _    _    _    _    _    _    _    _    _        ___     ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______|   |___|   |______
//----------------------------------------------------------------------------------

void s_connectionreset(void)
{  
  unsigned char i; 
  SHT_DATA_OUT=0; SHT_SCK=0;         //Initial state
  for(i=0;i<9;i++)                  //9 SCK cycles
      { 
      SHT_SCK=1;
      delay_us(1);
    SHT_SCK=0;
  }
  s_transstart();                   //transmission start
}

//----------------------------------------------------------------------------------
// resets the sensor by a softreset  
//----------------------------------------------------------------------------------
 
char s_softreset(void)
{ 
  unsigned char error=0;  
  s_connectionreset();              //reset communication
  error+=SHT_WriteByte(RESET);      //send RESET-command to sensor
  return error;                     //error=1 in case of no response form the sensor
}
 
//----------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
//----------------------------------------------------------------------------------
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{ 
  unsigned error=0;
  unsigned int i;

  s_transstart();                   //transmission start
  switch(mode){                     //send command to sensor
    case TEMP    : error+=SHT_WriteByte(MEASURE_TEMP); break;
    case HUMI    : error+=SHT_WriteByte(MEASURE_HUMI); break;
    default     : break;     
  }
  for (i=0;i<65535;i++) if(SHT_DATA_IN==0) break; //wait until sensor has finished the measurement
  if(SHT_DATA_IN) error+=1;                // or timeout (~2 sec.) is reached
  *(p_value+1)  =SHT_ReadByte(ACK);    //read the first byte (MSB)
  *(p_value)  =SHT_ReadByte(ACK);    //read the second byte (LSB)
  *p_checksum =SHT_ReadByte(noACK);  //read checksum
  return error;
}   

//----------------------------------------------------------------------------------------
// calculates temperature [°C] and humidity [%RH] 
// input :  humi [Ticks] (12 bit) 
//          temp [Ticks] (14 bit)
// output:  humi [%RH]
//          temp [°C] 
//----------------------------------------------------------------------------------------

void calc_sth11(float *p_humidity ,float *p_temperature)
{ 

  //float rh=*p_humidity;             // rh:      Humidity [Ticks] 12 Bit 
  //float t=*p_temperature;           // t:       Temperature [Ticks] 14 Bit
  float rh_lin;                     // rh_lin:  Humidity linear
  float rh_true;                    // rh_true: Temperature compensated humidity
  float t_C;                        // t_C   :  Temperature [°C]

  t_C=*p_temperature*0.01 - 40;                  //calc. temperature from ticks to [°C]
  rh_lin=C3*(*p_humidity)*(*p_humidity) + C2*(*p_humidity) + C1;     //calc. humidity from ticks to [%RH]
  rh_true=(t_C-25)*(T1+T2*(*p_humidity))+rh_lin;   //calc. temperature compensated humidity [%RH]
  if(rh_true>100)rh_true=100;       //cut if the value is outside of
  if(rh_true<0.1)rh_true=0.1;       //the physical possible range

  *p_temperature=t_C;               //return temperature [°C]
  *p_humidity=rh_true;              //return humidity[%RH]
}

//--------------------------------------------------------------------
// calculates dew point
// input:   humidity [%RH], temperature [°C]
// output:  dew point [°C]
//--------------------------------------------------------------------

float calc_dewpoint(float h,float t)
{ 
  float logEx,dew_point;
  logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
  dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
  return dew_point;
}   

//----------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
//----------------------------------------------------------------------------------
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
{ 
  unsigned char error=0;
  s_transstart();                   //transmission start
  error=SHT_WriteByte(STATUS_REG_R); //send command to sensor
  *p_value=SHT_ReadByte(ACK);        //read status register (8-bit)
  *p_checksum=SHT_ReadByte(noACK);   //read checksum (8-bit)  
  return error;                     //error=1 in case of no response form the sensor
}
  
//----------------------------------------------------------------------------------
// writes the status register with checksum (8-bit)
//----------------------------------------------------------------------------------
char s_write_statusreg(unsigned char *p_value)
{ 
  unsigned char error=0;
  s_transstart();                   //transmission start
  error+=SHT_WriteByte(STATUS_REG_W);//send command to sensor
  error+=SHT_WriteByte(*p_value);    //send value of status register
  return error;                     //error>=1 in case of no response form the sensor
}

Ardian     3oz^


      Lebih bergaul dan terhubung dengan lebih baik. Tambah lebih banyak teman ke Yahoo! Messenger sekarang! http://id.messenger.yahoo.com/invite/

[Non-text portions of this message have been removed]

Attachments

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.