找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 890|回复: 2
打印 上一主题 下一主题
收起左侧

51单片机的两位汉显程序

[复制链接]
跳转到指定楼层
楼主
ID:393479 发表于 2018-9-4 13:55 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <reg52.h>
#include<stdio.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int

sbit DS = P0^1;      // 串行数据输入
sbit ST_CP = P0^0;        //数据锁存时钟
sbit SH_CP = P0^2;        //数据输入时钟
sbit A = P2^0;
sbit B1 = P2^1;
sbit C = P2^2;
sbit D = P2^3;
sbit EN = P2^4;
                                                                                                 
uchar a=0;                                //用于串口中断中的速度和运行方式命令
uchar d=0;                                                 //用于串口中断中的字数的命令
uchar zimo=0;
uchar zishu=0;
uchar speed=20;
uchar movestyle=1;
uint DisplayMode=0;
uchar s=0;
uint count = 0;
uchar ctrl = 0x00;                                           //用于行选
uchar z;                                        //左右移函数
uchar m;
uchar buf[192];
uchar speed;                                        //控制速度函数
uchar e;                     //控制速度函数
                                                                                       

uchar code tabstart[192]=
{
0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFC,0x07,0xFD,0xFF,
0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0x00,0x01,0xFF,0xFF,/*"上",0*/
/* (16 X 16 , 宋体 )*/

0xFF,0xFF,0x00,0x01,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xBF,0xFD,0xDF,
0xFD,0xEF,0xFD,0xF7,0xFD,0xF7,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,/*"下",1*/
/* (16 X 16 , 宋体 )*/
0xFF,0xFF,0x00,0x01,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xBF,0xFD,0xDF,
0xFD,0xEF,0xFD,0xF7,0xFD,0xF7,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,/*"下",1*/
/* (16 X 16 , 宋体 )*/
0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFC,0x07,0xFD,0xFF,
0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0x00,0x01,0xFF,0xFF,/*"上",0*/
/* (16 X 16 , 宋体 )*/

0xFF,0xFF,0x00,0x01,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xBF,0xFD,0xDF,
0xFD,0xEF,0xFD,0xF7,0xFD,0xF7,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,/*"下",1*/
/* (16 X 16 , 宋体 )*/
0xFF,0xFF,0x00,0x01,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xBF,0xFD,0xDF,
0xFD,0xEF,0xFD,0xF7,0xFD,0xF7,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,0xFD,0xFF,/*"下",1*/
/* (16 X 16 , 宋体 )*/




}
;  
void Control(void)
{
        if(ctrl == 0x11||ctrl ==0xEE)        //停止
        {
       
                DisplayMode = 4;
        }
        if(ctrl == 0x22||ctrl ==0xDD)        //开始
        {
                speed = 20;               
        }
        if(ctrl == 0x33||ctrl ==0xCC)        //加速
        {
               
                if((speed == 60000) || (speed > 20))
                        speed =20;
                speed -= 5;       
                if(speed <= 0)
                        speed = 1;               
                               
        }
        if(ctrl == 0x44||ctrl ==0xBB)        //减速
        {
                if((speed == 60000) || (speed < 10))
                        speed = 20;
                speed += 10;                                                                                               
        }
        if(ctrl == 0x55||ctrl ==0xAA)        //左移
        {
                DisplayMode = 0;                                                                                               
        }
        if(ctrl == 0x56||ctrl ==0xA9)        //右移
        {
                DisplayMode = 1;                                                                                               
        }
        if(ctrl == 0x57||ctrl ==0xA8)        //上移
        {
                DisplayMode = 2;                                                                                               
        }
        if(ctrl == 0x58||ctrl ==0xA7)        //下移
        {
                DisplayMode = 3;                                                                                               
        }

}


void send(uchar dat)
{
uint i;
for(i=0;i<8;i++)
  {
   SH_CP=0;
   if(dat&0x01)
   DS=0;
   else
   DS=1;
   dat=dat>>1;
   SH_CP = 0;
   SH_CP = 1;
  }
}

void display()
{           
uint h;
for(h=0;h<16;h++)
{
send(buf[32+2*h+1]);
send(buf[32+2*h]);
send(buf[2*h+1]);
send(buf[2*h]);


P2 = s;
  s++;
  ST_CP = 0;
ST_CP = 1;
if(s==16)
s = 0;
}
}
void delay()//控制速度
{
for(e=2*speed;e>0;e--)
{
  display();
}
}
void left()


         {                  for(z=0;z<16;z++)
          {
           uchar temp1 = 0,temp2 = 0,temp3 = 0,temp4 = 0,temp5 = 0,temp6 = 0,temp7 = 0,temp8 = 0,temp9 = 0,temp10 = 0,temp11 = 0,temp12 = 0;
           temp12=_crol_(buf[160+2*z+1]&0x80,1);
           temp11=_crol_(buf[160+2*z]&0x80,1) ;
           temp10=_crol_(buf[128+2*z+1]&0x80,1);
           temp9=_crol_(buf[128+2*z]&0x80,1) ;
           temp8=_crol_(buf[96+2*z+1]&0x80,1);
           temp7=_crol_(buf[96+2*z]&0x80,1) ;
           temp6=_crol_(buf[64+2*z+1]&0x80,1);
           temp5=_crol_(buf[64+2*z]&0x80,1);
           temp4=_crol_(buf[32+2*z+1]&0x80,1);
           temp3=_crol_(buf[32+2*z]&0x80,1);                          
           temp2=_crol_(buf[2*z+1]&0x80,1);
           temp1=_crol_(buf[2*z]&0x80,1);          //取出高位并移入最低位

           buf[2*z]=buf[2*z]<<1|temp2;
           buf[2*z+1]=buf[2*z+1]<<1|temp3;
           buf[32+2*z]=buf[32+2*z]<<1|temp4;
           buf[32+2*z+1]=buf[32+2*z+1]<<1|temp5;
           buf[64+2*z]=buf[64+2*z]<<1|temp6;
           buf[64+2*z+1]=buf[64+2*z+1]<<1|temp7;
           buf[96+2*z]=buf[96+2*z]<<1|temp8;
           buf[96+2*z+1]=buf[96+2*z+1]<<1|temp9;
           buf[128+2*z]=buf[128+2*z]<<1|temp10;
           buf[128+2*z+1]=buf[128+2*z+1]<<1|temp11;
           buf[160+2*z]=buf[160+2*z]<<1|temp12;
           buf[160+2*z+1]=buf[160+2*z+1]<<1|temp1;                                    
                                                                                                 //移位并赋值
         }
          delay();
           }
       
  void right()


         {
           for(z=0;z<16;z++)
          {
           uchar temp1 = 0,temp2 = 0,temp3 = 0,temp4 = 0,temp5 = 0,temp6 = 0,temp7 = 0,temp8 = 0,temp9 = 0,temp10 = 0,temp11 = 0,temp12 = 0;
           temp12=_cror_(buf[160+2*z+1]&0x01,1);
           temp11=_cror_(buf[160+2*z]&0x01,1);
           temp10=_cror_(buf[128+2*z+1]&0x01,1);
           temp9=_cror_(buf[128+2*z]&0x01,1);
           temp8=_cror_(buf[96+2*z+1]&0x01,1);
           temp7=_cror_(buf[96+2*z]&0x01,1);
           temp6=_cror_(buf[64+2*z+1]&0x01,1);
           temp5=_cror_(buf[64+2*z]&0x01,1);
           temp4=_cror_(buf[32+2*z+1]&0x01,1);
           temp3=_cror_(buf[32+2*z]&0x01,1);
           temp2=_cror_(buf[2*z+1]&0x01,1);
           temp1=_cror_(buf[2*z]&0x01,1);  //取出最低位,并移到最高位

           buf[2*z]=buf[2*z]>>1|temp12;
           buf[2*z+1]=buf[2*z+1]>>1|temp1;
           buf[32+2*z]=buf[32+2*z]>>1|temp2;
           buf[32+2*z+1]=buf[32+2*z+1]>>1|temp3;
           buf[64+2*z]=buf[64+2*z]>>1|temp4;
           buf[64+2*z+1]=buf[64+2*z+1]>>1|temp5;
           buf[96+2*z]=buf[96+2*z]>>1|temp6;
           buf[96+2*z+1]=buf[96+2*z+1]>>1|temp7;
           buf[128+2*z]=buf[128+2*z]>>1|temp8;
           buf[128+2*z+1]=buf[128+2*z+1]>>1|temp9;
           buf[160+2*z]=buf[160+2*z]>>1|temp10;
           buf[160+2*z+1]=buf[160+2*z+1]>>1|temp11;                //移位并赋值
          }
          delay();                       
                        
   }

void up()
{
delay();
  s--;
}
void down()
{
delay();
  s++;
}
void sysinit(void)
{
uchar k;
for(k=0;k<192;k++)
{
  buf[k] = tabstart[k];
}
TMOD = 0x20;        //定时器T1使用工作方式2
        TH1 = 0xFd;           // 设置初值
        TL1 = 0xFd;
        TR1 = 1;            // 开始计时
        SCON = 0x50;        //工作方式1,波特率9600bps,允许接收
//   PCON |= 0x80;
        ES = 1;
        EA = 1;                    // 打开所有中断
        TI = 0;
        RI = 0;          //定时器1工作
}
void main()
{ sysinit();

while(1)
{
switch(DisplayMode)
{
case 0:left();
       break;
case 1:right();
       break;
case 2:up();
       break;
case 3:down();
       break;
case 4:display();
       break;


}
}
}

void com_isr(void) interrupt 4                      //串口中断
{
        uchar tmp;       
   if (RI==1)
        {
                   RI=0;
                        if(count>=192)
                        {
                                 ctrl= SBUF;
//                                 SBUF=ctrl;
                                count = 0;       
                                Control();       
                        }
                        else
                        {
                                tmp = SBUF;       
                                                //将寄存器复位等待接收下一个
                                buf[count] = tmp;                //将接收的数据存于变量中
                                count++;  
                        }               
                               
        }
       
       
}


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:1 发表于 2018-9-4 16:27 | 只看该作者
补全原理图或者详细说明一下电路连接即可获得100+黑币
回复

使用道具 举报

板凳
ID:393479 发表于 2019-7-7 12:44 | 只看该作者
admin 发表于 2018-9-4 16:27
补全原理图或者详细说明一下电路连接即可获得100+黑币

原理图已上传

点阵.pdf

104.28 KB, 下载次数: 2, 下载积分: 黑币 -5

原理图

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表