|
ds18b20單總線驅(qū)動(dòng),要掛接多個(gè),就得掃描ROM提取序列號(hào),還得匹配ROM取溫度值,有點(diǎn)麻煩。分析其驅(qū)動(dòng)原理,完全可以用P口的8個(gè)位同時(shí)驅(qū)動(dòng)8個(gè)ds18b20,實(shí)現(xiàn)溫度顯示。
以下是proteus仿真效果:
2018-12-25_171040.png (27.64 KB, 下載次數(shù): 30)
下載附件
2018-12-25 17:12 上傳
以下是代碼:
main.c
- #include "config.h"
- #include "timer.h"
- #include "lcd1604.h"
- #include "ds18b20.h"
- uchar temp_array[16], *p_array;
- //按位取值
- uchar code dbit[8] = {1, 2, 4, 8, 16, 32, 64, 128};
- uchar code pos[8] = {0,0,1,1,2,2,3,3};
- void dis_temps(void)
- {
- uint16 temp;
- uint8 i, j;
- uchar str[6] = "";
- bit temp_flag;
-
- for(i=0; i<8; i++)
- {
- for(j=0; j<16; j++)
- {
- temp >>= 1;
- if((temp_array[j] & dbit[i]) == dbit[i])
- {
- temp |=0x8000;
- }
- }
- if((temp & 0xf800) == 0xf800)
- {
- temp_flag = 1;
- temp = (~temp) + 1;
- }else{
- temp_flag = 0;
- }
- float2str(temp*0.0625, str, 1);
-
- if(temp_flag == 1)
- {
- if((i%2)==0)
- {
- LCD1604_dis_str(0, pos[i], "-");
- }else{
- LCD1604_dis_str(8, pos[i], "-");
- }
- }else{
- if((i%2)==0)
- {
- LCD1604_dis_str(0, pos[i], " ");
- }else{
- LCD1604_dis_str(8, pos[i], " ");
- }
- }
- if((i%2)==0)
- {
- LCD1604_dis_str(1, pos[i], str);
- }else{
- LCD1604_dis_str(9, pos[i], str);
- }
- }
- }
- void main()
- {
- LCD1604_init();
- p_array = temp_array;
- DS18B20_start();
- while(1)
- {
- DS18B20_get_temp(p_array);
- dis_temps();
- DS18B20_start();
- delay_ms(500);
- }
- }
復(fù)制代碼 ds18b20.c
|
評(píng)分
-
查看全部評(píng)分
|