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

📄 main.txt

📁 能够详细测量正负温度的且小数点后四位的测温系统
💻 TXT
字号:
#include <iom16v.h>
#include <macros.h>
#include <1602.h>
#define CLR_DIR_1WIRE DDRC&=~BIT(1)     //只要修改这里的参数就可以了!呵呵! 
#define SET_DIR_1WIRE DDRC|=BIT(1)      //里面什么都不用该! 
#define CLR_OP_1WIRE PORTC&=~BIT(1) 
#define SET_OP_1WIRE PORTC|=BIT(1)       
#define CHECK_IP_1WIRE (PINC & 0x02)    //检测 
unsigned char table[]={'0','1','2','3','4','5','6','7','8','9'};
unsigned char wmh,wmm,wml; 
unsigned int  temp,temp0,temp1,temp2;

void init_1820()  
{  
    SET_DIR_1WIRE;                //设置PC2 为输出 
    SET_OP_1WIRE;   
    CLR_OP_1WIRE;  
    delay_nus(480);    //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<<m))    //写数据了,先写低位的! 
       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);    //读数据,从低位开始  
      if(k)  
      temp|=(1<<n);  
      else  
      temp&=~(1<<n);  
      delay_nus(50); //60~120us      
      SET_DIR_1WIRE;  
   }  
   return (temp);  
}   

void gettemp()                   //读取温度值 
{   
     unsigned char temh,teml;  
     init_1820();        //复位18b20  
     write_1820(0xcc);   // 发出转换命令  
     write_1820(0x44); 
     init_1820();  
     write_1820(0xcc);  //发出读命令  
     write_1820(0xbe);  
     teml=read_1820();  //读数据  
     temh=read_1820(); 
	 temp0=teml&0x0F;             //小数位
     temp1=(temh<<4)|(teml>>4);  //整数位
     temp2=(temh>>3)&0x1F;        //正负温度
     wmh=temp1/100;        //一百以上输出百位字
     wmm=(temp1%100)/10;   //出口参数了!wmh是显示的高位,wml使显示的低位 
     wml=(temp1%100)%10; 
     temp=temp0*625;        //小数值
}   
void main(void)
{
    DDRC=0XFF;
    PORTC=0X00;
    DDRA=0XFF;
    PORTA=0X00;
    LCD_init();
  while (1)
   {
         gettemp();
		 LCD_write_string(0,0,"Now the temp is");
         LCD_write_char(1,1,table[wmh]);
         LCD_write_char(2,1,table[wmm]);
	     LCD_write_char(3,1,table[wml]);
	     LCD_write_char(4,1,'.');
	     LCD_write_char(5,1,table[temp/1000]);
         LCD_write_char(6,1,table[(temp%1000)/100]);
	     LCD_write_char(7,1,table[(temp%100)/10]);
	     LCD_write_char(8,1,table[temp%10]);
     
      if(temp2)//负温度
        { 
       
         LCD_write_char(0,1,'-');
         
        }
     if(!temp2)//正温度
       { 
       
         LCD_write_char(0,1,'+');
         
       }
    }
}

⌨️ 快捷键说明

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