標(biāo)題: 寫了一下表情燈的單片機(jī)代碼各位大神代碼還有優(yōu)化的地方嗎? [打印本頁]

作者: yu1994    時(shí)間: 2020-8-11 21:08
標(biāo)題: 寫了一下表情燈的單片機(jī)代碼各位大神代碼還有優(yōu)化的地方嗎?
#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


作者: 1692483014    時(shí)間: 2020-8-12 13:45
建議用循環(huán)語句,這樣太占ROM,且浪費(fèi)時(shí)間
作者: 13487086265    時(shí)間: 2020-8-12 14:51
賦值可以用for循環(huán)
作者: qiuge    時(shí)間: 2020-8-14 21:20
源碼呢




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1