找回密码
 立即注册

QQ登录

只需一步,快速开始

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

error C129: missing ';' before 'Ds18b20Init' 请问大佬们怎么修改?

[复制链接]
ID:545543 发表于 2019-5-29 22:24 | 显示全部楼层 |阅读模式
  1. #include<reg52.h>
  2. void LcdDisplay(int);
  3. void DigDisplay();
  4. void chaomax();
  5. void chaomin();
  6. void guomax();
  7. void guomin();
  8. //存储上下限变量
  9. int tempmax=30,tempmin=20,tempt,tempmode=0;
  10. int jileimax=0,jileimin=0,jileimid=0;

  11. void Delay1ms(unsigned char x)
  12. {
  13. unsigned char i,j;
  14.   for(i=0;i<x;i++)
  15.   for(j=0;j<112;j++);
  16. }
  17. uchar  Ds18b20Init()
  18. {
  19.         uchar i;
  20.         DSPORT = 0;                         //将总线拉低480us~960us
  21.         //i = 70;        
  22.         i = 900;        
  23.         while(i--);//延时642us
  24.         DSPORT = 1;                        //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
  25.         i = 0;
  26.         while(DSPORT)        //等待DS18B20拉低总线
  27.         {
  28.                 Delay1ms(1);
  29.                 i++;
  30.                 if(i>5)//等待>5MS
  31.                 {
  32.                         return 0;//初始化失败
  33.                 }
  34.         
  35.         }
  36.         return 1;//初始化成功
  37. }
  38. void Ds18b20WriteByte(uchar dat)//????????
  39. {
  40.         uint i, j;
  41.         for(j=0; j<8; j++)
  42.         {
  43.                 DSPORT = 0;                       //每写入一位数据之前先把总线拉低1us     
  44.                 i++;
  45.                 DSPORT = dat & 0x01;  //然后写入一个数据,从最低位开始
  46.                 i=8;
  47.                 while(i--); //延时8us        
  48.                 DSPORT = 1;        //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
  49.                 dat >>= 1;
  50.         }
  51. }
  52. [u][i][b]uchar Ds18b20ReadByte()[/b][/i][/u]
  53. {
  54.         uchar byte, bi;
  55.         uint i, j;        
  56.         for(j=8; j>0; j--)
  57.         {
  58.                 DSPORT = 0;//先将总线拉低1us
  59.                 i++;
  60.                 DSPORT = 1;//然后释放总线
  61.                 i++;
  62.                 i++;//延时2us等待数据稳定
  63.                 bi = DSPORT;         //读取数据,从最低位开始读取
  64.         /*将byte左移一位,然后或上右移7位后的bi,注意移动之后移掉那位补0。*/
  65.                 byte = (byte >> 1) | (bi << 7);                                                  
  66.                 i = 4;                //读取完之后等待4us再接着读取下一个数
  67.                 while(i--);
  68.         }                                
  69.         return byte;
  70. }
  71. void  Ds18b20ChangTemp()
  72. {
  73.         Ds18b20Init();
  74.         Delay1ms(1);
  75.         Ds18b20WriteByte(0xcc);                //跳过ROM操作命令                 
  76.         Ds18b20WriteByte(0x44);            //温度转换命令
  77.         Delay1ms(20);        //等待转换成功,而如果你是一直刷着的话,就不用这个延时了
  78. }
  79. void  Ds18b20ReadTempCom()
  80. {        
  81.         Ds18b20Init();
  82.         Delay1ms(1);
  83.         Ds18b20WriteByte(0xcc);         //跳过ROM操作命令
  84.         Ds18b20WriteByte(0xbe);         //发送读取温度命令
  85. }
  86. int Ds18b20ReadTemp()
  87. {
  88.         int temp = 0;
  89.         uchar tmh, tml;
  90.         Ds18b20ChangTemp();                                 //先写入转换命令
  91.         Ds18b20ReadTempCom();                        //然后等待转换完后发送读取温度命令
  92.         tml = Ds18b20ReadByte();                //读取温度值共16位,先读低字节
  93.         tmh = Ds18b20ReadByte();                //再读高字节
  94.         temp = tmh;
  95.         temp <<= 8;
  96.         temp |= tml;
  97.         return temp;
  98. }
  99. void LcdDisplay(int temp)          //lcd显示
  100.         float tp;  
  101.         if(temp< 0)                                //当温度值为负数
  102.           {
  103.                 DisplayData[0] = 0x40;
  104.                 //因为读取的温度是实际温度的补码,所以减1,再取反求出原码
  105.                 temp=temp-1;
  106.                 temp=~temp;
  107.                 tp=temp;
  108.                 temp=tp*0.0625*100+0.5;        
  109.                 //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
  110.                 //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
  111.                 //算加上0.5,还是在小数点后面。
  112.           }
  113.          else
  114.           {                        
  115.                 DisplayData[0] = 0x00;
  116.                 tp=temp;//因为数据处理有小数点所以将温度赋给一个浮点型变量
  117.                 //如果温度是正的那么,那么正数的原码就是补码它本身
  118.                 temp=tp*0.0625*100+0.5;        
  119.                 //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
  120.                 //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
  121.                 //算加上0.5,还是在小数点后面。
  122.         }
  123.         DisplayData[0] = DIG_CODE[tempmax / 10];
  124.         DisplayData[1] = DIG_CODE[tempmax % 10];
  125.         DisplayData[4] = DIG_CODE[tempmin / 10];
  126.         DisplayData[5] = DIG_CODE[tempmin % 10];
  127.         DisplayData[2] = DIG_CODE[temp % 10000 / 1000];
  128.         DisplayData[3] = DIG_CODE[temp % 1000/100];
  129.          DigDisplay();                                           //扫描显示
  130. void DigDisplay()
  131. {
  132.         unsigned char i;
  133.         unsigned int j;
  134.         for(i=0;i<6;i++)
  135.         {
  136.                 switch(i)         //位选,选择点亮的数码管
  137.                 {
  138.                         
  139.                         case(0):
  140.                                 LS1=0;LS2=1;LS3=1;LS4=1;LS5=1;LS6=1; break;//显示第0位
  141.                         case(1):
  142.                                 LS1=1;LS2=0;LS3=1;LS4=1;LS5=1;LS6=1; break;//显示第1位
  143.                         
  144.                         case(2):
  145.                                 LS1=1;LS2=1;LS3=0;LS4=1;LS5=1;LS6=1; break;//显示第2位
  146.                         case(3):
  147.                                 LS1=1;LS2=1;LS3=1;LS4=0;LS5=1;LS6=1; break;//显示第3位
  148.                         
  149.                         case(4):
  150.                                 LS1=1;LS2=1;LS3=1;LS4=1;LS5=0;LS6=1; break;//显示第4位
  151.                         case(5):
  152.                                 LS1=1;LS2=1;LS3=1;LS4=1;LS5=1;LS6=0; break;//显示第5位
  153.                 }
  154.                 GPIO_DIG=DisplayData[i];//发送段码
  155.                 j=200;                                                 //扫描间隔时间设定
  156.                 while(j--);        
  157.                 GPIO_DIG=0x00;//消隐
  158.         }
  159. }

  160. void main()
  161. {
  162.         buzzer=1;
  163.         
  164.         while(1)
  165.         {
  166.                 LcdDisplay(Ds18b20ReadTemp());
  167.         }
  168. }
  169. //#include"temp.h"

  170. void chaomax()
  171.       {
  172.         buzzer=1;
  173.                         
  174.        }
  175.         if(key1==0)
  176.                 {
  177.                         Delay1ms(300);//按键防抖
  178.                         if(key1==0)
  179.                         {
  180.                         tempmax++;
  181.                         }
  182.                 }
  183.                         if(key2==0)
  184.                 {
  185.                         Delay1ms(300);
  186.                         if(key2==0)
  187.                         {
  188.                         tempmax--;
  189.                         }                        
  190.                 }
  191.                         if(key3==0)
  192.                 {
  193.                         Delay1ms(300);
  194.                         if(key3==0)
  195.                         {
  196.                         tempmin++;
  197.                         }        
  198.                 }
  199.                         if(key4==0)
  200.                 {
  201.                         Delay1ms(300);
  202.                         if(key4==0)
  203.                         {
  204.                         tempmin--;
  205.                         }
  206.                 }

  207. if(temp<(tempmin*100))//*100是为了使当前值和最小值在同一数量级比较
  208.         {
  209.                 jileimin++;
  210.                 jileimid=0;               
  211.                 if(jileimin>3)//扫描3次以确定已经低于下限值
  212.                 {
  213.                 tempmode=1;        //区分当前值是否是原值还是恢复值,原值为0,由上限值恢复是2,由下限值恢复时1
  214.           chaomin();
  215.                 jileimin=0;//积累值置位
  216.                 }
  217.         }
  218.         if(temp>(tempmax*100))
  219.         {
  220.                 jileimax++;
  221.                 jileimid=0;
  222.                 if(jileimax>3)
  223.                 {
  224.                 tempmode=2;         
  225.                         chaomax();
  226.                         jileimax=0;
  227.                 }
  228.         }
  229.         
  230.         if((tempmin*100)<temp&&temp<(tempmax*100))
  231.         {
  232.                 jileimax=0;
  233.                 jileimin=0;
  234.                 jileimid++;
  235.         if(jileimid>3)
  236.         {
  237.            switch(tempmode)
  238.                 {
  239.                          case 0:buzzer=0;break;
  240.                          case 1:guomin();break;
  241.                          case 2:guomax();break;                 
  242.                 }               
  243.                 tempmode=0;
  244.                 buzzer=0;
  245.                 jileimid=0;
  246.         }
  247.         }
  248. //三声短鸣表示低于温度下限
  249. void chaomin()
  250.     {
  251.        buzzer=1;
  252.                          Delay1ms(200);
  253.                          buzzer=0;
  254.                          Delay1ms(200);
  255.                          buzzer=1;
  256.                          Delay1ms(200);
  257.                          buzzer=0;
  258.                          Delay1ms(200);
  259.                          buzzer=1;
  260.                          Delay1ms(200);
  261.                          buzzer=0;
  262.                          Delay1ms(1000);
  263.                 }

  264. //三声短鸣表示超过温度上限               
  265. void guomax()
  266.                 {
  267.                          buzzer=0;
  268.                          Delay1ms(200);
  269.                    buzzer=1;
  270.                          Delay1ms(200);
  271.                          buzzer=0;
  272.                          Delay1ms(200);
  273.                          buzzer=1;
  274.                          Delay1ms(200);
  275.                          buzzer=0;
  276.                          Delay1ms(200);
  277.                          buzzer=1;
  278.                          Delay1ms(200);
  279.                          buzzer=0;
  280.                          Delay1ms(200);
  281.                 }
  282. //2秒长鸣表示超过温度下限               
  283. void guomin()
  284.     {
  285.       buzzer=1;
  286.                         Delay1ms(2000);
  287.                         buzzer=0;
  288.     }
复制代码


回复

使用道具 举报

ID:155507 发表于 2019-5-29 23:54 | 显示全部楼层
你这个程序不完整,你检查一下

少了这个
#define uchar unsigned char
#define uint unsigned int
回复

使用道具 举报

ID:155507 发表于 2019-5-30 19:37 | 显示全部楼层
原理图得提供啊,怎么接线的
单片机程序是要针对相应的硬件环境的,
给你改了一下试试。这个要看你的硬件电路了


  1. #include <reg52.h>

  2. #define uchar unsigned char
  3. #define uint unsigned int
  4. #define GPIO_DIG P1

  5. sbit DSPORT=P3^6; //DS18B20管脚定义
  6. sbit buzzer=P3^7;
  7. sbit key1=P3^2; //按键
  8. sbit key2=P3^3;
  9. sbit key3=P3^4;
  10. sbit key4=P3^5;

  11. sbit LS1=P2^0; //位选
  12. sbit LS2=P2^1;
  13. sbit LS3=P2^2;
  14. sbit LS4=P2^3;
  15. sbit LS5=P2^4;
  16. sbit LS6=P2^5;

  17. void LcdDisplay(int);
  18. void DigDisplay();
  19. void chaomax();
  20. void chaomin();
  21. void guomax();
  22. void guomin();
  23. void key();

  24. //存储上下限变量
  25. int tempmax=30, tempmin=20, tempt, tempmode=0;
  26. int jileimax=0, jileimin=0, jileimid=0;

  27. void Delay1ms(unsigned char x)
  28. {
  29.         unsigned char i,j;
  30.         for(i=0;i<x;i++)
  31.             for(j=0;j<112;j++);
  32. }
  33. uchar  Ds18b20Init()
  34. {
  35.         uchar i;
  36.         DSPORT = 0;                         //将总线拉低480us~960us
  37.         //i = 70;        
  38.         i = 900;        
  39.         while(i--);//延时642us
  40.         DSPORT = 1;                        //然后拉高总线,如果DS18B20做出反应会将在15us~60us后总线拉低
  41.         i = 0;
  42.         while(DSPORT)        //等待DS18B20拉低总线
  43.         {
  44.                 Delay1ms(1);
  45.                 i++;
  46.                 if(i>5)//等待>5MS
  47.                 {
  48.                         return 0;//初始化失败
  49.                 }
  50.                
  51.         }
  52.         return 1;//初始化成功
  53. }
  54. void Ds18b20WriteByte(uchar dat)//????????
  55. {
  56.         uint i, j;
  57.         for(j=0; j<8; j++)
  58.         {
  59.                 DSPORT = 0;                       //每写入一位数据之前先把总线拉低1us     
  60.                 i++;
  61.                 DSPORT = dat & 0x01;  //然后写入一个数据,从最低位开始
  62.                 i=8;
  63.                 while(i--); //延时8us        
  64.                 DSPORT = 1;        //然后释放总线,至少1us给总线恢复时间才能接着写入第二个数值
  65.                 dat >>= 1;
  66.         }
  67. }
  68. uchar Ds18b20ReadByte()
  69. {
  70.         uchar byte, bi;
  71.         uint i, j;        
  72.         for(j=8; j>0; j--)
  73.         {
  74.                 DSPORT = 0; //先将总线拉低1us
  75.                 i++;
  76.                 DSPORT = 1; //然后释放总线
  77.                 i++;
  78.                 i++; //延时2us等待数据稳定
  79.                 bi = DSPORT;         //读取数据,从最低位开始读取
  80.                 /*将byte左移一位,然后或上右移7位后的bi,注意移动之后移掉那位补0。*/
  81.                 byte = (byte >> 1) | (bi << 7);                                                  
  82.                 i = 4;                //读取完之后等待4us再接着读取下一个数
  83.                 while(i--);
  84.         }                                
  85.         return byte;
  86. }
  87. void  Ds18b20ChangTemp()
  88. {
  89.         Ds18b20Init();
  90.         Delay1ms(1);
  91.         Ds18b20WriteByte(0xcc);    //跳过ROM操作命令                 
  92.         Ds18b20WriteByte(0x44);    //温度转换命令
  93.         Delay1ms(20);         //等待转换成功,而如果你是一直刷着的话,就不用这个延时了
  94. }
  95. void  Ds18b20ReadTempCom()
  96. {        
  97.         Ds18b20Init();
  98.         Delay1ms(1);
  99.         Ds18b20WriteByte(0xcc);         //跳过ROM操作命令
  100.         Ds18b20WriteByte(0xbe);         //发送读取温度命令
  101. }
  102. int Ds18b20ReadTemp()
  103. {
  104.         int temp = 0;
  105.         uchar tmh, tml;
  106.         Ds18b20ChangTemp();                     //先写入转换命令
  107.         Ds18b20ReadTempCom();                   //然后等待转换完后发送读取温度命令
  108.         tml = Ds18b20ReadByte();                //读取温度值共16位,先读低字节
  109.         tmh = Ds18b20ReadByte();                //再读高字节
  110.         temp = tmh;
  111.         temp <<= 8;
  112.         temp |= tml;
  113.         return temp;
  114. }
  115. void LcdDisplay(int temp)          //lcd显示???
  116. {

  117.         float tp;  
  118.         if(temp< 0)                                //当温度值为负数
  119.         {
  120.                 DisplayData[0] = 0x40;
  121.                 //因为读取的温度是实际温度的补码,所以减1,再取反求出原码
  122.                 temp=temp-1;
  123.                 temp=~temp;
  124.                 tp=temp;
  125.                 temp=tp*0.0625*100+0.5;        
  126.                 //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
  127.                 //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
  128.                 //算加上0.5,还是在小数点后面。
  129.         }
  130.         else
  131.         {                        
  132.                 DisplayData[0] = 0x00;
  133.                 tp=temp;//因为数据处理有小数点所以将温度赋给一个浮点型变量
  134.                 //如果温度是正的那么,那么正数的原码就是补码它本身
  135.                 temp=tp*0.0625*100+0.5;        
  136.                 //留两个小数点就*100,+0.5是四舍五入,因为C语言浮点数转换为整型的时候把小数点
  137.                 //后面的数自动去掉,不管是否大于0.5,而+0.5之后大于0.5的就是进1了,小于0.5的就
  138.                 //算加上0.5,还是在小数点后面。
  139.         }
  140.         DisplayData[0] = DIG_CODE[tempmax / 10];
  141.         DisplayData[1] = DIG_CODE[tempmax % 10];
  142.         DisplayData[4] = DIG_CODE[tempmin / 10];
  143.         DisplayData[5] = DIG_CODE[tempmin % 10];
  144.         DisplayData[2] = DIG_CODE[temp % 10000 / 1000];
  145.         DisplayData[3] = DIG_CODE[temp % 1000/100];
  146.         DigDisplay();   
  147. }                                        //扫描显示
  148. void DigDisplay()
  149. {
  150.         unsigned char i;
  151.         unsigned int j;
  152.         for(i=0;i<6;i++)
  153.         {
  154.                 switch(i)         //位选,选择点亮的数码管
  155.                 {
  156.                        
  157.                         case(0):
  158.                         LS1=0;LS2=1;LS3=1;LS4=1;LS5=1;LS6=1; break;//显示第0位
  159.                         case(1):
  160.                         LS1=1;LS2=0;LS3=1;LS4=1;LS5=1;LS6=1; break;//显示第1位
  161.                        
  162.                         case(2):
  163.                         LS1=1;LS2=1;LS3=0;LS4=1;LS5=1;LS6=1; break;//显示第2位
  164.                         case(3):
  165.                         LS1=1;LS2=1;LS3=1;LS4=0;LS5=1;LS6=1; break;//显示第3位
  166.                        
  167.                         case(4):
  168.                         LS1=1;LS2=1;LS3=1;LS4=1;LS5=0;LS6=1; break;//显示第4位
  169.                         case(5):
  170.                         LS1=1;LS2=1;LS3=1;LS4=1;LS5=1;LS6=0; break;//显示第5位
  171.                 }
  172.                 GPIO_DIG=DisplayData[i]; //发送段码
  173.                 j=200;                                         //扫描间隔时间设定
  174.                 while(j--);        
  175.                 GPIO_DIG=0x00; //消隐
  176.         }
  177. }

  178. void main()
  179. {
  180.         buzzer=1;
  181.        
  182.         while(1)
  183.         {
  184.                 LcdDisplay(Ds18b20ReadTemp());
  185.                 key();
  186.         }
  187. }
  188. //#include"temp.h"

  189. void chaomax()
  190. {
  191.         buzzer=1;
  192.        
  193. }

  194. void key()
  195. {

  196.         if(key1==0)
  197.         {
  198.                 Delay1ms(300);//按键防抖
  199.                 if(key1==0)
  200.                 {
  201.                         tempmax++;
  202.                 }
  203.         }
  204.         if(key2==0)
  205.         {
  206.                 Delay1ms(300);
  207.                 if(key2==0)
  208.                 {
  209.                         tempmax--;
  210.                 }                        
  211.         }
  212.         if(key3==0)
  213.         {
  214.                 Delay1ms(300);
  215.                 if(key3==0)
  216.                 {
  217.                         tempmin++;
  218.                 }        
  219.         }
  220.         if(key4==0)
  221.         {
  222.                 Delay1ms(300);
  223.                 if(key4==0)
  224.                 {
  225.                         tempmin--;
  226.                 }
  227.         }

  228.         if(temp<(tempmin*100))//*100是为了使当前值和最小值在同一数量级比较
  229.         {
  230.                 jileimin++;
  231.                 jileimid=0;               
  232.                 if(jileimin>3)//扫描3次以确定已经低于下限值
  233.                 {
  234.                         tempmode=1;        //区分当前值是否是原值还是恢复值,原值为0,由上限值恢复是2,由下限值恢复时1
  235.                         chaomin();
  236.                         jileimin=0;//积累值置位
  237.                 }
  238.         }
  239.         if(temp>(tempmax*100))
  240.         {
  241.                 jileimax++;
  242.                 jileimid=0;
  243.                 if(jileimax>3)
  244.                 {
  245.                         tempmode=2;         
  246.                         chaomax();
  247.                         jileimax=0;
  248.                 }
  249.         }

  250.         if((tempmin*100)<temp&&temp<(tempmax*100))
  251.         {
  252.                 jileimax=0;
  253.                 jileimin=0;
  254.                 jileimid++;
  255.                 if(jileimid>3)
  256.                 {
  257.                         switch(tempmode)
  258.                         {
  259.                         case 0:buzzer=0;break;
  260.                         case 1:guomin();break;
  261.                         case 2:guomax();break;                 
  262.                         }               
  263.                         tempmode=0;
  264.                         buzzer=0;
  265.                         jileimid=0;
  266.                 }
  267.         }
  268. }
  269. //三声短鸣表示低于温度下限
  270. void chaomin()
  271. {
  272.         buzzer=1;
  273.         Delay1ms(200);
  274.         buzzer=0;
  275.         Delay1ms(200);
  276.         buzzer=1;
  277.         Delay1ms(200);
  278.         buzzer=0;
  279.         Delay1ms(200);
  280.         buzzer=1;
  281.         Delay1ms(200);
  282.         buzzer=0;
  283.         Delay1ms(1000);
  284. }

  285. //三声短鸣表示超过温度上限               
  286. void guomax()
  287. {
  288.         buzzer=0;
  289.         Delay1ms(200);
  290.         buzzer=1;
  291.         Delay1ms(200);
  292.         buzzer=0;
  293.         Delay1ms(200);
  294.         buzzer=1;
  295.         Delay1ms(200);
  296.         buzzer=0;
  297.         Delay1ms(200);
  298.         buzzer=1;
  299.         Delay1ms(200);
  300.         buzzer=0;
  301.         Delay1ms(200);
  302. }
  303. //2秒长鸣表示超过温度下限               
  304. void guomin()
  305. {
  306.         buzzer=1;
  307.         Delay1ms(2000);
  308.         buzzer=0;
  309. }

复制代码
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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