ATMEGA16 and DS18B20 digital tube display

Publisher:SparklingBeautyLatest update time:2015-04-28 Source: 51heiKeywords:ATMEGA16 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
A DS18B20 with a large error value caused me a lot of trouble. I always thought it was a program error, so I changed it and it worked! It took me three days to check the program

/*This program is an eight-bit common cathode digital tube with two 573 controlled dynamic scans*/
#include
#include
#define uchar unsigned char
#define uint unsigned int
//Note: The function of code is to store the following data in the program memory. If code is not used, it will be placed in the random access memory.
#pragma data:code
const table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,
          0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//Without decimal point
const table2[]={0xbf,0x86,0xdb,0xcf,0xe6,
                 0xed,0xfd,0x87,0xff,0xdf};//Decimal point encoding     
      
      
/*If uchar table[] is used, it will be placed in the data memory. Never use it this way, it will take up more space. */
/*Two 573, segment code PA3, bit code PA4*/
//Connect 18B20 DQ to PC2 
#define DQ_IN DDRC&=~BIT(2)
//The previous sentence is to set DQ as input
#define DQ_OUT DDRC|=BIT(2)
//The previous sentence is to set DQ as output
#define DQ_SET PORTC|=BIT(2)
//The previous sentence is to set DQ to high level
#define DQ_CLR PORTC&=~BIT(2)
//The previous sentence is to set DQ to low level
#define DQ_RD PINC&BIT(2)
//The previous sentence is to read data from PC2uchar  
disdata[4];
void delay(uint ms)
{
        uint i,j;
 for(i=ms;i>0;i--)
  
    for(j=220;j>0;j--);
     
}
void delayus(uint us)
{
  while(us--);
}
void show(uchar j,uchar k)
{
    DDRA|=BIT(3);
 DDRA|=BIT(4);
 DDRB=0XFF;    
    PORTA|=BIT(3);
 PORTB=table[j];
 PORTA&=~BIT(3); PORTB
 
 =0XFF; PORTB&  =  ~BIT
 (k  );



void show_point(uchar j,uchar k)
{
    DDRA|=BIT(3);
 DDRA|=BIT(4);
 DDRB=0XFF;    
    PORTA|=BIT(3);
 PORTB=table2[j];
 PORTA&=~BIT(3);
 
 PORTB=0XFF;
 PORTB&=~BIT(k);
 PORTA|=BIT(4);
 PORTA&=~BIT(4);
 delay(5);
}
rest_18B20()
{
uchar i ;
DQ_OUT;//Set DQ as output
DQ_SET;//Pull DQ high
delayus(5);
DQ_CLR;//Pull DQ low
delayus(800);
DQ_SET;//Pull DQ high
//delayus(550);
DQ_IN;//Set DQ as input
i=DQ_RD;
delayus(800);
return i;
}
void write_18B20(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
  {
  DQ_OUT;//Set DQ as output
  DQ_CLR;//Pull DQ low
  delayus(30);
   if(dat&0x01)//Judge whether the current bit is high, if it is high, execute the following statement
   {
    DQ_SET;//Pull DQ high, let the bus sample to 1
   }
   else
   {
   DQ_CLR;//Pull DQ low
   }
   delayus(80);            
   DQ_SET;//Release the bus
   dat=dat>>1;
  }
}
uchar read_18B20()
{
uchar i,value;
for(i=0;i<8;i++)
  {
 
  DQ_OUT;//Set DQ as output
  DQ_CLR;//Pull DQ low
   value=value>>1;
  delayus(10);
  DQ_SET;//Release the bus and wait for MCU to sample
  DQ_IN;//Set DQ as input to sample
    if(DQ_RD)
      {
    value|=0x80;
      }
 
   delayus(100);           
 // DQ_SET;//Release the bus
 }
 return value;
}

void display(uint tvalue)
{
   disdata[0]=tvalue/1000;//hundreds
   disdata[1]=tvalue%1000/100;//tens
   disdata[2]=tvalue%100/10;//units
   disdata[3]=tvalue%10;//decimal places   
    show(disdata[0],0);
 delay(2);
    show(disdata[1],1);
 delay(2);
    show_point(disdata[2],2);
 //show(0x80,2);
 show(disdata[3],3);
 //The following display method can also be
used/* uint i,temp[4];
  for(i=0;i<4;i++)
   {
     temp[3-i]=dat%10;
  dat=dat/10;
   }
  
   for(i=0;i<4;i++)
   {
       show(temp[i],i);
    delay(2);*/
  /* // The following three sentences are used to turn off the display and are used for dynamic display
   DDRA|=BIT(3);
      DDRA|=BIT(4);
      DDRB=0XFF;
   }*/
}
void main()
{int L,H;
uint tvalue;
   while(1)
      {
   rest_18B20();
   write_18B20(0xcc);//skip reading the serial number
   write_18B20(0x44);//start temperature conversion
   delayus(50);
   //reset again before reading
   rest_18B20();
   write_18B20(0xcc);//skip reading the serial number
   write_18B20(0xbe);//start sampling, read the register
   delayus(50);
   L=read_18B20();//read the lower eight bits first
   H=read_18B20();//read the higher eight bits later
  tvalue=H;
     tvalue<<=8;
  tvalue=tvalue|L;
    tvalue=tvalue*0.625;//Expand 10 times
   display(tvalue);
   }
}

Keywords:ATMEGA16 Reference address:ATMEGA16 and DS18B20 digital tube display

Previous article:AVR on-chip TWI bus (I2C bus protocol)
Next article:Atmega16 and DS1302 digital tube display program

Recommended ReadingLatest update time:2024-11-16 19:46

C51 single bus timing diagram analysis and low-level programming configuration (DS18B20 as an example)
Preface The commonly used data communications between microcontrollers and peripherals are: I2C, SPI, and SCI buses. I2C: Synchronous serial two-wire system (1 clock line and 1 data line) SPI: Synchronous serial three-wire system (1 clock line 1 input line 1 output line) SCI: Asynchronous communication (1 input line 1
[Microcontroller]
C51 single bus timing diagram analysis and low-level programming configuration (DS18B20 as an example)
51 MCU Course Design: Temperature Alarm Based on DS18B20
This program is used to read the temperature of DS18B20 and has an alarm function. The project is divided into 3 files, main.c, temp.c, and temp.h. After I modified some of the code, it is suitable for Jilin Agricultural University 51 development board. Other friends can also transplant it to other models of developme
[Microcontroller]
Programming to implement single chip microcomputer ds18b20 temperature measurement and display system
Use the ds18b20 sensor on the development board to measure the current temperature, and use the digital tube to output the current temperature value. Achievement results: The test results show that the current temperature is 25.3 degrees Celsius. Implementation principle: (1) Independent digital tube Digital tu
[Microcontroller]
Programming to implement single chip microcomputer ds18b20 temperature measurement and display system
Design of digital thermometer based on DS18B20
In addition to measuring electrical signals, single-chip microcomputers can also be used to measure non-electrical signals such as temperature and humidity. Single-chip temperature detection and temperature control systems that can work independently have been widely used in many fields. The interface signal of the sin
[Microcontroller]
Design of digital thermometer based on DS18B20
Design of DS18B20 driver based on ARM9+Linux
introduction With the development of embedded technology, there are more and more embedded products based on ARM and Linux. DS18B20 temperature acquisition sensor is widely used in industry and life. Research and development of DS18B20 driver based on ARM9 and Linux can meet the application of most temperature acqui
[Power Management]
Design of DS18B20 driver based on ARM9+Linux
51 MCU + DS18B20 temperature control and insulation assembly program
;Water heater program made by DS18B20 and AT89C2051! The two-digit digital tube displays the temperature. When the temperature is set, the digital tube flashes and jumps to the measured temperature after a delay of a few seconds; The positive power supply of DS18B20 is the red line connected to +5 volts, the blue line
[Microcontroller]
MCU·DS18B20 Temperature Sensor
1. Characteristics of DS18B20 1. "One-wire bus" digital temperature sensor. (One-wire bus has good timing, which is relatively simple) 2.   3~5V wide voltage operating range 3.   Resolution 9-12 adjustable   2. Correct wiring of DS18B20                             Facing the flat side, negative i
[Microcontroller]
A new design of multi-point temperature measurement system (based on DS18B20 design of multi-point temperature measurement system
Design of a new multi-point temperature measurement system (multi-point temperature measurement system based on DS18B20) This paper introduces the basic characteristics of DS18B20 and the multi-point temperature measurement system designed based on DS18B20. The system hardware connection and software
[Analog Electronics]
A new design of multi-point temperature measurement system (based on DS18B20 design of multi-point temperature measurement system
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号