找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 5492|回復(fù): 7
收起左側(cè)

如何用51單片機實現(xiàn)排序?

[復(fù)制鏈接]
ID:493416 發(fā)表于 2019-3-19 20:31 | 顯示全部樓層 |閱讀模式
要求:通過鍵盤任意輸入8個數(shù)字,實時按照輸入順序依次從左到右在數(shù)碼管上顯示。


下面是我自己寫的代碼
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
uint i,button,temp,num,j=0,n;
uchar code button_table[]={0xee,0xde,0xbe,0x7e,
                                 0xed,0xdd,0xbd,0x7d,
                                 0xeb,0xdb};
uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar address[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
void delay(n)              
{
        for(i=n;i>0;i--)
        ;
}
void delay2()
{for(n=50000;n>0;n--)
        ;
}
/*****************************************************************/
void main()
{
        while(1)
        {
                P3=0xf0;
                if(P3!=0xf0)
                {
                        delay(10000);
            if(P3!=0xf0)
                        {
                                temp=P3;
        P3=0x0f;               
              button=P3|temp;
                  for(i=0;i<10;i++)
                    {
                        if(button==button_table[ i])
                        {        num=i;break;}
                    }
                        P2=address[j];
            P0=table[num];
            delay2();
                  j++;
                if(j==8)
                 {j=0;}
            }
          }
        ;
}
}

但是這個只能實現(xiàn)顯示當前按鍵所對應(yīng)的數(shù)字,圖上是我第三個按鍵按的6,用這種動態(tài)顯示的數(shù)碼管無法顯示8個數(shù)啊,應(yīng)該怎么辦?求大佬解答一下,謝謝~



1.png
回復(fù)

使用道具 舉報

ID:351395 發(fā)表于 2019-3-20 09:29 | 顯示全部樓層
用數(shù)組,然后,全部獨個比較,變換大小位置,幾個循環(huán)后就好了。
回復(fù)

使用道具 舉報

ID:437981 發(fā)表于 2019-3-20 13:50 | 顯示全部樓層
把數(shù)據(jù)存在數(shù)組中,在進行“冒泡排序”,“快速排序”....
回復(fù)

使用道具 舉報

ID:164602 發(fā)表于 2019-3-20 14:26 | 顯示全部樓層
#include<reg51.h>

#define GPIO_DIG P0
#define GPIO_KEY P3

unsigned char code DIG_CODE[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71};

unsigned char KeyValue;

bit KeyState;

unsigned char DisplayData[8];

void Delay10ms();
void KeyDown();
void DigDisplay();

void main(void)
{
        unsigned char i;
        KeyState=0;
        while(1)
        {
                KeyDown();
                 if(KeyState==1)
                {
                        switch (i)
                        {
                                case (0):DisplayData[7]=DIG_CODE[KeyValue];break;
                                case (1):DisplayData[6]=DIG_CODE[KeyValue];break;
                                case (2):DisplayData[5]=DIG_CODE[KeyValue];break;
                                case (3):DisplayData[4]=DIG_CODE[KeyValue];break;
                                case (4):DisplayData[3]=DIG_CODE[KeyValue];break;
                                case (5):DisplayData[2]=DIG_CODE[KeyValue];break;
                                case (6):DisplayData[1]=DIG_CODE[KeyValue];break;
                                case (7):DisplayData[0]=DIG_CODE[KeyValue];break;
                        }                       
                        i++;
                        if (i==8)
                        {
                                i=0;
                        }
                        KeyState=0;
                }
                DigDisplay();
        }                               
}

void DigDisplay()
{
        unsigned char i,j;
        for(i=0;i<8;i++)
        {
                switch(i)
                {
                        case(0):
                                P2=0xfe; break;
                        case(1):
                                P2=0xfd; break;
                        case(2):
                                P2=0xfb; break;
                        case(3):
                                P2=0xf7; break;
                        case(4):
                                P2=0xef; break;
                        case(5):
                                P2=0xdf; break;
                        case(6):
                                P2=0xbf; break;
                        case(7):
                                P2=0x7f; break;
                }
                GPIO_DIG=DisplayData[i];
                j=10;
                while(j--);       
                GPIO_DIG=0x00;
        }
}

void KeyDown(void)
{
        unsigned char a=0;
        GPIO_KEY=0x0f;
        if(GPIO_KEY!=0x0f)
        {
                Delay10ms();
                if(GPIO_KEY!=0x0f)
                {
                        KeyState=1;
                        switch(GPIO_KEY)
                        {
                                case(0X07):        KeyValue=0;break;
                                case(0X0b):        KeyValue=1;break;
                                case(0X0d): KeyValue=2;break;
                                case(0X0e):        KeyValue=3;break;
                        }
                        GPIO_KEY=0Xf0;
                        Delay10ms();
                        switch(GPIO_KEY)
                        {
                                case(0X70):        KeyValue=KeyValue;break;
                                case(0Xb0):        KeyValue=KeyValue+4;break;
                                case(0Xd0): KeyValue=KeyValue+8;break;
                                case(0Xe0):        KeyValue=KeyValue+12;break;
                        }
                        while((a<200)&&(GPIO_KEY!=0xf0))
                        {
                                Delay10ms();
                                a++;
                        }
                        a=0;
                }
        }
}

void Delay10ms(void)
{
    unsigned char a,b,c;
    for(c=1;c>0;c--)
        for(b=38;b>0;b--)
            for(a=130;a>0;a--);
}
回復(fù)

使用道具 舉報

ID:426861 發(fā)表于 2019-3-20 15:34 | 顯示全部樓層
C語言有成型的排序比較方法,你只需要處理好鍵盤接收數(shù)據(jù)和屏幕顯示數(shù)據(jù)就好了
回復(fù)

使用道具 舉報

ID:493416 發(fā)表于 2019-3-20 20:06 | 顯示全部樓層

請問一下
1.第一次進入main函數(shù)的時候switch(i) 是為什么,這里的i應(yīng)該沒有賦值啊。又是怎么從0到8循環(huán)的呢。
2.還有這里j=10;
                while(j--);        
                GPIO_DIG=0x00;
這里是做了什么,每次都給j賦10的話while判定的時候一定為真啊 為什么每次要讓數(shù)碼管熄滅?
回復(fù)

使用道具 舉報

ID:164602 發(fā)表于 2019-3-21 07:58 | 顯示全部樓層
冷瞳啊 發(fā)表于 2019-3-20 20:06
請問一下
1.第一次進入main函數(shù)的時候switch(i) 是為什么,這里的i應(yīng)該沒有賦值啊。又是怎么從0到8循 ...

變量i就是從零到八的循環(huán)
第一次進入的switch就是你按鍵的次數(shù)并給顯示緩存賦值
第二個問題就是消影。
回復(fù)

使用道具 舉報

ID:213173 發(fā)表于 2019-3-21 16:49 | 顯示全部樓層
給你把程序改好了,用冒泡法排序。希望能對你有所幫助。
無標題.jpg

  1. #include <reg51.h>
  2. #define uint unsigned int
  3. #define uchar unsigned char
  4. uchar code  key_table[]={0xee,0xde,0xbe,0x7e,0xed,0xdd,0xbd,0x7d,0xeb,0xdb};//鍵碼
  5. uchar code dula_table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};//段碼
  6. uchar code wela_table[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//位碼
  7. uchar numn[8];//緩存
  8. uchar nums[8];//緩存


  9. void keyscan()        //按鍵掃描
  10. {
  11.         static uchar count=0;//消抖計數(shù)
  12.         static bit sign=0;//按鍵自鎖標志
  13.         uchar i,j,temp;//臨時變量
  14.         P3=0xf0;
  15.         if(P3!=0xf0)
  16.         {
  17.                 if((++count>=20)&&(sign==0))
  18.                 {
  19.                         sign=1;
  20.                         temp=P3;
  21.                         P3=temp|0x0f;               
  22.                         temp=P3;
  23.                         for(i=0;i<10;i++)
  24.                         {
  25.                                 if(temp==key_table[i])
  26.                                 {      
  27.                                         for(j=7;j>0;j--)
  28.                                                 numn[j]=numn[j-1];//按輸入順序移位保存鍵值,超出拋棄
  29.                                         numn[0]=i;                        //保存最后輸入鍵值
  30.                                         for(j=0;j<8;j++)
  31.                                         {
  32.                                                 nums[j]=numn[j];//轉(zhuǎn)移有效數(shù)據(jù)
  33.                                         }
  34.                                         break;
  35.                                 }
  36.                         }
  37.                 }
  38.         }
  39.         else  //鍵抬起
  40.         {               
  41.                 count=0;//計數(shù)清0
  42.                 sign=0;//自鎖清0
  43.         }
  44. }
  45. void Line_up()//冒泡法排序程序
  46. {
  47.         uchar i, j, temp;
  48.         for(i=0; i<8-1; i++)
  49.         {
  50.                 for(j=0;j<8-1-i;j++)
  51.                 {
  52.                         if(nums[j]>nums[j+1])
  53.                         {
  54.                                 temp=nums[j];
  55.                                 nums[j]=nums[j+1];
  56.                                 nums[j+1]=temp;
  57.                         }
  58.                 }
  59.         }
  60. }
  61. void display()//動態(tài)顯示程序
  62. {
  63.         static uchar i=0;
  64.         P0=0x00;//消隱
  65.         P2=wela_table[i];//位碼
  66.         P0=dula_table[nums[i]];//段碼
  67.         if(++i==8)
  68.                 i=0;
  69. }

  70. void main()
  71. {
  72.         while(1)
  73.         {
  74.                 keyscan();//按鍵掃描
  75.                 Line_up();//排序程序
  76.                 display();//顯示程序
  77.         }
  78. }
復(fù)制代碼



回復(fù)

使用道具 舉報

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

本版積分規(guī)則

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

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

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