专注电子技术学习与研究
当前位置:单片机教程网 >> MCU设计实例 >> 浏览文章

51单片机做的万年历之游戏机

作者:佚名   来源:本站原创   点击数:  更新时间:2014年04月26日   【字体:

寒假在家没什么事情就在家里做了这个DIY,传上来供广大爱好者参观学习,资料里有源码等等,这里就不详细的介绍了,这里面有5个游戏,视频链接http://v.youku.com/v_show/id_XNjcwMTM5Njg4.html 

 

每个模块的源程序+综合程序+电路图

视频:

 

感谢那些开源程序的人们!!!








全部源代码下载: http://www.51hei.com/f/IAP15F2K61S2.zip
项目电路图下载: http://www.51hei.com/bbs/dpj-25260-1.html


下面是12864测试程序:

#include "STC15F2K60S2.h"

#include "intrins.h"

#include "ico.h"

#include<rtx51tny.h>

 

sbit LCD_RS=P4^4;

sbit LCD_WR=P4^2;

sbit LCD_E =P4^1;

 

void LCD_Busy();//检测芯片忙不忙

void Write_cmd(unsigned char cmd);//写指令到lcd

void Write_date(unsigned char date);//写数据到lcd

unsigned char Read_Date();//读数据函数

void LCD_Init(void);//lcd初始化

void LCD_Inverse(unsigned char x);//反白显示选择

void LCD_HangInverse(unsigned char x,unsigned char e);//哪一行进行反白

void LCD_Pos(unsigned char x,unsigned char y);

void LCD_String(unsigned char *s);//发送字符串

void LCD_Hanzi(unsigned char *hanzi);//整体显示汉字

void LCD_Picture(unsigned char *picture);//刷图片

void LCD_ClearGDRAM();//清空GDRAM

unsigned long int LCD_Pow(unsigned char m,unsigned char n);

void LCD_Shownum(unsigned long int num,unsigned char len);

void Delay_ms(unsigned char ms); //@11.0592MHz

void Delay_us(); //@11.0592MHz

 

void LCD_Point(unsigned char x,unsigned y);//画点函数

void LCD_DrawLine(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2);

void LCD_DrawRectangle(unsigned char x1,unsigned char y1,unsigned char x2,unsigned char y2);

void Draw_Circle(unsigned char x0,unsigned char y0,unsigned char r);

void LCD_RandomPicture(unsigned char x,unsigned char y,unsigned char size,unsigned char *pic);//任意位置显示任意大小和图片

 

unsigned char code Lcd_Con_X_Y[4][2]={{0x80,0x80},{0x80,0x90},{0x88,0x80},{0x88,0x90}};        //LCD行反白显示坐标

 

void main()

{

// unsigned char iii=0;

// LCD_Init();

 

 

// LCD_Picture(pic);//刷图片

// LCD_ClearGDRAM();

// LCD_HangInverse(2,1);//哪一行进行反白

// LCD_Hanzi(hanzi);

// LCD_Point(1,2);

// LCD_DrawLine(0,0,50,60);

// LCD_DrawRectangle(0,0,60,60);

// Draw_Circle(30,30,20);

LCD_Pos(1,1);

LCD_Shownum(111,3);

LCD_RandomPicture(0,63,24,Game);

LCD_RandomPicture(50,63,24,Rtc);

LCD_RandomPicture(100,63,24,Music);

LCD_RandomPicture(0,30,24,Picture);

LCD_RandomPicture(50,30,24,System);

LCD_RandomPicture(100,30,24,Contact);

while(1);

}

 

void LCD_Busy()//检测芯片忙不忙

{

bit BF=0;

LCD_E =0;

LCD_RS=0;

LCD_WR=1;

P2=0xff;

do

{

LCD_E =1;

BF=P2&0x80;

LCD_E =0;

}while(BF);

}

void Write_cmd(unsigned char cmd)//写指令到lcd

{

LCD_Busy();  

LCD_E =0;

LCD_RS=0;

LCD_WR=0;

P2    =cmd;//Delay_ms(1); //@11.0592MHz

LCD_E =1;//Delay_ms(1); //@11.0592MHz

LCD_E =0;

}

 

void Write_date(unsigned char date)//写数据到lcd

{

LCD_Busy();

LCD_E =0;

LCD_RS=1;

LCD_WR=0;

P2    =date;//Delay_ms(1); //@11.0592MHz

LCD_E =1;//Delay_ms(1); //@11.0592MHz

LCD_E =0;

}

 

unsigned char Read_Date()//读数据函数

{

unsigned char date=0;

LCD_Busy();

P2=0xff;

LCD_E =0;

LCD_RS=1;

LCD_WR=1;

LCD_E =1;

date  =P2;

LCD_E =0;

return date;

}

 

void LCD_Init(void)

{

LCD_E =0;

LCD_RS=0;

LCD_WR=0;

Write_cmd(0x30);//8-BIT 控制接口 基本指令集

Delay_ms(5);

Write_cmd(0x02);//地址归位

Delay_ms(5);

Write_cmd(0x0c);//整体显示 光标关 反白关

Write_cmd(0x01);//清除显示

Delay_ms(5);

Delay_ms(5);

 

void LCD_Inverse(unsigned char x)//反白显示选择

{

Write_cmd(0x34);//8-BIT 控制接口 扩充指令集

Write_cmd(0x04+x-1);//选择哪一行进行反白显示

Write_cmd(0x30);//8-BIT 控制接口 基本指令集

}

 

void LCD_Pos(unsigned char x,unsigned char y)//设定坐标函数

{

switch (x)

{

case 1:x=0x80;

break;

case 2:x=0x90;

break;

case 3:x=0x88;

break;

case 4:x=0x98;

break;

default:

break;

}

Write_cmd(x+y);

}

 

void LCD_String(unsigned char *s)//发送字符串

{

while(*s)

{

Write_date(*s++);

}

}

 

void LCD_Hanzi(unsigned char *hanzi)//整体显示汉字

{

unsigned char i=0;

LCD_Pos(1,0);

while(*hanzi)

{

Write_date(*hanzi++);

i++;

switch (i)

{

case 16:LCD_Pos(2,0);

break;

case 32:LCD_Pos(3,0);

break;

case 48:LCD_Pos(4,0);

break;

default:

break;

}

}

}

 

//x行数

//e使能反白

void LCD_HangInverse(unsigned char x,unsigned char e)//哪一行进行反白

{

unsigned char i=0,j=0;

Write_cmd(0x34);//扩充指令 绘图显示关

Delay_ms(1);

for(j=0;j<16;j++)//一大行包含16个小行

{

Write_cmd(Lcd_Con_X_Y[x-1][1]+j);//y

Write_cmd(Lcd_Con_X_Y[x-1][0]);//x

for(i=0;i<8;i++)//没小行写8次数据,每次写两个字节

{

if(e)

{

Write_date(0xff);//D15-D8

Write_date(0xff);//D7-D0

}

else

{

Write_date(0);//D15-D8

Write_date(0);//D7-D0

}

}

}

Write_cmd(0x36);//扩充指令 绘图显示开

Delay_ms(1);

Write_cmd(0x30);//基本指令

}

 

void LCD_Picture(unsigned char *picture)//刷图片

{

unsigned char i=0,j=0;

Write_cmd(0x34);//扩充指令 绘图显示关

for(j=0;j<32;j++)//上半屏

{

Write_cmd(0x80+j);//y

Write_cmd(0x80);//x

for(i=0;i<16;i++)

{

Write_date(*picture++);

}

}

for(j=0;j<32;j++)//下半屏

{

Write_cmd(0x80+j);//y

Write_cmd(0x88);//x

for(i=0;i<16;i++)

{

Write_date(*picture++);

}

}

Write_cmd(0x36);//扩充指令 绘图显示开

Delay_ms(1);

Write_cmd(0x30);//基本指令

}

 

void LCD_ClearGDRAM()//清空GDRAM

{

unsigned char i=0,j=0;

Write_cmd(0x34);//扩充指令 绘图显示关

for(j=0;j<32;j++)//上半屏

{

Write_cmd(0x80+j);//y

Write_cmd(0x80);//x

for(i=0;i<16;i++)

{

Write_date(0x00);

}

}

for(j=0;j<32;j++)//下半屏

{

Write_cmd(0x80+j);//y

Write_cmd(0x88);//x

for(i=0;i<16;i++)

{

Write_date(0x00);

}

}

Write_cmd(0x36);//扩充指令 绘图显示开

Delay_ms(1);

Write_cmd(0x30);//基本指令

}

//m^n函数

//返回值m^n

unsigned long int LCD_Pow(unsigned char m,unsigned char n)

{

unsigned long int result=1;  

while(n--)result*=m;    

return result;

}

 

//发送数字函数  

void LCD_Shownum(unsigned long int num,unsigned char len)

{        

unsigned char t,temp;

unsigned char enshow=0;   

for(t=0;t<len;t++)

{

temp=(num/LCD_Pow(10,len-t-1))%10;

if(enshow==0&&t<(len-1))

{

if(temp==0)

{

continue;

}else enshow=1; 

 

}

Write_date(temp+48);

}

}

 

void Delay_ms(unsigned char ms) //@11.0592MHz

{

unsigned char i, j;

for(;ms>0;ms--)

{

_nop_();

_nop_();

_nop_();

i = 11;   

j = 190;

do

{

while (--j);

} while (--i);

}

}

 

void Delay_us() //@11.0592MHz

{

_nop_();

_nop_();

_nop_();

}

 

void LCD_Point(unsigned char x,unsigned y)//画点函数

{

unsigned char x_place,y_place,date1,date2;

y_place=y/16;

x_place=x/16;

switch (y_place)

{

case 0:y_place=0x90+15-y%16;x_place=0x88+x/16;

break;

case 1:y_place=0x80+15-y%16;x_place=0x88+x/16;

break;

case 2:y_place=0x90+15-y%16;x_place=0x80+x/16;

break;

case 3:y_place=0x80+15-y%16;x_place=0x80+x/16;

default:

break;

}

 

Write_cmd(0x34);//扩充指令 绘图显示开

Write_cmd(y_place);//扩充指令 绘图显示开

Write_cmd(x_place);//扩充指令 绘图显示开

Read_Date();//假读取数据一次

date1=Read_Date();//高   0x0f,0xf0

date2=Read_Date();//低

 

Write_cmd(y_place);//扩充指令 绘图显示开

Write_cmd(x_place);//扩充指令 绘图显示开

if(x%16<8)

{

Write_date(date1 | (0x01 << (7 - x%16)) );

Write_date(date2);

}

else

{

Write_date(date1);

Write_date(date2|(0x01 << (15 - x%16)));

}

 

Write_cmd(0x36);//扩充指令 绘图显示开

Write_cmd(0x30);//基本指令集

}

 

//画线

//x1,y1:起点坐标

//x2,y2:终点坐标  

void LCD_DrawLine(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2)

{

unsigned char t; 

char xerr=0,yerr=0,delta_x,delta_y,distance; 

char incx,incy,uRow,uCol; 

delta_x=x2-x1; //计算坐标增量 

delta_y=y2-y1; 

uRow=x1; 

uCol=y1; 

if(delta_x>0)incx=1; //设置单步方向 

else if(delta_x==0)incx=0;//垂直线 

else {incx=-1;delta_x=-delta_x;} 

if(delta_y>0)incy=1; 

else if(delta_y==0)incy=0;//水平线 

else{incy=-1;delta_y=-delta_y;} 

if( delta_x>delta_y)distance=delta_x; //选取基本增量坐标轴 

else distance=delta_y; 

for(t=0;t<=distance+1;t++ )//画线输出 

{  

LCD_Point(uRow,uCol);//画点 

xerr+=delta_x ; 

yerr+=delta_y ; 

if(xerr>distance) 

xerr-=distance; 

uRow+=incx; 

if(yerr>distance) 

yerr-=distance; 

uCol+=incy; 

}  

}    

//画矩形  

//(x1,y1),(x2,y2):矩形的对角坐标

void LCD_DrawRectangle(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2)

{

LCD_DrawLine(x1,y1,x2,y1);

LCD_DrawLine(x1,y1,x1,y2);

LCD_DrawLine(x1,y2,x2,y2);

LCD_DrawLine(x2,y1,x2,y2);

}

//在指定位置画一个指定大小的圆

//(x,y):中心点

//r    :半径

void Draw_Circle(unsigned char x0,unsigned char y0,unsigned char r)

{

char a,b;

char di;

a=0;b=r;  

di=3-(r<<1);             //判断下个点位置的标志

while(a<=b)

{

LCD_Point(x0+a,y0-b);             //5

  LCD_Point(x0+b,y0-a);             //0           

LCD_Point(x0+b,y0+a);             //4               

LCD_Point(x0+a,y0+b);             //6 

LCD_Point(x0-a,y0+b);             //1       

  LCD_Point(x0-b,y0+a);             

LCD_Point(x0-a,y0-b);             //2             

  LCD_Point(x0-b,y0-a);             //7             

a++;

//使用Bresenham算法画圆     

if(di<0)di +=4*a+6;  

else

{

di+=10+4*(a-b);   

b--;

}    

}

}

 

void LCD_RandomPicture(unsigned char x,unsigned char y,unsigned char size,unsigned char *pic)//任意位置显示任意大小和图片

{

unsigned char x_i=x;

unsigned char i,j;

unsigned char value;

Write_cmd(0x34);//扩充指令 绘图显示关

for(i=0;i<size;i++)

{

for(j=0;j<size;j++)

{

if(j%8==0)

{

value=*(pic+i*size/8+j/8);

}

if(value&0x80)

LCD_Point(x,y);//画点函数

x++;

value<<=1;

}

y--;

x=x_i;

}

Write_cmd(0x36);//扩充指令 绘图显示开

Write_cmd(0x30);//基本指令集

}

       

 

关闭窗口

相关文章