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

📄 iic_lcd.c

📁 LV24000的单片机DEMO程序
💻 C
字号:
/************************************************************************
*
*   Copyright(c) 2004 ItoM BV
*   All Rights Reserved.
*
*   LV2400x evaluation kit: Driving Bolymin-LCD (I2C-LCD with NT7651 controller)
*   File name:	IIC_LCD.c
*
*************************************************************************/

#include <stdio.h>
#include "common.h"
#include "Lv24Ekit.h"
#include "LcdDef.h"

#ifdef CF8MHZ
#define IIC_DELAY _nop()
#endif //CF8MHZ

#ifdef CF12MHZ
#define IIC_DELAY _nop(); _nop()
#endif //CF12MHZ

/*-------------------------------------------------------------------
        Equations for Bolymin LCD
-------------------------------------------------------------------*/
#ifdef BOLYMIN_LCD
#define LCD_I2C_ADDR_W	0x74	// I2C addess of LCD device for writing (bit 0=0)
#define LCD_I2C_ADDR_R	0x75	// I2C addess of LCD device for reading (bit 0=1)

// System IO
#define I2CLCD_IO_PORT	_P1	// Port for driving the I2C line of the LCD
#define I2CLCD_DIR_PORT	_P1DDR	// Port for controlling the LCD IO direction (input/output)

#define LCD_RST		1	// Port 1.1: Reset LCD	(Output)
#define I2C_SDA		2	// Port 1.2: SDA	(Output)
#define I2C_SCL		3	// Port 1.3: SCL	(Output)
#endif //BOLYMIN_LCD

/*-------------------------------------------------------------------
        Data for Bolymin-LCD
-------------------------------------------------------------------*/
#ifdef BOLYMIN_LCD
BYTE _rom g_InitLcdSequence[26]=
{
	0x80, 0x35,	// Function set: 2-Lines, Extended Mode
	0x00, 0x98,	// Store Vlcd in register VA
	0x00, 0x08,	// Set Icon Blink
	0x00, 0x05,	// Set Display Config
	0x00, 0x02,	// Set Screen config
	0x00, 0x12,	// Set temperature coefficient (TCx)
	0x00, 0x34,	// Function set: 2-Lines, Normal-mode
	0x00, 0x0E,	// Display control: Display ON, Cursor ON, Blink OFF
	0x00, 0x02,	// Return Home
	0x00, 0x06,	// Entry mode set: Increment
	0x00, 0x14,	// Cursor/Display shift
	0x00, 0x01,	// Clear Display
	0x00, 0x80,	// Set DDRAM address (= 0)
 };

BYTE _rom g_IconList[]=
{
	ICON_ATENNA,   
	ICON_TELEPHONE,
	ICON_WIDE_SOUND,
	ICON_ENTRY,
	ICON_TRIANGLE_UP,
	ICON_TRIANGLE_UD,
	ICON_LOCKED,
	ICON_MUTED,
	ICON_EMPTY_BAT,
	ICON_FULL_BAT,
	ICON_TIP,
};
#define BLM_ICON_CNT (sizeof(g_IconList)/sizeof(g_IconList[0]))

#endif //BOLYMIN_LCD

/*-------------------------------------------------------------------
        Local proto (I2C-routines)
-------------------------------------------------------------------*/
#ifdef BOLYMIN_LCD
void IIC_Start(void);
void IIC_Stop(void);
void IIC_Ack(void);
void IIC_SendByte(BYTE byData);
#endif //BOLYMIN_LCD

/*-------------------------------------------------------------------
        LCD routines
-------------------------------------------------------------------*/
#ifdef BOLYMIN_LCD
void InitLcd(void) 
{
	BYTE i;

	// Generate reset LCD pulse
	_putbit(1, I2CLCD_IO_PORT, LCD_RST);	// LCD reset = 1
	DelayUs(DLT_5ms);
	_putbit(0, I2CLCD_IO_PORT, LCD_RST);	// LCD reset = 0
	for (i=0; i<10; i++)
		DelayUs(DLT_20ms);		// Probably 300 ms

	// Send init sequence to the device
	IIC_Start();

	// Send device address + access
      	IIC_SendByte(LCD_I2C_ADDR_W);
	IIC_Ack();

	// Send the init bytes
	for (i=0; i<26; i++)
	{
	      	IIC_SendByte(g_InitLcdSequence[i]);
		IIC_Ack();
	}
        IIC_Stop();

	// Clear CG-RAM
	SendLcdCmd(DD_RAM_ADDR);	// Clear address bit 6 of CG-RAM
	IIC_DELAY;			// Delay for CG-RAM
	SendLcdCmd(CG_RAM_ADDR);	// Icon are mapped in CG-RAM
	IIC_DELAY;			// Delay for CG-RAM
	for (i=0; i<146; i++)
	{
		SendLcdData(0x00);
		IIC_DELAY;		// Delay for CG-RAM
	}

	// Hide cursor as default
	ShowCursor(FALSE);

	// Turn off Icons
	TurnOffIcons();
}// End InitLcd
#endif //BOLYMIN_LCD

#ifdef BOLYMIN_LCD
void TurnOffIcons(void)
{
	BYTE i;

	for (i=0; i<BLM_ICON_CNT; i++)
		ShowIcon(g_IconList[i], HIDE_ICON);
} // End TurnOffIcons
#endif //BOLYMIN_LCD

#ifdef BOLYMIN_LCD
void ClearDisplay(void)
{
	SendLcdCmd(CLR_DISP); 	// Clear display 01 or 0000 0001
  	DelayUs(DLT_2ms);	// Wait 1.53 ms
} // End ClearDisplay
#endif //BOLYMIN_LCD

#ifdef BOLYMIN_LCD
void ShowCursor(BOOL bShow)
{
	if (bShow)
		SendLcdCmd(DISP_ON_C);	// Display on, cursor on, no blinking
	else
		SendLcdCmd(DISP_ON);	// Display on, no cursor
} // End ShowCursor
#endif //BOLYMIN_LCD

#ifdef BOLYMIN_LCD
void ClearLcdPos(BYTE byPos, BYTE byLen)
{
	// Fill the LCD with number of spaces (specified by byLen) from position byPos
	BYTE i;

	// Breng the cursor to specified position
	SendLcdCmd(byPos); 	// Set cursor position

   	for (i=0; i<byLen; i++)
		SendLcdData(LCDC_SPACE);
	SendLcdCmd(byPos); 	// Set cursor position back
} // End ClearLcdPos
#endif //BOLYMIN_LCD

#ifdef BOLYMIN_LCD
/* Send command to LCD 4 bit mode (with WaitLcdReady) */
void SendLcdCmd(BYTE byCmd)
{
	// Send init sequence to the device
	IIC_Start();

	// Send device address + access
      	IIC_SendByte(LCD_I2C_ADDR_W);
	IIC_Ack();

	// Send the control byte (RS=0 for command)
      	IIC_SendByte(0x00);
	IIC_Ack();

	// Send the command
      	IIC_SendByte(byCmd);
	IIC_Ack();

	// End sequence
        IIC_Stop();
}// End SendLcdCmd 
#endif //BOLYMIN_LCD

#ifdef BOLYMIN_LCD
void SendLcdData(BYTE byData)
{
	// Send init sequence to the device
	IIC_Start();

	// Send device address + access
      	IIC_SendByte(LCD_I2C_ADDR_W);
	IIC_Ack();

	// Send the control byte (RS=1 for data)
      	IIC_SendByte(0x40);
	IIC_Ack();

	// Send the command
      	IIC_SendByte(byData);
	IIC_Ack();

	// End sequence
        IIC_Stop();
}// End SendLcdData
#endif //BOLYMIN_LCD

#ifdef BOLYMIN_LCD
void SendLcdChar(BYTE byData)
{
	// ASCII conversion for Bolymin-LCD
	SendLcdData(byData|0x80);
}// End PrintChar
#endif //BOLYMIN_LCD

#ifdef BOLYMIN_LCD
void ShowIcon(BYTE byIcon, BYTE byState)
{
	// See LcdDef.h for possible Icons
	// byState can be SHOW_ICON/HIDE_ICON
	SendLcdCmd(DD_RAM_ADDR|0);	// Set DRAM address to clear A6 of CGRAM
	SendLcdCmd(byIcon);		// Set CGRAM address RS=0, D7=0, D6=1, D[5:0]=CGRAM[A5:A0]
       	SendLcdData(byState);
} // End ShowIcon
#endif //BOLYMIN_LCD

#ifdef BOLYMIN_LCD
void WaitLcdReady()
{
} // End WaitLcdReady
#endif //BOLYMIN_LCD

/*-------------------------------------------------------------------
        I2C routines for I2C master
-------------------------------------------------------------------*/
#ifdef BOLYMIN_LCD
void IIC_Start()
{
	_putbit(1, I2CLCD_IO_PORT, I2C_SCL); 	// Set SCL
	IIC_DELAY;
	_putbit(1, I2CLCD_IO_PORT, I2C_SDA); 	// Set SDA
	IIC_DELAY;
	_putbit(0, I2CLCD_IO_PORT, I2C_SDA); 	// Clear SDA
} // End IIC_Start 
#endif //BOLYMIN_LCD

#ifdef BOLYMIN_LCD
void IIC_Stop()
{
	_putbit(0, I2CLCD_IO_PORT, I2C_SDA);	// Clear SDA
	_putbit(1, I2CLCD_IO_PORT, I2C_SCL);	// Set SCL
	IIC_DELAY;
	_putbit(1, I2CLCD_IO_PORT, I2C_SDA);	// Set SDA
} // End IIC_Stop
#endif //BOLYMIN_LCD

#ifdef BOLYMIN_LCD
void IIC_Ack()
{
	_putbit(1, I2CLCD_IO_PORT, I2C_SCL);	// Set SCL
	IIC_DELAY;
	_putbit(1, I2CLCD_IO_PORT, I2C_SDA);	// Set SDA
	IIC_DELAY;
	_putbit(0, I2CLCD_IO_PORT, I2C_SCL);	// Clear SCL
} // End IIC_Ack
#endif //BOLYMIN_LCD

#ifdef BOLYMIN_LCD
void IIC_SendByte(BYTE byData)
{
	BYTE i;

	_putbit(0, I2CLCD_IO_PORT, I2C_SCL);	// Clear SCL
	IIC_DELAY;	// xxx

	// Send 8 bits
	for (i=0; i<8; i++)
	{
		// Drive SDA according to level of the data bit
		if (byData & 0x80)
			_putbit(1, I2CLCD_IO_PORT, I2C_SDA); 
		else
			_putbit(0, I2CLCD_IO_PORT, I2C_SDA); 
		IIC_DELAY;
		_putbit(1, I2CLCD_IO_PORT, I2C_SCL); // Clock = H
		IIC_DELAY;
		byData <<= 1;
		_putbit(0, I2CLCD_IO_PORT, I2C_SCL); // Clock = L
		IIC_DELAY;
	}
} // End IIC_SendByte 
#endif //BOLYMIN_LCD

⌨️ 快捷键说明

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