這是我花了幾天時間做的讓stc89c51和lcd12864(帶字庫)的顯示波形,
附件里面包含了原理圖和單片機源代碼。
做出來的圖像坐標都很好。
源代碼有注釋,這是部分代碼,具體的大家去下載吧;
制作出來的實物圖如下:
IMG_20201126_162559.jpg (2.92 MB, 下載次數: 51)
下載附件
這是圖像
2020-11-26 16:27 上傳
單片機源程序如下:
- #include <reg51.h>
- #include <intrins.h>
- #include <math.h>
- #define uint unsigned int
- #define uchar unsigned char
- #define pi 3.1419526
- uchar f=16;
- uchar u=8;
- sbit RS=P2^6;
- sbit RW=P2^5;
- sbit PSB=P3^2; //lcd串行還是并行選擇端
- sbit RST=P3^4; //lcd的復位端口
- sbit EN=P2^7;
- /*延時*/
- void delay(uint x)
- {
- uint y;
- for(;x>0;x--)
- for(y=110;y>0;y--);
- }
- /*寫入指令*/
- void write_com(uchar com)
- {
- RS=0;
- RW=0;
- EN=0;
- P0=com;
- delay(1);
- EN=1;
- delay(3);
- EN=0;
- }
- /*寫入數據*/
- void write_data(uchar num)
- {
- RS=1;
- RW=0;
- EN=0;
- P0=num;
- delay(1);
- EN=1;
- delay(3);
- EN=0;
- }
- /*讀取數據*/
- uchar Read_data()
- {
- uchar read;
-
- RS=1;
- RW=1;
- EN=0;
- delay(1);
- EN=1;
- delay(2);
- read=P0;
- EN=0;
- delay(5);
- return read;
- }
- /*畫圖清屏*/
- void clear_lcd()
- {
- uchar i,j;
- write_com(0x34); //擴充指令集動作
- for(i=0;i<32;i++)
- {
- write_com(0x80+i);
- write_com(0x80);
- for(j=0;j<32;j++)
- {
- write_data(0x00);
- }
- }
- write_com(0x36); //擴充指令集動作
- write_com(0x30); //基本指令集動作
-
- }
- /***********畫點函數**************/
- void DrawPoint( unsigned char X, unsigned char Y, unsigned char Color )
- {
- unsigned char Row , Tier , Tier_bit ;
- unsigned char ReadOldH , ReadOldL ;
- write_com( 0x34 ) ; //寫入擴充指令命令
- write_com( 0x36 ) ;//顯示圖象
- Tier = X >> 4 ;
- Tier_bit = X & 0x0f ;
- if( Y < 32 )
- {
- Row = Y ;
- }
- else
- {
- Row = Y - 32 ;
- Tier += 8 ;
- }
- write_com( Row + 0x80 ) ;
- write_com( Tier + 0x80 ) ;
- Read_data() ;
- ReadOldH = Read_data() ;
- ReadOldL = Read_data() ;
- write_com( Row + 0x80 ) ;
- write_com( Tier + 0x80 ) ;
- if( Tier_bit < 8 )
- {
- switch( Color)
- {
- case 0 : ReadOldH &=( ~( 0x01 << ( 7 - Tier_bit ))) ; break ;
- case 1 : ReadOldH |= ( 0x01 << ( 7 - Tier_bit )) ; break ;
- case 2 : ReadOldH ^= ( 0x01 << ( 7 - Tier_bit )) ; break ;
- default : break ;
- }
- write_data( ReadOldH ) ;
- write_data( ReadOldL ) ;
- }
- else
- {
- switch(Color)
- {
- case 0 : ReadOldL &= (~( 0x01 << ( 15 - Tier_bit ))) ; break ;
- case 1 : ReadOldL |= ( 0x01 << ( 15 - Tier_bit )) ; break ;
- case 2 : ReadOldL ^= ( 0x01 << ( 15 - Tier_bit )) ; break ;
- default : break ;
- }
- write_data( ReadOldH ) ;
- write_data( ReadOldL ) ;
- }
- write_com( 0x30 ) ;
- }
- /*液晶初始化*/
- void lcd_init()
- {
- PSB=1;
- RST=1;
- write_com(0x30);
- delay(1);
- write_com(0x3e);
- delay(1);
- write_com(0x0c);
- delay(1);
- write_com(0x01);
- delay(1);
- }
- //畫水平直線
- void Draw_xlabel_line(uchar x0,uchar x1,uchar y,uchar color)
- {
- uchar temp;
- if(x0>x1)
- {
- temp=x1;
- x1=x0;
- x0=temp;
- }
- for(;x0<=x1;x0++)
- DrawPoint(x0,y,color);
- }
- //畫垂直直線
- void Draw_row_line(uchar x,uchar y0,uchar y1,uchar color)
- {
- uchar temp;
- if(y0>y1)
- {
- temp=y1;
- y1=y0;
- y0=temp;
- }
- for(;y0<=y1;y0++)
- DrawPoint(x,y0,color);
- }
- void sin_display()
- {
- uchar i,j;
- lcd_init();
- clear_lcd();
- //畫y軸箭頭
- DrawPoint(1,1,1);
- DrawPoint(3,1,1);
- DrawPoint(0,2,1);
- DrawPoint(4,2,1);
- Draw_row_line(2,0,60,1); //畫Y軸直線
- DrawPoint(126,60,1);
- DrawPoint(126,62,1);
- DrawPoint(125,59,1);
- DrawPoint(125,63,1);
- Draw_xlabel_line(2,127,61,1); //畫X軸直線
- for(i=3;i<127;i++)//畫sin函數,
- {
- j=u*sin(pi*i/f)+30;
- DrawPoint(i,j,1);
- }
- }
- void main()
- {
-
- sin_display();
-
- }
復制代碼
都是自己原創的,請大家多多指教。
以上程序51hei下載地址:
完整sin函數.zip
(45.44 KB, 下載次數: 27)
2020-11-26 16:30 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|