找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 4146|回复: 2
收起左侧

花样流水灯及AD转换程序源码

[复制链接]
ID:317033 发表于 2018-4-26 16:38 | 显示全部楼层 |阅读模式
  1. #include<reg51.h>
  2. #include<intrins.h>

  3. #define uint unsigned int
  4. #define uchar unsigned char       

  5. sbit P0_0=P0^0;
  6. sbit P0_1=P0^1;
  7. sbit P0_2=P0^2;
  8. sbit P0_3=P0^3;
  9. sbit P0_4=P0^4;
  10. sbit P0_5=P0^5;
  11. sbit P0_6=P0^6;
  12. sbit P0_7=P0^7;

  13. sbit P1_0=P1^0;
  14. sbit P1_1=P1^1;
  15. sbit P1_2=P1^2;
  16. sbit P1_3=P1^3;
  17. sbit P1_4=P1^4;
  18. sbit P1_5=P1^5;
  19. sbit P1_6=P1^6;
  20. sbit P1_7=P1^7;

  21. sbit P3_0=P3^0;
  22. sbit P3_1=P3^1;
  23. sbit P3_3=P3^3;
  24. sbit P3_4=P3^4;
  25. sbit P3_5=P3^5;
  26. sbit P3_6=P3^6;
  27. sbit P3_7=P3^7;

  28. sbit P2_0=P2^0;
  29. sbit P2_1=P2^1;
  30. sbit P2_2=P2^2;
  31. sbit P2_3=P2^3;
  32. sbit P2_4=P2^4;
  33. sbit P2_5=P2^5;
  34. sbit P2_6=P2^6;
  35. sbit P2_7=P2^7;

  36. uint pwm=99;                        //调节占空比变量
  37. uint su=0;
  38. uint vaule=0;
  39. char moshi=0;
  40. char lm=0;
  41. uint sudu=60;
  42. sbit hwx=P3^2;                //红外接收数据引脚,
  43. unsigned char ly_lhj[4];                                        //保存NEC的四个字节数据
  44. bit ly_flag=0;                                                                //成功接收标志
  45. void delay1(void){               
  46.         unsigned char i=10;
  47.         while(i--);
  48. }

  49. void delay(unsigned int i)//延时函数
  50. {
  51.         unsigned int a,b;
  52.         for(a=sudu;a>0;a--)
  53.                 for(b=i;b>0;b--);
  54. }

  55. void dis1()                        //花样一函数
  56. {
  57.         uchar j;
  58.         int time=70;
  59.         P2=0XFE;
  60.         delay(time);
  61.         for(j=0;j<7;j++)
  62.         {
  63.                 P2=(P2<<1);
  64.                 delay(time);
  65.                 if(ly_flag==1){ly_flag=0;goto hm;}
  66.         }
  67.        
  68.         P3_0=0;
  69.         delay(time);
  70.         P3_1=0;
  71.         delay(time);
  72.         P3_3=0;
  73.         delay(time);
  74.         if(ly_flag==1){ly_flag=0;goto hm;}
  75.         P3_4=0;
  76.         delay(time);
  77.         P3_5=0;
  78.         delay(time);
  79.         P3_6=0;
  80.         delay(time);
  81.         if(ly_flag==1){ly_flag=0;goto hm;}
  82.         P3_7=0;
  83.         delay(time);

  84.         P0=0XFE;
  85.         delay(time);
  86.         for(j=0;j<7;j++)
  87.         {
  88.                 P0=(P0<<1);
  89.                 delay(time);
  90.                 if(ly_flag==1){ly_flag=0;goto hm;}
  91.         }
  92.        
  93.         P1=0XFE;
  94.         delay(time);
  95.         for(j=0;j<7;j++)
  96.         {
  97.                 P1=(P1<<1);
  98.                 delay(time);
  99.                 if(ly_flag==1){ly_flag=0;goto hm;}
  100.         }
  101.         delay(1000);
  102.         if(ly_flag==1){ly_flag=0;goto hm;}
  103.         delay(1000);
  104.         if(ly_flag==1){ly_flag=0;goto hm;}
  105.         delay(1000);
  106.         if(ly_flag==1){ly_flag=0;goto hm;}
  107.         delay(1000);
  108. hm:;
  109. }

  110. void dis2()
  111. {
  112.         char j,aa;
  113.         int time=100;
  114.         aa=0x80;
  115.         for(j=0;j<7;j++)
  116.         {
  117.                 P1=aa;  
  118.                 delay(time);
  119.                 aa=_cror_(aa,1);
  120.                 aa=aa | 0x80;       
  121.                 if(ly_flag==1){ly_flag=0;goto hm;}
  122.         }
  123.         P1=0XFF;

  124.         aa=0x80;
  125.         for(j=0;j<7;j++)
  126.         {
  127.                 P0=aa;  
  128.                 delay(time);
  129.                 aa=_cror_(aa,1);
  130.                 aa=aa | 0x80;
  131.                 if(ly_flag==1){ly_flag=0;goto hm;}       
  132.         }
  133.         P0=0XFF;       

  134.         P3_7=1;
  135.         delay(time);
  136.         P3_6=1;
  137.         delay(time);
  138.         P3_5=1;
  139.         delay(time);
  140.         P3_4=1;
  141.         delay(300);
  142.         if(ly_flag==1){ly_flag=0;goto hm;}
  143.         P3_3=1;
  144.         delay(time);
  145.         P3_1=1;
  146.         delay(time);
  147.         P3_0=1;
  148.         delay(time);
  149.         if(ly_flag==1){ly_flag=0;goto hm;}
  150.        
  151.         aa=0x80;
  152.         for(j=0;j<7;j++)
  153.         {
  154.                 P2=aa;  
  155.                 delay(time);
  156.                 aa=_cror_(aa,1);
  157.                 aa=aa | 0x80;
  158.                 if(ly_flag==1){ly_flag=0;goto hm;}       
  159.         }
  160.         P2=0XFF;
  161. hm:;               
  162. }

  163. void dis3()
  164. {
  165.         uchar j;
  166.         P2=0XFE;
  167.         delay(100);
  168.         for(j=0;j<7;j++)
  169.         {
  170.                 P2=(P2<<1)|0X01;
  171.                 delay(100);
  172.                 if(ly_flag==1){ly_flag=0;goto hm;}
  173.         }
  174.         P2=0XFF;

  175.         P3_0=0;
  176.         delay(100);
  177.         P3_0=1;
  178.         P3_1=0;
  179.         delay(100);
  180.         P3_1=1;
  181.         P3_3=0;
  182.         delay(100);
  183.         P3_3=1;
  184.         P3_4=0;
  185.         delay(100);
  186.         if(ly_flag==1){ly_flag=0;goto hm;}
  187.         P3_4=1;
  188.         P3_5=0;
  189.         delay(100);
  190.         P3_5=1;
  191.         P3_6=0;
  192.         delay(100);
  193.         P3_6=1;
  194.         P3_7=0;
  195.         delay(100);
  196.         P3_7=1;
  197.         if(ly_flag==1){ly_flag=0;goto hm;}

  198.         P0=0XFE;
  199.         delay(100);
  200.         for(j=0;j<7;j++)
  201.         {
  202.                 P0=(P0<<1)|0X01;
  203.                 delay(100);
  204.                 if(ly_flag==1){ly_flag=0;goto hm;}
  205.         }
  206.         P0=0XFF;
  207.        
  208.         P1=0XFE;
  209.         delay(100);
  210.         for(j=0;j<7;j++)
  211.         {
  212.                 P1=(P1<<1)|0X01;
  213.                 delay(100);
  214.                 if(ly_flag==1){ly_flag=0;goto hm;}
  215.         }
  216.         P1=0XFF;
  217. hm:;       
  218. }

  219. void dis4()
  220. {

  221.         char j;
  222.         P2=0X00;
  223.         delay(300);       

  224.         P3_0=0;
  225.         P3_1=0;
  226.         P3_3=0;
  227.         P3_4=0;
  228.         P3_5=0;
  229.         P3_6=0;
  230.         P3_7=0;
  231.         delay(300);       
  232.         if(ly_flag==1){ly_flag=0;goto hm;}

  233.         P0=0X00;
  234.         delay(300);       

  235.         P1=0X00;
  236.         delay(300);        //////

  237.         for(j=0;j<6;j++)
  238.         {
  239.                 P0=0XFE;
  240.                 P1=0XFF;
  241.        
  242.                 P2=0X00;
  243.                 P3_0=0;
  244.                 P3_1=0;
  245.                 P3_3=0;
  246.                 P3_4=0;
  247.                 P3_5=0;
  248.                 P3_6=0;
  249.                 P3_7=0;
  250.                 delay(300);       
  251.                 if(ly_flag==1){ly_flag=0;goto hm;}
  252.                 P0=0X01;
  253.                 P1=0X00;
  254.        
  255.                 P2=0XFF;
  256.                 P3_0=1;
  257.                 P3_1=1;
  258.                 P3_3=1;
  259.                 P3_4=1;
  260.                 P3_5=1;
  261.                 P3_6=1;
  262.                 P3_7=1;
  263.                 delay(300);               
  264.         }
  265. hm:;
  266. }

  267. void dis5()
  268. {
  269.         char i;
  270.         int time=200;
  271.         P0=0XFF;
  272.         P3=0XFF;
  273.         for(i=0;i<12;i++)
  274.         {
  275.                 P2=0XFD;
  276.                 P1=0X7F;
  277.                 delay(time);       
  278.                 P2=0XFB;
  279.                 P1=0XBF;  
  280.                 delay(time);
  281.                 P2=0XF7;
  282.                 P1=0XDF;  
  283.                 delay(time);
  284.                 if(ly_flag==1){ly_flag=0;goto hm;}
  285.                 P2=0XEF;
  286.                 P1=0XEF;  
  287.                 delay(time);
  288.                 P2=0XDF;
  289.                 P1=0XF7;  
  290.                 delay(time);
  291.                 P2=0XBF;
  292.                 P1=0XFB;  
  293.                 delay(time);
  294.                 if(ly_flag==1){ly_flag=0;goto hm;}
  295.                 P2=0X7F;
  296.                 P1=0XFD;  
  297.                 delay(time);

  298.                 P2=0XFF;
  299.                 P3=0XFE;
  300.                 P1=0XFE;  
  301.                 delay(time);//
  302.                 P1=0XFF;

  303.                 P3=0XFD;
  304.                 P0=0X7F;
  305.                 delay(time);
  306.                 if(ly_flag==1){ly_flag=0;goto hm;}
  307.                 P3=0XF7;  
  308.                 P0=0XBF;
  309.                 delay(time);
  310.                 P3=0XEF;         
  311.                 P0=0XDF;
  312.                 delay(time);
  313.                 P3=0XDF;
  314.                 P0=0XEF;
  315.                 delay(time);
  316.                 if(ly_flag==1){ly_flag=0;goto hm;}
  317.                 P3=0XBF;
  318.                 P0=0XF7;
  319.                 delay(time);
  320.                 P3=0X7F;
  321.                 P0=0XFB;
  322.                 delay(time);
  323.                 P3=0XFF;
  324.                 P0=0XFC;
  325.                 delay(time);
  326.                 P0=0XFF;
  327.                 if(i<4)time=time-40;
  328.         }
  329. hm:;
  330. }

  331. void dis6()
  332. {
  333.         char j;
  334.         int time=200;
  335.         for(j=0;j<12;j++)
  336.         {
  337.                 P0=0XFC;//
  338.                 P2=0XFD;//P21
  339.                 P1=0X7F;//P17
  340.                 delay(time);
  341.                 P3=0X7F;//P37
  342.                 P0=0XFB;//P02
  343.                 P2=0XFB;//P22
  344.                 P1=0XBF;//P16
  345.                 delay(time);
  346.                 P3=0XBF;//P36
  347.                 P0=0XF7;//P03
  348.                 P2=0XF7;//P23
  349.                 P1=0XDF;//P15
  350.                 delay(time);
  351.                 if(ly_flag==1){ly_flag=0;goto hm;}
  352.                 P3=0XDF;//P35
  353.                 P0=0XEF;//P04
  354.                 P2=0XEF;//P24
  355.                 P1=0XEF;//P14  
  356.                 delay(time);
  357.                 P3=0XEF;//P34
  358.                 P0=0XDF;//P05
  359.                 P2=0XDF;//P25
  360.                 P1=0XF7;//P13                 
  361.                 delay(time);
  362.                 P3=0XF7;//P33  
  363.                 P0=0XBF;//P06
  364.                 P2=0XBF;//P26
  365.                 P1=0XFB;//P12  
  366.                 delay(time);
  367.                 if(ly_flag==1){ly_flag=0;goto hm;}
  368.                 P3=0XFD;//P31
  369.                 P0=0X7F;//P07
  370.                 P2=0X7F;//P27
  371.                 P1=0XFD;//P11         
  372.                 delay(time);
  373.                 P2=0XFF;
  374.                 P0=0XFF;
  375.                 P1=0XFE;//P10
  376.                 P3=0XFE;//P30
  377.                 delay(time);
  378.                 P1=0XFD;//P11
  379.                 P2=0X7F;//P27
  380.                 P0=0X7F;//P07
  381.                 P3=0XFD;//P31
  382.                 delay(time);
  383.                 if(ly_flag==1){ly_flag=0;goto hm;}
  384.                 P1=0XFB;//P12
  385.                 P2=0XBF;//P26
  386.                 P3=0XF7;//P33
  387.                 P0=0XBF;//P06
  388.                 delay(time);
  389.                 P1=0XF7;//P13
  390.                 P2=0XDF;//P25
  391.                 P3=0XEF;//P34
  392.                 P0=0XDF;//P05
  393.                 delay(time);       
  394.                 P1=0XEF;//P14
  395.                 P2=0XEF;//P24
  396.                 P3=0XDF;//P35
  397.                 P0=0XEF;//P04
  398.                 delay(time);
  399.                 if(ly_flag==1){ly_flag=0;goto hm;}
  400.                 P1=0XDF;//P15
  401.                 P2=0XF7;//P23
  402.                 P3=0XBF;//P36
  403.                 P0=0XF7;//P03
  404.                 delay(time);
  405.                 P1=0XBF;//P16
  406.                 P2=0XFB; //P22
  407.                 P3=0X7F;//P37
  408.                 P0=0XFB;//P02
  409.                 delay(time);
  410.                 P1=0X7F;//P17
  411.                 P2=0XFD;//P21
  412.                 P3=0XFF;//
  413.                 P0=0XFC;
  414.                 delay(time);
  415.                 P1=0XFF;//P17
  416.                 P2=0XFF;//P21
  417.                 P0=0XFF;
  418.                 if(j<4)time=time-40;                               
  419.         }
  420. hm:;       
  421. }
  422. void dis7()
  423. {
  424.         char j;
  425.         int time=70;
  426.         for(j=0;j<3;j++)
  427.         {
  428.                 P0=0XFC;
  429.                 delay(time);
  430.                 P3=0X7F;
  431.                 P0=0XF8;
  432.                 delay(time);
  433.                 P3=0X3F;
  434.                 P0=0XF0;
  435.                 delay(time);
  436.                 P3=0X1F;
  437.                 P0=0XE0;
  438.                 delay(time);
  439.                 P3=0X0F;
  440.                 P0=0XC0;
  441.                 delay(time);
  442.                 P3=0X07;
  443.                 P0=0X80;
  444.                 delay(time);
  445.                 P3=0X05;
  446.                 P0=0X00;
  447.                 delay(time);//
  448.                 if(ly_flag==1){ly_flag=0;goto hm;}
  449.                 P3=0X04;
  450.                 P1=0XFE;
  451.                 delay(time);
  452.                 P2=0X7F;
  453.                 P1=0XFC;
  454.                 delay(time);
  455.                 P2=0X3F;
  456.                 P1=0XF8;
  457.                 delay(time);
  458.                 P2=0X1F;
  459.                 P1=0XF0;
  460.                 delay(time);
  461.                 P2=0X0F;
  462.                 P1=0XE0;
  463.                 delay(time);
  464.                 P2=0X07;
  465.                 P1=0XC0;
  466.                 delay(time);
  467.                 if(ly_flag==1){ly_flag=0;goto hm;}
  468.                 P2=0X03;
  469.                 P1=0X80;
  470.                 delay(time);
  471.                 P2=0X01;
  472.                 P1=0X00;
  473.                 delay(time);
  474.                 P2=0X00;
  475.                 delay(time);
  476.                 if(ly_flag==1){ly_flag=0;goto hm;}
  477.                 delay(1000);
  478.                 if(ly_flag==1){ly_flag=0;goto hm;}
  479.                 delay(1000);
  480.                 if(ly_flag==1){ly_flag=0;goto hm;}
  481.                 delay(1000);
  482.                 P0=0XFF;
  483.                 P1=0XFF;
  484.                 P2=0XFF;
  485.                 P3=0XFF;       
  486.         }
  487. hm:;
  488. }
  489. void dis8()
  490. {
  491.         char j;
  492.         int time=250;
  493.         for(j=0;j<10;j++)
  494.         {
  495.                 P2=0XAA;
  496.                 P3=0X56;
  497.                 P0=0X55;
  498.                 P1=0X55;
  499.                 delay(time);
  500.                 P2=0X55;
  501.                 P3=0XAD;
  502.                 P0=0XAA;
  503.                 P1=0XAA;
  504.                 delay(time);
  505.                 if(ly_flag==1){ly_flag=0;goto hm;}
  506.                 if(j<3)time=time-60;       
  507.         }
  508. hm:;
  509. }
  510. void dis9()
  511. {
  512.         char j;
  513.         for(j=0;j<4;j++)
  514.         {
  515.                 P2=0X00;
  516.                 P3=0X04;
  517.                 P0=0XFE;
  518.                 P1=0XFF;
  519.                 delay(200);       
  520.                 P2=0XFF;
  521.                 P3=0XFF;
  522.                 P0=0X01;
  523.                 P1=0X00;
  524.                 delay(200);       
  525.                 if(ly_flag==1){ly_flag=0;goto hm;}
  526.         }
  527. hm:;
  528. }
  529. void dis10()
  530. {
  531.         char j;
  532.         for(j=0;j<4;j++)
  533.         {
  534.                 P2=0X00;
  535.                 P1=0X00;
  536.                 P3=0XFE;
  537.                 P0=0XFF;
  538.                 delay(200);       

  539.                 P2=0XFF;
  540.                 P1=0XFF;
  541.                 P3=0X04;
  542.                 P0=0X00;
  543.                 delay(200);       
  544.                 if(ly_flag==1){ly_flag=0;goto hm;}
  545.         }
  546. hm:;
  547. }
  548. void dis11()
  549. {
  550.         char j;
  551.         for(j=0;j<5;j++)
  552.         {
  553.                 P2=0X00;
  554.                 P3=0X04;
  555.                 P0=0X00;
  556.                 P1=0X00;
  557.                 delay(100);
  558.                 P2=0XFF;
  559.                 P3=0XFF;
  560.                 P0=0XFF;
  561.                 P1=0XFF;
  562.                 delay(100);
  563.                 if(ly_flag==1){ly_flag=0;goto hm;}       
  564.         }
  565. hm:;
  566. }
  567. void dis12()
  568. {
  569.         char j;
  570.         int time=70;
  571.         for(j=0;j<6;j++)
  572.         {
  573.                 P2=0XFE;
  574.                 P3=0XFE;
  575.                 P0=0XFE;
  576.                 P1=0XFE;
  577.                 delay(time);
  578.                 P2=0XFD;
  579.                 P3=0XFD;
  580.                 P0=0XFD;
  581.                 P1=0XFD;
  582.                 delay(time);
  583.                 P2=0XFB;
  584.                 P3=0XF7;
  585.                 P0=0XFB;
  586.                 P1=0XFB;
  587.                 delay(time);
  588.                 P2=0XF7;
  589.                 P3=0XEF;
  590.                 P0=0XF7;
  591.                 P1=0XF7;
  592.                 delay(time);
  593.                 if(ly_flag==1){ly_flag=0;goto hm;}
  594.                 P2=0XEF;
  595.                 P3=0XDF;
  596.                 P0=0XEF;
  597.                 P1=0XEF;
  598.                 delay(time);
  599.                 P2=0XDF;
  600.                 P3=0XBF;
  601.                 P0=0XDF;
  602.                 P1=0XDF;
  603.                 delay(time);
  604.                 P2=0XBF;
  605.                 P3=0X7F;
  606.                 P0=0XBF;
  607.                 P1=0XBF;
  608.                 delay(time);
  609.                 P2=0X7F;
  610.                 P3=0XFF;
  611.                 P0=0X7F;
  612.                 P1=0X7F;
  613.                 delay(time);
  614.         }
  615. hm:;       
  616. }

  617. void dis13()
  618. {
  619.         int time=200;
  620.         P2=0XFE;
  621.         P3=0XFE;
  622.         P0=0XFE;
  623.         P1=0XFE;
  624.         delay(time);
  625.         P2=0XFC;
  626.         P3=0XFC;
  627.         P0=0XFC;
  628.         P1=0XFC;
  629.         delay(time);
  630.         P2=0XF8;
  631.         P3=0XF4;
  632.         P0=0XF8;
  633.         P1=0XF8;
  634.         delay(time);
  635.         if(ly_flag==1){ly_flag=0;goto hm;}
  636.         P2=0XF0;
  637.         P3=0XE4;
  638.         P0=0XF0;
  639.         P1=0XF0;
  640.         delay(time);
  641.         P2=0XE0;
  642.         P3=0XC4;
  643.         P0=0XE0;
  644.         P1=0XE0;
  645.         delay(time);       
  646.         P2=0XC0;
  647.         P3=0X84;
  648.         P0=0XC0;
  649.         P1=0XC0;
  650.         delay(time);
  651.         if(ly_flag==1){ly_flag=0;goto hm;}
  652.         P2=0X80;
  653.         P3=0X04;
  654.         P0=0X80;
  655.         P1=0X80;
  656.         delay(time);
  657.         P2=0X00;
  658.         P3=0X04;
  659.         P0=0X00;
  660.         P1=0X00;
  661.         delay(time);
  662. hm:;
  663. }
  664. void dis14()
  665. {
  666.     P0 = 0xFF;
  667.     P1 = 0xFF;
  668.     P2 = 0xFF;
  669.     P3 = 0xFF;
  670.         delay(10);
  671.     P0 = 0x00;
  672.     P1 = 0x00;
  673.     P2 = 0x00;
  674.     P3 = 0x04;
  675.         delay(1000);
  676.         if(ly_flag==1){ly_flag=0;goto hm;}
  677.         delay(1000);
  678.         if(ly_flag==1){ly_flag=0;goto hm;}
  679.         delay(1000);
  680.     P0 = 0xFF;
  681.     P1 = 0xFF;
  682.     P2 = 0xFF;
  683.     P3 = 0xFF;
  684. hm:;
  685. }
  686. void dis15()
  687. {
  688.         int time=80;

  689.         P3_0=0;
  690.         P1_0=0;
  691.         delay(time);

  692.         P2_7=0;
  693.         P3_1=0;
  694.         P1_1=0;
  695.         P0_7=0;
  696.         delay(time);

  697.         P3_3=0;
  698.         P2_6=0;
  699.         P1_2=0;
  700.         P0_6=0;
  701.         delay(time);
  702.         if(ly_flag==1){ly_flag=0;goto hm;}
  703.        
  704.         P3_4=0;
  705.         P2_5=0;
  706.         P1_3=0;
  707.         P0_5=0;
  708.         delay(time);
  709.        
  710.         P3_5=0;
  711.         P2_4=0;
  712.         P1_4=0;
  713.         P0_4=0;
  714.         delay(time);
  715.        
  716.         P3_6=0;
  717.         P2_3=0;
  718.         P1_5=0;
  719.         P0_3=0;
  720.         delay(time);
  721.         if(ly_flag==1){ly_flag=0;goto hm;}
  722.         P1_6=0;
  723.         P0_2=0;       
  724.         P3_7=0;
  725.         P2_2=0;
  726.         delay(time);
  727.        
  728.         P2_1=0;
  729.         P0_0=0;
  730.         P1_7=0;
  731.         P0_1=0;
  732.         delay(time);
  733.         P2_0=0;
  734.         delay(time);
  735.     P0 = 0xFF;
  736.     P1 = 0xFF;
  737.     P2 = 0xFF;
  738.     P3 = 0xFF;
  739. hm:;                       
  740. }
  741. void init()
  742. {
  743.     P0 = 0x00;
  744.     P1 = 0x00;
  745.     P2 = 0x00;
  746.     P3 = 0x04;
  747.         delay(3000);
  748.     P0 = 0xFF;
  749.     P1 = 0xFF;
  750.     P2 = 0xFF;
  751.     P3 = 0xFF;

  752.         IT0=1;                                //设置成下降沿触发方式
  753.         EX0=1;                                  //开启外部中断 1,按键5

  754.         TMOD=0X12;                                //定时器1的方式1,16位计数器,用来计数时间

  755.         TL1=0;
  756.         TH1=0;                                        //定时/计数器1初始值

  757.         TH0=210;                                                             //写入预置初值(取值1-255,数越大PWM频率越高)
  758.         TL0=210;                                                             //写入预置值 (取值1-255,数越大PWM频率越高)
  759.         TR0=1;                                                               //启动定时器
  760.         ET0=1;                                                               //允许定时器0中断

  761.         EA=1;                                        //首先开启总中断
  762. }
  763. char zh=0,gs=0;
  764. void main()
  765. {
  766.         init();
  767.         while(1)
  768.         {
  769.                 if(moshi==0)
  770.                 {
  771.                         if(zh==0)dis1();
  772.                         if(zh==1)if(gs==0)dis1();
  773.                 }               
  774.                 if(moshi==0)
  775.                 {
  776.                         if(zh==0)dis2();
  777.                         if(zh==1)if(gs==1)dis2();
  778.                 }
  779.                 if(moshi==0)
  780.                 {
  781.                         if(zh==0)dis3();
  782.                         if(zh==1)if(gs==2)dis3();
  783.                 }
  784.                 if(moshi==0)
  785.                 {
  786.                         if(zh==0)dis4();
  787.                         if(zh==1)if(gs==3)dis4();
  788.                 }
  789.                 if(moshi==0)
  790.                 {
  791.                         if(zh==0)dis5();
  792.                         if(zh==1)if(gs==4)dis5();
  793.                 }
  794.                 if(moshi==0)
  795.                 {
  796.                         if(zh==0)dis6();
  797.                         if(zh==1)if(gs==5)dis6();
  798.                 }
  799.                 if(moshi==0)
  800.                 {
  801.                         if(zh==0)dis7();
  802.                         if(zh==1)if(gs==6)dis7();
  803.                 }
  804.                 if(moshi==0)
  805.                 {
  806.                         if(zh==0)dis8();
  807.                         if(zh==1)if(gs==7)dis8();
  808.                 }
  809.                 if(moshi==0)
  810.                 {
  811.                         if(zh==0)dis9();
  812.                         if(zh==1)if(gs==8)dis9();
  813.                 }
  814.                 if(moshi==0)
  815.                 {
  816.                         if(zh==0)dis10();
  817.                         if(zh==1)if(gs==9)dis10();
  818.                 }
  819.                 if(moshi==0)
  820.                 {
  821.                         if(zh==0)dis11();
  822.                         if(zh==1)if(gs==10)dis11();
  823.                 }
  824.                 if(moshi==0)
  825.                 {
  826.                         if(zh==0)dis12();
  827.                         if(zh==1)if(gs==11)dis12();
  828.                 }
  829.                 if(moshi==0)
  830.                 {
  831.                         if(zh==0)dis13();
  832.                         if(zh==1)if(gs==12)dis13();
  833.                 }
  834.                 if(moshi==0)
  835.                 {
  836.                         if(zh==0)dis14();
  837.                         if(zh==1)if(gs==13)dis14();
  838.                 }
  839.                 if(moshi==0)
  840.                 {
  841.                         if(zh==0)dis15();
  842.                         if(zh==1)if(gs==14)dis15();
  843.                 }
  844.                 if(moshi==3)
  845.                 {
  846.                         if(lm==0)
  847.                         {
  848.                             P0 = 0x00;
  849.                             P1 = 0x00;
  850.                             P2 = 0x00;
  851.                             P3 = 0x04;                       
  852.                         }
  853.                         if(lm==1)
  854.                         {
  855.                             P0 = 0xFF;
  856.                             P1 = 0xFF;
  857.                             P2 = 0xFF;
  858.                             P3 = 0xFF;                       
  859.                         }
  860.                 }               
  861.         }
  862. }

  863. /***********外中断1入口函数,P32引脚,红外线接收IC数据脚**************************/
  864. void hongwai(void)   interrupt 0
  865. {
  866.         unsigned char i,ia;
  867.         /**************开始判断是否为NEC波形引导码的前9MS和后4.5MS******************************/
  868.         TL1=0;
  869.         TH1=0;                                //定时/计数器1初始值
  870.         TR1=1;                                //定时器以12M晶振12分频计数,即1us计数
  871.         while(!hwx);
  872.         TR1=0;                                //信号翻转停止计数
  873.         if(TH1<30||TH1>40)        //NEC引导码前9MS,计数约9000,TH1约等于35,给个误差值,用30-40之间来判断
  874.                 return;

  875.         TH1=0;                                //定时/计数器1初始值
  876.         TR1=1;
  877.         while(hwx){
  878.                 delay1();
  879.                 if(TH1>22)                //NEC引导码引导码的后4。5MS,计数约4500,TH1约等于17
  880.                         return;               
  881.         }
  882.         TR1=0;
  883.         if(TH1<12)                        //NEC引导码引导码的后4。5MS,计数约4500,TH1约等于17
  884.                 return;

  885.         /***********开始接收四个字节内容**************************************/       
  886.         for(i=0;i<4;i++){
  887.                 for(ia=0;ia<8;ia++){                               
  888.                         while(!hwx);        //低电平开始,不处理只等待高电平
  889.                        
  890.                         TH1=0;
  891.                         TR1=1;                        //高电平开始,启动计数
  892.                         while(hwx){
  893.                                 delay1();
  894.                                 if(TH1>15)               
  895.                                         return;               
  896.                         }
  897.                         TR1=0;
  898.                         //高电平结束,判断数据1或0向变量移入
  899.                         ly_lhj[i]>>=1;                        //数据由高位移入低位
  900.                         if(TH1>4)                                //时间量TH1高于4,即高于1MS判断为1
  901.                                 ly_lhj[i]|=0x80;
  902.                 }
  903.         }

  904.         if(moshi==2)//小夜灯亮度调节
  905.         {

  906.                 if(ly_lhj[2]==0x07)//加
  907.                 {
  908.                         pwm=pwm+10;
  909.                         if(pwm>99)pwm=99;
  910.                 }
  911.        
  912.                 if(ly_lhj[2]==0x09)//减
  913.                 {
  914.                         pwm=pwm-10;
  915.                         if(pwm<10)pwm=10;       
  916.                 }
  917.         }

  918.         if(moshi==0)//动画模式速度调节
  919.         {

  920.                 if(ly_lhj[2]==0x40)//速度加
  921.                 {
  922.                         sudu=sudu-10;
  923.                         if(sudu<10)sudu=10;       
  924.                 }
  925.        
  926.                 if(ly_lhj[2]==0x19)//速度减
  927.                 {
  928.                         sudu=sudu+10;
  929.                         if(sudu>100)sudu=100;                               
  930.                 }
  931.         }

  932.         if(ly_lhj[2]==0x43)//自动动画模式
  933.         {
  934.                 moshi=0;
  935.                 zh=0;
  936.         }

  937.         if(ly_lhj[2]==0x44)//呼吸灯模式
  938.         {
  939.                 moshi=1;
  940.                 ly_flag=1;        //接收成功
  941.         }

  942.         if(ly_lhj[2]==0x15)//小夜灯模式
  943.         {
  944.                 moshi=2;
  945.                 ly_flag=1;        //接收成功
  946.         }

  947.         if(ly_lhj[2]==0x45)//全灭
  948.         {
  949.                 moshi=3;
  950.                 lm=1;
  951.             P0 = 0xFF;
  952.             P1 = 0xFF;
  953.             P2 = 0xFF;
  954.             P3 = 0xFF;
  955.                 ly_flag=1;        //接收成功               
  956.         }

  957.         if(ly_lhj[2]==0x47)//全亮
  958.         {
  959.                 moshi=3;
  960.                 lm=0;
  961.             P0 = 0x00;
  962.             P1 = 0x00;
  963.             P2 = 0x00;
  964.             P3 = 0x04;
  965.                 ly_flag=1;        //接收成功
  966.         }

  967.         if(ly_lhj[2]==0x42)//手动动画选择
  968.         {
  969.                 moshi=0;
  970.                 zh=1;
  971.         }
  972.         if(ly_lhj[2]==0x52)//手动动画选择
  973.         {
  974.                 gs++;
  975.                 if(gs>14)gs=0;
  976.         }
  977.         if(ly_lhj[2]==0x4A)//手动动画选择
  978.         {
  979.                 gs--;
  980.                 if(gs<0)gs=14;
  981.         }

  982.         TF1=0;
  983. }

  984. //定时器一中断函数,产生波形
  985. void timer_init0(void) interrupt 1 using 2  
  986. {
  987.         static  uchar   t ;           //PWM计数
  988.         if(moshi==1 || moshi==2)
  989.         {
  990.                 t++;                                    //每次定时器溢出加1
  991.        
  992.                 if(t == 100)                           //PWM周期 100个单位
  993.                 {
  994.                         t=0;                                  //使t=0,开始新的PWM周期
  995.                     P0 = 0xFF;
  996.                     P1 = 0xFF;
  997.                     P2 = 0xFF;
  998.                     P3 = 0xFF;               
  999.                 }                                          
  1000.                
  1001.                 if(pwm == t)                  //按照当前占空比切换输出为高电平
  1002.                 {  
  1003.                     P0 = 0x00;
  1004.                     P1 = 0x00;
  1005.                     P2 = 0x00;
  1006.                     P3 = 0x04;       
  1007.                 }       
  1008.         }

  1009.         if(moshi==1)
  1010.         {
  1011.                 su++;
  1012.                 if(su==6){su=0;vaule++;}
  1013.                
  1014.                 if(vaule==100)pwm=25;
  1015.                 if(vaule==200)pwm=30;
  1016.                 if(vaule==300)pwm=35;
  1017.                 if(vaule==400)pwm=40;
  1018.                 if(vaule==500)pwm=45;
  1019.                 if(vaule==600)pwm=50;
  1020.                 if(vaule==700)pwm=55;
  1021.                 if(vaule==800)pwm=60;
  1022.                 if(vaule==900)pwm=65;
  1023.                 if(vaule==1000)pwm=70;
  1024.                 if(vaule==1100)pwm=75;
  1025.                 if(vaule==1200)pwm=80;
  1026.                 if(vaule==1300)pwm=85;
  1027.                 if(vaule==1400)pwm=90;
  1028.                 if(vaule==1500)pwm=95;
  1029.                 if(vaule==1600)pwm=99;
  1030.                 if(vaule==2000)pwm=95;
  1031.                 if(vaule==2100)pwm=90;
  1032.                 if(vaule==2200)pwm=85;
  1033.                 if(vaule==2300)pwm=80;
  1034.                 if(vaule==2400)pwm=75;
  1035.                 if(vaule==2500)pwm=70;
  1036.                 if(vaule==2600)pwm=65;
  1037.                 if(vaule==2700)pwm=60;
  1038.                 if(vaule==2800)pwm=55;
  1039.                 if(vaule==2900)pwm=50;
  1040.                 if(vaule==3000)pwm=45;
  1041.                 if(vaule==3100)pwm=40;
  1042.                 if(vaule==3200)pwm=35;
  1043.                 if(vaule==3300)pwm=30;
  1044.                 if(vaule==3400){pwm=25;vaule=0;}
  1045.         }
  1046. }
复制代码


遥控版心形灯程序.rar

50.56 KB, 下载次数: 9, 下载积分: 黑币 -5

回复

使用道具 举报

ID:1 发表于 2018-4-26 18:23 | 显示全部楼层
原理图呢?
回复

使用道具 举报

ID:317033 发表于 2018-4-28 20:38 | 显示全部楼层

C:\Users\15714\Documents\Tencent Files\1032349556\FileRecv\MobileFile
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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