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

📄 yw001.c

📁 基于DS18B20的数字温度计
💻 C
字号:
#include <avr/io.h>
#include <avr/signal.h>
#include <avr/interrupt.h>


#define uchar unsigned char
#define uint unsigned int


float temp0;
uint temp;
uchar temp_buff[9];  //Read Scratchpad is 9Bytes, Read Rom is 8 Bytes
uchar id_buff[8];
uchar z_biao[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x6F,0xBF};

uchar *p;
uchar crc_data;
uchar flag;

float tm;
int up[4];

const uchar crc_table[256]=
{
    0,  94,  188,  226,  97,  63,  221,   131,  194,  156,   126,  32,  163,	253,   31,    65, 
	157,195, 33,   127,  252, 162, 64,     30,  95,    1,   227,   189,  62,     96,  130,  220,
	35,  125, 159, 193,  66,  28,  254,   160,  225,   191,  93,   3,   128,    222,   60,   98,
	190, 224, 2,    92,  223, 129,  99,   61,   124,  34,   192,   158,  29,     67,   161,  255,
	70,  24,  250,  164, 39,  121, 155,   197,  132,  218,   56,   102,  229,    187,  89,   7,
	219, 133, 103, 57,  186,  228, 6,     88,   25,   71,   165,   251,  120,    38,  196,  154,
	101, 59,  217, 135,  4,   90,  184,   230,  167,  249,  27,    69,   198,    152, 122,  36,
	248, 166, 68,  26,   153, 199, 37,    123,  58,   100,  134,   216,  91,     5,   231,  185,
	140, 210, 48,  110,  237, 179, 81,    15,   78,   16,   242,   172,  47,     113, 147,  205,
	17,  79,  173, 243,  112, 46,  204,   146,  211,  141,  111,   49,   178,    236,  14,  80,
	175, 241, 19,   77,  206, 144, 114,    44,  109,  51,   209, 143,    12,      82,  176, 238,
	50, 108,  142,  208, 83,  13, 239,   177,   240,  174,  76,  18,   145,     207,   45,  115,
	202, 148,  118,  40, 171, 245, 23,   73,    8,    86,   180,  234,  105,    55,   213,  139,
	87,   9,  235,  181,  54,  104, 138,  212,  149,   203,  41,  119,   244,    170,   72, 22, 
	233, 183,  85,  11,  136,  214, 52,   106,  43,   117,  151,  201,   74,     20,  246, 168,
	116, 42,   200, 150,  21,  75,  169,  247,  182,  232,   10,   84,   215,   137,   107, 53   
};



uchar flagint=0;

void delay(uint us)
{
    while(us--);  //us=100, delay=200us

}


void init_18b20(void)
{
    
  	uchar rd;
	DDRA =0x01;  // PA0 as Output
	PORTA =0x01;
	delay(100);
	PORTA = 0x00;
	delay(270);   // Delay 540us
	PORTA = 0x01;
	delay(18);    // Delay 36us
	DDRA = 0x00;
	delay(0);
	rd=PINA;
	rd=(rd & 0x01);
	
	if(rd==0)
	   flag=1;
	else
	   flag=0;
	delay(240);    // Delay 480us
	DDRA=0x01;
	PORTA=0x01;
}

void write(uchar wr)  // Write single byte
{
    uchar i;
	DDRA=0x01;
	for(i=0;i<8;i++)
	{
	    PORTA=0x00;
		delay(3);    // Hold on 0 for < 15us
		PORTA=(wr & 0x01);
		delay(30);   // Delay 60us
		PORTA=0x01;
		delay(0);
		wr>>=1;
	}
}

uchar read_byte(void)   // Read single byte
{
    uchar i,u=0,rd;
	for(i=0;i<8;i++)
	{
	    DDRA = 0x01;
		PORTA = 0x00;
		u>>=1;
		delay(3);      // delay =12us 
		PORTA = 0x01;
		DDRA = 0x00;
		delay(0);
		rd=PINA;
		if(rd & 0x01)
		   u |= 0x80;
        delay(32);
	}
	return(u);
}

void read_bytes(uchar j)  // Read j Bytes
{
    uchar i;
	for(i=0;i<j;i++)
	{
	    *p=read_byte();
		p++;
	}
}

uchar crc(uchar j)  // Lookup table for CRC check
{
    uchar i,crc_data=0;
	for(i=0;i<j;i++)
	{
	 	crc_data=crc_table[crc_data ^ temp_buff[i]];
	}
    return(crc_data);
}

void get_temp(void) // CRC OK
{
    //p=temp_buf;
	read_bytes(9);
	if(crc(9)==0)
	{
		temp=temp_buff[1]*0x100 + temp_buff[0];
		temp0=temp*0.0625;
	}
}


void config(void)
{
	init_18b20();
	write(0xCC);   //skip ROM
	write(0x4E);   //write scratchpad
	write(0x50);   //TH  80 degrees
	write(0x00);   //TL   0 degrees
	write(0x7F);   //set 12bit

	init_18b20();
	write(0xCC);
	write(0x48);   // save config value
	
	init_18b20();
	write(0xCC);   //skip ROM
	write(0xB8);   //Recall config value
}


void read_id()   //Read id
{
 	init_18b20();
	write(0x33);
	read_bytes(8);
}


/*display function

    PB0~7  link to  a~dp
    PD4    link to  com1~4
*/
void displayT(uchar s)      //共阳数码管
{
  DDRB=0xFF;
  DDRC=0x0F;
  PORTB=0xFF;
  PORTD=0x00;        //灭灯

  if(tm<0)
  {  
    PORTB = z_biao[9];   
	PORTC = 0x08;        //最高位显示 "—" 
    delay(3);
	
	PORTB = z_biao[up[0]];
    PORTC = 0x04;            
    delay(3); 

    PORTC = 0x0F;
    PORTB = z_biao[up[1]] & 0x7F;    //点亮小数点
    PORTC = 0x02;
    delay(3);

    PORTC = 0x0F;
    PORTB = z_biao[up[2]];
    PCRTC = 0x01;
    delay(3); 
  } 
  else
  {
    PORTB = z_biao[up[0]];
    PORTC = 0x08;            //数码管选通开关 : 此处为最高位
    delay(3); 

    PORTC = 0x0F;
    PORTB = z_biao[up[1]] & 0x7F;    //点亮小数点
    PORTC = 0x04;
    delay(3);

    PORTC = 0x0F;
    PORTB = z_biao[up[2]];
    PCRTC = 0x02;
    delay(3);

    PORTC = 0x0F;
    PORTB = z_biao[up[4]];
    PORTC = 0x01;
    delay(3);
  }  

}




uchar xr,yr;
uint i,j;

int main() 
{
 

	flag=0;
	SFIOR &= 0xEB; // SFIOR.PUD = 0   
    
    p=id_buff;
	read_id();
	config();
 
    TCCR1A = 0x00;
    TCCR1B = 0x03;  //  8/64MHz
    TIMSK |= 0x04;  //  Enable Overflow Interrupt

   	
	

    SREG |= 0x80;   //  Global Interrupt Enable
    
    while(1)
	{
         
		        

	
		 SREG |= 0x80;
		 // To Do: Display Temperature

	}
    return 0;
}


// ADC Interrupt:  SIGNAL(SIG_ADC)
// INT0 Interrupt: SIGNAl(SIG_INTERRUPT0)
// INT1 Interrupt: SIGNAl(SIG_INTERRUPT1)
// INT2 Interrupt: SIGNAl(SIG_INTERRUPT2)

SIGNAL(SIG_OVERFLOW1)
{
   /*  // Test time 1.04s
   DDRB=0x01;
   if(flagint)
   {
       flagint=0;
	   PORTB=0x01;
   }
   else
   {
       flagint=1;
	   PORTB=0x00;
   }

   TCNT1=0xBDC;

   */
  
   flagint++;    // 1s

   if(flagint>1)
   {
       flagint=0;
	   init_18b20();
	   write(0xCC);   //skip ROM
	   write(0x44);   //temp convert
	   init_18b20();
	   write(0xCC);   //skip ROM
	   write(0xBE);   //read temperature
	   p=temp_buff;
	   get_temp();
	   delay(10);    //delay 2ms
	  
   }
   TCNT1=0xBDC;

   tm=temp0+0.005;  
   up[0]=(int)(tm/10.0);
   up[1]=(int)(tm-up[0]*10.0);
   up[2]=(int)((tm-up[0]*10.0-up[1])*10);
   up[3]=(int)((tm-up[0]*10.0-up[1]-up[2]/10.0)*100.0);   //must /10.0 if /10 ,wrong
   displayT();
 
}





⌨️ 快捷键说明

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