收藏 分销(赏)

自制PIC温度计电流1Am电压3V.doc

上传人:pc****0 文档编号:7775819 上传时间:2025-01-16 格式:DOC 页数:10 大小:182KB 下载积分:10 金币
下载 相关 举报
自制PIC温度计电流1Am电压3V.doc_第1页
第1页 / 共10页
自制PIC温度计电流1Am电压3V.doc_第2页
第2页 / 共10页


点击查看更多>>
资源描述
自制PIC温度计电流1Am电压3V #include<pic.h> __CONFIG(0xFF32); //芯片配置字,看门狗关,上电延时开,掉电检测关,低压编程关,加密,4M晶体HS振荡 static volatile bit TRISC0 @ (unsigned)&TRISC*8+0; static volatile bit TRISC1 @ (unsigned)&TRISC*8+1; static volatile bit TRISC2 @ (unsigned)&TRISC*8+2; static volatile bit TRISC3 @ (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 volatile 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 //定义18B20数据端口 #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;TRISB=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 HC164VCC 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=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; //转换后的温度值整数部分 bank1 unsigned char TX = 0; //转换后的温度值小数部分 bank1 unsigned int wd; //转换后的温度值BCD码形式 bank1 unsigned char temp; bank1 unsigned char 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的显示代码 //自己的 //const 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) { do { 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) * 功 能:数码管显示 * 入口参数: * 出口参数: * 说 明: ****************************************************************************/ 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(); //显示时钟开 //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--) { DATAD=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);//第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 HC164VCC=0; HC164_OFF(); } /**************************************************************************** * 名 称:reset() * 功 能:复位DS18B20 * 入口参数: * 出口参数: * 说 明: ****************************************************************************/ //32.768kHz /*void reset() { unsigned 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 = 0; //没有接收到应答信号,继续复位 else presence = 0; //接收到应答信号 delay1(1); //延时430us//480us } }*/ //4MHz void reset() { unsigned char presence = 1; while (presence) { DQ_LOW() ; //主机拉至低电平 delay(1, 95); //延时503us//498us DQ_HIGH(); //释放总线等电阻拉高总线,并保持15~60us delay(1, 10); //延时70us//72us if (DQ == 1) presence = 0; //没有接收到应答信号,继续复位 else presence = 0; //接收到应答信号 delay(1, 82); //延时430us//433us delay(1,1); delay(1,1); } } /**************************************************************************** * 名 称:write_byte() * 功 能:写18b20写字节 * 入口参数:uch val 待写的数据 * 出口参数: * 说 明: ****************************************************************************/ //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(); //从高拉至低电平,产生写时间隙 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;i--) { temp = val & 0x01; //最低位移出 DQ_LOW(); NOP();NOP();NOP();NOP();NOP(); //从高拉至低电平,产生写时间隙 if (temp == 1) DQ_HIGH(); //如果写1,拉高电平 temp=1; delay(1, 8); //延时63us DQ_HIGH(); NOP();NOP(); val = val >> 1; //右移一位 } } /**************************************************************************** * 名 称:read_byte() * 功 能:18b20读字节 * 入口参数: * 出口参数:读出18B20的内容 * 说 明: ****************************************************************************/ //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();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--) { value >>= 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); } /**************************************************************************** * 名 称:main() * 功 能:主函数 * 入口参数: * 出口参数: * 说 明: ****************************************************************************/ void main() { unsigned char m1=0; //调用系统初始化函数 unsigned char i1; 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; //显示度数符号 // display(m1,m1+1,m1+2); //调用多次显示函数,确保温度转换完成所需要的时间 for(i1=30;i1!=0;i1--) display(shi,ge,shifen); m1++; } /**/ reset(); //再次复位,等待从机应答 write_byte(0XCC); //忽略ROM匹配 write_byte(0XBE); //发送读温度命令 TLV = read_byte(); //读出温度低8 THV = read_byte(); //读出温度高8位 DQ_HIGH(); //释放总线 if(THV>127) { THV=255-THV; TLV=256-TLV; flag=1; } else { flag=0; } 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 = 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(); //调用结果显示函数 } }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 百科休闲 > 其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服