找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1577|回复: 0
收起左侧

蓝桥杯单片机第10届省赛源程序

[复制链接]
ID:841799 发表于 2021-5-27 17:27 | 显示全部楼层 |阅读模式
51hei.png 51hei.png 51hei.png

单片机源程序如下:
  1. #include <STC15F2K60S2.H>
  2. #include <intrins.h>
  3. #include "iic.h"
  4. #define uchar unsigned char
  5. #define uint unsigned int
  6. void Delay1ms();
  7. uchar code duanxuan[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0XBF,0XFF,0XC6,0x8e,0xc1};//0,1,2,3,4,5,6,7,8,9,-,空,c,f,u
  8. uchar code duanxuan1[]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x8e,0xc1};  //0,1,2,3,4,5,6,7,8,9,f,u
  9. bit dianya_biao=1,pinlv_biao=0,DI,shuma_biao=0,led_biao=0,dianya_moshi=0;
  10. uint count_ya=0,count_pin=0,count_yashua=0,count_pinshua=0,dat_f,dat_u,dat_u1,flag,DAC_jiemian=0,LED_jiemian=0,zhujiemian=0,shuma_jiemian=0,hao=2;
  11. void led_xianshi();
  12. void dac();
  13. void keyscan4();
  14. void all_chushihua();
  15. void Delay5ms();
  16. void dingshiqi_chushihua();
  17. void hc138(uchar n);
  18. void display(uchar pos,uchar dat);
  19. void shuma_xianshi();
  20. void xian();
  21. void main()
  22. {
  23.         all_chushihua();
  24.   dingshiqi_chushihua();
  25.        
  26.         while(1)
  27.         {
  28.          led_xianshi();
  29.                 keyscan4();
  30.                 xian();
  31.         }
  32. }
  33. void all_chushihua()
  34. {
  35.         P0=0XFF;  //关闭LED灯
  36.         P2=(P2&0X1F)|0X80;  //Y4C=1;
  37.   P2=(P2&0X1F);
  38.         P0=0X00;   //关闭蜂鸣器,继电器
  39.   P2=(P2&0X1F)|0XA0;  //Y5C=1;
  40.   P2=(P2&0X1F);
  41.         P0=0XFF;  //选择所有数码管
  42.         P2=(P2&0X1F)|0XC0;  //Y6C=1;
  43.         P2=(P2&0X1F);
  44.         P0=0XFF;  //关闭所有数码管
  45.         P2=(P2&0X1F)|0XE0;  //Y7C=1;
  46.         P2=(P2&0X1F);
  47. }
  48. void hc138(uchar n)
  49. {
  50.         switch(n)
  51.         {
  52.                 case 4:P2=(P2&0X1F)|0X80;break;
  53.                 case 5:P2=(P2&0X1F)|0XA0;break;
  54.                 case 6:P2=(P2&0X1F)|0XC0;break;
  55.                 case 7:P2=(P2&0X1F)|0XE0;break;
  56.                 case 8:P2=P2&0X1F;break;
  57.         }
  58. }
  59. void display(uchar pos,uchar dat)
  60. {
  61.         P0=0X01<<pos;
  62.         hc138(6);  //位选通道打开
  63.         hc138(8);
  64.         P0=dat;
  65.         hc138(7);  //段选通道打开
  66.         hc138(8);
  67. }
  68. void dingshiqi_chushihua()
  69. {
  70.         TMOD=0X16;  //t1定时器16位定时,t0定时器8位自动重装计数
  71.         TH0=0xff;
  72.         TL0=0XFF;
  73.         TH1=(65536-2000)/256;
  74.         TL1=(65536-2000)%256;
  75.         EA=1;
  76.         ET1=1;
  77.         ET0=1;
  78.         TR1=1;
  79.         TR0=1;
  80. }
  81. void t0dingshiqi() interrupt 1
  82. {
  83.         count_pin++;
  84. }
  85. void t1dingshiqi() interrupt 3
  86. {
  87.         TH1=(65536-2000)/256;
  88.         TL1=(65536-2000)%256;
  89.         count_pinshua++;
  90.         count_yashua++;
  91.         if(count_yashua==250)
  92.         {
  93.                 dianya_biao=1;
  94.                 count_yashua=0;
  95.         }
  96.         if(count_pinshua==500)
  97.         {
  98.                         count_pinshua=0;
  99.                     dat_f=count_pin;
  100.                     count_pin=0;
  101.                                
  102.         }
  103. }
  104. void shuma_xianshi()
  105. {
  106.         if(shuma_biao==0)
  107.         {
  108.                
  109.                                 display(0,duanxuan[13]);
  110.                         Delay1ms();
  111.                         if(dat_f>9999)
  112.                         {
  113.                                 display(3,duanxuan[dat_f/10000]);
  114.                                 Delay1ms();
  115.                         }
  116.                        
  117.                         if(dat_f>999)
  118.                         {
  119.                                 display(4,duanxuan[(dat_f/1000)%10]);
  120.                                 Delay1ms();
  121.                         }
  122.                        
  123.                         if(dat_f>99)
  124.                         {
  125.                                 display(5,duanxuan[(dat_f/100)%10]);
  126.                                 Delay1ms();
  127.                         }
  128.                        
  129.                         if(dat_f>9)
  130.                         {
  131.                                 display(6,duanxuan[(dat_f)/10%10]);
  132.                                 Delay1ms();
  133.                         }
  134.                        
  135.                         display(7,duanxuan[dat_f%10]);
  136.                         Delay1ms();
  137.                         hc138(8);
  138.                                
  139.         }
  140.         else if(shuma_biao==1)
  141.         {
  142.                 P0=0XFF;
  143.                 hc138(6);
  144.                 hc138(8);
  145.                 P0=0XFF;
  146.                 hc138(7);
  147.                 hc138(8);
  148.         }
  149.        
  150.        
  151. }
  152. void led_xianshi()
  153. {
  154.           P0=0XFF;
  155.                 hc138(4);
  156.                 hc138(8);
  157.         flag=dat_u1/1.961;
  158.         if(led_biao==0)
  159.         {
  160.           
  161.                 if(zhujiemian%2==1)
  162.                 {
  163.                         P0=(P0&0XFC)|0X01;
  164.                         hc138(4);
  165.                         hc138(8);
  166.                 }
  167.                 if(zhujiemian%2==0)
  168.                 {
  169.                        
  170.                         P0=(P0&0xfc)|0X02;
  171.                         hc138(4);
  172.                         hc138(8);
  173.                 }
  174.                 if((flag<150)||((flag>=250)&&(flag<350)))
  175.                 {
  176.                         P0=(P0&0XFB)|0X04;
  177.                         hc138(4);
  178.                         hc138(8);
  179.                 }
  180.                 if(((flag>=150)&&(flag<250))||(flag>=350))
  181.                 {
  182.                         P0=(P0&0XFB)|0X00;
  183.                         hc138(4);
  184.                         hc138(8);
  185.                 }
  186.                 if((dat_f>1000)||((dat_f>=5000)&&(dat_f<10000)))
  187.                 {
  188.                         P0=(P0&0XF7)|0x08;
  189.                         hc138(4);
  190.                         hc138(8);
  191.                 }
  192.                 if(((dat_f>=1000)&&(dat_f<5000))||(dat_f>=10000))
  193.                 {
  194.                         P0=(P0&0XF7)|0X00;
  195.                         hc138(4);
  196.                         hc138(8);
  197.                 }
  198.                 if(dianya_moshi==0)
  199.                 {
  200.                         P0=(P0&0XEF)|0X10;
  201.                         hc138(4);
  202.                         hc138(8);
  203.                 }
  204.                 if(dianya_moshi==1)
  205.                 {
  206.                         P0=(P0&0XEF)|0X00;
  207.                         hc138(4);
  208.                         hc138(8);
  209.                 }
  210.                
  211.                
  212.         }
  213.         else
  214.         {
  215.                 P0=0xff;
  216.                 hc138(4);
  217.                 hc138(8);
  218.         }
  219.        
  220. }
  221. void dac()
  222. {
  223.         if(dianya_biao)
  224.         {
  225.                 dianya_biao=0;
  226.                 if(dianya_moshi==0)
  227.                 {
  228.                         dac_out(102.4);   //5v对应255,2v对应102.4
  229.                         //dat_u=IIC_RecByte();//虽然按理这样写但是显示2.55v,故等于个2保险
  230.                         dat_u=200;
  231.                 }
  232.                 else
  233.                 {
  234.                         dat_u1=getad(0x03);
  235.                 }
  236.         }
  237.         if(shuma_biao==0)
  238.         {
  239.                         if(dianya_moshi==0)
  240.                         {
  241.                                 display(0,duanxuan[14]);
  242.                                 Delay1ms();
  243.                                 display(5,duanxuan1[dat_u/100]);
  244.                                 Delay1ms();
  245.                                 display(6,duanxuan[dat_u/10%10]);
  246.                                 Delay1ms();
  247.                                 display(7,duanxuan[dat_u/1%10]);
  248.                                 Delay1ms();
  249.                         }
  250.                         else if(dianya_moshi==1)
  251.                         {
  252.                                
  253.                                 display(0,duanxuan1[11]);
  254.                                 Delay1ms();
  255.                                 display(5,duanxuan1[dat_u1/100]);
  256.                                 Delay1ms();
  257.                                 display(6,duanxuan[dat_u1/10%10]);
  258.                                 Delay1ms();
  259.                                 display(7,duanxuan[dat_u1/1%10]);
  260.                                 Delay1ms();
  261.                         }
  262.         }
  263.         else if(shuma_biao==1)
  264.         {
  265.                 P0=0XFF;
  266.                 hc138(6);
  267.                 hc138(8);
  268.                 P0=0XFF;
  269.                 hc138(7);
  270.                 hc138(8);
  271.         }               
  272.        
  273. }
  274. void xian()
  275. {
  276.         switch(hao)
  277.         {
  278.                 case 0:
  279.                         if(zhujiemian%2==1)
  280.                         {
  281.                                        
  282.                                 shuma_xianshi();
  283.                         }
  284.                         if((zhujiemian%2==0)&&(zhujiemian!=0))
  285.                         {
  286.                                 dianya_moshi=0;
  287.                                 dac();
  288.                         }  break;
  289.                 case 1:
  290.                         if(DAC_jiemian%2==1)
  291.                         {
  292.                                 dianya_moshi=0;
  293.                                 dac();
  294.                         }
  295.                         if((DAC_jiemian%2==0)&&(DAC_jiemian!=0))
  296.                         {
  297.                                 dianya_moshi=1;
  298.                                 dac();
  299.                         }  break;
  300.                 case 2:
  301.                         dac(); break;
  302.         }
  303. }
  304. void keyscan4()
  305. {

  306.         if(P33==0)
  307.         {
  308.                 Delay5ms();
  309.                 if(P33==0)
  310.                 {
  311.                         zhujiemian++;
  312.                         hao=0;
  313.                 }       
  314.                 while(!P33);
  315.                
  316.                
  317.         }

  318.         else if(P32==0)
  319.         {
  320.                 Delay5ms();
  321.                 if(P32==0)
  322.                 {
  323.                         DAC_jiemian++;
  324.                         hao=1;
  325.                 }
  326.                         while(!P32);
  327.                
  328.         }
  329.         else if(P31==0)
  330.         {
  331.                 Delay5ms();
  332.                 if(P31==0)
  333.                 {
  334.                         LED_jiemian++;
  335.                         if(LED_jiemian%2==1)
  336.                         {
  337.                                 led_biao=1;
  338.                         }
  339.                         if((LED_jiemian%2==0)&&(LED_jiemian!=0))
  340.                         {
  341.                                 led_biao=0;
  342.                         }  
  343.                 }
  344.                 while(!P31);               
  345.         }
  346.         else if(P30==0)
  347.         {
  348.                 Delay5ms();
  349.                 if(P30==0)
  350.                 {
  351.                         shuma_jiemian++;
  352.                         if(shuma_jiemian%2==1)
  353.                         {
  354.                                 shuma_biao=1;
  355.                         }
  356.                         if((shuma_jiemian%2==0)&&(shuma_jiemian!=0))
  357.                         {
  358.                                 shuma_biao=0;
  359.                         }
  360.                 }
  361.                 while(!P30);
  362.                
  363.                        
  364.         }
  365. }
  366. void Delay5ms()                //@12.000MHz
  367. {
  368.         unsigned char i, j;

  369.         i = 59;
  370.         j = 90;
  371.         do
  372.         {
  373.                 while (--j);
  374.         } while (--i);
  375. }
  376. void Delay1ms()                //@12.000MHz
  377. {
  378.         unsigned char i, j;

  379.         i = 12;
  380.         j = 169;
  381.         do
  382.         {
  383.                 while (--j);
  384.         } while (--i);
  385. }
复制代码

所有资料51hei提供下载:
蓝桥杯大赛第10届.rar (320.06 KB, 下载次数: 16)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表