温度传感器检测温度报警装置

该项目有四项要求:

(1)检测温度

(2)检测温度超出限定温度,启动报警装置

(3)报警装置启动时,跑马灯进行工作,闪烁起来

(4)利用串口,实时传输温度数据

下面是源代码,运行是完全没有问题的,若读者有更佳的方式,希望可以交流一下.

delay.c

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /*  
  2. *参数 :t(0 - 255)  
  3. *延时时间 :约2 * t + 12  
  4. */  
  5.   
  6. void delay_us(unsigned char t)  
  7. {  
  8.     while(--t);  
  9. }  
  10.   
  11. void delay_ms(unsigned char t)  
  12. {  
  13.     while(t--)  
  14.     {  
  15.         delay_us(245);  
  16.     delay_us(245);  
  17.     }  
  18. }  
  19.   
  20. // void delay_s(unsigned char t)  
  21. // {  
  22. //     while(t--)  
  23. //     {  
  24. //     delay_ms(200);     
  25. //     delay_ms(200);  
  26. //         delay_ms(200);  
  27. //         delay_ms(200);  
  28. //         delay_ms(200);  
  29. //     }  
  30. // }  

delay.h

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #ifndef _DELAY_H_  
  2. #define _DELAY_H_  
  3.   
  4. extern void delay_us(unsigned char t);  
  5. extern void delay_ms(unsigned char t);  
  6. // extern void delay_s(unsigned char t);  
  7.   
  8. #endif  

lcd.c

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include "./lcd/lcd.h"  
  2.   
  3. #define LCDPORT P0  
  4. #define LCD_WRITE_DATA 1  
  5. #define LCD_WRITE_COM 0  
  6.   
  7. sbit RS = P2^4;  
  8. sbit RW = P2^5;  
  9. sbit E  = P2^6;  
  10.   
  11. void lcd_init()  
  12. {  
  13.       delay_ms(15);  
  14.       lcd1602_write(0x38,LCD_WRITE_COM); //设置8位数据接口,两行显示,5x7点阵字符  
  15.       delay_ms(5);  
  16.       lcd1602_write(0x38,LCD_WRITE_COM); //设置8位数据接口,两行显示,5x7点阵字符  
  17.       delay_ms(5);  
  18.       lcd1602_write(0x38,LCD_WRITE_COM); //设置8位数据接口,两行显示,5x7点阵字符  
  19.       delay_ms(5);  
  20.       lcd1602_write(0x38,LCD_WRITE_COM); //设置8位数据接口,两行显示,5x7点阵字符  
  21.       delay_ms(5);  
  22.       lcd1602_write(0x08,LCD_WRITE_COM); //关闭显示,关闭光标和闪烁  
  23.       delay_ms(5);  
  24.       lcd1602_write(0x01,LCD_WRITE_COM); //清屏  
  25.       delay_ms(5);  
  26.       lcd1602_write(0x06,LCD_WRITE_COM); //设置指针方式,画面不平移  
  27.       delay_ms(5);  
  28.       lcd1602_write(0x0c,LCD_WRITE_COM);  //设置指针方式,画面不平移  
  29.       delay_ms(5);  
  30. }  
  31.   
  32. void lcd1602_write(unsigned char byte,unsigned char flag)  
  33. {  
  34.       if(flag)  
  35.       {  
  36.           RS = 1;  
  37.       }  
  38.       else  
  39.       {  
  40.           RS = 0;    //选择输入数据为数据  
  41.       }  
  42.       RW = 0;    //写  
  43.       E = 1;     //选中LCD  
  44.       LCDPORT = byte;  
  45.       delay_us(5);  
  46.           E = 0;     //失能LCD  
  47. }  
  48.   
  49. void lcd_dis_char(unsigned char x, unsigned char y, unsigned char byte)  
  50. {  
  51.     if((x > 15) || (y > 1))  
  52.     {  
  53.          return ;  
  54.     }  
  55.     if(0 == y)  
  56.     {  
  57.      lcd1602_write(0x80 + x,LCD_WRITE_COM);  
  58.     }  
  59.     else  
  60.     {  
  61.      lcd1602_write(0x80 + 0x40 + x,LCD_WRITE_COM);  
  62.     }  
  63.      lcd1602_write(byte,LCD_WRITE_DATA);  
  64. }  
  65.   
  66. /*显示字符串*/  
  67. void lcd1602_dis_str(unsigned char x, unsigned char y, unsigned char *str)  
  68. {  
  69.      if((x > 15) || (y > 1))  
  70.      {  
  71.        return ;  
  72.      }  
  73.      if(0 == y)  
  74.      {  
  75.        lcd1602_write(0x80 + x,LCD_WRITE_COM);  
  76.      }  
  77.      else  
  78.      {  
  79.        lcd1602_write(0x80 + 0x40 + x,LCD_WRITE_COM);  
  80.      }  
  81.      while(*str != '\0')  
  82.      {  
  83.            lcd1602_write(*str,LCD_WRITE_DATA);  
  84.        str++;  
  85.      }  
  86. }  
  87.   
  88. void lcd_dis_self()  
  89. {  
  90.     lcd1602_write(0x40,LCD_WRITE_COM);  
  91.     lcd1602_write(0x07,LCD_WRITE_DATA);  
  92.     lcd1602_write(0x05,LCD_WRITE_DATA);  
  93.     lcd1602_write(0x07,LCD_WRITE_DATA);    
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. lcd1602_write(0x0,LCD_WRITE_DATA);  
  2. lcd1602_write(0x0,LCD_WRITE_DATA);  
  3. lcd1602_write(0x0,LCD_WRITE_DATA);  
  4. lcd1602_write(0x0,LCD_WRITE_DATA);  
  5. lcd1602_write(0x0,LCD_WRITE_DATA);  
  6.   
  7. lcd1602_write(0x80 + 0x40 + 13,LCD_WRITE_COM);  
  8. lcd1602_write(0x0,LCD_WRITE_DATA);    


lcd.h

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <span style="font-size:18px;"><strong>#ifndef __LCD1602_H__  
  2. #define __LCD1602_H__  
  3.   
  4. #include "./delay/delay.h"  
  5. #include <reg52.h>  
  6.   
  7. extern void lcd_init();  
  8. extern void lcd1602_write(unsigned char byte,unsigned char flag);  
  9. extern void lcd_dis_char(unsigned char x, unsigned char y, unsigned char byte);  
  10. extern void lcd1602_dis_str(unsigned char x, unsigned char y, unsigned char *str);  
  11. extern void lcd_dis_self();  
  12.   
  13. #endif</strong></span>  


uart.c

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <reg52.h>  
  2. #include <stdio.h>  
  3. #include "./delay/delay.h"  
  4.   
  5. void uart_init()  
  6. {  
  7.     SCON = 0x50;       
  8.     TMOD |= 0x20;      
  9.     TH1 = 0xfd;         
  10.     TR1 = 1;           
  11. }  

uart.h

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #ifndef _UART_H_  
  2. #define _UART_H_  
  3.   
  4. extern void uart_init();  
  5. // extern void uart_send_byte(unsigned char byte);  
  6. // extern void uart_send_str(unsigned char *s);  
  7.   
  8. #endif  


主函数:ds18b20.c

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. #include <reg52.h>  
  2. #include <intrins.h>  
  3. #include <stdio.h>  
  4. #include "./lcd/lcd.h"  
  5. #include "./delay/delay.h"  
  6. #include "./uart/uart.h"  
  7.   
  8. #define LEDPORT P1  
  9.   
  10. sbit ds = P2^3;  
  11. sbit beep = P2^7;  
  12. bit ack = 0;  
  13.   
  14. void ds_reset()  
  15. {  
  16.       ds = 1;       //总线拉高  
  17.       ds = 0;           
  18.       delay_us(200);  
  19.       delay_us(200);  
  20.       ds = 1;           //释放总线  
  21.       delay_us(30);  
  22.       if(0 == ds)       //检测是否被拉低  
  23.       {  
  24.           ack = 1;  
  25.       }  
  26.       else  
  27.       {  
  28.           ack = 0;  
  29.       }  
  30.       delay_us(200);  
  31.       delay_us(100);  
  32. }  
  33.   
  34. void ds_send_byte(unsigned char byte)  
  35. {  
  36.       unsigned char i;  
  37.       for(i = 0; i < 8; i++)  
  38.       {  
  39.         ds = 0;  
  40.             _nop_();  
  41.             _nop_();   //1us  
  42.         ds = byte & 0x01;     //最低位开始赋值  
  43.         byte >>= 1;  
  44.         delay_us(30);  
  45.         ds = 1;  
  46.       }  
  47.       delay_us(30);  
  48. }  
  49.   
  50. bit ds_read_bit()  
  51. {  
  52.       bit tmp;  
  53.       ds = 1;  
  54.       ds = 0;  
  55.       _nop_();  
  56.       _nop_();  
  57.       ds = 1;  
  58.       tmp = ds;  
  59.       delay_us(30);  
  60.       return tmp;  
  61. }  
  62.   
  63. unsigned char ds_read_byte()  
  64. {  
  65.       unsigned char i,j,k;  
  66.       for(i = 0; i < 8; i++)  
  67.       {  
  68.             j = ds_read_bit();  
  69.               
  70.             k = (j << 7) | (k >> 1);  
  71.        }  
  72.        return k;  
  73. }  
  74.   
  75. void main()  
  76. {  
  77.       unsigned char a;  
  78.       unsigned int b;  
  79.       unsigned char i;  
  80.       unsigned int temp;  
  81.       unsigned char num = 0x7f;  
  82.       unsigned char disbuf[20];  
  83.       float wendu;  
  84.       
  85.       lcd_init();  
  86.       uart_init();    
  87.       
  88.       while(1)  
  89.       {                       
  90.           ds_reset();  
  91.           ds_send_byte(0xcc);  
  92.           ds_send_byte(0x44);  
  93.               
  94.           ds_reset();  
  95.           ds_send_byte(0xcc);  
  96.           ds_send_byte(0xbe);       
  97.           a = ds_read_byte();  
  98.           b = ds_read_byte();  
  99.           temp = (b << 8) | a;  
  100.           wendu = (float)temp * 0.0625;  
  101.           sprintf(disbuf,"%6.2f",wendu);  
  102.           lcd1602_dis_str(4,0,"WELCOME!");  
  103.           lcd1602_dis_str(1,1,"wendu:");  
  104.           lcd1602_dis_str(7,1,disbuf);  
  105.           lcd_dis_self();                                                                                                                                     
  106.           lcd_dis_char(14,1,'C');         
  107.                   
  108.           if(wendu > 30)  
  109.           {  
  110.             LEDPORT = _crol_(num,1);  
  111.             num = LEDPORT;  
  112.             delay_us(50);  
  113.                       
  114.             for(i = 0; i < 100; i++)  
  115.             {  
  116.                          beep = ~beep;  
  117.              delay_us(100);  
  118.                     }         
  119.            }  
  120.                    
  121.             TI = 1;    //发送中断标志位  
  122.             printf("temperature is : %f\n",wendu);        
  123.        }  
  124. }  
  • 1
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
温度传感器是一种可以测量环境温度的装置,而在arduino中,我们可以通过使用温度传感器的例程来实现实时监测和记录环境温度的功能。 在arduino中,我们可以使用一些常见的温度传感器模块,比如DS18B20、DHT11或者LM35。要使用这些传感器,我们需要将其连接到arduino的引脚上,并在程序中引用相应的库文件。 比如,使用DS18B20温度传感器,我们需要引入OneWire库和DallasTemperature库,然后在程序中编写相应的代码来初始化传感器和读取温度数值。而如果使用DHT11传感器,则需要引入DHT库,并编写相应的代码来初始化传感器和读取温度及湿度数值。 一旦传感器连接和库文件引入完成,我们就可以通过arduino的串行监视器或者LCD屏幕来实时显示环境温度数值了。同时,我们还可以通过SD卡模块将温度数据保存到SD卡上,或者通过无线模块将数据发送到云端进行远程监测和记录。 除了简单的温度读取,一些温度传感器的例程还包括了温度补偿、温度校准、温度报警等功能。通过在例程中添加一些控制语句和判断条件,我们可以使arduino在检测到环境温度超出设定范围时发出报警,或者自动进行温度校准和补偿,以确保温度测量的准确性。 总之,温度传感器的arduino例程可以很方便地实现环境温度的监测和记录,并可以通过一些附加功能使温度传感器更加智能和多功能化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值