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

📄 1602.c

📁 电源性能测试,使用AT89S52+AT24C02+LCD1602,编译通过,有掉电保护功能
💻 C
字号:
#include <at89x51.h>
#include <intrins.h>
sbit LcdRs		= P2^0;
sbit LcdRw		= P2^1;
sbit LcdEn  	= P2^2;
#define  DBPort   P0
/***************************************************************************/
#define	WriteDeviceAddress 0xa0
#define	ReadDviceAddress 0xa1
sbit	SCL=P2^4;
sbit	SDA=P2^5;
unsigned char code tab[10]={'0','1','2','3','4','5','6','7','8','9'}; //1602液晶显示代码表
unsigned char number=2 ;
unsigned char num[3]={' ','2','\0'} ;
unsigned char second,minite,hour,day ; 
unsigned char buffer[13]={' ','0',' ',' ','0','0',':','0','0',':','0','0','\0'} ;
unsigned char shi,qian,shiwan ;
unsigned char buf[7]={' ',' ',' ',' ',' ','0','\0'} ;
unsigned char line=0;
unsigned char buff[2]={'L',' '};
unsigned char bufss[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
unsigned char bufs[8];
unsigned adder[9]={0x0,0x08,0x10,0x18,0x20,0x28,0x30,0x38,0x40};
bit flag1=0;
bit flag2=0;
unsigned  char q=0;
bit f0=1,f1=1,f2=1,f3=1,f4=1,f5=1,f6=1,f7=1;
//内部等待函数**************************************************************************
unsigned char LCD_Wait(void)
{
	LcdRs=0;
	LcdRw=1;
	_nop_();
	LcdEn=1;
	_nop_();
	LcdEn=0;
	return DBPort;		
}
//向LCD写入命令或数据************************************************************
#define LCD_COMMAND			0      // Command
#define LCD_DATA			1      // Data
void LCD_Write(bit style, unsigned char input)
{
	LcdEn=0;
	LcdRs=style;
	LcdRw=0;		_nop_();
	DBPort=input;	_nop_();//注意顺序
	LcdEn=1;		_nop_();//注意顺序
	LcdEn=0;		_nop_();
	LCD_Wait();	
}
void LCD_Initial()            //初始化LCD************************
{
	LcdEn=0;
	LCD_Write(LCD_COMMAND,0x38);           //8位数据端口,2行显示,5*7点阵
	LCD_Write(LCD_COMMAND,0x38);
	LCD_Write(LCD_COMMAND,0x0c);    //开启显示, 无光标
	LCD_Write(LCD_COMMAND,0x01);   //清屏
	LCD_Write(LCD_COMMAND,0x06);       //AC递增, 画面不动
}
void GotoXY(unsigned char x, unsigned char y)
{
	if(y==0)
		LCD_Write(LCD_COMMAND,0x80|x);
	if(y==1)
		LCD_Write(LCD_COMMAND,0x80|(x-0x40));
}
void Print(unsigned char *str)
{
	while(*str!='\0')
	{
		LCD_Write(LCD_DATA,*str);
		str++;
	}
}

///////////////////////////////////////////////////////////////////////////

void delay(unsigned char i)//12MHZ 延时1ms
{
	unsigned char j;
for(i;i;i--)
    
	for(j=112;j;j--);
}
/***************************************************************************/
void Start()
{
   
	SDA=1;
	_nop_();
	SCL=1;
	_nop_();
	_nop_();
	_nop_(); 
	_nop_(); 
	_nop_();
	SDA=0;
	_nop_();
	_nop_();
	_nop_(); 
	_nop_();
	SCL=0;
	_nop_();
}

/***************************************************************************/
void Stop()
{

	SDA=0;
	_nop_(); 
	_nop_();
	SCL=1;
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	_nop_();
	SDA=1; 
	_nop_();
	_nop_();
	_nop_();
	_nop_(); 
	_nop_();
}

/***************************************************************************/
void Ack() 
{
	SDA=0; 
	_nop_(); 
	_nop_();
	SCL=1; 
	_nop_();
	_nop_();
	_nop_(); 
	_nop_();  
	_nop_();
	SCL=0; 
	_nop_(); 
	_nop_();
	
}

/***************************************************************************/
void NoAck() 
{
	SDA=1; 
	_nop_(); 
	_nop_();
	SCL=1; 
	_nop_();
	_nop_();
	_nop_(); 
	_nop_(); 
	_nop_();
	SCL=0;
	_nop_(); 
	_nop_();
}

/***************************************************************************/
bit TestAck() 
{
	bit ErrorBit;
	SDA=1;
	_nop_();  
	_nop_();
	SCL=1;
	_nop_();
	_nop_();
	ErrorBit=SDA;
	SCL=0; 
	_nop_();
	return(ErrorBit);
}

/***************************************************************************/
 Write8Bit(unsigned char input)
{
	unsigned char temp;
	for(temp=8;temp!=0;temp--) 
	{
		SDA=(bit)(input&0x80);
		_nop_();
		SCL=1; 
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		SCL=0;
		input=input<<1;
	}
}

/***************************************************************************/
void Write24c02(unsigned char *Wdata,unsigned char RomAddress,unsigned char number) 
{
	Start();
	Write8Bit(WriteDeviceAddress);
	TestAck();
	Write8Bit(RomAddress);
	TestAck();
	for(;number;number--)
		{
			Write8Bit(*Wdata);
			TestAck();
			Wdata++;
		}
	Stop();
	delay(10);
}

/***************************************************************************/
unsigned char Read8Bit() 
{
	unsigned char temp,rbyte=0;
	for(temp=8;temp;temp--)
	{
		SDA=1;
		_nop_();
		SCL=1;
		_nop_(); 
		_nop_();
		rbyte=rbyte<<1;
		if(SDA)rbyte=rbyte|0x01;
		SCL=0;
		_nop_();
		_nop_();
		_nop_();
		_nop_();
		_nop_();
	}
	return(rbyte);
}

/***************************************************************************/
void Read24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes) {
	Start();
	Write8Bit(WriteDeviceAddress);
	TestAck();
	Write8Bit(RomAddress);
	TestAck();
	Start();
	Write8Bit(ReadDviceAddress);
	TestAck();
	while(bytes!=1) 
    {
		*RamAddress=Read8Bit();
		Ack();
		RamAddress++;
		bytes--;
	}
	*RamAddress=Read8Bit();
	NoAck();
	Stop();
}

/***************************************************************************/
void readmemory(void)
{
	Read24c02(&bufs[0],adder[line],8);
	day=bufs[0];
	hour=bufs[1]; 
	minite=bufs[2];
	second=bufs[3]; 
	number=bufs[4]; 
	shiwan=bufs[5];
	qian=bufs[6];
	shi=bufs[7];
}

void t0(void) interrupt 1 using 0 
{ 
	unsigned int tcnt,count; 
	unsigned char half_sec;
	++tcnt; 
	++count;
	if(count==200)
	    { count=0;
		  ++half_sec;
		  if(half_sec==number)
		   {half_sec=0;P2_7=~P2_7;}
         }
	if(tcnt==400) 
		{ 
		tcnt=0;
		++second;
		if(second==60) 
			{ 
			second=0; 
			++minite;
			if(minite==60) 
				{ 
				minite=0;
				++hour;   
				if(hour==24)
					{
					hour=0;
					++day; 
					}
				} 
			}
		}
}

void getkey(void)

{
	unsigned char k;
	k=P3&0xf3;
	if(k!=0xf3)
	{
		delay(2);if(k!=0xf3)
	    {
		if(P3_0==0)
			{  
				flag1=1; flag2=0; TR0=0;
			}
		if(P3_1==0)
			{  
				flag1=0; flag2=1; TR0=0;readmemory();
			} 
		if(P3_4==0)
			{
				flag2=0;TR0=0;
				Write24c02(&bufss[0],0x0,16);
				Write24c02(&bufss[0],0x10,16);
				Write24c02(&bufss[0],0x20,16);
				Write24c02(&bufss[0],0x30,16);
				Write24c02(&bufss[0],0x40,16);
				while(P3_4==0);
			}
			
		if(P3_5==0)
			{
				if(flag1){number++;if(number==99)number=98;}   
				if(flag2){line++;if(line==9)line=1;readmemory();}
				while(P3_5==0);
			}
		if(P3_6==0)
			{
				if(flag1){number--;if(number==0)number=1;}
				if(flag2){line--;if(line==0)line=8;readmemory();}
				while(P3_6==0);
			}
		if(P3_7==0)
			{
	            TR0=1;flag2=0;
			}
	
	     }
     }
}
void it0(void) interrupt 0 
{      
	++shi;
	if(shi==100)
	{
		shi=0;
		++qian;
		if(qian==100)
			{
				qian=0;
				++shiwan;
			}
	}
}

void display(void)
{   
	unsigned char i;
	buffer[11]=tab[second%10]; 
	buffer[10]=tab[second/10]; 
	buffer[8]=tab[minite%10]; 
	buffer[7]=tab[minite/10];  
	buffer[5]=tab[hour%10]; 
	buffer[4]=tab[hour/10];
	buffer[1]=tab[day%10];
	if(day>9)buffer[0]=tab[day/10];
	else buffer[0]=' ';
	GotoXY(4,0);
	Print(&buffer[0]);
	
	if(number>9)
	num[0]=tab[number/10];else num[0]=' ';
	num[1]=tab[number%10];
	GotoXY(0,1);//
	Print(&num[0]);
	
	buf[0]=tab[shiwan/10];
	buf[1]=tab[shiwan%10];
	buf[2]=tab[qian/10];
	buf[3]=tab[qian%10];
	buf[4]=tab[shi/10];
	buf[5]=tab[shi%10];
	for(i=0;(buf[i]=='0'||buf[i]==' ')&& i!=5; i++)                     	
	buf[i]=' ';
	GotoXY(7,1);
	Print(&buf[0]);
	
	if(flag2) {buff[0]='L';buff[1]=tab[line];}
	else      {buff[0]=' ';buff[1]=' ';}
	GotoXY(14,1);
	Print(&buff[0]);
}
void init(void)
{
	TMOD=0x2; 
	TH0=0x06; 
	TL0=0x06; 
	EX0=1;
	TR0=0; 
	ET0=1;
	IT0=1;
	EA=1;
	LCD_Initial();
	GotoXY(0,0);
	Print(" day            ");
	GotoXY(0,1);
	Print("  s/t       0   ");
}


void test(void)
{
	if(P1!=q)
	{      
		delay(2);
		if(P1!=q)
		{ 
			q=P1;
			bufs[0]=day;
			bufs[1]=hour; 
			bufs[2]=minite;
			bufs[3]=second; 
			bufs[4]=number;
			bufs[5]=shiwan;
			bufs[6]=qian;
			bufs[7]=shi;
			if(P1_0)
				{  
				if(f0){Write24c02(&bufs[0],0x08,8);f0=0;}	
				}
			
			if(P1_1)
				{  
				if(f1){Write24c02(&bufs[0],0x10,8);f1=0;}
				}
			if(P1_2)
				{  
				if(f2){Write24c02(&bufs[0],0x18,8);f2=0;}
				}
			if(P1_3)
				{  
				if(f3){Write24c02(&bufs[0],0x20,8);f3=0;}
				}
			if(P1_4)
				{  
				if(f4){Write24c02(&bufs[0],0x28,8);f4=0;}
				}
			
			if(P1_5)
				{  
				if(f5){Write24c02(&bufs[0],0x30,8);f5=0;}
				}
			if(P1_6)
				{  
				if(f6){Write24c02(&bufs[0],0x38,8);f6=0;}
				}
			
			if(P1_7)
				{  
				if(f7){Write24c02(&bufs[0],0x40,8);f7=0;}
				}
			}
	}
}
main()
{   
	init(); 
	while(1) 
	{
		display();
		getkey();
		test();
	}		  
}

⌨️ 快捷键说明

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