#include "reg52.h"
#include "intrins.h"
#include "math.h"
#define unchar unsigned char
#define unint unsigned int
#define dataPort P0
#define Vmax 16
#define freq 60
sbit rs = P3^5;
sbit rw = P3^6;
sbit en = P3^4;
float x,y;
unchar code xian1[]={"峰值16V" };
unchar code xian2[]={"频率60Hz" };
void delayms(unsigned int n)
{
unsigned char i;
for(;n>0;n--)
for(i=0;i<100;i++);
}
void checkBusy(void)
{
rs=0;
rw=1;
en=1;
dataPort=0xff;
while(dataPort & 0x80);
en=0;
}
void writeCommand(unsigned char cmd)
{
checkBusy();
rs=0;
rw=0;
en=1;
dataPort=cmd;
_nop_();
en=0;
}
void writeData(unsigned char adata)
{
checkBusy();
rs=1;
rw=0;
en=1;
dataPort=adata;
_nop_();
en=0;
}
unsigned char readData(void)
{
unsigned char RData;
dataPort=0xff;
checkBusy();
rs=1;
rw=1;
en=0;
en=1;
RData=dataPort;
en=0;
return RData;
}
void ClrGDRAM(void)
{
unsigned char x,y;
for(y=0;y<64;y++)
for(x=0;x<16;x++)
{
writeCommand(0x34);
writeCommand(y+0x80); //行地址
writeCommand(x+0x80); //列地址
writeCommand(0x30);
writeData(0x00);
writeData(0x00);
}
//writeCommand(0x30);
}
void LcmInit(void)
{
writeCommand(0x30);
delayms(50);
writeCommand(0x01);
delayms(50);
writeCommand(0x06);
delayms(50);
writeCommand(0x0c);
ClrGDRAM();
//psb=1;
}
/***********************************************************
函数名: drawPoint
函数说明:画点
传入参数:打点位置(x0,y0);color=1,点亮;color=0,擦除
传出参数:无
返回值: 无
**********************************************************/
void drawPoint(unsigned char x,unsigned char y,unsigned char color)
{
unsigned char row,collum,cbite;
unsigned char tempH,tempL;
writeCommand(0x34);
writeCommand(0x36);
collum=x>>4;
cbite=x&0x0f;
if(y<32)
row=y;
else
{row=y-32;
collum+=8;
}
writeCommand(0x80+row);
writeCommand(0x80+collum);
readData();
tempH=readData();
tempL=readData();
writeCommand(0x80+row);
writeCommand(0x80+collum);
if (color)
{
if(cbite<8)
{
tempH|=(1<<(7-cbite));
//tempL=(1<<(7-cbite));
}
else
{
//tempH=(1<<(15-cbite));
tempL|=(1<<(15-cbite));
}
}
else
{
if(cbite<8)
{
tempH&=~(1<<(7-cbite));
//tempL=(1<<(7-cbite));
}
else
{
//tempH=(1<<(15-cbite));
tempL&=~(1<<(15-cbite));
}
}
writeData(tempH);
writeData(tempL);
writeCommand(0x30);
}
/***********************************************************
函数名: drawRowLine
函数说明:画水平线
传入参数:(x0,y0),水平线的起点;(x1,y0)水平线的终点
color=1,点亮;color=0,擦除
传出参数:无
返回值: 无
**********************************************************/
void drawRowLine(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char color)
{
unsigned char temp;
if(x0>x1) // 对x0、x1大小进行排列,以便画图
{
temp = x1;
x1 = x0;
x0 = temp;
}
do
{
drawPoint(x0, y0, color); // 逐点显示,描出垂直线
x0++;
}
while(x1>=x0);
}
/***********************************************************
函数名: drawCollumLine
函数说明:画竖直线
传入参数:(x0,y0),竖直线的起点;(x0,y1)竖直线的终点;
color=1,点亮;color=0,擦除
传出参数:无
返回值: 无
************************************************************/
void drawCollumLine(unsigned char x0,unsigned char y0,unsigned char y1,unsigned char color)
{
unsigned char temp;
if(y0>y1)
{
temp=y0;
y0=y1;
y1=temp;
}
while (y0<=y1)
{
drawPoint(x0,y0,color);
y0++;
}
}
//在坐标(x,y)处显示字符串
void LcmPrint(unsigned char x,unsigned char y,unsigned char *adata)
{
unsigned char address;
unsigned char i=0;
switch (y)
{
case 0:address=0x80+x;break;
case 1:address=0x90+x;break;
case 2:address=0x88+x;break;
case 3:address=0x98+x;break;
default:break;
}
writeCommand(address);
while(*(adata+i))
{
writeData(*(adata+i));
i++;
}
}
void main()
{
x=y=0;
LcmInit();
drawRowLine(0,31,127,1);
drawCollumLine(0,0,63,1);
while(1)
{
for(x=0;x<128;x=x+0.05)
{
y=Vmax*sin(2*3.14*freq*x);
drawPoint(x,(y+31),1);
}
LcmPrint(0,3,xian1);
LcmPrint(4,3,xian2);
}
}
|