c51: DS1820

Publisher:脑力风潮Latest update time:2016-10-14 Source: eefocusKeywords:c51  DS1820 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
//DS1820 Application

//Resolution 12 bits

#include
#include


#define uchar unsigned char
#define uint unsigned int

uchar digit[10]="0123456789";

//Delay 1 millisecond program
void delayms();

//Delay s milliseconds program 
void delaynms(uint);

//Initialize LCD
void InitLCD();

//Write LCD instruction
void WriteInstruction(uchar);

//Write LCD data
void WriteData(uchar);

//Write address
void WriteAdd(uchar);


//Read LCD status
uchar BusyTest();

//LCD busy query
sbit LCDbusy=P1^7;

//LCD operation bit setting
sbit RS=P2^2;
sbit RW=P2^1;
sbit E=P2^0;
sbit BF=P1^7;

//----- DS1820 declare variable function
sbit DQ=P3^0;
sbit led=P3^1; //Signal indication

uchar TL,TH; //Read DS1820 temperature data high and low bits


//Initialize DS1820
bit InitDS1820();

//Read one byte from DS1820
uchar ReadOneChar();

//Write one byte to DS1820
WriteOneChar(uchar);


//Read temperature
void readtemp();

////Display temperature
void dispay();

uchar flag=0;

 


//main program
void main() 
{

 
 uchar i;

  
 uchar code str[]="thermometer is:";

 


 //Initialize LCD
 InitLCD();
 //Write address
 WriteAdd(0x01);
 //Write data
 i=0; 
 while(str[i]!='\0')
 {
  WriteData(str[i]);

  i++;
 }

 //Write address

 while(1)
 {
  readtemp();

  //If it is a negative temperature, display a minus signif
  (flag)
  {
   WriteAdd(0x44);
   WriteData('-');
  }
  else
  {
   WriteAdd(0x44);
   WriteData(' ');
  }

   
  
  WriteAdd(0x45);

  dispay();

  delaynms(1000);
 }
}


//Initialize DS1820
bit InitDS1820()
{
 uchar t;
 bit flag;  
 DQ = 1;          
 _nop_();
  
 DQ = 0; 
          
 //Maintain 600us  
 for(t=0;t<200;t++);

 //Release bus
    DQ = 1; 
 
 //Wait 15~30us, DS1820 outputs response signal,
 //Here delay 45us          
    for(t=0;t<15;t++);  
 
 //Sampling response signal
 flag=DQ; 

 //The response signal is maintained for 60~240us
 // Here the delay is 300us, 
 for(t=0;t<100;t++);  
      
    return (flag); //Return the detection success flag
}

//Read a character from DS1820
uchar ReadOneChar()
{
 uchar i,t;
 uchar dat;
 for(i=0;i<8;i++)
 {
  dat>>=1;
 
  DQ=1; //c51 driver
  _nop_();
  
  DQ=0; //c51 driver

  _nop_(); //Delay 1 us to release the bus

  DQ=1; //c51 releases bus
  
  //DS1820 prepares data
  _nop_();
  _nop_();
  _nop_();
  _nop_();
  _nop_();

  //samplingif
  (DQ==1) //c51 samplingdat
   |=0x80; 
  else
   dat|=0x00;

  //It takes 60us to complete a read cycle, and the delay here is 60us.
  //One cycle is about 3us
  for(t=0;t<20;t++);
 }
 return dat;

}

//Write one byte to DS1820
WriteOneChar(uchar dat)
{
 uchar i,t;

 for(i=0;i<8;i++)
 {
  DQ=1; //c51 driver_nop_
  ();
  DQ=0; //c51 releases the bus

  //Starting from the low bit, the i-th bit of data is transmitted to the bus
  DQ=dat&0x01;  
  
  //DS1820 samples 15~60us after DQ=0.
  //So the delay is 60us,
  for(t=0;t<20;t++);

  //Prepare the next data
  dat>>=1;
 }
}

//Read temperature
void readtemp()
{

 InitDS1820();
 
 //Ignore DS1820 address
 WriteOneChar(0xcc); 

 //Start temperature conversion, time 750ms
 WriteOneChar(0x44); 

 //Delay 1000ms,
 delaynms(1000);

 InitDS1820();
 //Ignore DS1820 address
 WriteOneChar(0xcc); 
 //Read temperature
 WriteOneChar(0xbe); 

 TL=ReadOneChar();
 TH=ReadOneChar(); 
}

//Display temperature
void dispay()
{
 uchar b,s,g; //Represents hundreds, tens and ones
 uchar d1,d2; //1st and 2nd decimal places
 unsigned int ti,td;

 if(TH<0xfc)
 {
  //positive temperature
  flag=0;
  ti=TH*16+TL/16; //integer part
  td=TL%16; //decimal part
 
 //

 }
 else
 {
  //Negative temperature,
  flag=1;
  ti=TH*256+TL;
  ti=~ti+1;
  ti=ti/16;
  td=(~TL+1)%16;
 }
          
 g=ti%10 ;
 s=(ti%100)/10;
 b=ti/100;

 d1=td*10/16; // 1st decimal
 d2=td*100/16%10; // 2nd decimal

 //
 WriteData(digit[b]);

 WriteData(digit[s]);

 WriteData(digit[g]);

 WriteData('.'); 

 WriteData(digit[d1]);
 WriteData(digit[d2]);
}

 

 


//Initialize LCD
void InitLCD()
{
 delaynms(15); 
 WriteInstruction(0x38); //Display mode setting
 delaynms(5);
 WriteInstruction(0x38);
 delaynms(5);
 WriteInstruction(0x38);

 delaynms(5);
 WriteInstruction(0x0d); //Display mode setting: on display, no cursor, blinking cursor

 delaynms(5);
 WriteInstruction(0x06); //Display mode setting: cursor moves right, text does not move.

 delaynms(5);

 

 


 
}


//Write LCD instruction
void WriteInstruction(uchar instruction)
{
 //LCD is busy, waiting.
 while(BusyTest()==1);

 //Write instruction
 RS=0;
 RW=0;
 E=0;

 _nop_();
 _nop_();
 _nop_();

 //Instruction
 P1=instruction;

 _nop_();
 _nop_();
 _nop_();

 E=1;

 _nop_();
 _nop_();
 _nop_();

 E=0;
}


//Write LCD data
void WriteData(uchar d)
{
  //LCD is busy, waiting.
 while(BusyTest()==1);

 //Write data
 RS=1;
 RW=0;
 E=0;

 _nop_();
 _nop_();
 _nop_();

 //Instruction
 P1=d;

 _nop_();
 _nop_();
 _nop_();

 E=1;

 _nop_();
 _nop_();
 _nop_();

 E=0;

}

//Write address, belongs to write instruction
void WriteAdd(uchar ad)
{
 uchar addr=ad+0x80;
 WriteInstruction(addr);
 
}


//Read LCD status
uchar BusyTest()
{
 bit result;

 //Read LCD status
 RS=0;
 RW=1;
 E=1;

 _nop_();
 _nop_();
 _nop_();

 //Command
 result=BF;

 _nop_();
 _nop_();
 _nop_();

 E=0;

 return result;


}

 

 


void delaynms(uint s)
{
 uint tem;
 for(tem=0;tem  {
  delayms();
 }

}

//Delay 1ms
void delayms()
{
 uchar i;
 for(i=0;i<250;i++);
 for(i=0;i<80;i++);
}

 

 

c51: DS1820 - Sandy - Sandy's Blog
Keywords:c51  DS1820 Reference address:c51: DS1820

Previous article:c51: Read the DS1820 serial number and display it
Next article:C51: LCD1602 displays the measured value

Recommended ReadingLatest update time:2024-11-16 15:48

C51 MCU fine-tuning SH-20504 program
/*************************************************************************************************************** * Copyright belongs to huaer ** * Function: SH-20504 controls 56BYG250C microcontroller program ** * Output: PWM ** * Input: None ** * ** * Circuit connection: Common anode (direction +/offline +/PWM+
[Microcontroller]
C51 Programming Interrupts and Register Usage
If registers are used in the interrupt service function ISR, the use of using must be handled properly: 1. The interrupt service function uses using to specify a register bank different from the main function (the main function generally uses Register bank 0). 2. ISRs with the same interrupt priority can use using to
[Microcontroller]
C51 Manual Calculation Timer Interrupt Introduction
The following is calculated by software: 12M crystal oscillator, timer mode 16 bit, 12T, 1 millisecond time void Timer0Init(void) //1 millisecond@12.000MHz{ AUXR &= 0x7F; //Timer clock 12T mode TMOD &= 0xF0; //Set timer mode TMOD |= 0x01; //Set timer mode TL0 = 0x18; //Set the initial timing value TH0 = 0xFC;
[Microcontroller]
The format of C51 interrupt function
The format of C51 interrupt function is: void FuncIr(void) interrupt x  void function name (void) interrupt n using m      {       function body statement          }          interrupt using is a keyword introduced into the C51 interrupt service program and can only be used in the C51 interrupt service program lo
[Microcontroller]
Keil C51 Tutorial---Basic Knowledge of Development System (I)
Keil C51 Tutorial---Basic Knowledge of Development System (I) Section 1 System Overview     Keil C51 is a 51 series compatible microcontroller C language software development system produced by Keil Software in the United States. Compared with assembly language, C language has obvious advantages in function, struct
[Microcontroller]
C51 implements PID algorithm code
When we really want to use the PID algorithm, we find that it is not so easy to implement the code in the book on our 51. Simply put, it cannot be called directly. A careful analysis will reveal that the C language code for PID implementation in the textbooks and on the Internet is almost all done with float
[Microcontroller]
c51 bit usage
For example, when i=0, the value of tab is 0x3f, so the function of bit_dat((bit)(tab &0x80)); is: Step 1: bitwise AND tab with 0x80, that is, bitwise AND 0x3f with 0x80 (and 0x40 with the 6th bit). This results in the highest bit (7th bit) of 0x3f being obtained, and all other bits are masked to 0. Step 2: Perform a
[Microcontroller]
MCU decoding 315M pt2262 encoded c51 program
This is a c51 program that uses software to decode pt2262 signals. It has been tested and can be used. Please note that the receiving module should be far away from the crystal oscillator of the microcontroller, otherwise the interference is too serious to decode or the distance is very close. /*------------------
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号