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

📄 zlg_avalon_lcd128_64.c

📁 周立功 SOPC嵌入式系统实验教材 LCD12864驱动代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/****************************************************************************
*名称:  ST7920_CgramAddress
*功能:  Set CGRAM address
*入口参数:  CgramAddr
*说明:  Base instruction
****************************************************************************/
void ST7920_CgramAddress(alt_u8 CgramAddr)
{
    CgramAddr &= 0x3F;
	  ST7920_SendCMD (0x34);      //Expand instruction
	  ST7920_SendCMD (0x02);	
	  ST7920_SendCMD (0x30);      //Base instruction
	  ST7920_SendCMD (CgramAddr|0x40);
}

/****************************************************************************
*名称:  ST7920_DdramAddress
*功能:  Set CGRAM address
*入口参数:  DdramAddr
*说明:  Base instruction
****************************************************************************/
void ST7920_DdramAddress(alt_u8 DdramAddr)
{
    DdramAddr &= 0x3F;
	ST7920_SendCMD (DdramAddr|0x80);//
}

//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
/****************************************************************************
*名称:  ST7920_IdleMode
*功能:  Idle mode enable
*说明:  Expand instruction
****************************************************************************/
void ST7920_IdleMode(void)
{
	ST7920_SendCMD (0x34);      //Expand instruction
	ST7920_SendCMD (0x01);
	ST7920_SendCMD (0x30);      //Base instruction
}

/****************************************************************************
*名称:  ST7920_RollInstructionEnable
*功能:  Roll instruction enable
*说明:  Expand instruction
****************************************************************************/
void ST7920_RollInstructionEnable(void)
{
	ST7920_SendCMD (0x34);      //Expand instruction
	ST7920_SendCMD (0x03);
	ST7920_SendCMD (0x30);      //Base instruction
}

/****************************************************************************
*名称:  ST7920_RollInstructionDisable
*功能:  Roll instruction Disable
*说明:  Expand instruction
****************************************************************************/
void ST7920_RollInstructionDisable(void)
{
	ST7920_SendCMD (0x34);      //Expand instruction
	ST7920_SendCMD (0x02);	
	ST7920_SendCMD (0x30);      //Base instruction
}
/****************************************************************************
*名称:  ST7920_IramAddress
*功能:  Set Iram address
*说明:  Expand instruction
****************************************************************************/
void ST7920_IramAddress(alt_u8 IramAddr)
{
	ST7920_SendCMD (0x34);      //Expand instruction
	ST7920_SendCMD (0x02);	
	ST7920_SendCMD (IramAddr|0x40);
	ST7920_SendCMD (0x30);      //Base instruction
}
/****************************************************************************
*名称:  ST7920_SetRollAddress
*功能:  Roll instruction set address
*入口参数:  WhichLine
*说明:  Expand instruction
****************************************************************************/
void ST7920_SetRollAddress(alt_u8 WhichdotLine)
{
	ST7920_SendCMD (0x34);      //Expand instruction
	ST7920_SendCMD (0x03);          //RollInstructionEnable
	ST7920_SendCMD (WhichdotLine|0x40);
	ST7920_SendCMD (0x30);      //Base instruction	
}

/****************************************************************************
*名称:  ST7920_NegativeDisplay
*功能:  Negative dispaly
*入口参数:  
*说明:  Expand instruction
****************************************************************************/
void ST7920_NegativeDisplay(alt_u8 row,alt_u8 col,alt_u8 width)
{
	alt_u8 x,y;
	alt_u8 i,j;

	row = row&0x03;
	y = (row&0x01)<<4;
	x = row>1?col+8:col;
	for(i=0; i<16; i++)
	{	//vertical setting
		ST7920_SendCMD (0x34);
		ST7920_SendCMD (0x80|y);
		y++;
		//horizontal setting 
	    ST7920_SendCMD (0x80|x);
	    ST7920_SendCMD (0x30);
	    for(j=0;j<width;j++)
			ST7920_SendDATA(0xff);
	}
	ST7920_PictureOn();    
}
/****************************************************************************
*名称:  ST7920_CancelNegDisp
*功能:  Cancel Negative dispaly
*入口参数:  
*说明:  Expand instruction
****************************************************************************/
void ST7920_CancelNegDisp(alt_u8 row,alt_u8 col,alt_u8 width)
{
	alt_u8 x,y;
	alt_u8 i,j;

	row = row&0x03;
	y = (row&0x01)<<4;
	x = row>1?col+8:col;
	for(i=0; i<16; i++)
	{	//vertical setting
		ST7920_SendCMD (0x34);
		ST7920_SendCMD (0x80|y);
		y++;
		//horizontal setting 
	    ST7920_SendCMD (0x80|x);
	    ST7920_SendCMD (0x30);
	    for(j=0;j<width;j++)
			ST7920_SendDATA(0x00);
	}
	ST7920_PictureOn();    
}

/****************************************************************************
*名称:  ST7920_ClearGDRAM
*功能:  Clear Gdram
*入口参数:  
*说明:  Expand instruction
****************************************************************************/
void ST7920_ClearGDRAM(void)
{
	alt_u8 i,j;

	for(i=0; i<32; i++)
	{	//vertical setting
    	ST7920_SendCMD (0x34);
		ST7920_SendCMD (0x80|i);
		//horizontal setting 
	    ST7920_SendCMD (0x80);
    	ST7920_SendCMD (0x30);
	  	for(j=0;j<16;j++)
	    	ST7920_SendDATA(0x00);
	}
	for(i=0; i<32; i++)
	{	//vertical setting
    	ST7920_SendCMD (0x34);
		ST7920_SendCMD (0x80|i);
		//horizontal setting 
	  	ST7920_SendCMD (0x80|8);
    	ST7920_SendCMD (0x30);
	 	for(j=0;j<16;j++)
	    	ST7920_SendDATA(0x00);
	}
	   
}

/****************************************************************************
*名称:  ST7920_Sleep
*功能:  get to sleep
*说明:  Expand instruction
****************************************************************************/
void ST7920_Sleep(void)
{
	ST7920_SendCMD (0x34);      //Expand instruction
	ST7920_SendCMD (0x08);
	ST7920_SendCMD (0x30);      //Base instruction
}

/****************************************************************************
*名称:  ST7920_WakeUp
*功能:  screen wake up!
*说明:  Expand instruction
****************************************************************************/
void ST7920_WakeUp(void)
{	
	ST7920_SendCMD (0x34);      //Expand instruction
	ST7920_SendCMD (0x0c);
	ST7920_SendCMD (0x30);      //Base instruction
}

/****************************************************************************
*名称:  ST7920_PictureOn
*功能:  Open the picture to screen
*说明:  Expand instruction
****************************************************************************/
void ST7920_PictureOn(void)
{
	ST7920_SendCMD (0x34);      //Expand instruction
	ST7920_SendCMD (0x36);
	ST7920_SendCMD (0x30);      //Base instruction
}

/****************************************************************************
*名称:  ST7920_PictureOff
*功能:  Close the picture of screen
*说明:  Expand instruction
****************************************************************************/
void ST7920_PictureOff(void)
{
	ST7920_SendCMD (0x34);      //Expand instruction
	ST7920_SendCMD (0x30);
	ST7920_SendCMD (0x30);      //Base instruction
}

/****************************************************************************
*名称:  ST7920_GdramAddress
*功能:  Set GDRAM to AC,setup it's address about V or H
*入口参数:  VerticalAddr,HorizontalAddr 
*说明:  Expand instruction
****************************************************************************/
void ST7920_GdramAddress(alt_u8 VerticalAddr,alt_u8 HorizontalAddr)
{
	ST7920_SendCMD (0x34);      //Expand instruction
	ST7920_SendCMD (VerticalAddr|0x80);
	ST7920_SendCMD (HorizontalAddr|0x80);
	ST7920_SendCMD (0x30);      //Base instruction
}

/****************************************************************************
*名称:  LCD_init
*功能:  initialize the LCD
*入口参数: None 
*说明:  
****************************************************************************/
void LCD_init(void)
{
	//DelayNmS(40);
	alt_busy_sleep(40000);//Delay 40ms
	//*LCD_WRITE_COMMAND = 0x30;
	LCD_WRITE_COMMAND(0x30);
	//DelayNmS(10);
	alt_busy_sleep(10000);//Delay 10ms
	//*LCD_WRITE_COMMAND = 0x30;
	LCD_WRITE_COMMAND(0x30);
	//DelayNmS(5);
	alt_busy_sleep(5000);//Delay 5ms
	//*LCD_WRITE_COMMAND = 0x30;
	LCD_WRITE_COMMAND(0x30);
	ST7920_SendCMD(LCD_CMD_8BIT);
	ST7920_SendCMD(LCD_CMD_DISP_ON);
	ST7920_SendCMD(LCD_CMD_CLEAR);
	ST7920_SendCMD(LCD_CMD_AC_INC);	
}

/****************************************************************************
*名称:  LCD_printf
*功能:  在指定位置显示指定长度的字符
*入口参数:  row   :列位置0-7
	        col   :行位置0-3
	        *pdata: 显示数据缓冲区
	        len   :显示字符长度 0-64 一个汉字为2个字符
*说明:  支持中英文混合显示,支持自动换行,地址为双字节长度自增。
=======================================	
ST7920最多可驱动256X64点阵的LCD,市面上很多128X64的LCD也用7920驱动。这些
12864的屏幕是把ST7920所驱动的上面一半256*32点阵取出使用的,这个区域被分割成
左右两半各128*32点,将右面一半折到下面去了。以其DDRAM汉字显示地址排列说明:
ST7920的16*4汉字地址如下:
80,81,82,83,84,85,86,87,88,89,8A,8B,8C,8D,8E,8F
90,91,92,93,94,95,96,97,98,99,9A,9B,9C,9D,9E,9F
A0,A1,A2,A3,A4,A5,A6,A7,A8,A9,AA,AB,AC,AD,AE,AF
B0,B1,B2,B3,B4,B5,B6,B7,B8,B9,BA,BB,BC,BD,BE,BF
市售12864屏幕的汉字地址如下:
80,81,82,83,84,85,86,87
90,91,92,93,94,95,96,97
88,89,8A,8B,8C,8D,8E,8F
98,99,9A,9B,9C,9D,9E,9F
因此编程时要通过查表进行相应的变换。
=======================================
****************************************************************************/
int LCD_printf(alt_u8 row,alt_u8 col,alt_u8 *pdata,alt_u8 len)
{
	int ret_code;
	if(len>64)  //最多显示64个字符,超出部分忽略
		len = 64;
	row = row&0x03;
	col = col&0x07;
	while(len!=0)
	{
		ST7920_DdramAddress(DDRAMaddr[row][col]);//进行行调换
		for(;col<16&&len!=0;col++)
		{
			ret_code = ST7920_SendDATA(*pdata++);
			if(ret_code == LCD_ERR_TIMEOUT)
				return ret_code;
			len--;
		}
		row++;
		row = row&0x03;
	  	col = 0;
	}	
	return LCD_ERR_OK;
}
/****************************************************************************
*名称:  LCD_PlotPic
*功能:  显示图形128*64点阵。
*入口参数: *pdata       图形128*64点阵缓冲区指针
****************************************************************************/
void LCD_PlotPic(alt_u8 *pdata)
{
	alt_u8 i,j;
  //ST7920_PictureOff();
	for(i=0; i<32; i++)
	{	//vertical setting
    	ST7920_SendCMD(0x34);
		ST7920_SendCMD(0x80|i);
		//horizontal setting 
	  	ST7920_SendCMD(0x80);
    	ST7920_SendCMD(0x30);
	  	for(j=0;j<16;j++)
	     	ST7920_SendDATA(*pdata++);
	}
	for(i=0; i<32; i++)
	{	//vertical setting
    	ST7920_SendCMD(0x34);
		ST7920_SendCMD(0x80|i);
		//horizontal setting 
	  	ST7920_SendCMD(0x80|8);
    	ST7920_SendCMD(0x30);
	 	for(j=0;j<16;j++)
	    	ST7920_SendDATA(*pdata++);
	}	
	ST7920_PictureOn();
}

⌨️ 快捷键说明

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