1、 自制PIC温度计电流1Am电压3V
#include
2、@ (unsigned)&TRISC*8+3; static volatile bit TRISC4 @ (unsigned)&TRISC*8+4; static volatile bit TRISC5 @ (unsigned)&TRISC*8+5; static volatile bit TRISB0 @ (unsigned)&TRISB*8+0; static volatile bit TRISB1 @ (unsigned)&TRISB*8+1; static volatile bit TRISB2 @ (unsigned)&TRISB*8+2; static volati
3、le bit TRISB3 @ (unsigned)&TRISB*8+3; static volatile bit TRISB4 @ (unsigned)&TRISB*8+4; static volatile bit TRISB5 @ (unsigned)&TRISB*8+5; #define uch unsigned char //给unsigned char起别名 uch #define uint unsigned int #define DQ RC3 //定义18B
4、20数据端口 #define DQ_DIR TRISC3 //定义18B20D口方向寄存器 #define DQ_HIGH() DQ_DIR =1 //设置数据口为输入 #define DQ_LOW() DQ_DIR = 0;DQ = 0 //设置数据口为输出 #define CLK RB1 #define CLK_ON() temp=TRISB;TRISB=temp&0xfd //设置LCD时钟为输出 #define CLK_OFF() temp=TRISB;TRIS
5、B=temp|0x02 //设置LCD时钟为输入 //#define CLK_ON() TRISB1=0 //#define CLK_OFF() TRISB1=1 #define HC164_ON() temp=TRISB;TRISB=temp&0xfe #define HC164_OFF() temp=TRISB;TRISB=temp|0x01 //#define HC164_ON() RB0=1;TRISB0=0 //164芯片加电 //#define HC164_OFF() RB0=0;TRISB0=1 //164断电 #define HC164VC
6、C RB0 #define DATAD_ON() temp=TRISB;TRISB=temp&0xfb #define DATAD_OFF() temp=TRISB;TRISB=temp|0x04 //#define DATAD_ON() TRISB2=0 //设置LCD数据为输出 //#define DATAD_OFF() TRISB2=1 //设置LCD数据为输入 #define DATAD RB2 #define LCD_ON() temp=TRISC;TRISC=temp&0xfe #define LCD_OFF() temp=TRISC;TRISC
7、temp|0x01 //#define LCD_ON() TRISC0=0 //设置LCD显示开 //#define LCD_OFF() TRISC0=1 //设置LCD显示关 #define LCDCOM RC0 bank1 unsigned char TLV = 0 ; //采集到的温度高8位 bank1 unsigned char THV = 0; //采集到的温度低8位 bank1 unsigned char TZ = 0;
8、 //转换后的温度值整数部分 bank1 unsigned char TX = 0; //转换后的温度值小数部分 bank1 unsigned int wd; //转换后的温度值BCD码形式 bank1 unsigned char temp; bank1 unsigned char shi; //整数十位 bank1 unsigned char ge;
9、 //整数个位 bank1 unsigned char shifen; //十分位 bank1 unsigned char baifen; //百分位 bank1 unsigned char qianfen; //千分位 bank1 unsigned char wanfen; //万分位 bank1 bit flag=0; //用于标志是否是负的度数 //LED共阴
10、 const unsigned char TABLE[] = {0x77,0x14,0xB3,0xB6,0xD4,0xE6,0xE7,0x34,0xF7,0xF6}; //0-9的显示代码 //自己的 //const unsigned char TABLE[] = {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; //0-9的显示代码 /**************************************************************************** * 名 称:delay
11、 * 功 能:延时函数 * 入口参数: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) { do {
12、 do{NOP();} while (--y); } while (--x); } void delay1(unsigned char x) { do x--; while(x); } /**************************************************************************** * 名 称:display(unsinged char i,unsigned char j,unsigned char k) * 功 能:数码管显示 * 入口参数: * 出口参数: *
13、说 明: ****************************************************************************/ 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();//显示数据开 CLK=0; //显示时钟置0 CLK_ON(); //显示时钟开 /
14、/for(n=14;n!=0;n--) { m=TABLE[k];//第1位显示 for(l=8;l!=0;l--) { DATAD=m&0x01; CLK=0; CLK=1; m=m>>1; } m=TABLE[j]|0x08;//第2位显示 for(l=8;l!=0;l--) { DATAD=m&0x01; CLK=0; CLK=1; m=m>>1; } m=TABLE[i]; for(l=8;l!=0;l--) { DATA
15、D=m&0x01; CLK=0; CLK=1; m=m>>1; } LCDCOM=0; LCD_ON();//开LCD delay(2,150);//2ms } LCD_OFF();//关LCD //for(n=14;n!=0;n--) { m=~TABLE[k];//第1位显示 for(l=8;l!=0;l--) { DATAD=m&0x01; CLK=0; CLK=1; m=m>>1; } m=~(TABLE[j]|0x08);/
16、/第2位显示 for(l=8;l!=0;l--) { DATAD=m&0x01; CLK=0; CLK=1; m=m>>1; } m=~TABLE[i]; for(l=8;l!=0;l--) { DATAD=m&0x01; CLK=0; CLK=1; m=m>>1; } LCDCOM=1; LCD_ON();//开LCD delay(2,150); //delay(5,10); } LCD_OFF();//关LCD HC1
17、64VCC=0; HC164_OFF(); } /**************************************************************************** * 名 称:reset() * 功 能:复位DS18B20 * 入口参数: * 出口参数: * 说 明: ****************************************************************************/ //32.768kHz /*void reset() { unsigne
18、d char presence = 1; while (presence) { DQ_LOW() ; //主机拉至低电平 delay1(3); //延时503us//520us DQ_HIGH(); //释放总线等电阻拉高总线,并保持15~60us NOP();NOP();NOP();NOP(); //延时70us//80us if (DQ == 1) presence =
19、0; //没有接收到应答信号,继续复位 else presence = 0; //接收到应答信号 delay1(1); //延时430us//480us } }*/ //4MHz void reset() { unsigned char presence = 1; while (presence) { DQ_LOW() ; //主机拉至低电平 delay
20、1, 95); //延时503us//498us DQ_HIGH(); //释放总线等电阻拉高总线,并保持15~60us delay(1, 10); //延时70us//72us if (DQ == 1) presence = 0; //没有接收到应答信号,继续复位 else presence = 0;
21、//接收到应答信号 delay(1, 82); //延时430us//433us delay(1,1); delay(1,1); } } /**************************************************************************** * 名 称:write_byte() * 功 能:写18b20写字节 * 入口参数:uch val 待写的数据 * 出口参数: * 说 明: ***********************
22、/ //32.768KHz /*void write_byte(uch val) { uch i; uch temp; for (i = 8;i > 0;i--) { temp = val & 0x01; //最低位移出 if (temp == 1) { DQ_LOW(); //从高拉至低电平,产生写
23、时间隙 DQ_HIGH(); //如果写1,拉高电平 } else DQ_LOW(); temp=1; NOP();NOP();NOP(); //延时63us//60us DQ_HIGH(); val = val >> 1; //右移一位 } }*/ //4MHz void write_byte(uch val) { uch i; uch temp; for (i = 8;i > 0
24、i--) { temp = val & 0x01; //最低位移出 DQ_LOW(); NOP();NOP();NOP();NOP();NOP(); //从高拉至低电平,产生写时间隙 if (temp == 1) DQ_HIGH(); //如果写1,拉高电平 temp=1; delay(1, 8); //延时63us DQ_H
25、IGH(); NOP();NOP(); val = val >> 1; //右移一位 } } /**************************************************************************** * 名 称:read_byte() * 功 能:18b20读字节 * 入口参数: * 出口参数:读出18B20的内容 * 说 明: ********************************************************
26、/ //32.768KHz /*uch read_byte() { uch i; uch value = 0; //读出温度 static bit j; for (i = 8;i > 0;i--) { value >>= 1; DQ_LOW(); // NOP();NOP();NOP();NOP();NOP();NOP(); //6us DQ_HIGH(); //拉至高电平 // NOP
27、);NOP();NOP();NOP(); //4us j = DQ; if (j) value |= 0x80; // delay(1, 8); //63us } return (value); }*/ //4MHz uch read_byte() { uch i; uch value = 0; //读出温度 static bit j; for (i = 8;i > 0;i--) { v
28、alue >>= 1; DQ_LOW(); NOP();NOP();NOP();NOP();NOP();NOP(); //6us DQ_HIGH(); //拉至高电平 NOP();NOP();NOP();NOP(); //4us j = DQ; if (j) value |= 0x80; delay(1, 8); //63us } return (value); } /***********
29、 * 名 称:main() * 功 能:主函数 * 入口参数: * 出口参数: * 说 明: ****************************************************************************/ void main() { unsigned char m1=0; //调用系统初始化函数 unsigned char i1;
30、 while (1) { OPTION=0XC0; DQ_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;
31、 //显示度数符号 // display(m1,m1+1,m1+2); //调用多次显示函数,确保温度转换完成所需要的时间 for(i1=30;i1!=0;i1--) display(shi,ge,shifen); m1++; } /**/ reset(); //再次复位,等待从机应答 write_byte(0XCC); //忽略ROM匹配 write_byte(0
32、XBE); //发送读温度命令 TLV = read_byte(); //读出温度低8 THV = read_byte(); //读出温度高8位 DQ_HIGH(); //释放总线 if(THV>127) { THV=255-THV; TLV=256-TLV; flag=1; } else { flag=0;
33、 } TZ = (TLV >> 4) | (THV << 4) & 0X3f; //温度整数部分 TX = TLV << 4; //温度小数部分 if (TZ > 100) TZ / 100; //不显示百位 ge = TZ % 10; //整数部分个位 shi = TZ / 10; //整数十位 wd = 0; if (TX & 0x80) wd
34、 wd + 5000; if (TX & 0x40) wd = wd + 2500; if (TX & 0x20) wd = wd + 1250; if (TX & 0x10) wd = wd + 625; //以上4条指令把小数部分转换为BCD码形式 shifen = wd / 1000; //十分位 baifen = (wd % 1000) / 100; //百分位 qianfen = (wd % 100) / 10; //千分位 wanfen = wd % 10; //万分位 NOP(); //display(); //调用结果显示函数 } }






