原创 DS18B20驱动程序问题。

2008-7-25 23:15 1486 3 3 分类: MCU/ 嵌入式

第一次开博,发一个我最近搞得DS18B20驱动,在LCD上显示的时候,当温度显降低或者升高几度的时候,它不会一度一度的增加,而是只显示最终的温度,而且当温度低于20度的时候,个位的数字在LCD上是显示乱码的。但找来找去也没找到错误,想在这里请高手请教下。


#include <reg52.h>
#include <intrins.h>
#define u_char unsigned char
#define Temp_Acu 0.0625
sbit DQ = P2^5;
u_char Temp;
u_char Tempreture[2];
u_char Shi,Ge;


////////////////////////////////////////////
//                 Delay
////////////////////////////////////////////
void Delay(int delay_time)
{
  int s;
  for(s=0;s<delay_time;s++);
}
//take 24us then every count 16us


///////////////////////////////////////////
//                Reset
///////////////////////////////////////////
u_char Reset(void)
{
  u_char presence;
  DQ="0";    //transaction start;
  Delay(29);        //delay min 480us;
  DQ="1";    //release bus
  Delay(3);   //delay 15~60us;
  presence="DQ";  //read the slave response;
  Delay(25);  //delay 200us;
  return presence; //0 present 1 miss
}


//////////////////////////////////////////
//                Bitread
//////////////////////////////////////////


u_char Read_bit(void)
{
  u_char i;
  u_char temp;
  DQ="0";       //bus start;
  DQ="1";    //release bus;
  for(i=0;i<3;i++); //Delay 15us.
  temp="DQ";
  return temp;  //the data is valid range in the 15us;
}


//////////////////////////////////////////
//                Byteread
//////////////////////////////////////////
u_char Read_byte(void)
{
  u_char i;
  u_char value="0";      
  for(i=0;i<8;i++)  //
  {
    if(Read_bit()) value|=0x01<<i;
 Delay(6);
  }
  return value;
}


//////////////////////////////////////////
//                Write_bit
//////////////////////////////////////////


void Write_bit(char bitval)
{
  DQ="0";
  if(bitval==1)DQ=1;
  Delay(5);
  DQ="1";
}


//////////////////////////////////////////
//               Write_byte
//////////////////////////////////////////
void Write_byte(u_char Data)
{
  u_char i;
  u_char temp;
  for(i=0;i<8;i++)
  {
    temp="Data">>i;
 temp&=0x01;
 Write_bit(temp);
  }
  Delay(5);
}
////////////////////////////////////////
//                Measure Temp
////////////////////////////////////////


void Measure_temp(void)
{
  Reset();
  Write_byte(0xCC);
  Write_byte(0x44);
  Delay(5);
  Reset();
  Write_byte(0xCC);
  Write_byte(0xBE);


  Tempreture[0]=Read_byte();
  Delay(2);
  Tempreture[1]=Read_byte();


  Tempreture[1]=Tempreture[1]&0x07;    // 0000 0111.
  Tempreture[0]=Tempreture[0]>>4;       // 0000 1111.
  Temp="Tempreture"[1]*16+Tempreture[0];
  Shi="Temp/10";
  Ge="Temp"%10;
}


 


///////////////////////////////////////////////////////////////////////////////////////////////


//////////////////////////////////////////////////////////////////////////////////////////////


这里是LCD显示程序:


#include <stdio.h>
#include <reg52.h>
#include <intrins.h>
#include <DS18B20.h>


#define u_char  unsigned char
#define u_int   unsigned int
#define  font_width  8
#define  font_page 2
extern Shi,Ge;
sbit LED= P0^7;


typedef struct
{
  u_char *image;
  u_char pages;
  u_char lenth;
}IMAGE_STRUCT;


u_char code test_image[]=


{
0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,
0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,
0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,
0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,
0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,
0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,   //the first page.
             //the next is the second page.
0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,/*"T",0*/
0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,/*"h",1*/
0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,/*"e",2*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",3*/
0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,/*"T",4*/
0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,/*"e",5*/
0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,/*"m",6*/
0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,/*"p",7*/
0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,/*"r",8*/
0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,/*"e",9*/
0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,/*"t",10*/
0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,/*"u",11*/
0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,/*"r",12*/
0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,/*"e",13*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",14*/
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,/*"i",15*/
0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,/*"s",16*/
0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00 /*":",17*/


};
IMAGE_STRUCT code TEST=
{
  &test_image,
  0x02,
  0x90
};


u_char code Number[]=
{
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,/*"0",0*/


0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,/*"1",1*/


0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,/*"2",2*/


0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,/*"3",3*/


0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,/*"4",4*/


0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,/*"5",5*/


0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,/*"6",6*/


0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,/*"7",7*/


0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,/*"8",8*/


0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00, /*"9",9*/


0xFC,0x06,0x02,0xFE,0x02,0x02,0x06,0xFC,0x07,0x0A,0x11,0x20,0x21,0x12,0x0C,0x07 /*small icon 10 */
};


sbit _RES = P3^7; 
sbit E1  = P3^3;
sbit E2  = P3^6;
sbit E3  = P3^2;
sbit A0  = P3^4;
sbit R_W = P3^5;
// DataBus = P1


//====================
// Display Data
//====================
/*--  160x32         --*/
/*--  LM3123 Logo    --*/
//====================
// delay routine
//====================
void delay100us(void) //delay 100us for 12MHz
{
u_char data i;
for(i=0;i<=70;i++)
 {_nop_(); _nop_();}
}


void delay1ms(void)  //delay 1ms for 12MHz
{
u_char data i;
for(i=0;i<=250;i++)
 {_nop_(); _nop_();}
}
/*
void delay100ms(void)
{
u_char data j;
for(j=0;j<=100;j++)
 {delay1ms();} 
}
*/


//====================
// write command
//====================
void wr_command(u_char ChipNo,command)
{
P1 = command;
A0=0;
R_W=0;
_nop_();
switch(ChipNo)
 {
 case 0: E1=1; E2=1; E3=1;  break;// write into all driver
 case 1: E1=1; E2=0; E3=0;  break;// write into 1st driver
 case 2: E1=0; E2=1; E3=0;  break;// write into 2nd driver
 case 3: E1=0; E2=0; E3=1;  break;// write into 3rd driver
 }
E1=0; E2=0; E3=0;
delay100us();
}


//====================
// write data
//====================
void wr_data(u_char ChipNo,ddata)
{
P1 = ddata;
A0=1;
R_W=0;
_nop_();
switch(ChipNo)
 {
 case 0: E1=1; E2=1; E3=1; break; // write into all driver
 case 1: E1=1; E2=0; E3=0; break; // write into 1st driver
 case 2: E1=0; E2=1; E3=0; break; // write into 2nd driver
 case 3: E1=0; E2=0; E3=1; break; // write into 3rd driver
 default:break;
 }
E1=0; E2=0; E3=0;
}


//====================
// clear screen
//====================
void Clr_Screen(void) // fill 00h into DD RAM
{
u_char i,j;
for(i=0;i<4;i++)
     {
     wr_command(0,0xb8 | i); // select page
     wr_command(0,0x00); // start form column 0
     for(j=0;j<80;j++)
      {
      wr_data(0,0x00);// fill 00h in to DD RAM
      }
     }
}


//=====================================
//=====================================
void lcdDisplayChar(u_char number,u_char Y,u_char Top)
{
  u_char page,line;
  u_char TempData,index;


  if(number>=0)
  {
    index="number-0";
  }
  for(page=Top;page<Top+font_page;page++)
  {
 for(line=Y;line<Y+font_width;line++)
    {
   TempData="Number"[index*font_width*font_page+((line-Y)+(page-Top)*font_width)];
   wr_command(0,0xb8|page);
   if(line<60)
   {
     wr_command(1,0x14+line);  
  
     wr_data(1,TempData);
   }
   if((line>=60)&(line<=139))
   {
     wr_command(2,0x00+line-60);


     wr_data(2,TempData);
      }
   if((line>=140)&(line<160))
   {
  wr_command(3,0x13+line-140);
  
     wr_data(3,TempData);
      }    


    }


  }
  
}


//==========================================
//              CHAR CLEAR
//==========================================
#ifdef DEBUG
 void lcd_clear_Char(u_char Y,u_char Top)
{
  u_char page,line;
  u_char TempData;


  for(page=Top;page<Top+font_page;page++)
  {
 for(line=Y;line<Y+font_width;line++)
    {
   TempData="0x00";
   wr_command(0,0xb8|page);
   if(line<60)
   {
     wr_command(1,0x14+line);  
  
     wr_data(1,TempData);
   }
   if((line>=60)&(line<=139))
   {
     wr_command(2,0x00+line-60);


     wr_data(2,TempData);
      }
   if((line>=140)&(line<160))
   {
  wr_command(3,0x13+line-140);
  
     wr_data(3,TempData);
      }    


    }


  }
  
}
#endif
//===============================================
//                  IMAGE_display
//===============================================
#ifdef DEBUG
void image_display(IMAGE_STRUCT *display,u_char left,u_char top)
{
  u_char i,j;
  u_char Temp_Data;
  for(i=top;i<display->pages+top;i++)
  {
  
   for(j=left;j<left+display->lenth;j++)
   {
   Temp_Data=display->image[display->lenth*(i-top)+j];
      wr_command(0,0xb8|i);
      if(j<=59)
   {
      wr_command(1,0x14+j);
   wr_data(1,Temp_Data);
      }
   if((j>59)&(j<=138))
   {
      wr_command(2,j-60);
   wr_data(2,Temp_Data);
   }
   if((j>=139)&(j<=159))
   {
      wr_command(3,0x13+j-139);
   wr_data(3,Temp_Data);
   }
   }
   }
}
#endif
//====================
// Write Icons Data
//====================
#ifdef DEBUG
void WriteIcons(void)
{
    wr_command(0,0xb8 | 1); // icons mapped at page 1
    wr_command(1,0x13);     // icons Mapped at column 13h
 wr_data(1,0xF0);     // turn on all icons
}
#endif
// Init LCD module
//====================
void init_LCD(void)
{
 E1=0; E2=0; E3=0; // init E status
 _RES = 1;
 delay1ms();
 _RES = 0;
 delay1ms();
 _RES = 1;       // _RES=1, 68 I/F mode
 wr_command(0,0xa9); // set duty cycle 1/32
 wr_command(0,0xa4); // static drive = OFF
 wr_command(0,0xa1); // adc="1"
 wr_command(0,0xc0); // display start line =0;
 wr_command(0,0xaf); // display ON;
 //wr_command(0,0xee);   // read modify write="Off";
}


//************************
// Main Program
//************************
void main()


   LED="1";
   init_LCD();
   Clr_Screen();
   while(1)
   {
    Measure_temp();
    lcdDisplayChar(Shi,10,1);
 lcdDisplayChar(Ge,18,1);
   }


}



  



 


 


 


 


 


 


 


 


 


 


 

PARTNER CONTENT

文章评论0条评论)

登录后参与讨论
我要评论
0
3
关闭 站长推荐上一条 /1 下一条