找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2929|回复: 1
收起左侧

STM32单片机DS18B20源程序与Proteus仿真 元件清单等

  [复制链接]
ID:918075 发表于 2021-5-9 15:19 | 显示全部楼层 |阅读模式
利用protues仿真温度采集系统,可以显示温度

元件清单:
序号    元器件    型号    标志
1    LCD1602液晶    LCD1602    LCD1
2    电阻    10K    R1
3    STM32核心板    STM32_CORE    U1
4    DS18B20温度芯片    DS18B20    U2
5    电位器    10k    RP1
5    六脚自锁开关    /    U3
6    覆铜板或万用板    /    /
7    普通USB线_大头    /    /
8    导线(只万用板有)    /    /
9    焊锡    /    /

仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
51hei.png 51hei.png

电路原理图如下:
51hei.png

单片机源程序如下:
  1. #include "stm32f10x.h"
  2. #include "bsp-lcd1602.h"
  3. #include "delay.h"
  4. #include "sys.h"

  5. #include "ds18b20.h"
  6. #include <stdio.h>
  7. #include "math.h"


  8. short tem,tem1;
  9. u8 a,b,c,t;
  10. #ifdef __GNUC__
  11. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  12. #else
  13. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  14. #endif


  15. void Delay_DS18B20(int num)
  16. {
  17.         while(num--) ;
  18. }


  19. int main(void)
  20.        
  21. {

  22.         SystemCoreClockUpdate ();
  23.         LCD1602_Init();
  24.         LCD1602_ShowStr(2,0,"tempe=000.0C",13);
  25.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  26.         GPIO_InitTypeDef g;
  27.         g.GPIO_Mode = GPIO_Mode_Out_PP;
  28.         g.GPIO_Speed = GPIO_Speed_10MHz;
  29.         g.GPIO_Pin = GPIO_Pin_8  ;
  30.         GPIO_Init(GPIOB,&g);
  31.         GPIO_SetBits(GPIOB,GPIO_Pin_8);    //输出1

  32.    t=DS18B20_DQ_IN;
  33. while(t){
  34.                 DS18B20_IO_OUT();         //SET PG11 OUTPUT

  35.     DS18B20_DQ_OUT=0;         //拉低DQ
  36.           Delay_DS18B20(750);             //拉低750us
  37.     DS18B20_DQ_OUT=1;         //DQ=1
  38.           Delay_DS18B20(15);             //15US
  39.                 DS18B20_IO_IN();        //SET PG11 INPUT         
  40.     Delay_DS18B20(100);
  41.                 t=DS18B20_DQ_IN;
  42. //                 LCD_ShowNum(13,0,t);
  43. }
  44. tem=DS18B20_Get_Temp();
  45.                 Delay_DS18B20(1000000);

  46. while(1)
  47.         {       
  48.     tem=DS18B20_Get_Temp();
  49.                 if(        tem>0)
  50.                 {        a=tem/100;
  51.                 b=tem%100/10;
  52.                 c=tem%10;
  53.                 LCD_ShowChar(8,0,'+');
  54.         LCD_ShowNum(9,0,a);       
  55.         LCD_ShowNum(10,0,b);
  56.         LCD_ShowNum(12,0,c);
  57.                           
  58.                 }
  59.         else
  60.         {       
  61. //                tem1=(int)tem;
  62. //                tem1=abs(tem1);
  63.                 tem1=-(tem);
  64.                 tem1=tem1+1;
  65.                 a=tem1/100;
  66.                 b=tem1%100/10;
  67.                 c=tem1%10;
  68.         LCD_ShowChar(8,0,'-');
  69.         LCD_ShowNum(9,0,a);       
  70.         LCD_ShowNum(10,0,b);
  71.         LCD_ShowNum(12,0,c);
  72.                
  73.         }
  74.         Delay_DS18B20(100000);
  75.   }



  76. }
复制代码
  1. #include "ds18b20.h"
  2. #include "delay.h"       

  3.   



  4. void Delay_DS18B20_1us(int num)
  5. {
  6.         while(num--) ;
  7. }


  8. //复位DS18B20
  9. void DS18B20_Rst(void)          
  10. {                 
  11.         DS18B20_IO_OUT();         //SET PG11 OUTPUT
  12.     DS18B20_DQ_OUT=0;         //拉低DQ
  13.   Delay_DS18B20_1us(750);            //拉低750us
  14.     DS18B20_DQ_OUT=1;         //DQ=1
  15. Delay_DS18B20_1us(15);            //15US
  16. }
  17. //等待DS18B20的回应
  18. //返回1:未检测到DS18B20的存在
  19. //返回0:存在
  20. u8 DS18B20_Check(void)           
  21. {   
  22.         u8 retry=0;
  23.         DS18B20_IO_IN();        //SET PG11 INPUT         
  24.     while (DS18B20_DQ_IN&&retry<200)
  25.         {
  26.                 retry++;
  27.                 Delay_DS18B20_1us(1);
  28.         };         
  29.         if(retry>=200)return 1;
  30.         else retry=0;
  31.     while (!DS18B20_DQ_IN&&retry<240)
  32.         {
  33.                 retry++;
  34.                 Delay_DS18B20_1us(1);
  35.         };
  36.         if(retry>=240)return 1;            
  37.         return 0;
  38. }
  39. //从DS18B20读取一个位
  40. //返回值:1/0
  41. u8 DS18B20_Read_Bit(void)          
  42. {
  43.     u8 data;
  44.         DS18B20_IO_OUT();        //SET PG11 OUTPUT
  45.     DS18B20_DQ_OUT=0;
  46.         Delay_DS18B20_1us(2);
  47.     DS18B20_DQ_OUT=1;
  48.         DS18B20_IO_IN();        //SET PG11 INPUT
  49.         Delay_DS18B20_1us(12);
  50.         if(DS18B20_DQ_IN)data=1;
  51.     else data=0;         
  52.     Delay_DS18B20_1us(50);           
  53.     return data;
  54. }
  55. //从DS18B20读取一个字节
  56. //返回值:读到的数据
  57. u8 DS18B20_Read_Byte(void)     
  58. {        
  59.     u8 i,j,dat;
  60.     dat=0;
  61.         for (i=1;i<=8;i++)
  62.         {
  63.         j=DS18B20_Read_Bit();
  64.         dat=(j<<7)|(dat>>1);
  65.     }                                                    
  66.     return dat;
  67. }
  68. //写一个字节到DS18B20
  69. //dat:要写入的字节
  70. void DS18B20_Write_Byte(u8 dat)     
  71. {            
  72.     u8 j;
  73.     u8 testb;
  74.         DS18B20_IO_OUT();        //SET PG11 OUTPUT;
  75.     for (j=1;j<=8;j++)
  76.         {
  77.         testb=dat&0x01;
  78.         dat=dat>>1;
  79.         if (testb)
  80.         {
  81.             DS18B20_DQ_OUT=0;        // Write 1
  82.             Delay_DS18B20_1us(2);                           
  83.             DS18B20_DQ_OUT=1;
  84.             Delay_DS18B20_1us(60);            
  85.         }
  86.         else
  87.         {
  88.             DS18B20_DQ_OUT=0;        // Write 0
  89.             Delay_DS18B20_1us(60);            
  90.             DS18B20_DQ_OUT=1;
  91.             Delay_DS18B20_1us(2);                          
  92.         }
  93.     }
  94. }
  95. //开始温度转换
  96. void DS18B20_Start(void)
  97. {                                                                 
  98.     DS18B20_Rst();          
  99.         DS18B20_Check();         
  100.     DS18B20_Write_Byte(0xcc);        // skip rom
  101.     DS18B20_Write_Byte(0x44);        // convert
  102. }

  103. //初始化DS18B20的IO口 DQ 同时检测DS的存在
  104. //返回1:不存在
  105. //返回0:存在             
  106. u8 DS18B20_Init(void)
  107. {
  108.         GPIO_InitTypeDef  GPIO_InitStructure;
  109.        
  110.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);         //使能PORTG口时钟
  111.        
  112.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;                                //PORTG.11 推挽输出
  113.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                   
  114.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  115.         GPIO_Init(GPIOA, &GPIO_InitStructure);

  116.         GPIO_SetBits(GPIOA,GPIO_Pin_11);    //输出1

  117.         DS18B20_Rst();

  118.         return DS18B20_Check();
  119. }  
  120. //从ds18b20得到温度值
  121. //精度:0.1C
  122. //返回值:温度值 (-550~1250)
  123. short DS18B20_Get_Temp(void)
  124. {
  125.     u8 temp;
  126.     u8 TL,TH;
  127.         short tem;
  128.     DS18B20_Start ();                          // ds1820 start convert
  129.     DS18B20_Rst();
  130.     DS18B20_Check();         
  131.     DS18B20_Write_Byte(0xcc);        // skip rom
  132.     DS18B20_Write_Byte(0xbe);        // convert            
  133.     TL=DS18B20_Read_Byte();         // LSB   
  134.     TH=DS18B20_Read_Byte();         // MSB  
  135.                       
  136.     if(TH>7)
  137.     {
  138.         TH=~TH;
  139.         TL=~TL;
  140.         temp=0;                                        //温度为负  
  141.     }else temp=1;                                //温度为正                    
  142.     tem=TH;                                         //获得高八位
  143.     tem<<=8;   
  144.     tem+=TL;                                        //获得底八位
  145.     tem=(float)tem*0.625;                //转换     
  146.         if(temp)return tem;                 //返回温度值
  147.         else return -tem;   
  148. }
复制代码

所有资料51hei提供下载:
STM32 温度传感器DS18B20仿真.7z (454.03 KB, 下载次数: 171)

评分

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

查看全部评分

回复

使用道具 举报

ID:1052824 发表于 2023-12-17 23:32 | 显示全部楼层
你好,我按照你画为什么仿真不了,应该是没有接错的
屏幕截图 温度显示.png
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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