找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

帖子
查看: 2015|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

寫了一下表情燈的單片機(jī)代碼各位大神代碼還有優(yōu)化的地方嗎?

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:405030 發(fā)表于 2020-8-11 21:08 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
#include<reg52.h>
sbit a=P0^0;
sbit b=P0^1;
sbit c=P0^2;
sbit OE0=P0^3;
sbit OE1=P0^4;
sbit OE2=P0^5;
sbit KEY1=P3^2;
sbit KEY2=P3^3;
sbit KEY3=P3^4;
sbit KEY4=P3^5;
unsigned char code LedChar[7][8]={
{0xC3,0x81,0x00,0x00,0x00,0x81,0xFF,0xFF},
{0xFF,0x81,0x00,0x00,0x00,0x81,0xFF,0xFF},
{0xFF,0xFF,0x00,0x00,0x00,0xFF,0xFF,0xFF},
{0xFF,0xFF,0xFF,0x00,0xFF,0xFF,0xFF,0xFF},     //不按按鍵時(shí)
{0xEF,0xDF,0xBF,0x00,0xBF,0xDF,0xEF,0xFF},     //左轉(zhuǎn)
{0xF7,0xFB,0xFD,0x00,0xFD,0xFB,0xF7,0xFF},      //右轉(zhuǎn)
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x66,0x99},      //W
};     
unsigned char LedBuff[16]=
{0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
};
void buan();          //不按按鍵函數(shù)
void ting();           //剎車時(shí)函數(shù)
void you();            //左轉(zhuǎn)時(shí)函數(shù)
void zuo();            //右轉(zhuǎn)時(shí)函數(shù)
void main()
{
TMOD=0x01;
TH0=0xFC;
TL0=0x67;
EA=1;
ET0=1;
TR0=1;
while(1)
{
   if(KEY1==0)
  {
   zuo();
  }
  else if(KEY2==0)
  {
   you();
  }
  else
  {
  buan();
  }
}
}
/*中斷函數(shù)*/
void zhongduan() interrupt 1         //用于顯示刷新
{
static unsigned char i=0;
TH0=0xFC;
TL0=0x67;
P1=0xFF;
switch(i)
{
  case 0:a=0;b=0;c=0;OE0=0;OE1=1;OE2=1;i++;P1=LedBuff[0];break;
  case 1:a=0;b=0;c=1;OE0=0;OE1=1;OE2=1;i++;P1=LedBuff[1];break;
  case 2:a=0;b=1;c=0;OE0=0;OE1=1;OE2=1;i++;P1=LedBuff[2];break;
  case 3:a=0;b=1;c=1;OE0=0;OE1=1;OE2=1;i++;P1=LedBuff[3];break;
  case 4:a=1;b=0;c=0;OE0=0;OE1=1;OE2=1;i++;P1=LedBuff[4];break;
  case 5:a=1;b=0;c=1;OE0=0;OE1=1;OE2=1;i++;P1=LedBuff[5];break;
  case 6:a=1;b=1;c=0;OE0=0;OE1=1;OE2=1;i++;P1=LedBuff[6];break;
  case 7:a=1;b=1;c=1;OE0=0;OE1=1;OE2=1;i++;P1=LedBuff[7];break;
  case 8:a=0;b=0;c=0;OE0=1;OE1=0;OE2=1;i++;P1=LedBuff[8];break;
  case 9:a=0;b=0;c=1;OE0=1;OE1=0;OE2=1;i++;P1=LedBuff[9];break;
  case 10:a=0;b=1;c=0;OE0=1;OE1=0;OE2=1;i++;P1=LedBuff[10];break;
  case 11:a=0;b=1;c=1;OE0=1;OE1=0;OE2=1;i++;P1=LedBuff[11];break;
  case 12:a=1;b=0;c=0;OE0=1;OE1=0;OE2=1;i++;P1=LedBuff[12];break;
  case 13:a=1;b=0;c=1;OE0=1;OE1=0;OE2=1;i++;P1=LedBuff[13];break;
  case 14:a=1;b=1;c=0;OE0=1;OE1=0;OE2=1;i++;P1=LedBuff[14];break;
  case 15:a=1;b=1;c=1;OE0=1;OE1=0;OE2=1;i=0;P1=LedBuff[15];break;
  default:break;
}
}
/*左轉(zhuǎn)*/
void zuo()
{
LedBuff[0]=LedChar[4][0];
LedBuff[1]=LedChar[4][1];
LedBuff[2]=LedChar[4][2];
LedBuff[3]=LedChar[4][3];
LedBuff[4]=LedChar[4][4];
LedBuff[5]=LedChar[4][5];
LedBuff[6]=LedChar[4][6];
LedBuff[7]=LedChar[4][7];
LedBuff[8]=LedChar[6][0];
LedBuff[9]=LedChar[6][1];
LedBuff[10]=LedChar[6][2];
LedBuff[11]=LedChar[6][3];
LedBuff[12]=LedChar[6][4];
LedBuff[13]=LedChar[6][5];
LedBuff[14]=LedChar[6][6];
LedBuff[15]=LedChar[6][7];
}
/*右轉(zhuǎn)*/
void you()
{
LedBuff[0]=LedChar[5][0];
LedBuff[1]=LedChar[5][1];
LedBuff[2]=LedChar[5][2];
LedBuff[3]=LedChar[5][3];
LedBuff[4]=LedChar[5][4];
LedBuff[5]=LedChar[5][5];
LedBuff[6]=LedChar[5][6];
LedBuff[7]=LedChar[5][7];
LedBuff[8]=LedChar[6][0];
LedBuff[9]=LedChar[6][1];
LedBuff[10]=LedChar[6][2];
LedBuff[11]=LedChar[6][3];
LedBuff[12]=LedChar[6][4];
LedBuff[13]=LedChar[6][5];
LedBuff[14]=LedChar[6][6];
LedBuff[15]=LedChar[6][7];
}
/*不按按鍵*/
void buan()
{
static unsigned char cnt=0;
static unsigned int jisu=0;
static unsigned char f=0;
LedBuff[0]=LedChar[cnt][0];
LedBuff[1]=LedChar[cnt][1];
LedBuff[2]=LedChar[cnt][2];
LedBuff[3]=LedChar[cnt][3];
LedBuff[4]=LedChar[cnt][4];
LedBuff[5]=LedChar[cnt][5];
LedBuff[6]=LedChar[cnt][6];
LedBuff[7]=LedChar[cnt][7];
LedBuff[8]=LedChar[6][0];
LedBuff[9]=LedChar[6][1];
LedBuff[10]=LedChar[6][2];
LedBuff[11]=LedChar[6][3];
LedBuff[12]=LedChar[6][4];
LedBuff[13]=LedChar[6][5];
LedBuff[14]=LedChar[6][6];
LedBuff[15]=LedChar[6][7];
for(f=0;f>50;f++);
jisu++;
if(jisu>=400)
{
  jisu=0;
  cnt++;
  if(cnt>=4)
  {
   cnt=0;
  }
}
}
芯片用的是74hc138

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

使用道具 舉報(bào)

沙發(fā)
ID:804115 發(fā)表于 2020-8-12 13:45 | 只看該作者
建議用循環(huán)語句,這樣太占ROM,且浪費(fèi)時(shí)間
回復(fù)

使用道具 舉報(bào)

板凳
ID:701852 發(fā)表于 2020-8-12 14:51 | 只看該作者
賦值可以用for循環(huán)
回復(fù)

使用道具 舉報(bào)

地板
ID:74382 發(fā)表于 2020-8-14 21:20 | 只看該作者
源碼呢
回復(fù)

使用道具 舉報(bào)

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

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