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

📄 t.c

📁 使用18B20温度传感器的程序
💻 C
字号:
#include <stdio.h>
#include <reg52.h>
#include<absacc.h> 
#include<clock.h>
#include<ds18b20.h>                                                   
#define LCD_CLS		   	0x01
#define LCD_HOME	   	0x02
#define LCD_SETMODE	   	0x04
#define LCD_SETVISIBLE	0x08
#define LCD_SHIFT	   	0x10
#define LCD_SETFUNCTION	0x38
#define LCD_SETCGADDR	0x40
#define LCD_SETDDADDR	0x80

#define COMWR XBYTE 	[0x0000]
#define DATWR XBYTE 	[0x0001]
#define BUSYRD XBYTE 	[0x0002]
#define DATRD XBYTE 	[0x0003]
#define MAX_DISPLAY_CHAR 0x16

char text[] = {'I','o','L','O','V','E','u','Y','O','U',' ','I',' ','L','O','V','E',' ','Y','O','U','\n'};                                                                          
char pacmanopen[] = {0x0E,0x07,0x03,0x01,0x03,0x07,0x0E,0x00,'\n'}; 
char pacmanshut[] = {0x00,0x0F,0x1F,0x01,0x1F,0x0F,0x00,0x00,'\n'};
uchar idata clock_curent[6]={1,2,3,4,0,1};//装载当前时间和设定时间
uchar idata half=0,updata=1,temph,templ,t,temp[6];
void wrcgchr(char *arrayptr, int offset);
void wrcmd (char cmdcode);
void lcd_init ();
void pause(int num);
void clearscreen ();
void eat ();
void wrdata (char ddata);
void lcd_wait ();
void disp(char pos,char tdata);
void dispcurrenttime(void);//显示时间


void main(void)
{  
	unsigned int tmp;
	char *textptr = text,i;
	bit flag=0;
	IE=0x00;/*关中断*/
	IT0=1;	/*选择负跳变触发*/
	TMOD=0X01;/*计数模式*/
	IP=0X02;  /*优先级*/
	TL0=0XAF;//初值0.05秒
	TH0=0X3C;
	SP=0X60;
	IE=0x80;/*开中断*/	
	TR0=1; 
	
	lcd_init(); // Initialise the LCD Display 
	
	while(1)
	{
	IE=0x80;
	do	
	flag=reset18b20();
	while(flag==1);	
	
	writebyte18b20(0xcc);
	writebyte18b20(0x44);
	delay(50);
	do	
	flag=reset18b20();
	while(flag==1);
	
	writebyte18b20(0xcc);
	writebyte18b20(0xbe);	
	templ=readbyte18b20();
	temph=readbyte18b20();
	for(i=0;i<6;i++)	
	t=readbyte18b20();
	

/*	if(temph/16>9)
	temp[0]=(temph/16)-10+'A';
	else
	temp[0]=(temph/16)+'0';

	if(temph%16>9)
	temp[1]=(temph%16)-10+'A';
	else
	temp[1]=(temph%16)+'0';

	if(templ/16>9)
	temp[2]=(templ/16)-10+'A';
	else
	temp[2]=(templ/16)+'0';

	if(templ%16>9)
	temp[3]=(templ%16)-10+'A';
	else 
	temp[3]=(templ%16)+'0';

	for(i=8;i<12;i++)
	disp(i,temp[i-8]);
*///
	tmp=temph*256+templ;
	
	temp[4]='.';
	if(temph>=0xf8)
		{
			temp[0]='-';
			//templ=~templ;
			//templ++;
			tmp=~tmp;
			tmp++;
		}
	else
		temp[0]='+';
	if(templ&0x08)	
		temp[5]='5';
	else
		temp[5]='0';
	
	//templ/=2;
	tmp>>=4;
	for(i=3;i>0;i--)
	{
	temp[i]=(tmp%10)+'0';
	tmp=tmp/10;
//	temp[i]=(templ%10)+'0';
//	templ=templ/10;
	}
		
	for(i=0;i<6;i++)
	disp(10+i,temp[i]);		  

//}
	IE=0X82;
//	while(1)
//	{
		if(updata==1)
		{
			add_second(clock_curent);
			updata=0;
			dispcurrenttime();
		}
}


                               
   for (;;)                                // Loop Forever. 
    { textptr = text;
      while (*textptr!= '\n')             // Write the String.
       { 
	  	wrdata(*textptr++);	
        pause(2000);
       }
      pause(5000);
      eat();                               // Show and Animate the pacman. 
      pause(5000);
    }  
 }

void dispcurrenttime(void)//显示时间
{	
	if(clock_curent[5]==0)
		disp(0,11+'0');	//首位0消隐
	else 
		disp(0,clock_curent[5]+'0');//小时
	disp(1,clock_curent[4]+'0');
	disp(2,10+'0');
	disp(3,clock_curent[3]+'0');//分钟
	disp(4,clock_curent[2]+'0');
	disp(5,10+'0');
	disp(6,clock_curent[1]+'0');//秒钟
	disp(7,clock_curent[0]+'0');
}

void disp(char pos,char tdata)
{
wrcmd(LCD_SETDDADDR+pos);
wrdata(tdata);

}

void service_second(void) interrupt 1 /*外部中断置标志*/
{ 
	TR0=0;  	
	TL0=0XAF;
	TH0=0X3C;
	TR0=1;
	half++;
	if(half==2)
	{
		updata=1;
		half=0;
	}
}

void eat ()
 { 
int count = MAX_DISPLAY_CHAR-1, eatflag = 0 , tmp;
   while (count >= 0)
    { 
      wrcmd(LCD_SETDDADDR + count);                       // Work from right to left.        
      eatflag ? wrdata(0x00):  wrdata(0x01);              // Toggle the custom graphics between mouth open and mouth shut. 
      eatflag ^= 1;                                       
      for (tmp = count; tmp <=MAX_DISPLAY_CHAR; ++tmp)    // Anything to the right of pacman is wiped.
         wrdata(' ');
      count--;                                            // Decrement the count (moving us 1 to the left).                
      pause(4000);
    }
   clearscreen();                                         // Clear the final pacman graphic when we are done.
 }
 
void pause(int num)
{ 
  while(num--)
 ;
} 

void lcd_init (void)
 {
   wrcmd(LCD_SETFUNCTION);                    // 8-bit mode - 1 line - 5x7 font. 
   wrcmd(LCD_SETVISIBLE+0x04);                // Display no cursor - no blink.
   wrcmd(LCD_SETMODE+0x02);                   // Automatic Increment - No Display shift. 											// Write Custom Character to CG RAM.
   wrcgchr(pacmanopen, 0);                    // Offset 0 - 40h-47h in CGRAM.
   wrcgchr(pacmanshut, 8);                    // Offset 8 - 48h-4Fh in CGRAM.
   wrcmd(LCD_SETDDADDR);                      // Address DDRAM with 0 offset 80h.
 }
 
void clearscreen ()
 { 
 	wrcmd(LCD_CLS);
   	wrcmd(LCD_SETDDADDR);
 }

void wrcmd (char cmdcode)
 { 
 	lcd_wait(); 
 	COMWR=cmdcode;
                              
 }
     
void wrdata (char ddata)
{
	lcd_wait();
	DATWR=ddata;
                                       // Call the wait routine.
 }

void wrcgchr(char *arrayptr, int offset)
 {
 	wrcmd(LCD_SETCGADDR + offset);               
   while (*arrayptr != '\n') 
     wrdata (*arrayptr++);  
 }

void lcd_wait ()
 { char status;
   do
    {  	P0=0xff;
		status=P0;       
    } 
	while (status & 0x80);    
 }






⌨️ 快捷键说明

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