⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 avr程序.c

📁 这个SHT11非常全
💻 C
📖 第 1 页 / 共 2 页
字号:
这个芯片怎么说呢!比较贵!!但性能比较不错! 
但就是不知道怎么标定(我想看看到底准不准,实践出真理)!! 
下面是用mega8改写的程序,在测量程序里我改了一下读取温、湿度的顺序,因为按照原程序的顺序读出来结果不正确,改了后就这正确了(这是没修改的)。有什么问题大家一起讨论。QQ:48801015 
/********************************************* 
This program was produced by the 
CodeWizardAVR V1.23.8c Professional 
Automatic Program Generator 
?Copyright 1998-2003 HP InfoTech s.r.l. 
http://www.hpinfotech.ro 
e-mail:office@hpinfotech.ro 

Project :  
Version :  
Date    : 30.03.2004 
Author  : Nick Schres                      
Company :                                  
Comments:  


Chip type           : ATmega8 
Program type        : Application 
Clock frequency     : 8,000000 MHz 
Memory model        : Small 
External SRAM size  : 0 
Data Stack size     : 256 
*********************************************/ 

#include <mega8.h>  
#include <delay.h>       
#include <stdlib.h> 

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

// Declare your global variables here    

char s_write_byte(unsigned char value);  
char s_read_byte(unsigned char ack);    
void s_transstart(void);            
void s_connectionreset(void);           
char s_softreset(void);      
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum); 
char s_write_statusreg(unsigned char *p_value); 
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode); 
void calc_sth11(float *p_humidity ,float *p_temperature); 




typedef union  
{ unsigned int i; 
  float f; 
} value; 
  

enum {TEMP,HUMI}; 

#define        DATA_OUT           PORTC.0 
#define DATA_IN                PINC.0 
#define        SCK                   PORTC.1  

#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 

void main(void) 
{ 
// Declare your local variables here 
                 
  value humi_val,temp_val; 
  unsigned char error,checksum; 
  unsigned char outp;       
  char inp; 


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

// Port C initialization 
// Func0=In Func1=Out Func2=In Func3=In Func4=In Func5=In Func6=In  
// State0=T State1=0 State2=T State3=T State4=T State5=T State6=T  
PORTC=0x00; 
DDRC=0x02; 

// Port D initialization 
// Func0=In Func1=In Func2=In Func3=In Func4=In Func5=In Func6=In Func7=In  
// State0=T State1=T State2=T State3=T State4=T State5=T State6=T State7=T  
PORTD=0x00; 
DDRD=0x00; 

// Timer/Counter 0 initialization 
// Clock source: System Clock 
// Clock value: Timer 0 Stopped 
TCCR0=0x00; 
TCNT0=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; 
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 
GICR|=0x00; 
MCUCR=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 
// Analog Comparator Output: Off 
ACSR=0x80; 
SFIOR=0x00; 

// LCD module initialization 
lcd_init(20); 


// sample program that shows how to use SHT11 functions 
// 1. connection reset  
// 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit) 
// 3. calculate humidity [%RH] and temperature [癈] 
// 4. print temperature, humidity 
    
    s_connectionreset();      
   
  while(1) 
  { error=0;             
//    delay_ms(200);          
//    error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);  //measure humidity 
//    delay_ms(200); 
//    error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);  //measure temperature     

              
       error += s_read_statusreg(&inp, &checksum); 

    if(error!=0)  
       {s_connectionreset();                 //in case of an error: connection reset 
       lcd_clear(); 
       lcd_gotoxy(0,0); 
       lcd_putsf("Error");       
  
  
       }        
    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 
       
      itoa(inp,&outp);  
                            
      lcd_clear(); 
      lcd_gotoxy(0,0); 
      lcd_puts(&outp);               
                                     
      itoa(humi_val.i,&outp);        
      lcd_gotoxy(0,2); 
      lcd_puts(&outp);               
    } 
    //----------wait approx. 0.8s to avoid heating up SHTxx------------------------------       
    delay_ms(800); 
    //-----------------------------------------------------------------------------------                        
  } 
}                              


//---------------------------------------------------------------------------------- 
// writes a byte on the Sensibus and checks the acknowledge  
//---------------------------------------------------------------------------------- 
    
    
char s_write_byte(unsigned char value) 
{  
  unsigned char i,error=0;   
  DDRC = 0b00000011;    // DATA Ausgang 
  for (i=0x80;i>0;i/=2)                     //shift bit for masking 
  {  
    if (i & value)         DATA_OUT=1;                //masking value with i , write to SENSI-BUS 
    else DATA_OUT=0;    
    delay_us(2);                      
    SCK=1;                          //clk for SENSI-BUS 
    delay_us(5);                                                //pulswith approx. 5 us           
    SCK=0;        
  }       

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -