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

📄 st7920.c

📁 ST7920液晶控制器的AVR程序
💻 C
字号:
#include "ST7920.h"
void Delayus(ulong x);
void Delayms(uchar x);
void BUSYLOOP(void);
void WriteCommand(uchar command);
void WriteData(uchar data);

/**********************延时程序START********************************/
void Delayus(ulong x)  	
{	ulong i;
	i = x<<4;
	while(i--);
}
void Delayms(uchar x)
{   uint i;
	i = x;
	while(i--)Delayus(1000);
} 	 
void BUSYLOOP(void)
{
 	Delayus(10);
}
/**********************延时程序END********************************/
         
/**********************串行方式相关程序START*********************/								
void lcm_out(uchar a)
{
	char i,d;
	for(i=0;i<8;i++)
	{
		clrbit(LCD_CTRL,E);  
		d=a&0x80;
		if(d)
			setbit(LCD_CTRL,RW);
		else
			clrbit(LCD_CTRL,RW);
		a<<=1;
		setbit(LCD_CTRL,E); //上升弦发送
	}
}


void WriteCommand(uchar command)	//发送命令字
{    
	uchar s;  //,i; 
	s=command&0xf0;
	lcm_out(0xf8); 			   //送写命令指令  1111 1 RW=0 RS=0  0
	lcm_out(s);            //发送高四位数据
	s=command&0x0f;
	s<<=4;
	lcm_out(s);            //发送低四位数据
	//for(i=1;i>0;i--)BUSYLOOP();	//延时10us	 延时后写命令就消失?
}


void WriteData(uchar data)
{
    
	uchar s;  //,i; 
	s=data&0xf0;
	lcm_out(0xfa);         //送写数据指令  1111 1 RW=0 RS=1  0
	lcm_out(s);
	s=data&0x0f;
	s<<=4;
	lcm_out(s);
	//for(i=1;i>0;i--)BUSYLOOP();	//延时10us	延时后写命令就无效?
}
/**********************串行方式相关程序END*********************/
  
                
/**********************液晶文字显示相关程序START*************/


void lcdSetXY(uchar x,uchar y)  //定位坐标的位置,这个主要跟字符显示相关
{  //x:横0-15   y:纵 0-3  ;x,y对应12864的坐标
 	unsigned char position=0;
	x &= 0x0f;//x%16;
	switch(y%4)
	{    //第零行和第二行寄存器连续   第一行和第三行寄存器连续
	  case 0: position = 0x80 + (x>>1); break;		// |0x80	~	0x87|
		case 1: position = 0x90 + (x>>1); break;		// |0x90	~   0x97|
		case 2: position = 0x88 + (x>>1); break;		// |0x88 	~   0x8f|
		case 3: position = 0x98 + (x>>1); break;		// |0x98	~	0x9f|
		default: break;
	}
	WriteCommand(position);
} 

//x:横0-15   y:纵 0-63  
void SetGraphicAddr(uchar x,uchar y)
{
 	uchar ytemp;
	EX_FT();  //跟绘图设置有关  8BIT控制界面,扩充指令集,,绘图显示开
	ytemp = (y & 0x1f) | 0x80;
	WriteCommand(ytemp);							//先写st7920 垂直坐标
	ytemp = (y>>5); 		//12864的y坐标打印32的对应ST水平坐标0x88~0x8f,垂直坐标(y-32)
	x &= 0x0f;//x%16;
	x  = 0x80 +(ytemp<<3) + (x>>1);			 		//0 |0x80	~	0x87|	
	WriteCommand(x);								//1 |                   																																 
	                               					//. |数值表示st的位址   
							                 		//. |					
													//31|                   																												 		
													//0	|0x88	~   0x8f|   
	FunctionSet();					//功能设置 8位数据,基本指令                                                                                                                     	
} 


void lcdInit()		//液晶初始化						
{ 
	//初始化控制管脚
	DDR_LCD_CTRL |= BIT(RS);
	DDR_LCD_CTRL |= BIT(RW);  
	DDR_LCD_CTRL |= BIT(E);
	LCD_CTRL |= BIT(RS);
	LCD_CTRL |= BIT(RW);  
	LCD_CTRL |= BIT(E);
	
	FunctionSet();                      
	Delayus(150); 
	                             	
	FunctionSet(); 
	Delayus(50);
	                    	
  DisplayOff();
	Delayus(150);
                                                
	lcdClear(); //清屏程序 需要毫秒级的延时
	Delayus(150);
  EntryModeSet();  //设定写入时候指针右移               
	DisplayOn(); 
	Delayus(50); 
	DisplayOn(); 
	Delayus(50);                 
} 

void lcdDisplay( uchar x, uchar y, uchar *ptr ) //行写满自动换行
{   
	uchar i;
	uchar LineDispCnt = 0;
	LineDispCnt = (16-x+x%2);
	lcdSetXY(x,y);
	i = 0;
	while((ptr[i])!='\0')
	{
		WriteData(ptr[i]);
		i++;
		if( i%16 == LineDispCnt%16 ) 
		{
		   y++;
		   x = 0;
		   lcdSetXY(x,y);
		}
	}
}

void lcdDispChar( uchar x, uchar y, uchar c )
{
   lcdSetXY(x,y);
   WriteData(c);
}

void lcdDispGraphic(uchar length,uchar width, uchar x,uchar y,const unsigned char *ptr)
{   //x:横0-15   y:纵 0-63  
   uchar i,j;
   uchar widthOver=0;
   if((width + x) > 16) {widthOver = (width + x - 16);width = (16-x);}  //超出屏幕
   for(i = 0;i< length;i++)
   {
   	 	SetGraphicAddr(x,y);   				  //定位12864坐标
		for(j = 0; j< width;j++)
   	 	{
   	 		WriteData(*ptr++);
   	 	}
		if(widthOver)ptr +=widthOver;  
   	 	y++;
   }
}


void ClearArea(uchar length,uchar width, uchar x,uchar y,uchar back)
{
    uchar i=0,j=0;
   	 for(i = 0;i< length;i++)
   {
   	 	SetGraphicAddr(x,y);   				  //定位12864坐标
		for(j = 0; j< width;j++)
   	 	{
   	 		WriteData(back);
   	 	}
   	 	y++;
   }
}


void DrawLevelLing( uchar x0,uchar y0, uchar x1,uchar y1)
{
 	 ClearArea(1,x1 - x0,x0,y0,0xff);
}

/**********************液晶文字显示相关程序END*************/
                    

/**********************显示图形子程序START******************/
void PicClear1(void)
{
   uchar COUNT3,COUNT2,COUNT1,LCD_X,LCD_Y;
   COUNT3=0X02;
   LCD_X=0X80;
   for (;COUNT3!=0;COUNT3--)
   {
       LCD_Y=0X80;
       COUNT2=0X20;//32
       for (;COUNT2!=0;COUNT2--)
       {
         COUNT1=0X10;//16
         SetGraphicAddr(LCD_X,LCD_Y);
         for (;COUNT1!=0;COUNT1--)
          {
            WriteData(0);
          }
         LCD_Y+=1;
       }
       LCD_X=0X88;
   }
//   send_com(0x36);
//   send_com(0x30);   //可省
}

/********************************************************************
函 数 名:Draw_Pic  128*64
入口参数:x,y,*Draw
出口参数:无
建立日期:2007年8月26日
修改日期:
函数作用:
说    明:
********************************************************************/
/*************************************************/

void Draw_Pic(uchar x, uchar y, const uchar *Draw)
{
	uchar i, j, temp_x, temp_y;		//
	temp_x = x;						//
	temp_y = y;						//
	temp_x |= 0x80;					//
	temp_y |= 0x80;					//
	WriteCommand(0x01);//清屏
	Delayus(200);   //清屏需要时间  1ms左右
	WriteCommand(0x36);
	for(i=0;i<32;i++ )				//上半屏32行
	{
		WriteCommand(temp_y++); //设置绘图区的Y地址坐标0
		WriteCommand(temp_x);  //设置绘图区的X地址坐标0
		for(j=0;j<16;j++)			//
		{
			 WriteData(*Draw++);
		}
	}
	
	temp_x = 0x88;					//
	temp_y = 0x80;					//
	j = 0; 	 						//
	for(;i<64;i++ )
	{
		
		WriteCommand(temp_y++); //设置绘图区的Y地址坐标0
		WriteCommand(temp_x);  //设置绘图区的X地址坐标0
		for(j=0;j<16;j++)
		{
			WriteData(*Draw++);
		}
	}
	WriteCommand(0x30);//基本指令集,,绘图显示OFF 	 
}
/**********************显示图形子程序END******************/

⌨️ 快捷键说明

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