找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3934|回复: 4
打印 上一主题 下一主题
收起左侧

单片机C语言怎么将字符型变量改成整型变量?

[复制链接]
跳转到指定楼层
楼主
ID:350131 发表于 2018-8-17 19:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
程序在下面   
就是我想将单价和总价变量改成整型的   但直接改又编译不了  
请问改怎么解决
  1. #include <reg52.h>
  2. #include <intrins.h>
  3. #include <string.h>

  4. bit bdata flag_key;
  5. #include "main.h"
  6. #include "LCD1602.h"
  7. #include "HX711.h"
  8. #include "keyboard.h"
  9. #include "eeprom52.h"

  10. #define uchar unsigned char
  11. #define uint  unsigned int

  12. unsigned long HX711_Buffer = 0;
  13. unsigned long Weight_Maopi = 0;
  14. unsigned long Weight_Maopi_0 = 0;
  15. unsigned int qupi=0;
  16. long Weight_Shiwu = 0;
  17. //键盘处理变量
  18. unsigned char keycode;
  19. unsigned char DotPos;                                   //小数点标志及位置

  20. uint GapValue,GapValue1;

  21. unsigned char idata price;     //单价,长整型值,单位为分   
  22. unsigned char idata money;     //总价,长整型值,单位为分
  23. //定义标识
  24. volatile bit FlagTest = 0;                //定时测试标志,每0.5秒置位,测完清0
  25. volatile bit FlagKeyPress = 0;  //有键按下标志,处理完毕清0
  26. //校准参数
  27. //因为不同的传感器特性曲线不是很一致,因此,每一个传感器需要矫正这里这个参数才能使测量值很准确。
  28. //当发现测试出来的重量偏大时,增加该数值。
  29. //如果测试出来的重量偏小时,减小改数值。
  30. //该值可以为小数
  31. //#define GapValue 349
  32. sbit LED=P1^1;

  33. volatile bit ClearWeighFlag = 0; //传感器调零标志位,清除0漂

  34. /******************把数据保存到单片机内部eeprom中******************/
  35. void write_eeprom()
  36. {
  37.         SectorErase(0x1000);
  38.         GapValue1=GapValue&0x00ff;
  39.         byte_write(0x2000, GapValue1);
  40.         GapValue1=(GapValue&0xff00)>>8;
  41.         byte_write(0x2001, GapValue1);
  42.         byte_write(0x2060, a_a);      
  43. }

  44. /******************把数据从单片机内部eeprom中读出来*****************/
  45. void read_eeprom()
  46. {
  47.         GapValue   = byte_read(0x2001);
  48.         GapValue   = (GapValue<<8)|byte_read(0x2000);
  49.         a_a      = byte_read(0x2060);
  50. }

  51. /**************开机自检eeprom初始化*****************/
  52. void init_eeprom()
  53. {
  54.         read_eeprom();                //先读
  55.         if(a_a != 1)                //新的单片机初始单片机内问eeprom
  56.         {
  57.                 GapValue  = 3600;
  58.                 a_a = 1;
  59.                 write_eeprom();           //保存数据
  60.         }      
  61. }
  62.                                                                                                                                        
  63. //显示单价,单位为元,四位整数,两位小数
  64. void Display_Price()
  65. {
  66.             LCD1602_write_com(0x8c);
  67.                         LCD1602_write_data(price/100 + 0x30);
  68.                         LCD1602_write_data(price%100/10 + 0x30);
  69.                         LCD1602_write_data('.');
  70.                         LCD1602_write_data(price%10 + 0x30);
  71. }

  72. //显示重量,单位kg,两位整数,三位小数
  73. void Display_Weight()
  74. {
  75.             LCD1602_write_com(0x83);
  76.                         LCD1602_write_data(Weight_Shiwu/1000 + 0x30);
  77.                         LCD1602_write_data('.');
  78.                         LCD1602_write_data(Weight_Shiwu%1000/100 + 0x30);
  79.                         LCD1602_write_data(Weight_Shiwu%100/10 + 0x30);
  80.                         LCD1602_write_data(Weight_Shiwu%10 + 0x30);
  81. }

  82. //显示总价,单位为元,四位整数,两位小数
  83. void Display_Money()
  84. {
  85.   // unsigned int i,j;

  86.    if (money>9999)         //超出显示量程
  87.    {
  88.      LCD1602_write_com(0x80+0x40+6);
  89.      LCD1602_write_word("---.-");
  90.       return;      
  91.    }
  92.    if (money>=1000)
  93.    {
  94.        LCD1602_write_com(0x80+0x40+6);
  95.            LCD1602_write_data(money/1000 + 0x30);
  96.            LCD1602_write_data(money%1000/100 + 0x30);
  97.            LCD1602_write_data(money%100/10 + 0x30);
  98.            LCD1602_write_data('.');
  99.            LCD1602_write_data(money%10 + 0x30);
  100.    }
  101.    else if (money>=100)
  102.    {
  103.        LCD1602_write_com(0x80+0x40+6);
  104.            LCD1602_write_data(0x20);
  105.            LCD1602_write_data(money%1000/100 + 0x30);
  106.            LCD1602_write_data(money%100/10 + 0x30);
  107.            LCD1602_write_data('.');
  108.            LCD1602_write_data(money%10 + 0x30);
  109.    }
  110.     else if(money>=10)
  111.    {
  112.        LCD1602_write_com(0x80+0x40+6);
  113.           LCD1602_write_data(0x20);
  114.             LCD1602_write_com(0x80+0x40+7);
  115.            LCD1602_write_data(0x20);
  116.            LCD1602_write_data(money%100/10 + 0x30);
  117.            LCD1602_write_data('.');
  118.            LCD1602_write_data(money%10+ 0x30);
  119.    }   
  120.    else
  121.      {
  122.        LCD1602_write_com(0x80+0x40+6);
  123.            LCD1602_write_data(0x20);
  124.            LCD1602_write_com(0x80+0x40+7);
  125.            LCD1602_write_data(0x20);
  126.            LCD1602_write_com(0x80+0x40+8);
  127.            LCD1602_write_data(0 + 0x30);
  128.            LCD1602_write_data('.');
  129.            LCD1602_write_data(money%10 + 0x30);
  130.    }         
  131. }

  132. //数据初始化
  133. void Data_Init()
  134. {
  135.    price = 0;
  136.    DotPos = 0;
  137. }
  138. //定时器0初始化
  139. void Timer0_Init()
  140. {
  141.         ET0 = 1;        //允许定时器0中断
  142.         TMOD = 1;       //定时器工作方式选择
  143.         TL0 = 0xb0;     
  144.         TH0 = 0x3c;     //定时器赋予初值
  145.         TR0 = 1;        //启动定时器
  146. }

  147. //定时器0中断
  148. void Timer0_ISR (void) interrupt 1 using 0
  149. {
  150. uchar Counter;
  151.         TL0 = 0xb0;
  152.         TH0 = 0x3c;     //定时器赋予初值

  153.         //每0.5秒钟刷新重量
  154.     Counter ++;
  155.     if (Counter >= 10)
  156.     {
  157.        FlagTest = 1;
  158.            Counter = 0;
  159.     }
  160. }


  161. //按键响应程序,参数是键值
  162. //返回键值:
  163. //         1    2    3    10           //10:清零重量,兼去皮功能      
  164. //         4    5    6    11           //11:清除单价
  165. //         7    8    9    12           //12:显示数值偏大调节按键  
  166. //         14   0    15   13           //14:无功能(用于扩展)15:小数点   13:显示数值偏小调节按键

  167. void KeyPress(uchar keycode)
  168. {
  169.         switch (keycode)
  170.         {
  171.                 case 0:
  172.                 case 1:
  173.                 case 2:
  174.                 case 3:
  175.                 case 4:
  176.                 case 5:
  177.                 case 6:
  178.                 case 7:
  179.                 case 8:
  180.                 case 9:        //目前在设置整数位,要注意price是整型,存储单位为分
  181.                         if (DotPos == 0)
  182.                         {                        //最多只能设置到千位
  183.                                 if (price<100)
  184.                                 {
  185.                                         price=price*10+keycode*10;
  186.                                         /*输入第一个数字时,price是等于0的,当第一次输入一个数字,例如输入5,那么计算:price=price*10+5*10=50
  187.                                         我们看到液晶上显示的是05.0,但是在程序里直接处理小数显示是比较麻烦的,所以我们是将这个数乘以10,扩大10倍后处理的
  188.                                         那么当第二次输入数字时,例如输入4,那么计算:price=50*10+4*10=540,液晶显示的就是54.0
  189.                                         */
  190.                                 }
  191.                         }//目前在设置小数位
  192.                         else if (DotPos==1)  //小数点后第一位
  193.                         {
  194.                                 price=price+keycode;
  195.                                 DotPos=2;
  196.                         }
  197.                         Display_Price();
  198.                         break;
  199.                 case 10:   //去皮键
  200.                         if(qupi==0)
  201.                                 qupi=Weight_Shiwu;
  202.                         else
  203.                                 qupi=0;
  204.                         Display_Price();
  205.                         //                 FlagSetPrice = 0;
  206.                         DotPos = 0;
  207.                         break;
  208.                 case 11:        //删除键
  209.                         price=0;
  210.                         DotPos=0;
  211.                         Display_Price();
  212.                         break;
  213.                 case 12:           //加
  214.                         if(GapValue<10000)
  215.                         GapValue++;      
  216.                        
  217.                         break;
  218.                 case 13:   //减
  219.                         if(GapValue>1)
  220.                         GapValue--;
  221.                         break;
  222.                 case 15:   //小数点按下
  223.                         DotPos = 1;      //小数点后第一位
  224.                         break;

  225.    }
  226. }
  227. //****************************************************
  228. //主函数
  229. //****************************************************
  230. void main()
  231. {
  232.         init_eeprom();  //开始初始化保存的数据
  233.         Init_LCD1602();                                                                        //初始化LCD1602
  234.    EA = 0;
  235.    Data_Init();
  236.    Timer0_Init();
  237.    //初中始化完成,开断
  238.    EA = 1;
  239.       
  240. //        Get_Maopi();
  241.         LCD1602_write_com(0x80);                                                //指针设置
  242.    LCD1602_write_word(" Welcome To Use ");        //  
  243.    LCD1602_write_com(0x80+0x40);                                                //指针设置
  244.    LCD1602_write_word("Electronic Scale");
  245. //   Delay_ms(2000);
  246.    Get_Maopi();
  247.    LCD1602_write_com(0x80);                                                //指针设置
  248.    LCD1602_write_word("WE:0.000 PR:00.0");
  249.    LCD1602_write_com(0x80+0x40);                                //指针设置
  250.    LCD1602_write_word("MONEY:  0.00    ");
  251.    Display_Price();
  252. //        Get_Maopi();                                //称毛皮重量

  253.         while(1)
  254.         {
  255. //每0.5秒称重一次
  256.           if (FlagTest==1)
  257.                 {
  258.                         Get_Weight();
  259.                         FlagTest = 0;
  260.                 }                       
  261.                  
  262.           keycode = Getkeyboard();
  263.           //有效键值0-15
  264.           if (keycode<16)
  265.           {
  266.                  KeyPress(keycode);
  267.                  Buzzer=0;
  268.                  Delay_ms(100);
  269.                  Buzzer=1;
  270.                  while(keycode<16)
  271.                  {
  272.                         if(keycode==12||keycode==13)
  273.                         {
  274.                                 Buzzer=0;
  275.                                  Delay_ms(10);
  276.                                  Buzzer=1;
  277.                                  KeyPress(keycode);
  278.                                  Get_Weight();
  279.                                  flag_key=1;
  280.                         }
  281.                         keycode = Getkeyboard();
  282.                  }
  283.                  write_eeprom();                           //保存数据
  284.           }
  285.         }
  286. }
  287. //****************************************************
  288. //称重
  289. //****************************************************
  290. void Get_Weight()
  291. {
  292.         Weight_Shiwu = HX711_Read();
  293.         Weight_Shiwu = Weight_Shiwu - Weight_Maopi;                //获取净重
  294.       
  295.         Weight_Shiwu = (unsigned int)((float)Weight_Shiwu*10/GapValue)-qupi;         //计算实物的实际重量                                                                                                                              
  296.         if(Weight_Shiwu > 10000)                //超重报警
  297.         {
  298.                 Buzzer = !Buzzer;      
  299.                 LED=!LED;
  300.                 LCD1602_write_com(0x83);
  301.            LCD1602_write_word("-.---");
  302.         }
  303.         else
  304.         {
  305.                 if(Weight_Shiwu==0)
  306.                 LED=1;
  307.                 else if(Weight_Shiwu>0)
  308.                 LED=0;
  309.                 Buzzer = 1;
  310.                 Display_Weight();
  311.                 money = Weight_Shiwu*price/1000;  //money单位为分
  312.                //显示总金额
  313.                Display_Money();
  314.         }
  315. }

  316. //****************************************************
  317. //获取毛皮重量
  318. //****************************************************
  319. void Get_Maopi()
  320. {
  321.         unsigned char clear;
  322. mm:        Weight_Maopi_0 = HX711_Read();
  323.         for(clear=0;clear<10;clear++)
  324.         {
  325.                 Buzzer=1;
  326.                 LED=1;
  327.                 Delay_ms(100);
  328.                 LED=0;
  329.                 Delay_ms(100);      
  330.         }
  331.         Weight_Maopi = HX711_Read();
  332.         if(Weight_Maopi/GapValue!=Weight_Maopi_0/GapValue)
  333.         goto mm;
  334.         Buzzer=0;
  335.         Delay_ms(500);
  336.         Buzzer=1;
  337. }

  338. //****************************************************
  339. //MS延时函数(12M晶振下测试)
  340. //****************************************************
  341. void Delay_ms(unsigned int n)
  342. {
  343.         unsigned int  i,j;
  344.         for(i=0;i<n;i++)
  345.                 for(j=0;j<123;j++);
  346. }
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:155507 发表于 2018-8-18 08:34 | 只看该作者
unsigned char idata price;     //单价,长整型值,单位为分   
unsigned char idata money;     //总价,长整型值,单位为分

unsigned char是一个8位的二进制码 他可以存储字符代表的ascii码也可以表示一个0-255的数字,一般变量的大小不超过255的话尽量用char,因为这样可以节省一些RAM

2000在作为money的入参时,会被强制转换成unsigned char,此时超出范围的部门会被忽略。具体来说,2000的二进制值是111 1101 0000,在强制转换后,实际函数内部使用的 money的二进制值是1101 0000,也就是十进制的208。
因此不能达到正确的结果。

对于这种值域由大变小的数据类型转换,编译器一般都会有警告的。除非明确知道转换的结果对自己的代码无不良影响,应避免这种转换。
回复

使用道具 举报

板凳
ID:387521 发表于 2018-8-18 11:43 | 只看该作者
可以试试强制类型转换,但是就象一楼说的,一偶风险哦。
回复

使用道具 举报

地板
ID:387533 发表于 2018-8-18 12:45 | 只看该作者
强制转换类型,但是可能导致丢失数据最后输出错误
回复

使用道具 举报

5#
ID:390775 发表于 2018-8-31 17:10 | 只看该作者
可以用 atoi函数 在stdlib.h中也可以自己写一个函数  我写的如下:
int  my_atoi(const char *str)
{  
  if(str == NULL) {return -1;}   int t = 1, sum = 0;char *str1 = str; int len = strlen(str1) - 1;
while(len >= 0) {sum += my_Ctoi(str1[len]) * t;  t *= 10; len--;}
return sum;
}
int my_Ctoi(char c){ return c - '0';}  //将字符转换为整数;
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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