标题: 选择法数组排序实验 [打印本页]

作者: liuda    时间: 2015-1-21 23:08
标题: 选择法数组排序实验
是用51hei单片机开发板 电路图详见:http://www.51hei.com/f/51hei-5.pdf  的数码管部分, 用2个74hc573 锁存,p0口作为数据口 ,p3.6和p3.7分别是段和位的锁存端口.P2口上面是按键

  1. #include<reg52.h>
  2. #define uchar unsigned char
  3. #define uint unsigned int
  4. uchar code SEG7[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
  5. sbit D=P3^6;
  6. sbit V=P3^7;
  7. sbit KEY_S1=P2^4;
  8. uchar ACT[5]={0xfe,0xfd,0xfb,0xf7,0xef};
  9. uint data a[10]={23,58,123,54,63,589,888,19,333,220};
  10. //===============
  11.   void delay(unsigned int k)
  12. {
  13. unsigned int i,j;
  14. for(i=0;i<k;i++){
  15. for(j=0;j<121;j++)
  16. {;}}
  17. }
  18. //====================
  19. void sort(uint array[],uint n)
  20. {
  21. uint i,j,k,t;
  22. for(i=0;i<n-1;i++)
  23. {
  24. k=i;
  25. for(j=i+1;j<n;j++)
  26. {if(array[j]<array[k])k=j;}
  27. t=array[k];
  28. array[k]=array[i];
  29. array[i]=t;
  30.     }
  31. }
  32. //============
  33. void dis(uint array[],uint n)
  34. {
  35. uint m,t;
  36. for(m=0;m<n;m++)
  37. {
  38. for(t=0;t<300;t++)
  39. {
  40. D=1;P0=SEG7[array[m]%10];D=0;P0=0xff;V=1;P0=ACT[4];V=0;delay(5);
  41. D=1;P0=SEG7[(array[m]/10)%10];D=0;P0=0xff;V=1;P0=ACT[3];V=0;delay(5);
  42. D=1;P0=SEG7[array[m]/100];D=0;P0=0xff;V=1;P0=ACT[2];V=0;delay(5);
  43. D=1;P0=SEG7[m];D=0;P0=0xff;V=1;P0=ACT[0];V=0;delay(5);
  44.      }
  45.   }
  46. }
  47. //==================
  48. void main(void)
  49. {
  50. while(1)
  51. {
  52. dis(a,10);
  53. while(KEY_S1);
  54. sort(a,10);
  55. dis(a,10);
  56. while(1);
  57.    }
  58. }
复制代码







欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1