⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 8mh温度程序.txt

📁 18b20的源程序
💻 TXT
字号:
#include <avr/io.h> //和单片机类型相对应的头文件,选择Atmega8做实验; 
//#include <stdio.h> 
//#include <string.h>
#include <util/delay.h>     //延时的宏
#define uchar unsigned char 
#define uint unsigned int 
void init_1820(); 
uchar write_1820(uchar x); 
uchar read_1820(); 
void send_byte(uchar x); 
void delay(uint x); 
void disp_led(uchar buffer,uchar control); 
uchar  disp_table[16] = //{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
                         //0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
 
        {0x3f,
		0x06, 
		0x5b, 
		0x4f,  
		0x66,  
		0x6d,
		0x7d,
		0x07, 
        0x7f,
		0x6f,
		0x77,
		0x7c,
		0x39,
		0x5e,
		0x79,
		0x71};
uchar dp; 
long count;
uchar flag=0; 
void main(void) //主函数 
 { 
//  disp_led(0,0); 
//  delay(2000); 
//  OSCCAL=0X9d;//系统时钟校准,不同的芯片和不同的频率, 
  DDRC=0xff; 
  DDRD=0XFF;
  DDRB=0xff;
  PORTD=0XFF; 
  //WDR();       //看门狗计数清零 
  //WDTCR=0x0F; 
  PORTC=0xff;
  while(1) 
   {
               uchar i,temh,teml; 
                init_1820();        //复位18b20 
                write_1820(0xcc);   // 发出转换命令 
                write_1820(0x44); 
                delay(400); 
                init_1820(); 
               // WDR(); 
                write_1820(0xcc);  //发出读命令 
                write_1820(0xbe); 
               teml=read_1820();  //读数据 
              temh=read_1820();
				  if((temh&0xF8) == 0xF8) 
              { 
                 flag=1; 
                 //count=((0xFF-temh)*256+(0xFF-teml)+1)*5; 
                  count=((0xFF-temh)*256+(0xFF-teml)+1)*0.625; 
              } 
            else 
              {        
                  flag=0; 
                 // count=(temh*256+teml)*5; 
                  count=(temh*256+teml)*0.625; //计算具体温度 
              } 
               // WDR(); 
                disp_led(0,1);     //显示温度 
 
              for(i=0;i<100;i++)   //每次转换需要延时200ms以上 
                 delay(1000); 
                 
              
   } 
 } 
void delay(uint x)  //每一个数1.5us左右 
{ 
          while(x) 
         { 
            x--; 
         } 
} 
void init_1820() 
 {uint i;
    PORTD|=(1<<6);    
    PORTD&=~(1<<6); 
     for(i=0;i<10;i++)         //拉低 500us
   {
     _delay_us(50);    
   } 
    PORTD|=(1<<6); 
    DDRD&=~(1<<6); 
     _delay_us(50);     
    while(PIND&(1<<6)) 
    { 
    ;
    } 
    DDRD|=(1<<6); 
    PORTD|=(1<<6); 
     for(i=0;i<4;i++)  //回复的低电平在60到240US       
  {
     _delay_us(60);    
  }   
 } 
uchar write_1820(uchar x) 
 {   
     uchar m; 
         for(m=0;m<8;m++) 
         { 
                 PORTD&=~(1<<6); 
                 if(x&(1<<m))    //写数据,从低位开始 
                 PORTD|=(1<<6); 
                 else 
                 PORTD&=~(1<<6); 
                _delay_us(60);  //15~60us 
                 PORTD|=(1<<6); 
         } 
          PORTD|=(1<<6); 
 }  

uchar read_1820() 
{    
         uchar temp,k,n; 
         temp=0; 
         for(n=0;n<8;n++) 
            { 
                PORTD&=~(1<<6); 
				asm("nop");
                //delay(2);      
                PORTD|=(1<<6); 
                //delay(3);    
                DDRD&=~(1<<6); 
                k=(PIND&(1<<6));    //读数据,从低位开始 
                if(k) 
                temp|=(1<<n); 
                else 
                temp&=~(1<<n); 
                _delay_us(60); //60~120us    
                DDRD|=(1<<6); 
            } 
        return (temp); 
} 

void send_byte(uchar x)    //以下为显示程序 
{ 
        uchar i; 
         for(i=0;i<8;i++) 
         { 
              PORTC&=~(1<<5);   // PC5为底  为164提供时钟信号 
              if((x&(1<<(7-i)))||((dp==1)&&(i==0))) //判断每位数据的电平,及小数点判断 
              PORTC|=(1<<4);                  //若为高着PC4输出高电平   
                else 
                PORTC&=~(1<<4);                           //若为低着输出低电平 
                PORTC|=(1<<5);   //PC5 提供时钟信号 
         } 
        //PORTC|=((1<<0)|(1<<1)|(1<<2)); 
} 
//显示程序 CONTROL为控制显示 BUFFER为显示数据 
void disp_led(uchar buffer,uchar control) 
{ 
        uchar i,temp[4]; 
        uint tempcount; 
        dp=0;
		
        switch(control) 
        { 
                case 0:                 //CONTROL为零全部数码管显示buffer  
                { 
                        for(i=0;i<11;i++) 
                        send_byte(disp_table[buffer%10]);//显示数字 
                        break; 
                } 
                case 1:                //control为1,显示count中的数据为6位 
                {       //if(flag==0)  
				        tempcount=count;
                        for(i=0;i<4;i++)   //取出每位中的数据 
                        { 
                                temp[i]=tempcount%10; 
                                tempcount/=10; 
                        } 
                        send_byte(disp_table[buffer/10]);  //最开始显示buffer数据 
                        send_byte(disp_table[buffer%10]); 
                        send_byte(0x00);          
                        send_byte(0x00); 
                        send_byte(0x00);
                    
						PORTB=0xff;
                        i=3;
                        send_byte(disp_table[temp[3-i]]);
                        PORTB=0xfe;  
						dp=1;
						
						_delay_ms(1);
						PORTB=0xff;
						i=2;
                        send_byte(disp_table[temp[3-i]]); 
                        PORTB=0xfd;
						dp=0;
						_delay_ms(1);
						PORTB=0xff;
                        i=1;   
//                           
                        send_byte(disp_table[temp[3-i]]); 
						PORTB=0xfb;
                        dp=0; 
						
						_delay_ms(1);
						PORTB=0xff;
						if(flag==0)
						   {i=0;
                            send_byte(disp_table[temp[3-i]]); 
                            PORTB=0xf7;
						    dp=0;
						    _delay_ms(1);
                           }
                        else
                          {i=0;
                           send_byte(0x40); 
                           PORTB=0xf7;
						   dp=0;
						   _delay_ms(1);
					       } 
                break;  
                }
        } 
        PORTC|=(1<<4); 
}


void DelayUS(unsigned int time) 
{   
     while(time>1) 
            time--; 
} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -