DS18b20 programming under Atmega8, serial communication computer display

Publisher:boczsy2018Latest update time:2016-01-06 Source: eefocusKeywords:DS18b20 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
=******************************************************

MCU:Atmega8

Internal oscillator: 8M

Environment: ICCAVR 7.0

Program: sdyzxue Xue Haitao

2010/06/16

=*******************************************************
#include
#include
#define fosc 8000000
#define baud 9600
#define SEI() asm("sei")
#define CLR_DIR_1WIRE DDRC&=~BIT(1)     //Just modify the parameters here! Haha!
#define SET_DIR_1WIRE DDRC|=BIT(1)      //No need to modify anything inside!
#define CLR_OP_1WIRE PORTC&=~BIT(1)
#define SET_OP_1WIRE PORTC|=BIT(1)       
#define CHECK_IP_1WIRE (PINC & 0x02)    //Check
unsigned char wmh,wmm,wml;
unsigned int  temp,temp0,temp1,temp2;

unsigned char RX_data[10]={0};      //Data received by the serial port
unsigned char RX_counter=0;        //Counter of bytes received by the serial port

void delay_1us(void)                 //1us delay function
 {
  asm("nop");
 }

void delay_nus(unsigned int n)       //N us延时函数
  {
   unsigned int i=0;
   for (i=0;i    delay_1us();
  }
 
void delay_1ms(void)                 //1ms延时函数
  {
   unsigned int i;
   for (i=0;i<1140;i++);
  }
 
void delay_nms(unsigned int n)       //N ms延时函数
  {
   unsigned int i=0;
   for (i=0;i    delay_1ms();
  }
 
void putchar(unsigned char c)
{
   while (!(UCSRA&(1<    UDR=c;
}

//Character input function
unsigned char getchar(void)
{
   //Wait for receiving character
   while(!(UCSRA&(1<    return UDR;
}


//String output function
int puts(char *s)
{
   while (*s)
   {
        putchar(*s);
        s++;
    }
    return 1;
}

//String output function with carriage return and line feed
void puts_hh(char *s)
{
   while (*s)
   {
         putchar(*s);
        s++;
    }
//      putchar(0x0a); //line feedputchar 
     (0x0d);  //carriage return

}

//UART initialization
void uart_init(void)
{

UCSRB=(1<          //Enable sending and receiving  (RXCIE) and respond to the receive completion interrupt
     UBRRL=(fosc/16/baud+1)%256;
     UBRRH=(fosc/16/baud+1)/256;
     UCSRC=(1<         //8-bit data+1-bit stop bit
}


#pragma interrupt_handler UART_rx: iv_USART_RX
void UART_rx(void)                   //Serial port receive interrupt function
         RX_data[RX_counter] = UDR;
          
          if (RX_data[RX_counter]=='t')
            {
               RX_data[0]=RX_data[RX_counter];
               RX_counter=0;
            }
          
          RX_counter++;                   //Count the number of bytes received
       
}
void init_1820() 

   SET_DIR_1WIRE;                //Set PC2 as output
   SET_OP_1WIRE;   
   CLR_OP_1WIRE; 
   delay_nus(480);    //More than 480us 
   SET_OP_1WIRE; 
   CLR_DIR_1WIRE; 
   delay_nus(20);     //15~60us 
   while(CHECK_IP_1WIRE); 
   SET_DIR_1WIRE; 
   SET_OP_1WIRE; 
   delay_nus(140);   //60~240us 

void write_1820(unsigned char x) 
   
    unsigned char m; 
   for(m=0;m<8;m++) 
   
      CLR_OP_1WIRE; 
      if(x&(1<  //Write data, write the low bit first!
      SET_OP_1WIRE; 
      else 
      {CLR_OP_1WIRE;} 
      delay_nus(40);   //15~60us 
      SET_OP_1WIRE; 
   
    SET_OP_1WIRE; 

unsigned char read_1820() 
    
   unsigned char temp,k,n; 
   temp=0; 
   for(n=0;n<8;n++) 
      
     CLR_OP_1WIRE; 
     SET_OP_1WIRE; 
     CLR_DIR_1WIRE; 
     k=(CHECK_IP_1WIRE);    //Read data, start from the low bit 
     if(k) 
     temp|=(1<
     else 
     temp&=~(1<
     delay_nus(50); //60~120us      
     SET_DIR_1WIRE; 
  
  return (temp); 
}

void gettemp()                   //Read temperature value
 
    unsigned char temh,teml; 
    init_1820();        //Reset 18b20 
    write_1820(0xcc);   //Issue conversion command 
    write_1820(0x44);
    init_1820(); 
    write_1820(0xcc);  //Issue read command 
    write_1820(0xbe); 
    teml=read_1820();  //Read data 
    temh=read_1820();
 temp0=teml&0x0F;             //Decimal place
    temp1=(temh<<4)|(teml>>4);  //Integer place
    temp2=(temh>>3)&0x1F;        //Positive and negative temperature
    wmh=temp1/100;        //Above one hundred, output hundreds word
    wmm=(temp1%100)/10;   //Export parameters! wmh is the high tens digit displayed, wml is the low ones digit displayed
    wml=(temp1%100)%10;
    temp=temp0*625;        //decimal value
}


void main(void) //main function
{
 OSCCAL=0xb7; //system clock calibration, different chips and different frequencies,
   DDRC=0XFF;
   PORTC=0X00;
 uart_init();
 while(1)
          gettemp();

puts_hh("=====Temp Test=====");
   if(temp2)//Negative temperature
              {
             
              putchar('-');
               
              }
           if(!temp2)//Positive temperature
             {
             
              putchar('+');
               
             }

putchar(wmh+0x30);//
               hundredputchar(wmm+0x30);//tenputchar
               (wml+0x30);//
               oneputchar('.');
               putchar(temp/1000+0x30);
               putchar((temp%1000)/100+0x30);
               putchar((temp%100)/10+0x30);
               putchar(temp%10+0x30);
    
               putchar(0x0d);  //Enter

delay_nms(10000);   
    }
}

Keywords:DS18b20 Reference address:DS18b20 programming under Atmega8, serial communication computer display

Previous article:AVR series watchdog
Next article:Use the RS232 port of the PC to control the LED light

Latest Microcontroller Articles
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号