一個無控制器12864lcd小設計。包括顯示ASCII字符,漢字,圖片,畫直線,畫圓。
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
0.png (12.93 KB, 下載次數: 34)
下載附件
2018-4-8 15:20 上傳
0.jpg (45.62 KB, 下載次數: 29)
下載附件
2018-4-8 15:20 上傳
單片機源程序如下:
- #include<reg51.h>
- #include<intrins.h>
- #include<stdlib.h>
- #include<math.h>
- #include "ziku.h"
- #include "bmp.h"
- //***************************
- #define LCD P2;
- sbit RS=P3^0;
- sbit RW=P3^1;
- sbit EN=P3^2;
- sbit CS1=P3^4;
- sbit CS2=P3^3;
- //***************************
- sbit button1=P1^0;
- sbit button2=P1^1;
- sbit button3=P1^2;
- sbit button4=P1^3;
- sbit button5=P1^4;
- sbit button6=P1^5;
- //*****************************
- unsigned char code huan[]= // 歡
- {0x04,0x24,0x44,0x84,0x64,0x9C,0x40,0x30,0x0F,0xC8,0x08,0x08,0x28,0x18,0x00,0x00,
- 0x10,0x08,0x06,0x01,0x82,0x4C,0x20,0x18,0x06,0x01,0x06,0x18,0x20,0x40,0x80,0x00};
- unsigned char code ying[]= //迎
- {0x40,0x40,0x42,0xCC,0x00,0x00,0xFC,0x04,0x02,0x00,0xFC,0x04,0x04,0xFC,0x00,0x00,
- 0x00,0x40,0x20,0x1F,0x20,0x40,0x4F,0x44,0x42,0x40,0x7F,0x42,0x44,0x43,0x40,0x00};
- //**************************
- /*void busy()
- {
- P0=0x00;
- RS=0;
- RW=1;
- EN=1;
- while(P2&0x80);
- EN=0;
- } */
- //**************************
- void delay(unsigned int t)
- {
- unsigned int t1;
- for(t1=0;t1<t;t1++);
- }
- //**********************
- void write_cmd(unsigned char com)//寫指令
- {
- //busy();
- RS=0;RW=0;EN=0;
- P2=com;
- delay(5);
- EN=1;
- delay(5);
- EN=0;
- }
- //***************************
- void write_dat(unsigned char dat)//寫數據
- {
- //busy();
- RS=1;RW=0;EN=0;
- P2=dat;
- delay(5);
- EN=1;
- delay(5);
- EN=0;
- }
- //*****************
- void set_page(unsigned char page)//設置頁,12864LCD共有8頁,每頁有8行點陣點。
- {
- page=0xb8|page; //首頁地址為0XB8
- write_cmd(page); //page取值范圍為0~7,表示第1到8頁
- }
- //***************
- void set_line(unsigned char line)//設置顯示的起始行,共有0——63行,一般從0行開始顯示
- {
- line=0xc0|line; //起始行地址0XC0
- write_cmd(line); //line取值范圍為0~63,表示第1到64行
- }
- //***********************
- void set_column(unsigned char column)//設置顯示的列
- {
- column=0x40|column; //列的首地址為0X40,
- write_cmd(column); //column的取值范圍為0~63,共64列
- }
- //***********************
- void set_onoff(unsigned char onoff)//設置顯示開關,onoff取值為0或1
- {
- onoff|=0x3e;//0X3E是關顯示,0X3F是開顯示
- write_cmd(onoff);//所以若onoff為0,則表示關顯示,onoff為1,則表示開顯示
- }
- //********************
- void select_screen(unsigned char screen)
- {
- switch(screen)
- {
- case 0:CS1=0;CS2=0;break; //全屏
- case 1:CS1=0;CS2=1;break; //左半屏
- case 2:CS1=1;CS2=0;break; //右半屏
- default:break;
- }
- }
- //***********************
- void intialize() //12864初始化
- {
- //busy();
- RS=0;RW=0;EN=0;P2=0xff;
- write_cmd(0x30); //基本指令集*
- write_cmd(0x0c); //開顯示關光標
- write_cmd(0x01); //清屏
- }
- //**********************
- void clear()
- {
- unsigned char i,j;
- select_screen(0);//先選屏
- for(i=0;i<8;i++)//控制頁數0——7,共8頁
- {
- set_page(i);//設置頁
- set_column(0); //設置列,每頁都從第1列開始,共64列
- for(j=0;j<64;j++)//控制列數0——63,共64列
- write_dat(0x00);//寫入0,列地址指針會自動加1
- }
- }
- //*********************************
- void writechar(unsigned char screen,unsigned char page,unsigned char column,unsigned char *p)//顯示一個漢字(16*16)共8列4行(1~2屏,1~4行,1~4列)
- {
- unsigned char i;
- select_screen(screen);
- set_page(2*(page-1));
- set_column((column-1)*16);
- for(i=0;i<16;i++)
- write_dat(p[i]);
- set_page(2*(page-1)+1);
- set_column((column-1)*16);
- for(i=0;i<16;i++)
- write_dat(p[i+16]);
- }
- //*****************************
- void writeasc(unsigned char screen,unsigned char page,unsigned char column,unsigned char asc)//顯示一個ASC(8*16)共16列8行(1~2屏,1~4行,1~8列)
- {
- unsigned char i;
- select_screen(screen);
- set_page(2*(page-1));
- set_column(8*(column-1));
- for(i=0;i<8;i++)
- write_dat(asctab[(asc-32)*16+i]);
- set_page(2*(page-1)+1);
- set_column(8*(column-1));
- for(i=0;i<8;i++)
- write_dat(asctab[(asc-32)*16+8+i]);
- }
- //*****************************
- void writestring(unsigned char screen,unsigned char page,unsigned char column,unsigned char *string)
- {
- unsigned char i=0;
- while(screen==1&&string[i]!='\0'&&i<(17-column))
- {
- if(i<8){writeasc(screen,page,column+i,string[i]);i++;}
- else{writeasc(screen+1,page,column+i,string[i]);i++;}
- }
- while(screen==2&&string[i]!='\0'&&i<(17-column))
- {writeasc(screen,page,column+i,string[i]);i++;}
- }
- //*****************************
- void drawbmp(unsigned char page,unsigned char column,unsigned char *pic)
- {
- unsigned char j=0,i=0;
- CS1=0;CS2=1;
- for(j=0;j<8;j++)
- {
- write_cmd(0xb8+page-1+j);
- write_cmd(0x40+column-1);
- for(i=0;i<64;i++)
- write_dat(pic[128*j+i]);
- }
- CS1=1;CS2=0;
- for(j=0;j<8;j++)
- {
- write_cmd(0xb8+page-1+j);
- write_cmd(0x40+column-1);
- for(i=64;i<128;i++)
- write_dat(pic[128*j+i]);
- }
- }
- //***********************
- unsigned char ReadData()
- {
- unsigned char dsp_data;
- P2=0xff;
- RW=1;
- RS=1;
- EN=1;
- delay(5);
- EN=0;
- delay(5);
- EN=1;
- delay(5);
- dsp_data=P2;
- delay(5);
- EN=0;
- return(dsp_data);
- }
- void Pixel(unsigned char X,unsigned char Y)//橫縱坐標
- {
- unsigned char DX = (Y >> 3);
- unsigned char BX = Y - (DX << 3);
- unsigned char TempData = 0;
- if (X > 63) { CS1=1;CS2=0; X -= 64; }
- else { CS1=0;CS2=1; }
- write_cmd(0xb8+DX);
- write_cmd(0x40+X);
- TempData = ReadData();
- TempData |= (1 << BX); //TempData |= (1 << BX);TempData &= ~(1<<BX);TempData ^= (1 << BX);
- write_cmd(0xb8+DX);
- write_cmd(0x40+X);
- write_dat(TempData);
- }
- //*********************
- void drawline(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2)//起點終點橫縱坐標
- {
- char dx,dy;
- char inc_x,inc_y;
- int xerr = 0,yerr = 0; //初始化變量
- unsigned char i,ds;
- dx = x2 - x1; //計算坐標增量
- dy = y2 - y1;
- if(dx > 0)
- inc_x = 1; //設置單步方向
- else
- {
- if(dx == 0)
- {inc_x = 0;} //垂直線
- else
- {inc_x = -1; dx = -dx;}
- }
- if(dy > 0)
- inc_y = 1; //設置單步方向
- else
- {
- if(dy == 0)
- {inc_y = 0;} //水平線
- else
- {inc_y = -1; dy = -dy;}
- }
- if(dx > dy)
- ds = dx; //選取基本增量坐標軸
- else
- ds = dy;
- for(i = 0; i <= ds+1; i++) //畫線輸出
- {
- Pixel(x1, y1); //畫點
- xerr += dx;
- yerr += dy;
- if(xerr > ds)
- {xerr -= ds;x1 += inc_x;}
- if(yerr > ds)
- {yerr -= ds;y1 += inc_y;}
- }
- }
- //***********************
- void drawcircle(unsigned char x0 , unsigned char y0 , unsigned char r)//橫縱坐標,半徑
- {
- char a , b;
- char di;
- if(r > 31 || r == 0)//畫不下
- return;
- a = 0;
- b = r;
- di = 3 - 2 * r;//下一個點
- while(a <= b)
- {
- Pixel( x0 - b , y0 - a);
- Pixel( x0 + b , y0 - a);
- Pixel( x0 - a , y0 + b);
- Pixel( x0 - b , y0 - a);
- Pixel( x0 - a , y0 - b);
- Pixel( x0 + b , y0 + a);
- Pixel( x0 + a , y0 - b);
- Pixel( x0 + a , y0 + b);
- Pixel( x0 - b , y0 + a);
- a ++;
- //***bresenham********/
- if(di < 0)
- di += 4 * a + 6;
- else
- {
- di += 10 + 4 * (a - b);
- }
- Pixel( x0 + a , y0 + b);
- }
- }
- //*************************
- void main()
- {
- intialize();
- clear();
- while(1)
- {
- if(button1==0)
- {
- delay(3000);
- if(button1==0)
- {
- clear();writechar(1,4,4,huan);writechar(2,1,1,ying);
- while(!button1);
- }
- }
- if(button2==0)
- {
- delay(3000);
- if(button2==0)
- {
- clear();writestring(1,1,1,"A Beast of Prey");
- while(!button2);
- }
- }
- if(button3==0)
- {
- delay(3000);
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
12864.rar
(75.6 KB, 下載次數: 44)
2018-4-8 10:48 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|