找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 1427|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

12864正選撥程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:234985 發(fā)表于 2017-9-22 21:57 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
#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;
}

/***********************************************************
函數(shù)名:  drawPoint
函數(shù)說明:畫點
傳入?yún)?shù):打點位置(x0,y0);color=1,點亮;color=0,擦除
傳出參數(shù):無
返回值:  無
**********************************************************/
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);
}

/***********************************************************
函數(shù)名:  drawRowLine
函數(shù)說明:畫水平線
傳入?yún)?shù):(x0,y0),水平線的起點;(x1,y0)水平線的終點
                                color=1,點亮;color=0,擦除
傳出參數(shù):無
返回值:  無
**********************************************************/
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);
}

/***********************************************************
函數(shù)名:  drawCollumLine
函數(shù)說明:畫豎直線
傳入?yún)?shù):(x0,y0),豎直線的起點;(x0,y1)豎直線的終點;
                        color=1,點亮;color=0,擦除
傳出參數(shù):無
返回值:  無
************************************************************/
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);
        }
}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復(fù) 返回頂部 返回列表