找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 1213|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

51單片機(jī)的兩位漢顯程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:393479 發(fā)表于 2018-9-4 13:55 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
#include <reg52.h>
#include<stdio.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int

sbit DS = P0^1;      // 串行數(shù)據(jù)輸入
sbit ST_CP = P0^0;        //數(shù)據(jù)鎖存時(shí)鐘
sbit SH_CP = P0^2;        //數(shù)據(jù)輸入時(shí)鐘
sbit A = P2^0;
sbit B1 = P2^1;
sbit C = P2^2;
sbit D = P2^3;
sbit EN = P2^4;
                                                                                                 
uchar a=0;                                //用于串口中斷中的速度和運(yùn)行方式命令
uchar d=0;                                                 //用于串口中斷中的字?jǐn)?shù)的命令
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;                                        //左右移函數(shù)
uchar m;
uchar buf[192];
uchar speed;                                        //控制速度函數(shù)
uchar e;                     //控制速度函數(shù)
                                                                                       

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)        //開(kāi)始
        {
                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;        //定時(shí)器T1使用工作方式2
        TH1 = 0xFd;           // 設(shè)置初值
        TL1 = 0xFd;
        TR1 = 1;            // 開(kāi)始計(jì)時(shí)
        SCON = 0x50;        //工作方式1,波特率9600bps,允許接收
//   PCON |= 0x80;
        ES = 1;
        EA = 1;                    // 打開(kāi)所有中斷
        TI = 0;
        RI = 0;          //定時(shí)器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;       
                                                //將寄存器復(fù)位等待接收下一個(gè)
                                buf[count] = tmp;                //將接收的數(shù)據(jù)存于變量中
                                count++;  
                        }               
                               
        }
       
       
}


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:1 發(fā)表于 2018-9-4 16:27 | 只看該作者
補(bǔ)全原理圖或者詳細(xì)說(shuō)明一下電路連接即可獲得100+黑幣
回復(fù)

使用道具 舉報(bào)

板凳
ID:393479 發(fā)表于 2019-7-7 12:44 | 只看該作者
admin 發(fā)表于 2018-9-4 16:27
補(bǔ)全原理圖或者詳細(xì)說(shuō)明一下電路連接即可獲得100+黑幣

原理圖已上傳

點(diǎn)陣.pdf

104.28 KB, 下載次數(shù): 2, 下載積分: 黑幣 -5

原理圖

回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表