当前位置:首页 > 单片机 > 单片机
[导读] KS0108 128X64LCDC语言AVR程序/*LCD引脚定义1---GND2---VCC3---VLCD4---D/I5---R/W6---E7到14D0-D715--CS116--CS217--RESET18--VEE19--SW20--NC*/#include#includeunsignedcharCurOffset,CurRow,CurPag

KS0108 128X64LCDC语言AVR程序

/*
LCD引脚定义
1---GND
2---VCC
3---VLCD
4---D/I
5---R/W
6---E
7到14D0-D7
15--CS1
16--CS2
17--RESET
18--VEE

19--SW
20--NC
*/
#include
#include

unsignedcharCurOffset,CurRow,CurPage,CurCol;

/*常量定义*/
#defineLCD_STATUS_BUSY0x80
#defineSTART_LINE00xc0
#defineDISPLAY_ON0x3f
#defineDISPLAY_OFF0x3e
#definePARA10x40
//PORTA---数据口PORTB----控制口
volatileunsignedchar*LCD_DIR_PORT=&DDRA;
volatileunsignedchar*LCD_IP_PORT=&PINA;
volatileunsignedchar*LCD_OP_PORT=&PORTA;

volatileunsignedchar*LCD_EN_PORT=&PORTB;
volatileunsignedchar*LCD_CS2_PORT=&PORTB;
volatileunsignedchar*LCD_CS1_PORT=&PORTB;
volatileunsignedchar*LCD_RW_PORT=&PORTB;
volatileunsignedchar*LCD_DI_PORT=&PORTB;

charLCD_EN_BIT=BIT(4);
charLCD_CS2_BIT=BIT(3);
charLCD_CS1_BIT=BIT(2);
charLCD_RW_BIT=BIT(1);
charLCD_DI_BIT=BIT(0);

#defineSET_LCD_E*LCD_EN_PORT|=LCD_EN_BIT//LCD使能
#defineCLEAR_LCD_E*LCD_EN_PORT&=~LCD_EN_BIT//LCD禁止

#defineSET_LCD_DATA*LCD_DI_PORT|=LCD_DI_BIT//选择指令端口
#defineSET_LCD_CMD*LCD_DI_PORT&=~LCD_DI_BIT//选择数据端口

#defineSET_LCD_READ*LCD_RW_PORT|=LCD_RW_BIT//读模式
#defineSET_LCD_WRITE*LCD_RW_PORT&=~LCD_RW_BIT//写模式

#defineSET_LCD_CS2*LCD_CS2_PORT|=LCD_CS2_BIT//左屏选择禁止
#defineCLEAR_LCD_CS2*LCD_CS2_PORT&=~LCD_CS2_BIT//左屏选择使能

#defineSET_LCD_CS1*LCD_CS1_PORT|=LCD_CS1_BIT//左屏选择禁止
#defineCLEAR_LCD_CS1*LCD_CS1_PORT&=~LCD_CS1_BIT//左屏选择使能

#defineLEFT0
#defineRIGHT1
#defineCMD0
#defineDATA1

voidLCD_BUSY(unsignedcharlr)//判断忙标志
{
unsignedcharstatus;
CLI();
if(lr==LEFT)
{
CLEAR_LCD_CS2;//cs2=0
SET_LCD_CS1;//cs1=1
}
else
{
SET_LCD_CS2;//cs2=1
CLEAR_LCD_CS1;//cs1=0
}
SET_LCD_CMD;
*LCD_DIR_PORT=0x00;
*LCD_OP_PORT=0xff;
SET_LCD_READ;
SET_LCD_E;
asm("nop");asm("nop");
asm("nop");asm("nop");
while((*LCD_IP_PORT)&LCD_STATUS_BUSY)
{
CLEAR_LCD_E;
asm("nop");asm("nop");
asm("nop");asm("nop");
SET_LCD_E;
asm("nop");asm("nop");
asm("nop");asm("nop");
}
CLEAR_LCD_E;
SET_LCD_WRITE;
*LCD_OP_PORT=0xff;
SEI();
}

voidwrite_LCD(unsignedcharlr,unsignedcharcd,unsignedchardata)/*写入指令或数据*/
{
CLI();
LCD_BUSY(lr);
if(cd==CMD)SET_LCD_CMD;
elseSET_LCD_DATA;
SET_LCD_WRITE;
SET_LCD_E;
*LCD_DIR_PORT=0xff;
*LCD_OP_PORT=data;
asm("nop");asm("nop");
asm("nop");asm("nop");
CLEAR_LCD_E;
*LCD_OP_PORT=0xff;
SEI();
}
unsignedcharread_LCD(unsignedcharlr)/*读显示数据*/
{
unsignedchardata;

CLI();
LCD_BUSY(lr);
SET_LCD_DATA;
*LCD_DIR_PORT=0x00;
*LCD_OP_PORT=0xff;
SET_LCD_READ;
SET_LCD_E;
asm("nop");asm("nop");
asm("nop");asm("nop");
data=*LCD_IP_PORT;
CLEAR_LCD_E;
SET_LCD_WRITE;

LCD_BUSY(lr);
SET_LCD_DATA;
*LCD_DIR_PORT=0x00;
*LCD_OP_PORT=0xff;
SET_LCD_READ;
SET_LCD_E;
asm("nop");asm("nop");
asm("nop");asm("nop");
data=*LCD_IP_PORT;
CLEAR_LCD_E;
SET_LCD_WRITE;

SEI();
returndata;
}

voidset_start_line_L(unsignedcharline)/*设置显示起始行*///0-63
{
write_LCD(LEFT,CMD,0xc0|line);
}

voidset_start_line_R(unsignedcharline)/*设置显示起始行*///0-63
{
write_LCD(RIGHT,CMD,0xc0|line);
}

voidset_page_L(unsignedcharpage)/*设置X地址设置页*///0-7
{
write_LCD(LEFT,CMD,0xb8|page);
}
voidset_page_R(unsignedcharpage)/*设置X地址设置页*///0-7
{
write_LCD(RIGHT,CMD,0xb8|page);
}

voidset_col_addr_L(unsignedcharcol)/*设置Y地址*///0-63
{
write_LCD(LEFT,CMD,0x40|col);
}

voidset_col_addr_R(unsignedcharcol)/*设置Y地址*///0-63
{
write_LCD(RIGHT,CMD,0x40|col);
}

voidinit_lcd(void)/*初始化函数*/
{
set_start_line_L(0);/*显示起始行为0*/
set_start_line_R(0);/*显示起始行为0*/
write_LCD(LEFT,CMD,DISPLAY_ON);/*开显示*/
write_LCD(RIGHT,CMD,DISPLAY_ON);/*开显示*/
}

voidclr_lcd(void)/*清屏函数*/
{
unsignedcharpages,i;
for(pages=0;pages<8;pages++)
{
set_page_L(pages);/*X页地址*/
set_page_R(pages);/*X页地址*/
for(i=0;i<64;i++)
{
set_col_addr_L(i);//Y
set_col_addr_R(i);//Y
write_LCD(LEFT,DATA,0x0);
write_LCD(RIGHT,DATA,0x0);
}
}
}

//ASCII字模宽度及高度
#defineASC_CHR_WIDTH8
#defineASC_CHR_HEIGHT12

typedefstructtypFNT_ASC16/*汉字字模显示数据结构*/
{
charIndex[1];
charMsk[16];
};
structtypFNT_ASC16constASC_16[]={/*显示为8*16Curier10常规*/
"1",0x00,0x00,0x08,0x08,0xFC,0x00,0x00,0x00,0x00,0x00,0x04,0x04,0x07,0x04,0x04,0x00,
"2",0x00,0x00,0x08,0x04,0x84,0x44,0x38,0x00,0x00,0x00,0x06,0x05,0x04,0x04,0x04,0x00,
"3",0x00,0x00,0x08,0x04,0x44,0x44,0xB8,0x00,0x00,0x00,0x02,0x04,0x04,0x04,0x03,0x00,
"4",0x00,0x00,0x80,0x60,0x18,0xFC,0x00,0x00,0x00,0x00,0x01,0x01,0x05,0x07,0x05,0x00,
"5",0x00,0x00,0x7C,0x44,0x44,0x44,0x84,0x00,0x00,0x00,0x02,0x04,0x04,0x04,0x03,0x00,
"6",0x00,0x00,0xF0,0x48,0x44,0x44,0x80,0x00,0x00,0x00,0x03,0x04,0x04,0x04,0x03,0x00,
"7",0x00,0x00,0x0C,0x04,0x84,0x64,0x1C,0x00,0x00,0x00,0x00,0x06,0x01,0x00,0x00,0x00,
"8",0x00,0x00,0xB8,0x44,0x44,0x44,0xB8,0x00,0x00,0x00,0x03,0x04,0x04,0x04,0x03,0x00,
"9",0x00,0x00,0x38,0x44,0x44,0x44,0xF8,0x00,0x00,0x00,0x00,0x04,0x04,0x02,0x01,0x00,
"0",0x00,0x00,0xF8,0x04,0x04,0x04,0xF8,0x00,0x00,0x00,0x03,0x04,0x04,0x04,0x03,0x00,
".",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,
",",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x04,0x00,0x00,0x00,
":",0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,
";",0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x04,0x00,0x00,0x00,
"-",0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
"_",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,
"A",0x00,0x00,0x80,0x64,0x1C,0x60,0x80,0x00,0x00,0x04,0x07,0x05,0x01,0x05,0x07,0x04,
"B",0x00,0x04,0xFC,0x44,0x44,0x44,0x44,0xB8,0x00,0x04,0x07,0x04,0x04,0x04,0x04,0x03,
"C",0x00,0xF0,0x08,0x04,0x04,0x04,0x04,0x08,0x00,0x01,0x02,0x04,0x04,0x04,0x04,0x02,
"D",0x00,0x04,0xFC,0x04,0x04,0x04,0x08,0xF0,0x00,0x04,0x07,0x04,0x04,0x04,0x02,0x01,
"E",0x00,0x04,0xFC,0x44,0x44,0xE4,0x04,0x0C,0x00,0x04,0x07,0x04,0x04,0x04,0x04,0x06,
"F",0x00,0x04,0xFC,0x44,0x44,0xE4,0x04,0x0C,0x00,0x04,0x07,0x04,0x04,0x00,0x00,0x00,
"",0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
"=",0x00,0x00,0xA0,0xA0,0xA0,0xA0,0xA0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
"(",0x00,0x00,0x00,0xE0,0x18,0x04,0x00,0x00,0x00,0x00,0x00,0x03,0x0C,0x10,0x00,0x00,
")",0x00,0x00,0x00,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x00,0x10,0x0C,0x03,0x00,0x00,
"V",0x00,0x04,0x3C,0xC4,0x00,0xC4,0x3C,0x04,0x00,0x00,0x00,0x01,0x06,0x01,0x00,0x00,
"m",0x00,0x20,0xE0,0x20,0xC0,0x20,0xC0,0x00,0x00,0x04,0x07,0x00,0x07,0x00,0x07,0x04,
"H",0x00,0x04,0xFC,0x44,0x40,0x44,0xFC,0x04,0x00,0x04,0x07,0x04,0x00,0x04,0x07,0x04,
"*",0x90,0x90,0x60,0xFC,0x60,0x90,0x90,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x00,
};

#defineASC_Hz_WIDTH12
//#defineASC_HZ_HEIGHT12

typedefstructtypFNT_GB16/*12*16汉字字模显示数据结构*/
{
charIndex[2];
charMsk[24];
};
structtypFNT_GB16constGB_16[]={/*宋体9显示为12*16*/
"科",0x8A,0x6A,0xFE,0x29,0x49,0x80,0x92,0xA4,0x80,0xFF,0x40,0x00,0x01,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,
"技",0x44,0x44,0xFF,0x24,0x00,0x64,0xA4,0x3F,0xA4,0x64,0x04,0x00,0x04,0x04,0x07,0x00,0x04,0x04,0x02,0x01,0x02,0x04,0x04,0x00,
"有",0x42,0x22,0x12,0xFA,0x4E,0x4B,0x4A,0x4A,0xFA,0x02,0x02,0x00,0x00,0x00,0x00,0x07,0x01,0x01,0x01,0x05,0x07,0x00,0x00,0x00,
"限",0xFF,0x81,0x99,0xE7,0x00,0xFF,0x25,0xE5,0x25,0xBF,0x40,0x00,0x07,0x00,0x00,0x00,0x00,0x07,0x04,0x02,0x01,0x02,0x04,0x00,
"公",0x20,0x10,0x08,0x07,0xC0,0x30,0x83,0x0C,0x10,0x20,0x20,0x00,0x00,0x04,0x06,0x05,0x04,0x04,0x02,0x03,0x06,0x00,0x00,0x00,
"司",0x08,0x09,0xE9,0x29,0x29,0x29,0xED,0x09,0x01,0xFF,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x04,0x04,0x07,0x00,0x00,
};

unsignedcharGetPage(void)/*得到当前页*/
{
returnCurPage;
}

unsignedcharGetCol(void)/*得到当前列*/
{
returnCurCol;
}

voidSetPageCol(unsignedcharupage,unsignedcharucol)/*设置液晶的页和列*/
{
CurPage=upage;
CurCol=ucol;
if(ucol<64)
{
set_page_L(upage);
set_col_addr_L(ucol);
}
else
{
set_page_R(upage);
set_col_addr_R(ucol-64);
}
}

/*设置当前显示的页和列*/
voidSetRowCol(unsignedcharurow,unsignedcharucol)
{
unsignedcharpage;
CurRow=urow;
CurCol=ucol;

switch(urow)
{
case1:
page=0;
CurOffset=1;
break;
case2:
page=1;
CurOffset=2;
break;
case3:
page=3;
CurOffset=1;
break;
case4:
page=4;
CurOffset=2;
break;
case5:
page=6;
CurOffset=1;
break;
}
SetPageCol(page,ucol);
}

voiddisp_ch(unsignedcharc)
{
unsignedchark,j,uPage,uCol,ch_r,ch_w;
unsignedcharwidth;
unsignedcharlen;
uPage=GetPage();
uCol=GetCol();
len=sizeof(ASC_16)/sizeof(ASC_16[0]);
for(k=0;k{
if(c==ASC_16[k].Index[0])break;
}
if(k{

if(c==’-’||c==’:’)
width=ASC_CHR_WIDTH-2;
elseif(c==’|’)
width=ASC_HZ_WIDTH-ASC_CHR_WIDTH;
else
width=ASC_CHR_WIDTH;

if(CurOffset==1)//下半部是写半个字节
{
for(j=0;j{
SetPageCol(uPage,uCol+j);
ch_w=ASC_16[k].Msk[j];
if(uCol+j<64)write_LCD(LEFT,DATA,ch_w);
elsewrite_LCD(RIGHT,DATA,ch_w);
}
SetPageCol(uPage+1,uCol);

for(j=0;j{
SetPageCol(uPage+1,uCol+j);
if(uCol+j<64)ch_r=read_LCD(LEFT);
elsech_r=read_LCD(RIGHT);
ch_r&=0xf0;
ch_w=ASC_16[k].Msk[ASC_CHR_WIDTH+j]&0x0f;
ch_w|=ch_r;
SetPageCol(uPage+1,uCol+j);
if(uCol+j<64)write_LCD(LEFT,DATA,ch_w);
elsewrite_LCD(RIGHT,DATA,ch_w);
}
}
else//上半部是写半个字节
{
for(j=0;j{
SetPageCol(uPage,uCol+j);
if(uCol+j<64)ch_r=read_LCD(LEFT);
elsech_r=read_LCD(RIGHT);
ch_r&=0x0f;
ch_w=ASC_16[k].Msk[j];
ch_w=ch_w<<4;
ch_w|=ch_r;
SetPageCol(uPage,uCol+j);
if(uCol+j<64)write_LCD(LEFT,DATA,ch_w);
elsewrite_LCD(RIGHT,DATA,ch_w);
}
SetPageCol(uPage+1,uCol);
for(j=0;j{
SetPageCol(uPage+1,uCol+j);

ch_r=ASC_16[k].Msk[j];
ch_w=ASC_16[k].Msk[ASC_CHR_WIDTH+j];
ch_r=ch_r>>4;
ch_w=ch_w<<4;
ch_w|=ch_r;
SetPageCol(uPage+1,uCol+j);
if(uCol+j<64)write_LCD(LEFT,DATA,ch_w);
elsewrite_LCD(RIGHT,DATA,ch_w);
}
}
}
SetPageCol(uPage,uCol+width);
}


voiddisp_hz(unsignedchar*hz)
{
unsignedchark,j,uPage,uCol,ch_r,ch_w;
uPage=GetPage();
uCol=GetCol();
for(k=0;k{
if(hz[0]==GB_16[k].Index[0]&&hz[1]==GB_16[k].Index[1])
break;
}

if(CurOffset==1)
{
for(j=0;j{
SetPageCol(uPage,uCol+j);
ch_w=GB_16[k].Msk[j];
if(uCol+j<64)write_LCD(LEFT,DATA,ch_w);
elsewrite_LCD(RIGHT,DATA,ch_w);
}
SetPageCol(uPage+1,uCol);

for(j=0;j{
SetPageCol(uPage+1,uCol+j);
if(uCol+j<64)ch_r=read_LCD(LEFT);
elsech_r=read_LCD(RIGHT);
ch_r&=0xf0;
ch_w=GB_16[k].Msk[ASC_HZ_WIDTH+j]&0x0f;
ch_w|=ch_r;
SetPageCol(uPage+1,uCol+j);
if(uCol+j<64)write_LCD(LEFT,DATA,ch_w);
elsewrite_LCD(RIGHT,DATA,ch_w);
}
SetPageCol(uPage,uCol+ASC_HZ_WIDTH);
}

else//汉字上半部是写半个字节
{
for(j=0;j{
SetPageCol(uPage,uCol+j);
if(uCol+j<64)ch_r=read_LCD(LEFT);
elsech_r=read_LCD(RIGHT);
ch_r&=0x0f;
ch_w=GB_16[k].Msk[j];
ch_w=ch_w<<4;
ch_w|=ch_r;
SetPageCol(uPage,uCol+j);
if(uCol+j<64)write_LCD(LEFT,DATA,ch_w);
elsewrite_LCD(RIGHT,DATA,ch_w);
}
SetPageCol(uPage+1,uCol);
for(j=0;j{
SetPageCol(uPage+1,uCol+j);

ch_r=GB_16[k].Msk[j];
ch_w=GB_16[k].Msk[ASC_HZ_WIDTH+j];
ch_r=ch_r>>4;
ch_w=ch_w<<4;
ch_w|=ch_r;
SetPageCol(uPage+1,uCol+j);
if(uCol+j<64)write_LCD(LEFT,DATA,ch_w);
elsewrite_LCD(RIGHT,DATA,ch_w);
}
SetPageCol(uPage,uCol+ASC_HZ_WIDTH);
}
}

voiddisp_str(unsignedchar*p)
{
unsignedchari=0;
while(p[i]>0)
{
if(p[i]<128)
{/*ASCII*/
disp_ch(p[i]);
}
else
{/*中文*/
disp_hz(&p[i]);
i++;
}
i++;
}
}

/*************************************/
/*绘点函数*/
/*************************************/
/*XX--(0-128)YY--(0-63)FLAG=1绘点FLAG=0清点*/
voidpixel(unsignedcharxx,unsignedcharyy,unsignedcharflag)
{
unsignedinty,ch;
ch=yy%8;//余数

y=1;
for(;ch!=0;)
{
y=y*2;
ch--;
}
if(xx<64)
{
set_page_L(yy/8);
set_col_addr_L(xx);
ch=read_LCD(LEFT);
set_col_addr_L(xx);
if(flag)
write_LCD(LEFT,DATA,ch|y);
else
{
y=~y;
ch&=y;
write_LCD(LEFT,DATA,ch|y);
}
}
else
{
set_page_R(yy/8);
set_col_addr_R(xx-64);
ch=read_LCD(RIGHT);
set_col_addr_R(xx-64);
if(flag)
write_LCD(RIGHT,DATA,ch|y);
else
{
y=~y;
ch&=y;
write_LCD(RIGHT,DATA,ch|y);
}
}
}

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

9月2日消息,不造车的华为或将催生出更大的独角兽公司,随着阿维塔和赛力斯的入局,华为引望愈发显得引人瞩目。

关键字: 阿维塔 塞力斯 华为

加利福尼亚州圣克拉拉县2024年8月30日 /美通社/ -- 数字化转型技术解决方案公司Trianz今天宣布,该公司与Amazon Web Services (AWS)签订了...

关键字: AWS AN BSP 数字化

伦敦2024年8月29日 /美通社/ -- 英国汽车技术公司SODA.Auto推出其旗舰产品SODA V,这是全球首款涵盖汽车工程师从创意到认证的所有需求的工具,可用于创建软件定义汽车。 SODA V工具的开发耗时1.5...

关键字: 汽车 人工智能 智能驱动 BSP

北京2024年8月28日 /美通社/ -- 越来越多用户希望企业业务能7×24不间断运行,同时企业却面临越来越多业务中断的风险,如企业系统复杂性的增加,频繁的功能更新和发布等。如何确保业务连续性,提升韧性,成...

关键字: 亚马逊 解密 控制平面 BSP

8月30日消息,据媒体报道,腾讯和网易近期正在缩减他们对日本游戏市场的投资。

关键字: 腾讯 编码器 CPU

8月28日消息,今天上午,2024中国国际大数据产业博览会开幕式在贵阳举行,华为董事、质量流程IT总裁陶景文发表了演讲。

关键字: 华为 12nm EDA 半导体

8月28日消息,在2024中国国际大数据产业博览会上,华为常务董事、华为云CEO张平安发表演讲称,数字世界的话语权最终是由生态的繁荣决定的。

关键字: 华为 12nm 手机 卫星通信

要点: 有效应对环境变化,经营业绩稳中有升 落实提质增效举措,毛利润率延续升势 战略布局成效显著,战新业务引领增长 以科技创新为引领,提升企业核心竞争力 坚持高质量发展策略,塑强核心竞争优势...

关键字: 通信 BSP 电信运营商 数字经济

北京2024年8月27日 /美通社/ -- 8月21日,由中央广播电视总台与中国电影电视技术学会联合牵头组建的NVI技术创新联盟在BIRTV2024超高清全产业链发展研讨会上宣布正式成立。 活动现场 NVI技术创新联...

关键字: VI 传输协议 音频 BSP

北京2024年8月27日 /美通社/ -- 在8月23日举办的2024年长三角生态绿色一体化发展示范区联合招商会上,软通动力信息技术(集团)股份有限公司(以下简称"软通动力")与长三角投资(上海)有限...

关键字: BSP 信息技术
关闭
关闭