LCD160160画点程序-竹天笑学习笔记
黑白屏的LCD160160,驱动IC是UC1698.选择nor的第三块作为LCD的控制信号,RS用A0.
详细请看程序,本人也是新手,只想交流下,顺便交几个朋友,
黑白屏读点是需要空读一次,读两个字节,两个字节包含三个点的信息.从前到后565的分布.
写点是连续写1个半字节,包含3个点的信息,从前到后444的分布.具体请看uc1698芯片介绍.其它好像没有什么特殊之处,主要是用驱动RGB的屏来驱动黑白屏,所以可以算是原来一个点的RGB对应3个点的黑白.
下面上程序:
/* Includes ------------------------------------------------------------------*/
#include "lcd_fsmc.h"
/* Private define ------------------------------------------------------------*/
/* 使用总线方式时定义地址 */
/* 挂在不同的BANK,使用不同地址线时请自行换算地址 */
#define LCD_REG (*((volatile u8 *) 0x68000000)) /* RS = 0 */
#define LCD_RAM (*((volatile u8 *) 0x68000001)) /* RS = 1 */
void Delay(__IO u32 nCount)
{
for(; nCount != 0; nCount--);
}
/*******************************************************************************
* Function Name : - FSMC_Configuration
* Description : - 静态存储器控制器配置
* Input : - None
* Output : - None
* Return : - None
* Attention : - None
*******************************************************************************/
void FSMC_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF | RCC_APB2Periph_GPIOG | RCC_APB2Periph_AFIO, ENABLE);
/***************************************************************************
* Description : FSMC管脚初始化
* Attention : ---------------------------
* | 地址线 |
* | PF 0~ 5 - A 0~ 5 |
* | PF12~15 - A 6~ 9 |
* | PG 0~ 5 - A10~15 |
* | PD11~13 - A16~18 |
* ---------------------------
* | 数据线 |
* | PD14~15 - D 0~ 1 |
* | PD 0~ 1 - D 2~ 3 |
* | PE 7~10 - D 4~ 7 |
* ---------------------------
* | 公共信号 |
* | PD 4 - NOE |
* | PD 5 - NWE |
* | PD 6 - NWAIT - NAND512|
* ---------------------------
* | NOR块选择信号 |
* | PG10 - NE3 - LCD |
* ---------------------------
***************************************************************************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5//A0-A18
| GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOF,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14 | GPIO_Pin_15 | GPIO_Pin_0 | GPIO_Pin_1;//D0-D7
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;//NOE、NWE
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//NE3,LCD
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOG, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : FSMC_LCD_Init
* Description : 液晶FSMC配置
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void FSMC_LCD_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; //背光和复位
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
FSMC_NORSRAMTimingInitTypeDef p;
/*-- FSMC Configuration ----------------------------------------------------*/
p.FSMC_AddressSetupTime = 1;
p.FSMC_AddressHoldTime = 0;
p.FSMC_DataSetupTime = 12;
p.FSMC_BusTurnAroundDuration = 0;
p.FSMC_CLKDivision = 0;
p.FSMC_DataLatency = 15;
p.FSMC_AccessMode = FSMC_AccessMode_B;
FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM3;
FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_8b;
FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &p;
FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &p;
FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
/* Enable FSMC Bank1_NOR Bank */
FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM3, ENABLE);
GPIO_ResetBits(GPIOD,GPIO_Pin_8); //复位
Delay(1000);
GPIO_SetBits(GPIOD,GPIO_Pin_8);
LCD_LightOn(); //开背光
}
/*******************************************************************************
* Function Name : LCD_Initializtion
* Description : 液晶初始化
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void LCD_Initializtion(void)
{
FSMC_Configuration();
FSMC_LCD_Init();
LCD_WriteCommand(0xE2);// -24- System Reset
LCD_WriteCommand(0xeb);// -27- Set LCD Bias Ratio E8-EB
LCD_WriteCommand(0x81);// -11- set- Vbias potention meter
LCD_WriteCommand(0xB5);// set vop 0~255
LCD_WriteCommand(0xaf);// -22- power control set
LCD_WriteCommand(0x80);// set vop 0~255
LCD_WriteCommand(0xf4);// set windows programm starting column address
LCD_WriteCommand(0x25);// set 00
LCD_WriteCommand(0xf5);// set windows programm starting row address
LCD_WriteCommand(0x00);// set 00
LCD_WriteCommand(0xf6);// set windows programm ending column address
LCD_WriteCommand(0x5A);// set 3x50=150
LCD_WriteCommand(0xf7);// set windows programm ending row address
LCD_WriteCommand(0xa0);// set 150
LCD_WriteCommand(0xf8);// set windows programm mode inside mode ac3=0
LCD_WriteCommand(0xd1);// set windows programm mode inside mode ac3=0
LCD_WriteCommand(0xd5);// set windows programm mode inside mode ac3=0
LCD_Clear(0);
}
/*******************************************************************************
* Function Name : LCD_LightOn
* Description : 开背光
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void LCD_LightOn(void)
{
GPIO_ResetBits(GPIOD, GPIO_Pin_9);//开背光
}
/*******************************************************************************
* Function Name : LCD_LightOff
* Description : 关背光
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void LCD_LightOff(void)
{
GPIO_SetBits(GPIOD, GPIO_Pin_9);//关背光
}
/*******************************************************************************
* Function Name : LCD_WriteCommand
* Description : Writes to the LCD REG.
* Input : - cmd: LCD CMD.
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void LCD_WriteCommand(u8 cmd)
{
/* Write cmd */
LCD_REG = cmd;
}
/*******************************************************************************
* Function Name : LCD_WriteData
* Description : Writes to the LCD RAM.
* Input : - data: the pixel color in RGB mode (4-4-4).
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void LCD_WriteData(u8 data)
{
/* Write 8-bit data */
LCD_RAM = data;
}
/*******************************************************************************
* Function Name : LCD_ReadData
* Description : Read the LCD RAM.
* Input : None
* Output : None
* Return : - data: the pixel color in RGB mode (5-6-5).
* Attention : None
*******************************************************************************/
u8 LCD_ReadData(void)
{
/* Write 8-bit data */
return LCD_RAM;
}
/*******************************************************************************
* Function Name : LCD_Clear
* Description : 清屏
* Input : - Color :0白屏, 1黑屏
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void LCD_Clear(u8 Color)
{
u8 row=0, col=0;
LCD_WriteCommand(0x05);// set column adress LSB CA3 CA2 CA1 CA0 =05
LCD_WriteCommand(0x12);// set column adress MSB CA6 CA5 =02
LCD_WriteCommand(0x60);// set ROW adress LSB RA3 RA2 RA1 RA0 =00
LCD_WriteCommand(0x70);// set ROW adress MSB RA7 RA6 RA5 RA4 =00
for (row=0; row<160; row++)
{
for(col=0; col<81; col++) //
{
LCD_WriteData(Color); //a byte per a pixel
}
}
}
/*******************************************************************************
* Function Name : Disp_Point
* Description : 画点函数
* Input : - Xpos: 水平坐标,0-159之间
* - Ypos: 竖直坐标,0-159之间
* - Color: 颜 {MOD},color=0为变白,color=1为变黑,color=2为反转
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void Disp_Point(u8 Xpos, u8 Ypos, u8 Color)
{
u8 Xlowpos, Xhighpos, Ylowpos, Yhighpos;
u8 Xmidpos;
u8 firstData, secondData;
u16 Data, tempData;
Ylowpos = (( 159 - Ypos ) & 0x0f ) | 0x60;
Yhighpos = (( 159 - Ypos ) >> 4 ) | 0x70;
Xmidpos = Xpos % 3;
Xpos = Xpos / 3;
Xlowpos = (( Xpos + 0x25 ) & 0x0f ) | 0x00;
Xhighpos = (( Xpos + 0x25 ) >> 4 ) | 0x10;
LCD_WriteCommand(Xlowpos);// set column adress LSB CA3 CA2 CA1 CA0 =05
LCD_WriteCommand(Xhighpos);// set column adress MSB CA6 CA5 =02
LCD_WriteCommand(Ylowpos);// set ROW adress LSB RA3 RA2 RA1 RA0 =00
LCD_WriteCommand(Yhighpos);// set ROW adress MSB RA7 RA6 RA5 RA4 =00
LCD_ReadData();
firstData = LCD_ReadData();
secondData = LCD_ReadData();
Data = ( (u16)firstData << 8 ) | ( (u16)secondData );
switch ( Xmidpos )
{
case 0:tempData = 0xF800;break;
case 1:tempData = 0x07E0;break;
case 2:tempData = 0x001F;break;
default:break;
}
switch ( Color )
{
case 0:
{
Data = Data & (~tempData);
break;
}
case 1:
{
Data = Data | tempData;
break;
}
case 2:
{
Data = Data ^ tempData;
break;
}
default:break;
}
firstData = ( (u8)(Data >> 8) & 0xf0 ) | ( (u8)(Data >> 5) & 0x0f );
secondData = (u8)(Data << 4) & 0xf0 ;
LCD_WriteCommand(Xlowpos);// set column adress LSB CA3 CA2 CA1 CA0 =05
LCD_WriteCommand(Xhighpos);// set column adress MSB CA6 CA5 =02
LCD_WriteCommand(Ylowpos);// set ROW adress LSB RA3 RA2 RA1 RA0 =00
LCD_WriteCommand(Yhighpos);// set ROW adress MSB RA7 RA6 RA5 RA4 =00
LCD_WriteData(firstData);
LCD_WriteData(secondData);
}
/******************************************************************************
* Function Name : LCD_DispLine
* Description : 画线 Bresenham's line algorithm
* Input : - x0: A点行坐标
* - y0: A点列坐标
* - x1: B点行坐标
* - y1: B点列坐标
* - Color: 线颜 {MOD}
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void Disp_Line(u8 x0, u8 y0, u8 x1, u8 y1, u8 Color)
{
u8 dx, dy; /* 定义X Y轴上增加的变量值 */
s16 temp; /* 起点 终点大小比较 交换数据时的中间变量 */
u8 inc;
if ( x1 >= x0 )
{
dx = x1 - x0; /* X轴方向上的增量 */
}
else
{
dx = x0 - x1;
}
if ( y1 >= y0 )
{
dy = y1 - y0; /* Y轴方向上的增量 */
}
else
{
dy = y0 - y1;
}
if ( dx >= dy ) /* 靠近x轴,以x轴递增画线*/
{
if ( x0 > x1 )
{
temp = x1; /* 起点大于终点 交换数据 */
x1 = x0;
x0 = temp;
temp = y1;
y1 = y0;
y0 = temp;
}
if ( y0 > y1 )
{
inc = 0;
}
else
{
inc = 1;
}
}
else /* 靠近y轴,以y轴递增画线*/
{
if ( y0 > y1 )
{
temp = x1; /* 起点大于终点 交换数据 */
x1 = x0;
x0 = temp;
temp = y1;
y1 = y0;
y0 = temp;
}
if ( x0 > x1 )
{
inc = 0;
}
else
{
inc = 1;
}
}
if( dx == 0 ) /* X轴上没有增量 画垂直线 */
{
do
{
Disp_Point(x0, y0, Color); /* 逐点显示 描垂直线 */
y0++;
}
while( y1 >= y0 );
return;
}
if( dy == 0 ) /* Y轴上没有增量 画水平直线 */
{
do
{
Disp_Point(x0, y0, Color); /* 逐点显示 描水平线 */
x0++;
}
while( x1 >= x0 );
return;
}
/* 布兰森汉姆(Bresenham)算法画线 */
if( dx >= dy ) /* 靠近X轴 */
{
temp = 2 * dy - dx; /* 计算下个点的位置 */
while( x0 != x1 )
{
Disp_Point(x0, y0, Color); /* 画起点 */
x0++; /* X轴上加1 */
if( temp > 0 ) /* 判断下下个点的位置 */
{
if(inc==1) /* 为右上相邻点,即(x0+1,y0+1) */
{
y0++;
}
else
{
y0--;
}
temp += 2 * dy - 2 * dx;
}
else
{
temp += 2 * dy; /* 判断下下个点的位置 */
}
}
Disp_Point(x0, y0, Color);
}
else
{
temp = 2 * dx - dy; /* 靠近Y轴 */
while( y0 != y1 )
{
Disp_Point(x0, y0, Color);
y0++;
if( temp > 0 )
{
if(inc==1) /* 为右上相邻点,即(x0+1,y0+1) */
{
x0++;
}
else
{
x0--;
}
temp += 2 * dx - 2 * dy;
}
else
{
temp += 2 * dx;
}
}
Disp_Point(x0, y0, Color);
}
}
/******************************************************************************
* Function Name : Disp_Rectangle
* Description : 画矩形
* Input : - x0: A点行坐标
* - y0: A点列坐标
* - x3: D点行坐标
* - y3: D点列坐标
* - Color: 线颜 {MOD}
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void Disp_Rectangle(u8 x0, u8 y0, u8 x3, u8 y3, u8 Color)
{
Disp_Line(x0, y0, x0, y3, Color);
Disp_Line(x0, y3, x3, y3, Color);
Disp_Line(x3, y3, x3, y0, Color);
Disp_Line(x3, y0, x0, y0, Color);
}
#define DEG2RAD (3.1415926f / 180)
/******************************************************************************
* Function Name : Disp_Ellipse
* Description : 画椭圆
* Input : - xCenter: 圆心行坐标
* - yCenter: 圆心列坐标
* - Rx: x轴半径
* - Ry: y轴半径
* - Color: 线颜 {MOD}
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void Disp_Ellipse(u8 xCenter, u8 yCenter, u8 Rx, u8 Ry, u8 Color)
{
u16 t;
u8 x, y;
for ( t = 0; t < 360; t = t + 3 ) //画120个点
{
x = (u8)( xCenter + Rx * cos(t*DEG2RAD) + 0.5 ); //+0.5是为了四舍五入
y = (u8)( yCenter + Ry * sin(t*DEG2RAD) + 0.5 );
Disp_Point(x, y, Color);
}
}
/******************************************************************************
* Function Name : Disp_Button
* Description : 画实心矩形
* Input : - Xpos: 行坐标
* - Ypos: 列坐标
* - str: 字符串
* - charColor: 字符颜 {MOD}
* - bkColor: 背景颜 {MOD}
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void Disp_SolidRectangle(u8 x0, u8 y0, u8 x3, u8 y3, u8 Color, u8 bkColor)
{
u8 x;
if ( x0 > x3 ) //画填充
{
for ( x = x3 + 1; x < x0; x++ )
{
Disp_Line(x, y0, x ,y3, bkColor);
}
}
else
{
for ( x = x0 + 1; x < x3; x++ )
{
Disp_Line(x, y0, x ,y3, bkColor);
}
}
Disp_Rectangle(x0, y0, x3, y3, Color); //画外框
}
/******************************************************************************
* Function Name : Disp_SolidEllipse
* Description : 画实心椭圆
* Input : - Xpos: 行坐标
* - Ypos: 列坐标
* - str: 字符串
* - charColor: 字符颜 {MOD}
* - bkColor: 背景颜 {MOD}
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void Disp_SolidEllipse(u8 xCenter, u8 yCenter, u8 Rx, u8 Ry, u8 Color, u8 bkColor)
{
Disp_Ellipse(xCenter,yCenter,Rx,Ry,Color); //画外框
while ( (Rx != 0) || (Ry != 0) )
{
Rx--;
Ry--;
Disp_Ellipse(xCenter,yCenter,Rx,Ry,bkColor); //填充
}
}
交流QQ:3440-19-106
LCD160160.zip
(14.96 KB, 下载次数: 63)
2012-11-17 11:53 上传
点击文件名下载附件
UC1698.pdf
(655.42 KB, 下载次数: 86)
2012-11-17 11:53 上传
点击文件名下载附件
一周热门 更多>