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

📄 alt_up_character_lcd.c

📁 这是一个 NIOSII系统的 1602LCD 控制IP核
💻 C
字号:
#include "alt_up_character_lcd.h"
#include "system.h"
#include "alt_up_character_lcd_regs.h"

/////////////////////////////////////////////////////////////////////////////
/*
 * Internal Utility Functions:
 * these functions are not supposed to be called directly by the user so they
 * are not defined in the header file
 */
/////////////////////////////////////////////////////////////////////////////

/**
 * @brief calculate the DDRAM address according to the provided x and y coordniates
 *
 * @param x_pos  x coordniate ( 0 to 15, from left to right )
 * @param y_pos  y coordniate ( 1 for the first row, 2 for the second row )
 *
 * @return the converted address
 * @sa the datasheet for the LCD Display Controller on the DE2 Board
 * @note the function requires that the input are in the valid range
 *
 **/
alt_u32 get_DDRAM_addr(unsigned x_pos, unsigned y_pos)
{
	//assume valid inputs
	alt_u32 addr = 0x00000000;
	if (y_pos == 1)
	{
		addr |= x_pos;
	}
	else if (y_pos == 2)
	{
		addr |= x_pos;
		addr |= 0x00000040;
	}
	//the b_7 is always 1 for DDRAM address, see datasheet
	return (addr | 0x00000080);
}

/**
 * @brief send a command to the Instruction Register of the LCD controller
 *
 * @param cmd  the command bits 
 *
 * @return 0 for success
 **/
int alt_up_character_lcd_send_cmd(alt_u32 cmd)
{
 	// We use Instruction Register and Control Register interchangably
	IOWR_ALT_UP_CHARACTER_LCD_CONTROL(ALT_UP_CHARACTER_LCD_BASE, cmd);
	return 0;
}

/**
 * @brief send data to the Data Register of the LCD controller
 *
 * @param cmd  the data bits 
 *
 * @return 0 for success
 **/
int alt_up_character_lcd_send_data(alt_u32 data)
{
	IOWR_ALT_UP_CHARACTER_LCD_DATA(ALT_UP_CHARACTER_LCD_BASE, data);
	return 0;
}


////////////////////////////////////////////////////////////////////////////
/*
 * Interface Functions : 
 * The interface of these fucntions are provided to the user. See the header
 * file for a detailed description of each function
 */
////////////////////////////////////////////////////////////////////////////

void alt_up_character_lcd_init()
{
	alt_up_character_lcd_send_cmd(ALT_UP_CHARACTER_LCD_CTRL_CLEAR_DISPLAY);
}

int alt_up_character_lcd_write(const char *ptr, unsigned int len)
{
	unsigned int i;
	for (i = 0; i < len; i++)
	{
		alt_up_character_lcd_send_data(*(ptr+i));
	}
	return 0;
}

int alt_up_character_lcd_set_cursor_pos(unsigned x_pos, unsigned y_pos)
{
	//boundary check
	if (x_pos > 16 || y_pos >2 )
		return -1;
	// calculate address
	alt_u32 addr = get_DDRAM_addr(x_pos, y_pos);
	// set the cursor
	return alt_up_character_lcd_send_cmd(addr);
}


int alt_up_character_lcd_shift_cursor(int x_right_shift_offset)
{
	if (x_right_shift_offset == 0) 
		// don't ask me to do nothing 
		return 0;

	// see shift right or left
	alt_u32 shift_cmd = (x_right_shift_offset > 0) ? ALT_UP_CHARACTER_LCD_CTRL_CURSOR_SHIFT_RIGHT : ALT_UP_CHARACTER_LCD_CTRL_CURSOR_SHIFT_LEFT;
	// see how many to shift
	alt_u8 num_offset = (x_right_shift_offset > 0) ? x_right_shift_offset : -x_right_shift_offset;
	// do the shift
	while (num_offset-- > 0)
		alt_up_character_lcd_send_cmd(shift_cmd);
	return 0;
}

int alt_up_character_lcd_shift_display(int x_right_shift_offset)
{
	if (x_right_shift_offset == 0) 
		// don't ask me to do nothing 
		return 0;

	// see shift right or left
	alt_u32 shift_cmd = (x_right_shift_offset > 0) ? ALT_UP_CHARACTER_LCD_CTRL_DISPLAY_SHIFT_RIGHT : ALT_UP_CHARACTER_LCD_CTRL_DISPLAY_SHIFT_LEFT;
	// see how many to shift
	alt_u8 num_offset = (x_right_shift_offset > 0) ? x_right_shift_offset : -x_right_shift_offset;
	// do the shift
	while (num_offset-- > 0)
		alt_up_character_lcd_send_cmd(shift_cmd);
	return 0;
}

int alt_up_character_lcd_erase_pos(unsigned x_pos, unsigned y_pos)
{
	// boundary check
	if (x_pos > 16 || y_pos >2 )
		return -1;
	// get address
	alt_u32 addr = get_DDRAM_addr(x_pos, y_pos);
	// set cursor to dest point
	alt_up_character_lcd_send_cmd(addr);
	//send an empty char as erase (refer to the Character Generator ROM part of the Datasheet)
	return alt_up_character_lcd_send_data(0x00000002);
}

⌨️ 快捷键说明

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