制作出来的实物图如下:
这是图像
单片机源程序如下:
#include #include #include #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(); }
上一篇:单片机P1开关控制低四位流水灯
下一篇:超声波测距单片机C程序