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

📄 x.c

📁 020LCD12864显示数字文字适合初学者
💻 C
字号:
#include <c8051f020.h>

/*

LCD_
RS  <P4.0>	   
WR  <P4.1>     
E   <P4.2>
要求:P4.0,4.1,4.2设置为推拉输出;  
P5口设置为带弱上拉的漏极输出,3.3V电平标准即可

*/
#define  uchar unsigned char
#define  uint unsigned int

unsigned char code dig[]={"0123456789"};


/***************************时钟配置*****************************/
void SYSCLK_Init (void)
{  
   int i;                           // delay counter
   OSCXCN = 0x67;                   // start external oscillator with         // 22.1184MHz    crystal
   for (i=0; i < 512; i++) ;        // Wait for osc. to start up
   while (!(OSCXCN & 0x80)) ;       // Wait for crystal osc. to settle
   OSCICN = 0x88;                   // 选择外部时针   
}

/***************************端口设置*****************************/
void port_init(void)
{
   XBR0|= 0x04;                     // 串口能用
   XBR1= 0x00;
   XBR2= 0x40;                      // Enable crossbar and weak pull-ups
   ////
   P0MDOUT |= 0x3D;                   
   P3MDOUT = 0xff;			        //P3.0 P3.2为开漏输出	ad7650用
   P1MDOUT = 0xff;	                //P1口设置为push-pull输出
   P2MDOUT = 0xff;	                //P2口设置为push-pull输出
   //P7=0X0F;
}

void lcd_delay()
{	unsigned char delayTime;
	for(delayTime=50;delayTime>0;delayTime--);
}
  
/* 返回值Status:	当前地址计数器的值.	*/
unsigned char LCDBusyWait()							//等待LCD就绪
{
	unsigned char Status;
	P4&=0xfe;  //11111110,LCD_RS=0
    lcd_delay();
 	
	P4|=0x02;//00000010,LCD_RW=1
    lcd_delay();  
	
	P5 = 0xFF;
	P4|=0x04;//00000100,LCD_E=1

	lcd_delay();   
	Status=P5;
	P4&=0xfB;//11111011,LCD_E=0

	while(Status & 0x80)
	{
        lcd_delay(); 
		P4|=0x04;//00000100,LCD_E=1
        lcd_delay();  
		Status=P5;
		lcd_delay(); 
		P4&=0xfB;//11111011,LCD_E=0

	}
	P4&=0xfd;//11111101,LCD_RW=0

	return Status;
}

void WriteLCD_Data(unsigned char Data)			//向LCD写入一个字节的数据
{
	LCDBusyWait();
	P4|=0x01;//00000001,LCD_RW=0,LCD_RS=1

	lcd_delay();	 
	P5 = Data;
	lcd_delay();
	P4|=0x04;//00000100,LCD_E=1

	lcd_delay();
	P4&=0xfB;//11111011,LCD_E=0

}

void WriteLCD_Command(unsigned char Command)	//向LCD写入一个字节的命令
{
	LCDBusyWait();
	P4&=0xFE;//11111110,LCD_RS=0

	lcd_delay();
	P5 = Command;
	lcd_delay();
	P4|=0x04;//00000100,LCD_E=1

	lcd_delay();
	P4&=0xfB;//11111011,LCD_E=0

}

//改变地址,实现了逐行显示
unsigned char ChangeAddress(unsigned char StartAddress)
{
	switch(StartAddress)
	{
		case 0x87:WriteLCD_Command(0x90);StartAddress=0x90;break;
		case 0x8F:WriteLCD_Command(0x98);StartAddress=0x98;break;
		case 0x97:WriteLCD_Command(0x88);StartAddress=0x88;break;
		default:StartAddress+=1;
	}
	return StartAddress;
}

/*****************************显示字符串***************************************/

void WriteLCD(unsigned char StartAddress,unsigned char Size,unsigned char Charactors[])
{
	unsigned char *i=Charactors;	  //字符串的首地址
	WriteLCD_Command(StartAddress);
	if(Size) 		//Size不为0
	{
	//	bit Over=0;
		while(i<(Charactors+Size))		//判断字符串结尾
		{

			WriteLCD_Data(*i);
			i++;
			//WriteLCD_Data(*i);
			//i++;
			StartAddress=ChangeAddress(StartAddress); //改变地址,实现了逐行显示
		}
	}
	else			//Size为0
	{
		while(*i)						//判断字符串结尾,*i为0代表结束
		{
			WriteLCD_Data(*i);
			i++;
			WriteLCD_Data(*i);
			i++;
			StartAddress=ChangeAddress(StartAddress);
		}
	}
}
/****************************显示数字********************************/

void Show_num(unsigned char address,unsigned int num)
{
 unsigned char k,j,i=0;
 unsigned char w_data[5];
 WriteLCD_Command(address);
 do
   {
    w_data[i++]=num%10;
	num=num/10;
	j=i;
   }while(num!=0); 
   	
	for(k=j;j>0;j--)
	{
		WriteLCD_Data(dig[w_data[j-1]]);
	}   
}

void main()
{
    SYSCLK_Init ();
	port_init();
	WriteLCD_Command(0x30);	  //功能设置---8BIT控制界面,基本指令集
	WriteLCD_Command(0x0C);	  //显示打开,光标关,反白显示关
	WriteLCD_Command(0x01);	  //清除屏幕显示,将DDRAM的地址计数器归零
	WriteLCD(0x81,0,"DDS AD9852");
	while(1)
	{
    Show_num(0x90,6140);
	} 

}

⌨️ 快捷键说明

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