找回密码
 立即注册

QQ登录

只需一步,快速开始

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

基于单片机+TLC2543的热敏电阻测温源程序与Proteus仿真

[复制链接]
跳转到指定楼层
楼主
基于单片机的热敏电阻测温设计完整版包含文档、硬件电路搭建以及仿真结果。

元件清单:
P1    LCD1602液晶显示屏+16P插座
PR1    103排阻
Q1    8550三极管
R1, R5    10K电阻
R2, R6, R7    1K电阻
R3    热敏电阻 带线+2P白色插头
R4    电位器
SW1    自锁开关
U1    TLC2543芯片+DIP20插座
U2    STC89C52单片机+DIP40插座
Y1    12M晶振
   
9*15板子   
锡丝   
导线   
USB供电线或者电池盒   
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)

Altium Designer画的原理图和PCB图如下:(51hei附件中可下载工程文件)


单片机源程序如下:
  1. #include<reg52.h>                                 //头文件
  2. #include<intrins.h>
  3. #include"eeprom52.h"
  4. #include "math.h"
  5. #define uchar unsigned char                 //宏定义
  6. #define uint unsigned int
  7. #define LCD1602_dat P0


  8. sbit LCD1602_rs=P2^5;//IO 定义
  9. sbit LCD1602_rw=P2^6;
  10. sbit LCD1602_e=P2^7;
  11. sbit beep=P2^0;
  12. sbit led_1=P1^5;
  13. sbit led_2=P1^6;

  14. sbit key_1=P3^5;
  15. sbit key_2=P3^6;
  16. sbit key_3=P3^7;


  17. sbit TCL2543_EOC  = P1^0;
  18. sbit TCL2543_CLK  = P1^1;
  19. sbit TCL2543_ADIN = P1^2;
  20. sbit TCL2543_DOUT = P1^3;
  21. sbit TCL2543_CS   = P1^4;


  22. float zhi;
  23. int shu;

  24. char temp,temp_h,temp_l;
  25. uchar state,ms;

  26. bit s1,beep1;

  27. void delay(uint T)
  28. {
  29.         while(T--);
  30. }

  31. // 其中 port 为通道: 通道0:port = 0x01 通道1:port = 0x02 通道2:port = 0x04 ...
  32. uint read2543(unsigned char port)  
  33. {  
  34.         unsigned int i;   
  35.         uint ad_value=0;  
  36.         TCL2543_CLK=0;   
  37.         TCL2543_CS=0;   
  38.         TCL2543_EOC=1;   
  39.         port<<=4;   
  40.         for(i=0;i<12;i++)   
  41.         {  
  42.                 if(TCL2543_DOUT) ad_value|=0x01;   
  43.                 TCL2543_ADIN=(bit)(port&0x80);   
  44.                 TCL2543_CLK=1;   
  45.                 _nop_();  
  46.                 _nop_();   
  47.                 _nop_();  
  48.                 TCL2543_CLK=0;   
  49.                 _nop_();   
  50.                 _nop_();  
  51.                 _nop_();  
  52.                 port=port<<1;  
  53.                 ad_value=ad_value<<1;   
  54.         }
  55.         TCL2543_CS=1;   
  56.         ad_value=ad_value>>1;   
  57.         return ad_value;  
  58. }

  59. void LCD1602_write(uchar order,dat)                                  //1602 一个字节  处理
  60. {
  61.     LCD1602_e=0;
  62.     LCD1602_rs=order;
  63.     LCD1602_dat=dat;
  64.     LCD1602_rw=0;
  65.     LCD1602_e=1;
  66.     delay(1);
  67.     LCD1602_e=0;                                                                                                                                                                                                     
  68. }

  69. void LCD1602_writebyte(uchar *prointer)                                   //1602 字符串    处理
  70. {
  71.     while(*prointer!='\0')
  72.     {
  73.         LCD1602_write(1,*prointer);
  74.         prointer++;
  75.     }
  76. }

  77. void LCD1602_cls()                                                                         //1602 初始化
  78. {
  79.         LCD1602_write(0,0x01);     //1602 清屏 指令
  80.         delay(1500);
  81.         LCD1602_write(0,0x38);     // 功能设置 8位、5*7点阵
  82.         delay(1500);
  83.         LCD1602_write(0,0x0c);     //设置 光标   不显示开关、不显示光标、字符不闪烁
  84.         LCD1602_write(0,0x06);
  85.         LCD1602_write(0,0xd0);
  86.         delay(1500);
  87. }


  88. void show()                        //显示数据
  89. {

  90.                 LCD1602_write(0,0x80);
  91.                 LCD1602_writebyte("Temp:");
  92.                 if(temp>=0)
  93.                 {
  94.                            if(temp>99)LCD1602_write(1,0x30+temp/100%10);
  95.                         else LCD1602_writebyte(" ");
  96.                         if(temp>9)LCD1602_write(1,0x30+temp/10%10);
  97.                         else LCD1602_writebyte(" ");
  98.                         LCD1602_write(1,0x30+temp%10);
  99.                 }else
  100.                 {
  101.                         LCD1602_writebyte("-");
  102.                         if(temp*-1>9)LCD1602_write(1,0x30+(temp*-1)/10%10);
  103.                         else LCD1602_writebyte(" ");
  104.                         LCD1602_write(1,0x30+(temp*-1)%10);        
  105.                 }
  106.                 LCD1602_write(1,0xdf);
  107.                 LCD1602_writebyte("C   ");



  108.                  LCD1602_write(0,0xC0);
  109.                 LCD1602_writebyte("H:");
  110.                 if(state==1&&s1==1)
  111.                 {
  112.                         LCD1602_writebyte("   ");
  113.                 }else
  114.                 {


  115.                         if(temp_h>=0)
  116.                         {
  117.                                    if(temp_h>99)LCD1602_write(1,0x30+temp_h/100%10);
  118.                                 else LCD1602_writebyte(" ");
  119.                                 if(temp_h>9)LCD1602_write(1,0x30+temp_h/10%10);
  120.                                 else LCD1602_writebyte(" ");
  121.                                 LCD1602_write(1,0x30+temp_h%10);
  122.                         }else
  123.                         {
  124.                                 LCD1602_writebyte("-");
  125.                                 if(temp_h*-1>9)LCD1602_write(1,0x30+(temp_h*-1)/10%10);
  126.                                 else LCD1602_writebyte(" ");
  127.                                 LCD1602_write(1,0x30+(temp_h*-1)%10);        
  128.                         }
  129.                 }
  130.                 LCD1602_write(1,0xdf);
  131.                 LCD1602_writebyte("C L:");
  132.                 if(state==2&&s1==1)
  133.                 {
  134.                         LCD1602_writebyte("   ");
  135.                 }else
  136.                 {
  137. //                        LCD1602_write(1,0x30+temp_l/10%10);
  138. //                        LCD1602_write(1,0x30+temp_l%10);

  139.                         if(temp_l>=0)
  140.                         {
  141.                                    if(temp_l>99)LCD1602_write(1,0x30+temp_l/100%10);
  142.                                 else LCD1602_writebyte(" ");
  143.                                 if(temp_l>9)LCD1602_write(1,0x30+temp_l/10%10);
  144.                                 else LCD1602_writebyte(" ");
  145.                                 LCD1602_write(1,0x30+temp_l%10);
  146.                         }else
  147.                         {
  148.                                 LCD1602_writebyte("-");
  149.                                 if(temp_l*-1>9)LCD1602_write(1,0x30+(temp_l*-1)/10%10);
  150.                                 else LCD1602_writebyte(" ");
  151.                                 LCD1602_write(1,0x30+(temp_l*-1)%10);        
  152.                         }
  153.                 }
  154.                 LCD1602_write(1,0xdf);
  155.                 LCD1602_writebyte("C");
  156.         
  157. }


  158. void proc()
  159. {
  160.         if(temp>temp_h)
  161.         {
  162.                 led_1=0;         
  163.         }else
  164.         {
  165.                 led_1=1;         
  166.         }
  167.         if(temp<temp_l)
  168.         {
  169.                 led_2=0;
  170.         }else
  171.         {
  172.                 led_2=1;        
  173.         }

  174.         if(temp>temp_h||temp<temp_l)
  175.         {
  176.                 beep1=1;
  177.         }else
  178.         {
  179.                 beep1=0;
  180.         }
  181. }


  182. void key()
  183. {

  184.         if(!key_1)
  185.         {
  186.                 delay(888);
  187.                 if(!key_1)
  188.                 {
  189.                         state=(state+1)%3;
  190.                         while(!key_1);
  191.                 }
  192.         }
  193.         if(state!=0)
  194.         {
  195.                 if(!key_2)
  196.                 {
  197.                         delay(888);
  198.                         if(!key_2)
  199.                         {
  200.                                 while(!key_2) show();
  201.                                 switch(state)
  202.                                 {
  203.                                         case 1:
  204.                                         if(temp_h<99)temp_h++;
  205.                                         SectorErase(0x2000);         //保存上限值
  206.                                         byte_write(0x2000,temp_h);
  207.                                         break;
  208.                                         case 2:
  209.                                         if(temp_h>temp_l+1)temp_l++;        
  210.                                         SectorErase(0x2200);         //保存上限值
  211.                                         byte_write(0x2200,temp_l);
  212.                                         break;
  213.                                 }
  214.                         }
  215.                 }
  216.                 if(!key_3)
  217.                 {
  218.                         delay(888);
  219.                         if(!key_3)
  220.                         {
  221.                                 while(!key_3) show();
  222.                                 switch(state)
  223.                                 {
  224.                                         case 1:
  225.                                         if(temp_h>temp_l+1)temp_h--;
  226.                                         SectorErase(0x2000);         //保存上限值
  227.                                         byte_write(0x2000,temp_h);
  228.                                         break;
  229.                                         case 2:
  230.                                         if(temp_l>-40)temp_l--;
  231.                                         SectorErase(0x2200);         //保存上限值
  232.                                         byte_write(0x2200,temp_l);
  233.                                         break;

  234.                                 }
  235.                         }
  236.                 }
  237.         }
  238. }

  239. float TempCalculate(float Rx,float B,float Revise,float BasicRx)
  240. {
  241. /*

  242. Rx:  热敏电阻当前阻值
  243. B:   热敏电阻参数B值
  244. Revise:  校正温度
  245. BasicRx:  热敏电阻25度时电阻(标称电阻数值)


  246. 返回: 摄氏度

  247. */  


  248.     Rx = Rx / BasicRx;
  249.    
  250.     Rx = log(Rx);
  251.    
  252.     Rx = (Rx) / B;
  253.    
  254.     Rx = Rx + 0.003356;
  255.     Rx = 1 / Rx;
  256.     Rx = Rx - 273.13;  
  257.     Rx = Rx + Revise;
  258.    
  259.    
  260.     return Rx;
  261.   
  262. }

  263. void main()
  264. {
  265.         float Rad;
  266.         LCD1602_cls();
  267.         TMOD=0x01;
  268.         TH0=0x4c;
  269.         TL0=0x00;
  270.         ET0=1;
  271.         TR0=1;
  272. ……………………

  273. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:

1.png (18.42 KB, 下载次数: 89)

1.png

2.png (50.38 KB, 下载次数: 93)

2.png

4.png (30.36 KB, 下载次数: 121)

4.png

热敏电阻.7z

2.86 MB, 下载次数: 98, 下载积分: 黑币 -5

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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