找回密码
 立即注册

QQ登录

只需一步,快速开始

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

数控稳压电源代码

[复制链接]
ID:834162 发表于 2021-4-12 09:02 | 显示全部楼层 |阅读模式
  1. #include"reg52.h"
  2. //#include"math.h"
  3. #include"stdlib.h"
  4. #include"function.h"


  5. sbit RS = P1^4;
  6. sbit RW = P1^5;
  7. sbit E  = P1^6;

  8. #define DBUS P0
  9. sbit clk_574_1 = P2^3;
  10. //sbit clk_574_2 = P1^3;
  11. //sbit lcd_rs    = P1^4;

  12. uint idac;
  13.         

  14. idata uchar GOUT[7];
  15. idata float temp1;
  16. idata float dac_temp;
  17. idata float sheding_temp;
  18. idata float sheding_temp1;
  19. char  code dx516[3] _at_ 0x003b;//这是为了仿真设置的(大虾仿真器)

  20. /************************************************************************************
  21.                               把浮点数拆开存储指针型数据  
  22.                                                           输入浮点数,方式0:设定值,1:检测值  
  23. ************************************************************************************/
  24. void chai(float x,bit y)  
  25. {
  26.         uint i;
  27.         for(i = 0;i < 7;i ++)
  28.                 GOUT[i] = 0;
  29.         if(y == 0)
  30.         {
  31.                 i = x / 10;
  32.                 if(i == 0)
  33.                         GOUT[0] = ' ';
  34.                 else
  35.                         GOUT[0]        = i + '0';
  36.                 GOUT[1] = (int)x % 10 + '0';
  37.                 x = x * 10;
  38.                 GOUT[3] = (int)x % 10 + '0';

  39.                 x = x * 10;
  40.                 if(((int)x % 10) >= 5 )// 防止后面大于4的数被舍去  
  41.                 {GOUT[3] = GOUT[3] + 1;}
  42.                 else
  43.                 {;}

  44.                 GOUT[4] = ' ';
  45.                 GOUT[5] = ' ';
  46.         }
  47.         else
  48.         {
  49.                 i = x / 10;
  50.                 if(i == 0)
  51.                         GOUT[0] = ' ';
  52.                 else
  53.                         GOUT[0] = i + '0';
  54.                 GOUT[1] = (int)x % 10 + '0';
  55.                 x = x * 10;
  56.                 GOUT[3] = (int)x % 10 + '0';
  57.                 x = x * 10;
  58.                 GOUT[4] = (int)x % 10 + '0';
  59.                 x = (x * 10);
  60.                 GOUT[5] = (unsigned int)x % 10 + '0';
  61.         }
  62.         GOUT[2] =  '.';
  63.         GOUT[6] =  '\0';
  64. }

  65. /************************************************************************************

  66. //向LCM发送一个字符串,长度64字符之内。  

  67. ************************************************************************************/
  68. void lcm_w_word(uchar x,uchar y,uchar *str)
  69. {
  70.    x = x+y;
  71.    w_code(x);
  72.   while(*str != '\0')
  73.   {
  74.        w_data(*str++);
  75.    }
  76.   *str = 0;
  77. }
  78. /************************************************************************************
  79.                               显示设定电压  
  80. ************************************************************************************/
  81. void  disp_sheding()
  82. {
  83.     lcm_w_word(0x90,0x00,"设定:");
  84.     sheding_temp1 = sheding_temp + 0.0005;//------加0.0005修正有时显示.0999
  85.         chai(sheding_temp1,1);  
  86.     lcm_w_word(0x90,3,GOUT);
  87.         w_code(0x96);
  88.     w_data('V');        
  89. }
  90. /************************************************************************************
  91.                  AD的16位整形数转换成浮点电压或电流值  
  92. ************************************************************************************/
  93. float adjisuan_U(uint x)
  94. {
  95.         temp1 = 11.990 / 0x5e1f;
  96.         temp1 = x * temp1;
  97.         return temp1;
  98. }

  99. float adjisuan_I(uint x)  //
  100. {
  101.         temp1 = 0.899 / 0x253d;
  102.         temp1 = (x * temp1)-0.005;
  103.         return temp1;
  104. }
  105. /************************************************************************************
  106. *
  107. *函数功能:输入浮点数  
  108. *
  109. ************************************************************************************/
  110. float scanf_float(void)
  111. {
  112.         static uchar i = 0;
  113.         static uchar x[5] = {'0','0','0','0','0'};
  114.         static bit k = 1;
  115.         uchar temp1,temp2;
  116.         temp1 = key_sm();
  117.         temp2 = key_sm();
  118.         if((temp2 == 0xff) || (temp1 != temp2))
  119.         {
  120.                 k = 1;
  121.                 return(atof(x));
  122.         }
  123.         else if(k)
  124.         {
  125.                 k = 0;
  126.                 x[i] = temp1;
  127.                 i ++;
  128.                 if((temp1 == key_esc) || (i == 4))
  129.                 {
  130.                         x[0] = '0';
  131.                         x[1] = '0';
  132.                         x[2] = '0';
  133.                         x[3] = '0';
  134.                         x[4] = '0';
  135.                         i = 0;
  136.                 }
  137.                 return(atof(x));
  138.         }
  139. }

  140. /************************************************************************************

  141.                               扫描按键,并根据键值修改DAC的电压  

  142. ************************************************************************************/
  143. void key_dac()
  144. {
  145.                 uchar key_qiao;
  146.            key_qiao = key_sm();
  147.         switch(key_sm())
  148.                 {
  149.         case key_shang:dac_temp = dac_temp + 1.0;break;//---按下上键电压加1V
  150.         case key_xia  :dac_temp = dac_temp - 1.0;break;//---按下下键电压减1
  151.         case key_zuo  :dac_temp = dac_temp - 0.1;break;//---按下左键电压减0.1
  152.     case key_you  :dac_temp = dac_temp + 0.1;break;//---按下右键电压加0.1
  153.     case key_0    :dac_temp = 0;             break;//---按下0键电压归0
  154.         default: key_qiao = 0xff;
  155.                  }
  156.                 if(key_qiao != 0xff)  //  10月1日 这里原来是0x00就读AD不正常            
  157.                            {
  158.                        RS  =  1;                        
  159.                            RW  =  0;        //--------避免液晶屏干扰数据总线
  160.                            if(dac_temp < 0){dac_temp = 0;}        //----判断电压低于0大于30时把数据归0
  161.                         if(dac_temp >= 30.10){dac_temp = 0;}        
  162.                         sheding_temp = dac_temp ;
  163.                         disp_sheding();
  164.                         idac = conver(dac_temp);  
  165.                           dac(idac);
  166.                         delay(50000);
  167.                         }
  168. }
  169. /************************************************************************************
  170.                                      主函数   
  171. ************************************************************************************/
  172. main()
  173. {

  174.                         lcd_rst(0);
  175.                         rst_ad();        
  176.                         dac_temp = 0 ;
  177.                        RS  =  1;                        
  178.                        RW  =  0;        //--------避免液晶屏干扰数据总线                  
  179.                              dac(conver(dac_temp));           //吸合低压继电器并输出0V
  180.                        LED_ONOFF(1);//  打开背光源   
  181.                           set_7705_ch(0);
  182.                     read_AD_data(0x38);
  183.                     set_7705_ch(1);
  184.                     read_AD_data(0x39);

  185.                    disp_sheding();//显示设定电压   
  186.                    while(1)
  187.                    {
  188.                    lcm_w_word(0x80,0x02,"稳压电源");
  189.                    lcm_w_word(0x90,0x00,"设定:");                  
  190.                    lcm_w_word(0x88,0x00,"电压:");   
  191.                    lcm_w_word(0x98,0x00,"电流:");
  192.                            
  193.                    write_ad(0x10);
  194.                write_ad(0x04);
  195.                    adjisuan_U(read_AD_data(0x38));
  196.                    chai(temp1,1);  
  197.            lcm_w_word(0x88,3,GOUT);
  198.                    key_dac();

  199.                    write_ad(0x11);
  200.                write_ad(0x14);
  201.                    adjisuan_I(read_AD_data(0x39));
  202.                    chai(temp1,1);  
  203.            lcm_w_word(0x98,3,GOUT);
  204.                    key_dac();
  205.                   
  206.                    }
  207.                                                                                                                                              
  208. }

复制代码
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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