当前位置:首页 > 单片机 > 单片机
[导读]#include#defineucharunsignedchar#defineuintunsignedint#definers_h(PORTC|=0x01)#definers_l(PORTC&=0xfe)#definers_o(TRISC&=0xfe)#definerw_h(PORTC|=0x02)#definerw_l(PORTC&=0xfd)#definerw_o(TRISC&=0xfd)#d

#include#defineucharunsignedchar#defineuintunsignedint#definers_h(PORTC|=0x01)#definers_l(PORTC&=0xfe)#definers_o(TRISC&=0xfe)#definerw_h(PORTC|=0x02)#definerw_l(PORTC&=0xfd)#definerw_o(TRISC&=0xfd)#defineen_h(PORTC|=0x04)#defineen_l(PORTC&=0xfb)#defineen_o(TRISC&=0xfb)#definetemp_h(PORTC|=0x08)#definetemp_l(PORTC&=0xf7)#definetemp_o(TRISC&=0xf7)#definetemp_i(TRISC|=0x08)#defineled_o(TRISC&=0xef)#defineled_l(PORTC&=0xef)#defineled_h(PORTC|=0x10)#defineLCDPORTBuchardat1,dat2;//保存读出的温度zunsignedlongintdat;voiddelayms(uintx)//4M晶振下,延时1ms{uinty,z;for(y=x;y>0;y--)for(z=110;z>0;z--);}voidDs18b20_reset(void)//DS18B20初始化{uintcount;uchari,flag=1;temp_o;temp_l;for(count=60;count>0;count--);//延时480ustemp_i;while(flag){if(RC3)flag=1;elseflag=0;}led_o;led_l;//开指示灯for(count=60;count>0;count--);//延时480us}voidDs18b20_write(uchardatt)//向DS18B20写一个字节{ucharcount;uchari;temp_o;for(i=8;i>0;i--){temp_o;temp_l;for(count=1;count>0;count--);if(datt&0x01==0x01)temp_i;else{temp_o;temp_l;}for(count=23;count>0;count--);//延时60ustemp_i;for(count=1;count>0;count--);datt>>=1;}}ucharDs18b20_read(void)//从DS18B20读一个字节{uchari,datt;ucharcount;for(i=8;i>0;i--){datt>>=1;temp_o;temp_l;for(count=1;count>0;count--);temp_i;//改为输入方向时,上拉电阻把数据线拉高,释放总线,此语句必须有,参考datasheet的P15for(count=1;count>0;count--);if(RC3)datt|=0x80;for(count=23;count>0;count--);//延时60us}returndatt;}voidlcd_com(ucharcom)//向LCD1602写命令{rs_o;rw_o;en_o;TRISB=0x00;//配置RB为输出方向rs_l;rw_l;LCD=com;delayms(1);en_h;delayms(1);en_l;delayms(1);}voidlcd_dat(uchardat)//向LCD1602写数据{rs_o;rw_o;TRISB=0x00;//配置RB为输出方向en_o;rs_h;rw_l;LCD=dat;delayms(1);en_h;delayms(1);en_l;delayms(1);}voidlcd_write(ucharc,ucharr,uchardat)//向LCD1602指定行、指定列、写数据{lcd_com(0x80+0x40*c+r);lcd_dat(dat);delayms(1);}voidlcd_init(void)//LCD1602初始化,初始化后第一行显示temperature:,第二行显示.C{lcd_com(0x38);lcd_com(0x0c);lcd_com(0x06);lcd_write(0,2,0x54);lcd_write(0,3,0x65);lcd_write(0,4,0x6d);lcd_write(0,5,0x70);lcd_write(0,6,0x65);lcd_write(0,7,0x72);lcd_write(0,8,0x61);lcd_write(0,9,0x74);lcd_write(0,10,0x75);lcd_write(0,11,0x72);lcd_write(0,12,0x65);lcd_write(0,13,0x3a);lcd_write(1,11,0xdf);lcd_write(1,12,0x43);}voidshow(void)//把温度值送LCD1602显示{ucharflag;uchart[4];uinttemp;if(dat2>=240){dat=(~(dat2*256+dat1)+1)*(0.0625*10);//取反加一,保留一位小数flag=1;}else{dat=(dat2*256+dat1)*(0.0625*10);flag=0;};temp=dat%10;t[0]=(0x30+temp);temp=dat%100;temp=temp/10;t[1]=(0x30+temp);temp=dat%1000;temp=temp/100;t[2]=(0x30+temp);temp=dat/1000;t[3]=(0x30+temp);if(flag==1)//负温度显示{lcd_write(1,10,t[0]);lcd_write(1,9,0xa5);lcd_write(1,8,t[1]);lcd_write(1,7,t[2]);lcd_write(1,6,t[3]);lcd_write(1,5,0x2d);}if(flag==0)//正温度显示{lcd_write(1,10,t[0]);lcd_write(1,9,0xa5);lcd_write(1,8,t[1]);lcd_write(1,7,t[2]);lcd_write(1,6,t[3]);lcd_write(1,5,0x20);//显示空格,刷掉负号}}voidmain(void){lcd_init();while(1){Ds18b20_reset();Ds18b20_write(0xcc);Ds18b20_write(0x44);//发送温度转换命令delayms(1000);//延时1s,等待温度转换完成Ds18b20_reset();Ds18b20_write(0xcc);Ds18b20_write(0xbe);//发送读温度寄存器命令dat1=Ds18b20_read();dat2=Ds18b20_read();show();led_h;//关指示灯delayms(2000);}}//调试总结://某IO口8位未全使用时,对整个IO口读取进行位运算无效

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除。
换一批
延伸阅读

//软件思路:选择RAO做为模拟输入通道; // 连续转换4次再求平均值做为转换结果 // 最后结构只取低8位 // 结果送数码管的低3位显示 #include //包含内部资源预定义...

关键字: ad转换 pic16f877a picc

unsigned char eeprom_read(unsigned char addr);void eeprom_write(unsigned char addr, unsigned char value);这两个函数...

关键字: pic16f877a eeprom使用

//*****************电子园PIC16F877A开发板 SPI总线驱动74595实验***************////CKP=0; 空闲为低电平//CKP=1; 空闲为高电平//STAT_CKE=0;...

关键字: pic16f877a spi总线 驱动74595

用c语言写的一个时闹钟程序单片机用16F877,主时钟用20MHz,用32768作定时时间。可以实现2路定闹,每一路都可分别设置和开关,采用4x4键盘,16x2的字符型LCD显示。连线在程序开头有...

关键字: pic16f877a 时钟 程序 语言

/****************************************************标题:万年历**说明:包含文件,*****************************...

关键字: pic16f877a 万年历 程序

//数码管显示//★★★★★★★★★数码管显示★★★★★★★★★★★\\\\ //单征机型号:PIC16F877A //功能描述:采用动态扫描方式,初始时显示123,当按下RB0时,数值加一,当按下RB1时,数值减一...

关键字: pic16f877a 数码管显示

/************************************************PIC16F877A 内部EEPROM读写实验*******Author:hnrainDate:2010-12-26Ver...

关键字: pic16f877a 内部eeprom 内部函数 读写实验

/*Capture mode时,外部CCP1事件触发后,CCPR1H和CCPR1L将获取Timer1的TMR1H和TMR1L中的数值对于CCP1的触发事件,设置键CCP1Con中的相应位CCP1M3--CCP1M0CCP...

关键字: capture mode pic16f877a

PIR1 Register The PIR1 register contains the individual flag bits for the peripheralinterrupts.PIR1 REGISTERPS...

关键字: pic16f877a 单片机 spi学习
关闭