以下代码在STC15W408AS单片机进行测试,后面还有自制的PCB的原理图和layout设计(因某些原因需要制作这样一块显示温度的小项目,又想节约成本,于是画的十分简陋…)
DS18B20驱动代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| #include "stc15.h"
sbit DQ = P1^1;
void Delay_OneWire(unsigned int t) { unsigned char i; while(t--) { for(i=0;i<12;i++); } }
void Write_DS18B20(unsigned char dat) { unsigned char i; for(i=0;i<8;i++) { DQ = 0; DQ = dat&0x01; Delay_OneWire(5); DQ = 1; dat >>= 1; } Delay_OneWire(5); }
unsigned char Read_DS18B20(void) { unsigned char i; unsigned char dat; for(i=0;i<8;i++) { DQ = 0; dat >>= 1; DQ = 1; if(DQ) { dat |= 0x80; } Delay_OneWire(5); } return dat; }
bit init_ds18b20(void) { bit initflag = 0; DQ = 1; Delay_OneWire(12); DQ = 0; Delay_OneWire(80); DQ = 1; Delay_OneWire(10); initflag = DQ; Delay_OneWire(5); return initflag; }
unsigned char rd_temperature(void) { unsigned char low,high; char temp; init_ds18b20(); Write_DS18B20(0xCC); Write_DS18B20(0x44); Delay_OneWire(200);
init_ds18b20(); Write_DS18B20(0xCC); Write_DS18B20(0xBE);
low = Read_DS18B20(); high = Read_DS18B20(); temp = high<<4; temp |= (low>>4); return temp; }
|
PCB文件及其示意图
PCB文件以压缩包形式打包。戳我下载
注:我画的是拼板,右侧的那块可能地线没有接到一起。
PCB原件布局
BOM表
Comment | Description | Footprint | Quantity |
---|
5261AS | 二位共阴数码管 | 5261AS | 1 |
LED | 发光二极管 | 0603 | 1 |
Header 4 | Header, 4-Pin | HDR1X4 | 1 |
Res | 10K | 0603 | 1 |
Res | 5.1R | 0603 | 1 |
STC15W408AS | C51单片机 | SOP-16 | 1 |
DS18B20模组 | DS18B20模组 | DS18B20模组 | 1 |
DS18B20模组相关文件由淘宝商家提供,以下是淘宝商家公开的百度网盘连接。
https://pan.baidu.com/s/19CwH6CAaWgVBMKmCA28ygQ 提取码:lggj
单片机代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
| #include "stc15.h" #include "intrins.h" #include "onewire.h" #include "absacc.h"
#define uchar unsigned char #define uint unsigned int
sbit LED = P3 ^ 2; sbit DIGITAL_a = P1 ^ 2; sbit DIGITAL_b = P1 ^ 3; sbit DIGITAL_c = P1 ^ 4; sbit DIGITAL_d = P1 ^ 5; sbit DIGITAL_e = P5 ^ 4; sbit DIGITAL_f = P5 ^ 5; sbit DIGITAL_g = P1 ^ 0; sbit DIGITAL_dp = P3 ^ 7; sbit DIGITAL_H = P3 ^ 6; sbit DIGITAL_L = P3 ^ 3;
bit temper_flag = 0; int temp = 0; int intr = 0;
void printf_num(num) { if (num == 0) { DIGITAL_a = 1; DIGITAL_b = 1; DIGITAL_c = 1; DIGITAL_d = 1; DIGITAL_e = 1; DIGITAL_f = 1; DIGITAL_g = 0; DIGITAL_dp = 0; } if (num == 1) { DIGITAL_a = 0; DIGITAL_b = 1; DIGITAL_c = 1; DIGITAL_d = 0; DIGITAL_e = 0; DIGITAL_f = 0; DIGITAL_g = 0; DIGITAL_dp = 0; } if (num == 2) { DIGITAL_a = 1; DIGITAL_b = 1; DIGITAL_c = 0; DIGITAL_d = 1; DIGITAL_e = 1; DIGITAL_f = 0; DIGITAL_g = 1; DIGITAL_dp = 0; } if (num == 3) { DIGITAL_a = 1; DIGITAL_b = 1; DIGITAL_c = 1; DIGITAL_d = 1; DIGITAL_e = 0; DIGITAL_f = 0; DIGITAL_g = 1; DIGITAL_dp = 0; } if (num == 4) { DIGITAL_a = 0; DIGITAL_b = 1; DIGITAL_c = 1; DIGITAL_d = 0; DIGITAL_e = 0; DIGITAL_f = 1; DIGITAL_g = 1; DIGITAL_dp = 0; } if (num == 5) { DIGITAL_a = 1; DIGITAL_b = 0; DIGITAL_c = 1; DIGITAL_d = 1; DIGITAL_e = 0; DIGITAL_f = 1; DIGITAL_g = 1; DIGITAL_dp = 0; } if (num == 6) { DIGITAL_a = 1; DIGITAL_b = 0; DIGITAL_c = 1; DIGITAL_d = 1; DIGITAL_e = 1; DIGITAL_f = 1; DIGITAL_g = 1; DIGITAL_dp = 0; } if (num == 7) { DIGITAL_a = 1; DIGITAL_b = 1; DIGITAL_c = 1; DIGITAL_d = 0; DIGITAL_e = 0; DIGITAL_f = 0; DIGITAL_g = 0; DIGITAL_dp = 0; } if (num == 8) { DIGITAL_a = 1; DIGITAL_b = 1; DIGITAL_c = 1; DIGITAL_d = 1; DIGITAL_e = 1; DIGITAL_f = 1; DIGITAL_g = 1; DIGITAL_dp = 0; } if (num == 9) { DIGITAL_a = 1; DIGITAL_b = 1; DIGITAL_c = 1; DIGITAL_d = 1; DIGITAL_e = 0; DIGITAL_f = 1; DIGITAL_g = 1; DIGITAL_dp = 0; } }
void printf_wendu(num) { DIGITAL_H = 0; printf_num(num / 10); DIGITAL_H = 1;
DIGITAL_a = 0; DIGITAL_b = 0; DIGITAL_c = 0; DIGITAL_d = 0; DIGITAL_e = 0; DIGITAL_f = 0; DIGITAL_g = 0; DIGITAL_dp = 0;
DIGITAL_L = 0; printf_num(num % 10); DIGITAL_L = 1;
DIGITAL_a = 0; DIGITAL_b = 0; DIGITAL_c = 0; DIGITAL_d = 0; DIGITAL_e = 0; DIGITAL_f = 0; DIGITAL_g = 0; DIGITAL_dp = 0;
}
void main(void) { float temperature; while (1) { if (temper_flag) { temper_flag = 0; temperature = rd_temperature(); temp = temperature; } printf_wendu(temp); intr++; if (intr == 100) { intr = 0; temper_flag = 1; }
}
}
|