一個51單片機程序使用正弦函數在lcd12864液晶屏上面顯示正弦波
下面是完整代碼下載:
正弦波.rar
(1.47 KB, 下載次數: 169)
2017-3-9 03:39 上傳
點擊文件名下載附件
部分代碼預覽:
- #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);
- }
- }
復制代碼
|