1、自制 PIC 温度计电流 1Am 电压 3VRC37 RC5/TOCKI5 RC46RB5/OSC1/CLKIN2 RB0/ICSPDAT 13RB1/ICSPCLK 12RB2 11RC0 10RC1 9RC2 8VS 14RB3/MCLR/VPP4 RB4/OSC2/CLKOUT3VDD1U1pic16F505VC1K R1123JP2Header 312JP1Header 2S1S_2x2VC1VC1GND1 VC1GND1GND1DATATDATATGND1VCIN VCIN VCINGND1GND1VCINCLKDATADCLK CLKCLKDATADCOMCOM1E1D1C1DP2
2、E2D2C2DP3E3D3C1G1F1A1B2G2F2A2B3G3F3A3Bcom1 1E2 1D31C4 1DP5 2E62C8 2D7 2DP9 3E103D11 3C12 3B 133A 143F 153G162B 172A 182G 202F 191B 211A221F 231G 24Dis1EDS8121E1D1C1DP1G1F1A1B2E2D2C2DP2G2F2A2B3E3D3C3G3F3A3BBAT13VVCRESET9 CLK8A1 B2 QA 3QB 4QC 5QD 6QE 10QF 11QG 12QH 13VDD 14GND7U2MC74HC164ADRESET9 CLK8
3、A1 B2 QA 3QB 4QC 5QD 6QE 10QF 11QG 12QH 13VDD 14GND7U4MC74HC164ADRESET9 CLK8A1 B2 QA 3QB 4QC 5QD 6QE 10QF 11QG 12QH 13VDD 14GND7U3MC74HC164AD12Y120pFC1Cap20pFC2CapGND11 23 4S2S1 23 4S3SS1S2GND1GND1S1S21K R21K R312Y2#include_CONFIG(0xFF32);/芯片配置字,看门狗关,上电延时开,掉电检测关,低压编程关,加密,4M 晶体 HS 振荡static volatile b
4、it TRISC0 (unsigned)static volatile bit TRISC1 (unsigned)static volatile bit TRISC2 (unsigned)static volatile bit TRISC3 (unsigned)static volatile bit TRISC4 (unsigned)static volatile bit TRISC5 (unsigned)static volatile bit TRISB0 (unsigned)static volatile bit TRISB1 (unsigned)static volatile bit T
5、RISB2 (unsigned)static volatile bit TRISB3 (unsigned)static volatile bit TRISB4 (unsigned)static volatile bit TRISB5 (unsigned)#define uch unsigned char /给 unsigned char 起别名 uch#define uint unsigned int#define DQ RC3 /定义 18B20 数据端口#define DQ_DIR TRISC3 /定义 18B20D 口方向寄存器#define DQ_HIGH() DQ_DIR =1 /设
6、置数据口为输入#define DQ_LOW() DQ_DIR = 0;DQ = 0 /设置数据口为输出#define CLK RB1#define CLK_ON() temp=TRISB;TRISB=tempTRISB=temp|0x02 /设置 LCD 时钟为输入/#define CLK_ON() TRISB1=0/#define CLK_OFF() TRISB1=1#define HC164_ON() temp=TRISB;TRISB=tempTRISB=temp|0x01/#define HC164_ON() RB0=1;TRISB0=0 /164 芯片加电/#define HC164_
7、OFF() RB0=0;TRISB0=1 /164 断电#define HC164VCC RB0#define DATAD_ON() temp=TRISB;TRISB=tempTRISB=temp|0x04/#define DATAD_ON() TRISB2=0 /设置 LCD 数据为输出/#define DATAD_OFF() TRISB2=1 /设置 LCD 数据为输入#define DATAD RB2#define LCD_ON() temp=TRISC;TRISC=tempTRISC=temp|0x01/#define LCD_ON() TRISC0=0 /设置 LCD 显示开/#de
8、fine LCD_OFF() TRISC0=1 /设置 LCD 显示关#define LCDCOM RC0bank1 unsigned char TLV = 0 ; /采集到的温度高 8 位bank1 unsigned char THV = 0; /采集到的温度低 8 位bank1 unsigned char TZ = 0; /转换后的温度值整数部分bank1 unsigned char TX = 0; /转换后的温度值小数部分bank1 unsigned int wd; /转换后的温度值 BCD 码形式bank1 unsigned char temp;bank1 unsigned char
9、shi; /整数十位bank1 unsigned char ge; /整数个位bank1 unsigned char shifen; /十分位bank1 unsigned char baifen; /百分位bank1 unsigned char qianfen; /千分位bank1 unsigned char wanfen; /万分位bank1 bit flag=0; /用于标志是否是负的度数/LED 共阴const unsigned char TABLE = 0x77,0x14,0xB3,0xB6,0xD4,0xE6,0xE7,0x34,0xF7,0xF6; /0-9 的显示代码/自己的/c
10、onst unsigned char TABLE = 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F; /0-9 的显示代码/* 名 称:delay() * 功 能:延时函数* 入口参数:char x, char y * 出口参数:* 说 明:其指令时间为:7+(3*(Y-1 )+7)*(X-1)如果再加上函数调用的 call 指令、页面设定、传递参数花掉的 7 个指令。则是:14+(3*(Y-1 )+7)*(X-1)*/void delay(unsigned char x,unsigned char y)dodoNOP();while (-
11、y);while (-x);void delay1(unsigned char x)do x-;while(x);/* 名 称:display(unsinged char i,unsigned char j,unsigned char k) * 功 能:数码管显示* 入口参数:* 出口参数:* 说 明:*/void display(unsigned char i,unsigned char j,unsigned char k)unsigned char m,n,l;HC164_ON();/HC164 加电HC164VCC=1;LCD_OFF();/LCD 关DATAD_ON();/显示数据开C
12、LK=0; /显示时钟置 0CLK_ON(); /显示时钟开/for(n=14;n!=0;n-)m=TABLEk;/第 1 位显示for(l=8;l!=0;l-)DATAD=mCLK=0;CLK=1;m=m1;m=TABLEj|0x08;/第 2 位显示for(l=8;l!=0;l-)DATAD=mCLK=0;CLK=1;m=m1;m=TABLEi;for(l=8;l!=0;l-)DATAD=mCLK=0;CLK=1;m=m1;LCDCOM=0;LCD_ON();/开 LCDdelay(2,150);/2msLCD_OFF();/关 LCD/for(n=14;n!=0;n-)m=TABLEk;
13、/第 1 位显示for(l=8;l!=0;l-)DATAD=mCLK=0;CLK=1;m=m1;m=(TABLEj|0x08);/第 2 位显示for(l=8;l!=0;l-)DATAD=mCLK=0;CLK=1;m=m1;m=TABLEi;for(l=8;l!=0;l-)DATAD=mCLK=0;CLK=1;m=m1;LCDCOM=1;LCD_ON();/开 LCDdelay(2,150);/delay(5,10);LCD_OFF();/关 LCDHC164VCC=0;HC164_OFF();/* 名 称:reset() * 功 能:复位 DS18B20* 入口参数:* 出口参数:* 说 明
14、:*/32.768kHz/*void reset()unsigned char presence = 1;while (presence)DQ_LOW() ; /主机拉至低电平delay1(3); /延时 503us/520usDQ_HIGH(); /释放总线等电阻拉高总线,并保持 1560usNOP();NOP();NOP();NOP(); /延时 70us/80usif (DQ = 1) presence = 0; /没有接收到应答信号,继续复位else presence = 0; /接收到应答信号delay1(1); /延时 430us/480us*/4MHzvoid reset()un
15、signed char presence = 1;while (presence)DQ_LOW() ; /主机拉至低电平delay(1, 95); /延时 503us/498usDQ_HIGH(); /释放总线等电阻拉高总线,并保持 1560usdelay(1, 10); /延时 70us/72usif (DQ = 1) presence = 0; /没有接收到应答信号,继续复位else presence = 0; /接收到应答信号delay(1, 82); /延时 430us/433usdelay(1,1);delay(1,1);/* 名 称:write_byte() * 功 能:写 18b
16、20 写字节* 入口参数:uch val 待写的数据* 出口参数:* 说 明:*/32.768KHz/*void write_byte(uch val)uch i;uch temp;for (i = 8;i 0;i-)temp = val /最低位移出if (temp = 1) DQ_LOW(); /从高拉至低电平,产生写时间隙DQ_HIGH(); /如果写 1,拉高电平elseDQ_LOW();temp=1;NOP();NOP();NOP(); /延时 63us/60usDQ_HIGH();val = val 1; /右移一位*/4MHzvoid write_byte(uch val)uch
17、 i;uch temp;for (i = 8;i 0;i-)temp = val /最低位移出DQ_LOW();NOP();NOP();NOP();NOP();NOP(); /从高拉至低电平,产生写时间隙if (temp = 1) DQ_HIGH(); /如果写 1,拉高电平temp=1;delay(1, 8); /延时 63usDQ_HIGH();NOP();NOP();val = val 1; /右移一位/* 名 称:read_byte() * 功 能:18b20 读字节* 入口参数:* 出口参数:读出 18B20 的内容* 说 明:*/32.768KHz/*uch read_byte()
18、uch i;uch value = 0; /读出温度static bit j;for (i = 8;i 0;i-)value = 1;DQ_LOW();/ NOP();NOP();NOP();NOP();NOP();NOP(); /6usDQ_HIGH(); /拉至高电平/ NOP();NOP();NOP();NOP(); /4usj = DQ;if (j) value |= 0x80;/ delay(1, 8); /63usreturn (value);*/4MHzuch read_byte()uch i;uch value = 0; /读出温度static bit j;for (i = 8
19、;i 0;i-)value = 1;DQ_LOW();NOP();NOP();NOP();NOP();NOP();NOP(); /6usDQ_HIGH(); /拉至高电平NOP();NOP();NOP();NOP(); /4usj = DQ;if (j) value |= 0x80;delay(1, 8); /63usreturn (value);/* 名 称:main() * 功 能:主函数* 入口参数:* 出口参数:* 说 明:*/void main()unsigned char m1=0; /调用系统初始化函数unsigned char i1;while (1)OPTION=0XC0;D
20、Q_HIGH();reset(); /复位等待从机应答write_byte(0XCC); /忽略 ROM 匹配write_byte(0X44); /发送温度转化命令/m=2;/for (i = 1;i!= 0;i-)/*/ if(m1+2=9)m1=0;RB4=flag; /显示度数符号/ display(m1,m1+1,m1+2); /调用多次显示函数,确保温度转换完成所需要的时间for(i1=30;i1!=0;i1-)display(shi,ge,shifen);m1+;/*/ reset(); /再次复位,等待从机应答write_byte(0XCC); /忽略 ROM 匹配write_b
21、yte(0XBE); /发送读温度命令TLV = read_byte(); /读出温度低 8THV = read_byte(); /读出温度高 8 位DQ_HIGH(); /释放总线if(THV127)THV=255-THV;TLV=256-TLV;flag=1;elseflag=0;TZ = (TLV 4) | (THV 100) TZ / 100; /不显示百位ge = TZ % 10; /整数部分个位shi = TZ / 10; /整数十位wd = 0;if (TX if (TX if (TX if (TX /以上 4 条指令把小数部分转换为BCD 码形式shifen = wd / 1000; /十分位baifen = (wd % 1000) / 100; /百分位qianfen = (wd % 100) / 10; /千分位wanfen = wd % 10; /万分位NOP();/display(); /调用结果显示函数