Pages: [1]   Go Down
Print
Author Topic: ผมเขียน SHT 15กับLCD16x4 แต่ทดลองจิงขึ้น จอดำเป็นแ&  (Read 7711 times)
0 Members and 1 Guest are viewing this topic.
puppii
มาใหม่
*
Offline Offline

Posts: 1


Email
« on: January 12, 2014, 11:06:26 PM »

ผมเขียน SHT 15กับLCD16x4 แต่ทดลองจิงขึ้น จอดำเป็นแถบ ใช้ฟังก์ชัน LCD 20x4 อยากรู้สาเหตุว่าเป็นที่โปรแกรมหรือป่าวคร๊าบ ขอช่วยแก้ไขให้หน่อยน่ะคร๊าบ

โค้ดคร๊าบ ที่เขียน


#include <16f877.h>

#fuses HS,NOWDT,NOLVP,NOBROWNOUT
#use delay(clock=20M)

#include "LCD420.c"
#include<math.h>

#define SHT15_DATA PIN_a1
#define SHT15_SCK PIN_a0

#define MEASURE_TEMP 0x03
#define MEASURE_HUMI 0x05
#define RESET 0x1E
//Temperature
#define D1 -39.75
#define D2 0.01
//Humidity
#define C1 -4.0
#define C2 0.0405
#define C3 -0.0000028
//true humidity
#define T1 0.01
#define T2 0.00008

long CRC;

//*************Tranmission Start*******************//
void SHT15Start()
{
  output_high(SHT15_DATA);
  output_low(SHT15_SCK);
  output_high(SHT15_SCK);
  output_low(SHT15_DATA);
  output_low(SHT15_SCK);
  output_high(SHT15_SCK);
  output_high(SHT15_DATA);
  output_low(SHT15_SCK);
}
//*************Reset SHT15*******************//
void SHT15Reset()
{
 int i;
 output_high(SHT15_DATA);
    for(i=0;i<9;i++)
    {
      output_high(SHT15_SCK);
      delay_us(2);
      output_low(SHT15_SCK);
      delay_us(2);
    }
 SHT15Start();
}
//****************Adr.Measure****************//
int SHT15Measure(int Data)
{
 int i;
 for (i=0x80;i>0;i/=2)
 {
    if(i&Data){
      output_high(SHT15_DATA);
     }
    else{
      output_low(SHT15_DATA);
     }
  delay_us(2);
  output_high(SHT15_SCK);
  delay_us(2);
  output_low(SHT15_SCK);
 }
output_float(SHT15_DATA);
delay_us(2);

output_high(SHT15_SCK);
delay_us(2);

i=input(SHT15_DATA);
output_low(SHT15_SCK);
delay_ms(250);
return(i);
}

//*************Read SHT15*******************//
long SHT15Read(void)
{
 int i;
 long Tmp,Value,MSB,LSB,CRC;

 MSB=0;
 LSB=0;
 CRC=0;

 for(i=0;i<8;i++)          //********Read MSB********//
 {
  MSB<<=1;
  output_high(SHT15_SCK);
  Tmp=input(SHT15_DATA);
  Delay_us(2);
  output_low(SHT15_SCK);
  Delay_us(2);
  if(Tmp){
    MSB|=1;
   }
 }
output_low(SHT15_DATA);
output_high(SHT15_SCK);
Delay_Us(2);
output_float(SHT15_DATA);
output_low(SHT15_SCK);
Delay_us(2);

 for(i=0;i<8;i++)          //********Read LSB********//
 {
   LSB<<=1;
   output_high(SHT15_SCK);
   Tmp=input(SHT15_DATA);
   Delay_us(2);
   output_low(SHT15_SCK);
   Delay_us(2);
    if(Tmp){
     LSB|=1;
    }
 }
output_low(SHT15_DATA);//Acknowledge routine for CRC
delay_us(2);
output_high(SHT15_SCK);
delay_us(2);
output_low(SHT15_SCK);
output_float(SHT15_DATA); //Change to Input
delay_us(2);

 for(i=0;i<8;i++)
 {
  CRC<<=1;
  output_high(SHT15_SCK);
  Tmp=input(SHT15_DATA);
  output_low(SHT15_SCK);
  if(Tmp){
   CRC|=1;
   }
 } 
 Value=make16(MSB,LSB); // makes 16 bit number out of two 8 bit numbers
 return(Value);
}
void SHT15SoftReset()
{
 SHT15Reset();
 SHT15measure(RESET);
}


//*************Main Function*******************//
void main(void)
{
  float Value_temp,Value_humi;
  float Temp,Humi_linear,Humi_True;
  int R;
 
  lcd_init();
  lcd_putc("\fProgram Ready...\n");
  SHT15softReset();
  while(TRUE)
  {
     delay_ms(12); //delay>11 ms before next command
  //start transmission
     SHT15Start();
  //measure Temperature
     R=SHT15Measure(MEASURE_TEMP);
  //check sensor connect
     if(R==1)
     {
      printf(lcd_putc,"\fSensor Error");
      delay_ms(1000);
      continue;
     }
     //calculation temperature
     Value_Temp=SHT15Read();   
     Temp=D1+(D2*Value_Temp);
   
  //Delay>11 ms for next command
     delay_ms(12);
  //start transmission
     SHT15Start();
  //measure Humidity
     SHT15Measure(MEASURE_HUMI);
     lValue_rh = SHT15Read();

      // relative humidity calculation
      fRh_lin =(C1+(C2*lValue_rh)+(C3*lValue_rh*lValue_rh));
      fRh_true =(((Temp-25)*(T1+(T2*lValue_rh)))+fRh_lin);   
  //send to LCD
     lcd_gotoxy(1,2);
     printf(lcd_putc,"Temp : %3.2f  %cc",Temp,0xDF);
     lcd_gotoxy(1,1);
     printf(lcd_putc,"Humi : %3.2f %%RH",fRh_true,0xDF);
     delay_ms(1000);
  }
}





Logged
Pages: [1]   Go Up
Print
 
Jump to: