/*******************************************************
文件名 : Nokia5110Lcd.h
描述 : 诺基亚 5110 LCD 驱动头文件
作者 : xylch101
编辑器 : Notepad 2
日期 : 2008年06月27日
*******************************************************/
文件名 : Nokia5110Lcd.c
描述 : 诺基亚 5110 LCD 驱动源文件
//清屏
ClearBuffer( );
CommitBuffer( );
}
/*********************************************
函数名 : LcdSetContrast
功能 : 设置 LCD 对比度
参数 : byte contrast 对比度值:0~127
返回 : 无
编辑器 : Notepad 2
*********************************************/
void LcdSetContrast( byte contrast )
{
LcdExec( 0x21, DC_CMD );
LcdExec( 0x80 | contrast, DC_CMD );
LcdExec( 0x20, DC_CMD );
}
/*********************************************
函数名 : LcdSetXY
功能 : 设置 LCD 当前坐标
参数 : byte X 横坐标:0~83
byte Y 纵坐标:0~05
返回 : 无
编辑器 : Notepad 2
*********************************************/
void LcdSetXY(byte X, byte Y)
{
LcdExec( 0x40 | Y, DC_CMD); // 行
LcdExec( 0x80 | X, DC_CMD); // 列
}
/*********************************************
函数名 : LcdExec
功能 : 执行 LCD 指令/数据
参数 : byte data 指令/数据值
LcdExecType dc 指令/数据选择(DC_CMD/DC_DATA)
返回 : 无
编辑器 : Notepad 2
*********************************************/
void LcdExec( byte data, LcdExecType dc )
{
PORTB &= ~LCD_CE_PIN;
if ( dc == DC_DATA )
{
PORTB |= LCD_DC_PIN;
}
else
{
PORTB &= ~LCD_DC_PIN;
}
SPDR = data;
while ( (SPSR & 0x80) != 0x80 );
PORTB |= LCD_CE_PIN;
}
/*********************************************
函数名 : UnClear
功能 : 高n位清零
参数 : byte data 8位数据
byte n 清零多少位
返回 : byte 清零后的结果
举例 : data取11111000 n取3 --> 00011000
编辑器 : Notepad 2
*********************************************/
byte UnClear( byte data, byte n )
{
byte tmpData, i;
tmpData = 0;
for( i = 0; i < n; i++ )
{
tmpData |= ( 1 << ( 7 - i ) );
}
return ( data & ( ~tmpData ) );
}
/*********************************************
函数名 : LnClear
功能 : 低n位清零
参数 : byte data 8位数据
byte n 清零多少位
返回 : byte 清零后的结果
举例 : data取00011111 n取3 --> 00011000
编辑器 : Notepad 2
*********************************************/
byte LnClear( byte data, byte n )
{
byte tmpData, i;
tmpData = 0;
for( i = 0; i < n; i++ )
{
tmpData |= ( 1 << i );
}
return ( data & ( ~tmpData ) );
}
/*********************************************
函数名 : SplitByte
功能 : 一个字节拆成两个字节
参数 : byte data 8位数据
byte nByLow 从低位起多少位拆开
byte bytes[ 2 ] 存放返回的两个字节
返回 : 无
举例 : data取11111111 nByLow取2 --> bytes[0]=11111100、bytes[1]=00000011
编辑器 : Notepad 2
*********************************************/
void SplitByte( byte data, byte nByLow, byte bytes[ 2 ] )
{
bytes[ 0 ] = data << nByLow;
bytes[ 1 ] = data >> ( 8 - nByLow );
}
/*********************************************
函数名 : BufferPixelStuff
功能 : 把图片、文字点阵写入全屏缓存(精确定位,右上角坐标可以为LCD上任意位置。
字符大小不限,自行修改数组点阵数据,单个字符的宽高参数不要传错了,16号
字模不要传成12,12了,12号字模不要传成16,16了)
参数 : byte type type=1-->图片点阵 / type=2-->ascii字符 / type=3-->中文字符
byte index 数据在数组中的位置(当type=2时,直接传ascii字符,如:'A')
byte x 横坐标:0~83
byte y 纵坐标:0~47
byte width 点阵数据的宽度(位数) 如果是字符就为一个字符的宽度、如果是图片就为整个图片的宽度
byte height 点阵数据的高度(位数) 如果是字符就为一个字符的高度、如果是图片就为整个图片的高度
返回 : 无
编辑器 : Notepad 2
*********************************************/
void BufferPixelStuff( byte type, byte index, byte x, byte y, byte width, byte height)
{
byte i, j, tmpRow;
byte row = y / 8;
byte offset = y % 8;
byte level = ( height % 8 == 0 ? height / 8 : ( height / 8 + 1 ) );
byte bytes[ 2 ] = { 0 };
byte byteTmp;
for( j=0; j < width; j++ )
{
tmpRow = row;
byteTmp = UnClear( LcdPixelBuffer[ tmpRow ][ x + j ], 8 - offset );
for( i=0; i < level; i++ )
{
if( type == 1 )
{
SplitByte( pgm_read_byte( &PIXEL_BMP[ index ][ i * width + j ] ), offset, bytes);
}
else if( type == 2 )
{
SplitByte( pgm_read_byte( &PIXEL_ENGLISH_CHAR[ index - 32 ][ i * width + j ] ), offset, bytes);
}
else if( type == 3 )
{
SplitByte( pgm_read_byte( &PIXEL_CHINESE_CHAR[ index ][ i * width + j ] ), offset, bytes);
}
else
{
return;
}
LcdPixelBuffer[ tmpRow++ ][ x + j ] = byteTmp | bytes[ 0 ];
byteTmp = bytes[ 1 ];
}
LcdPixelBuffer[ tmpRow ][ x + j ] = byteTmp | LnClear( LcdPixelBuffer[ tmpRow ] [x + j ], ( offset + height ) % 8 );
}
}
/*********************************************
函数名 : CacheEnglishString
功能 : 把ascii字符串写入全屏缓存(精确定位,右上角坐标可以为LCD上任意位置)
参数 : byte x 横坐标:0~83
byte y 纵坐标:0~47
byte width 一个字符的宽度(位数)
byte height 一个字符的高度(位数)
byte bWidth 字符间距
byte *dataPtr 要显示的字符串 (如:"Hello world!")
返回 : 无
编辑器 : Notepad 2
*********************************************/
void CacheEnglishString( byte x, byte y, byte width, byte height, byte bWidth, byte *dataPtr )
{
byte i = 0;
while ( *dataPtr )
{
BufferPixelStuff( 2, *dataPtr++, x + i * ( width + bWidth ), y, width, height );
i++;
}
}
/*********************************************
函数名 : CachePixel
功能 : 画一个点到入全屏缓存中(精确定位,坐标可以为LCD上任意位置)
参数 : byte x 横坐标:0~83
byte y 纵坐标:0~47
byte width 一个字符的宽度(位数)
byte height 一个字符的高度(位数)
LcdPixelMode mode 画点方式 (PIXEL_CLEAR-->擦除、PIXEL_DRAW-->画点、PIXEL_XOR-->取反)
返回 : 无
编辑器 : Notepad 2
*********************************************/
void CachePixel( byte x, byte y, LcdPixelMode mode )
{
byte row;
byte offset;
byte data;
if ( x > LCD_X_RES ) return;
if ( y > LCD_Y_RES ) return;
row = y / 8;
offset = y % 8;
data = LcdPixelBuffer[ row ][ x ];
if ( mode == PIXEL_CLEAR )
{
data &= (~(0x01 << offset));
}
else if ( mode == PIXEL_DRAW )
{
data |= (0x01 << offset);
}
else if ( mode == PIXEL_XOR )
{
data ^= (0x01 << offset);
}
LcdPixelBuffer[ row ][ x ] = data;
}
/*********************************************
函数名 : CommitBuffer
功能 : 提交缓存,把当前缓存上的数据写到LCD上
参数 : 无
返回 : 无
编辑器 : Notepad 2
*********************************************/
void CommitBuffer( void )
{
byte row, col;
LcdSetXY( 0x00, 0x00 );
for ( row = 0; row < LCD_Y_RES / 8; row++ )
{
for ( col = 0; col < LCD_X_RES; col++ )
{
LcdExec( LcdPixelBuffer[ row ][ col ], DC_DATA );
}
}
}
/*********************************************
函数名 : ClearBuffer
功能 : 清空缓存
参数 : 无
返回 : 无
编辑器 : Notepad 2
*********************************************/
void ClearBuffer( void )
{
byte row, col;
for( row = 0; row < LCD_Y_RES / 8; row++ )
{
for( col=0; col < LCD_X_RES; col++ )
{
LcdPixelBuffer[ row ][ col ] = 0x00;
}
}
}
/*******************************************************
文件名 : PixelStuff.h
描述 : 点阵数据
作者 : xylch101
编辑器 : Notepad 2
日期 : 2008年06月27日
*******************************************************/
0x08,0x04,0xFE,0x04,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0x70,0x1C,0x06,0x0E,0x3C,0xF0,0xC0,0x80,0x00,0x00,
0x00,0x00,0xFF,0xC0,0x70,0x18,0x0C,0x04,0x04,0x02,0x02,0x02,0x04,0x04,0x0C,0x18,0x10,0x30,0x20,0x60,0x40,0x40,0xC0,0x80,
0x80,0x80,0xC0,0x40,0x60,0x20,0x30,0x18,0x0E,0x00,0x04,0x06,0x27,0xE5,0xE1,0xE1,0x21,0x21,0x25,0x67,0xE7,0xC7,0x84,0x00,
0x00,0x00,0x3F,0x21,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0xA8,0x70,0x20,0x80,0xFF,0xFF,0xFF,0x80,0x80,0xC0,0xC0,0xE0,0x7F,0x3F,0x1F
},
{ //M ctr
0xFF,0x01,0xE1,0xBD,0xBD,0xBD,0xBD,0xE1,0x01,0x01,0x01,0x01,0xFD,0xFD,0xFD,0xFD,0x01,0x01,0x01,0x01,0x01,0xFD,0xFD,0xFD,
0xFD,0x01,0x01,0x01,0x01,0x01,0xFF,0x02,0xFE,0xFE,0x3C,0x78,0xF0,0xE0,0xC0,0x80,0xC0,0x70,0x18,0xFC,0xFE,0xFE,0xFE,0x02,
0xFF,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0xC0,0x7F,0x7F,0x7F,0x7F,0xC0,0x00,0x00,0x00,0x07,0xFD,0xFD,0xFD,
0xFD,0x07,0x00,0x00,0x00,0x00,0xFF,0x40,0x7F,0x7F,0x40,0x00,0x00,0x01,0x03,0x01,0x00,0x00,0x40,0x7F,0x7F,0x7F,0x7F,0x40,
0xFF,0x80,0x80,0xBF,0xBF,0xBF,0xBF,0x80,0x80,0x80,0x80,0x81,0xBF,0xBF,0xBF,0xBF,0x81,0x80,0x80,0x80,0x80,0xBF,0xBF,0xBF,
0xBF,0x80,0x80,0x80,0x80,0x80,0xFF,0x00,0x7C,0x82,0x82,0x82,0x44,0x02,0x02,0xFE,0x02,0x02,0xFE,0x12,0x12,0x32,0xCC,0x00
},
{ //正转图片
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x08,0xE8,0xE8,0xE8,0xE8,0xE8,0xE8,0xE8,0xE8,0xE8,0xE8,0xE8,
0xE8,0xFE,0xF4,0xE8,0x70,0x20,0x00,0x00,0xF8,0xF8,0xF8,0xF8,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x10,0x38,0x7C,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0x00,
0x00,0x03,0x01,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0xFE,0x7C,0x38,0x10,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x10,0x1F,0x1F,0x1F,0x00,0x00,0x04,0x0E,0x17,0x2F,0x7F,0x17,
0x17,0x17,0x17,0x17,0x17,0x17,0x17,0x17,0x17,0x17,0x17,0x17,0x10,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
},
{ //反转图片
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0x08,0xF8,0xF8,0xF8,0x00,0x00,0x20,0x70,0xE8,0xF4,0xFE,0xE8,
0xE8,0xE8,0xE8,0xE8,0xE8,0xE8,0xE8,0xE8,0xE8,0xE8,0xE8,0xE8,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x08,0x1C,0x3E,0x7F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x00,
0x00,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x3E,0x1C,0x08,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0x10,0x17,0x17,0x17,0x17,0x17,0x17,0x17,0x17,0x17,0x17,0x17,
0x17,0x7F,0x2F,0x17,0x0E,0x04,0x00,0x00,0x1F,0x1F,0x1F,0x1F,0x10,0x1F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
}
};
#endif