ATMEGA16 12864LCD驱动程序(KS0108芯片)
扫描二维码
随时随地手机看文章
#include
#include
#include
voidmain(void)
{
PORTB=0;
PORTC=0;
DDRB=255;
DDRC=255;
delay_ms(100);
RESET=1;//复位
delay_ms(100);
ks0108cls();//清屏
ks0108putchar(’A’);
ks0108gotoxy(70,2);
ks0108putchar(’#’);
while(1)
{
}
}
//文件
//www.avrDIY.com
#defineRESETPORTB.2
#defineRSPORTB.3
#defineRWPORTB.4
#defineEPORTB.5
#defineCS1PORTB.6
#defineCS2PORTB.7
#defineDATAPORTC
#include
//由于ks0108没有读坐标指令,所以就用2个全局变量来记录坐标
unsignedcharks0108x,ks0108y;
flashunsignedchardata[]={
0x00,0x00,0x00,0x00,0x00,//sp
0x00,0x00,0x2f,0x00,0x00,//!
0x00,0x07,0x00,0x07,0x00,//"
0x14,0x7f,0x14,0x7f,0x14,//#
0x24,0x2a,0x7f,0x2a,0x12,//$
0x62,0x64,0x08,0x13,0x23,//%
0x36,0x49,0x55,0x22,0x50,//&
0x00,0x05,0x03,0x00,0x00,//’
0x00,0x1c,0x22,0x41,0x00,//(
0x00,0x41,0x22,0x1c,0x00,//)
0x14,0x08,0x3E,0x08,0x14,//*
0x08,0x08,0x3E,0x08,0x08,//+
0x00,0x00,0xA0,0x60,0x00,//,
0x08,0x08,0x08,0x08,0x08,//-
0x00,0x60,0x60,0x00,0x00,//.
0x20,0x10,0x08,0x04,0x02,///
0x3E,0x51,0x49,0x45,0x3E,//0
0x00,0x42,0x7F,0x40,0x00,//1
0x42,0x61,0x51,0x49,0x46,//2
0x21,0x41,0x45,0x4B,0x31,//3
0x18,0x14,0x12,0x7F,0x10,//4
0x27,0x45,0x45,0x45,0x39,//5
0x3C,0x4A,0x49,0x49,0x30,//6
0x01,0x71,0x09,0x05,0x03,//7
0x36,0x49,0x49,0x49,0x36,//8
0x06,0x49,0x49,0x29,0x1E,//9
0x00,0x36,0x36,0x00,0x00,//:
0x00,0x56,0x36,0x00,0x00,//;
0x08,0x14,0x22,0x41,0x00,//<
0x14,0x14,0x14,0x14,0x14,//=
0x00,0x41,0x22,0x14,0x08,//>
0x02,0x01,0x51,0x09,0x06,//?
0x32,0x49,0x59,0x51,0x3E,//@
0x7C,0x12,0x11,0x12,0x7C,//A
0x7F,0x49,0x49,0x49,0x36,//B
0x3E,0x41,0x41,0x41,0x22,//C
0x7F,0x41,0x41,0x22,0x1C,//D
0x7F,0x49,0x49,0x49,0x41,//E
0x7F,0x09,0x09,0x09,0x01,//F
0x3E,0x41,0x49,0x49,0x7A,//G
0x7F,0x08,0x08,0x08,0x7F,//H
0x00,0x41,0x7F,0x41,0x00,//I
0x20,0x40,0x41,0x3F,0x01,//J
0x7F,0x08,0x14,0x22,0x41,//K
0x7F,0x40,0x40,0x40,0x40,//L
0x7F,0x02,0x0C,0x02,0x7F,//M
0x7F,0x04,0x08,0x10,0x7F,//N
0x3E,0x41,0x41,0x41,0x3E,//O
0x7F,0x09,0x09,0x09,0x06,//P
0x3E,0x41,0x51,0x21,0x5E,//Q
0x7F,0x09,0x19,0x29,0x46,//R
0x46,0x49,0x49,0x49,0x31,//S
0x01,0x01,0x7F,0x01,0x01,//T
0x3F,0x40,0x40,0x40,0x3F,//U
0x1F,0x20,0x40,0x20,0x1F,//V
0x3F,0x40,0x38,0x40,0x3F,//W
0x63,0x14,0x08,0x14,0x63,//X
0x07,0x08,0x70,0x08,0x07,//Y
0x61,0x51,0x49,0x45,0x43,//Z
0x00,0x7F,0x41,0x41,0x00,//[
0x55,0x2A,0x55,0x2A,0x55,//55
0x00,0x41,0x41,0x7F,0x00,//]
0x04,0x02,0x01,0x02,0x04,//^
0x40,0x40,0x40,0x40,0x40,//_
0x00,0x01,0x02,0x04,0x00,//’
0x20,0x54,0x54,0x54,0x78,//a
0x7F,0x48,0x44,0x44,0x38,//b
0x38,0x44,0x44,0x44,0x20,//c
0x38,0x44,0x44,0x48,0x7F,//d
0x38,0x54,0x54,0x54,0x18,//e
0x08,0x7E,0x09,0x01,0x02,//f
0x18,0xA4,0xA4,0xA4,0x7C,//g
0x7F,0x08,0x04,0x04,0x78,//h
0x00,0x44,0x7D,0x40,0x00,//i
0x40,0x80,0x84,0x7D,0x00,//j
0x7F,0x10,0x28,0x44,0x00,//k
0x00,0x41,0x7F,0x40,0x00,//l
0x7C,0x04,0x18,0x04,0x78,//m
0x7C,0x08,0x04,0x04,0x78,//n
0x38,0x44,0x44,0x44,0x38,//o
0xFC,0x24,0x24,0x24,0x18,//p
0x18,0x24,0x24,0x18,0xFC,//q
0x7C,0x08,0x04,0x04,0x08,//r
0x48,0x54,0x54,0x54,0x20,//s
0x04,0x3F,0x44,0x40,0x20,//t
0x3C,0x40,0x40,0x20,0x7C,//u
0x1C,0x20,0x40,0x20,0x1C,//v
0x3C,0x40,0x30,0x40,0x3C,//w
0x44,0x28,0x10,0x28,0x44,//x
0x1C,0xA0,0xA0,0xA0,0x7C,//y
0x44,0x64,0x54,0x4C,0x44,//z
0x00,0x08,0x36,0x41,0x00,//{
0x00,0x00,0x7F,0x00,0x00,//|
0x00,0x41,0x36,0x08,0x00,//}
0x08,0x10,0x08,0x04,0x08//~
};
voidks0108rs0(unsignedcharrs)//写一个指令
{
E=1;RS=0;RW=0;
DATA=rs;
E=0;
delay_ms(1);//KS0108真是慢,写指令/数据都要适当延时
}
voidks0108putbyte(unsignedchardatas)//写一个字节
{
if(ks0108x>127)//显示到最后列时就退出本函数,并停止写显示数据
return;
if(ks0108x<64)//判断列地址,确定在左屏还是在右屏写数据
{CS1=1;CS2=0;}
else
{CS1=0;CS2=1;}
E=1;RS=1;RW=0;//写一个显示数据
DATA=datas;
E=0;
ks0108x++;//每写一个数据,列地址就要加1
delay_ms(1);
}
voidks0108putchar(unsignedcharASCII)//写一个字符(就是写6个数据)
{
unsignedchari;
unsignedintNo;
No=ASCII-32;//字模数据是由空格’’开始,空格的ASCII的值就是32
No=No*5;//每个字符的字模是5个字节
ks0108putbyte(0);//每个字符之间空一列
for(i=0;i<5;i++)//一个字符的字模是5个字节,就是5*8点阵
{
ks0108putbyte(data[No]);
No++;
}
}
voidks0108gotoxy(unsignedcharx,unsignedchary)//写列/行地址
{
if(x>127)x=0;//如果列地址超出范围就回到0列
if(y>7)y=0;//如果行地址超出范围就回到0行
CS1=1;//左右屏都使能,写下相同的行地址
CS2=1;
ks0108rs0(y+184);//写页(行)地址,共有8页,第0页地址是184
if(x<64)//写列地址,共有64列,第0列地址是64
{CS1=1;CS2=0;ks0108rs0(x+64);}
else
{CS1=0;CS2=1;ks0108rs0(x);};
ks0108x=x;//记住列/行地址
ks0108y=y;
}
voidks0108cls(void)//清屏,光标回到左半屏左上角
{
unsignedchari0,i1;
CS1=1;//左/右屏都使能
CS2=1;
ks0108rs0(63);//打开显示
ks0108rs0(64);//列地址回到0
for(i0=0;i0<8;i0++)//页地址由7变到0
{
ks0108rs0(191-i0);
for(i1=0;i1<64;i1++)//对某一行全写入0
{
E=1;RS=1;RW=0;
DATA=0;
E=0;
delay_ms(1);
}
}
CS2=0;
ks0108x=0;
ks0108y=0;
}