DS18B20和ADC0832共同使用
时间:12-01
来源:互联网
点击:
#include
#define uchar unsigned charsbit DQ=P1^4;uchar data temp_data[2]={0x00,0x00};uchar data display[5]={0x00,0x00,0x00,0x00,0x00};unsigned char code ditab[16] ={0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09} ;//共阴数码管uchar code table[11] ={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};// 0,1,2,3,4,5 ,6,7,8,9,百位或当最高位十位为0时不显示uchar code table1[10]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};// 0.,1.,2.,3.,4.,5.,6.,7.,8.,9.各位带有小数点的显示//以下为水位显示区域#define uchar unsigned char#define uint unsigned int#define ulong unsigned long#define _rrca_() CY = ACC & 0x01 //产生RRC A指令#define _rlca_() CY = ACC & 0x80 //产生RLC A指令sbit bADcs=P1^7;sbit bADcl=P1^6;sbit bADda=P1^5;sbit flag=P1^3;//sbit str=P2^7;//sbit d=P2^6;//sbit clk=P2^5;bit choose=0;unsigned char data1;unsigned char count;unsigned char cycle;uint ge,xiao1,xiao2;unsigned long data2;unsigned char code tablewe[]={0xFE,0xFD,0xFB,0xF7,0xEF,0xDF,0xBF,0x7F};//自己写的void delay_18b20(unsigned int i){while(i--);}//函数区域水位void Delayus(uchar i){while(--i);}void ad(void){ uchar i;//data1=0;bADcs = 0;//当ADC0832未工作时其CS输入端应为高电平,此时芯片禁用.bADcl=0;bADda=1;bADcl=1;bADcl=0;//i downbADda=1;bADcl=1;bADcl=0; // 2 downbADda=0;bADcl=1;bADcl=0; // 3 downbADda=1;bADcl=1;bADcl=0; // 4 downfor(i=8;i>0;i--){data1<=1;bADcl=0;bADcl=1;if(bADda==1) data1|=0x01;bADda=1;}bADcs=1;}void cal(){data2=((ulong)data1*493)/252.5;ge=data2/100;xiao1=(data2%100)/10;xiao2=data2%10;}display2(){P2=tablewe[4]; //P2口低四位位选P0=0x00; //P0口数码管段选第五个数码管不用delay_18b20(50);P2=tablewe[5];P0=table1[ge];delay_18b20(50);P2=tablewe[6];P0=table[xiao1];delay_18b20(50);P2=tablewe[7];P0=table[xiao2];}//以上为水位函数//初始化函数Init_DS18B20(void){unsigned char x=0;DQ=1; //DQ复位delay_18b20(8); //稍做延时DQ=0; //单片机将DQ拉低delay_18b20(80); //精确延时 大于 480usDQ=1; //拉高总线delay_18b20(14);x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败delay_18b20(20);}ReadOneChar(void){unsigned char i=0;unsigned char dat=0;for(i=8;i>0;i--){DQ=0; // 给脉冲信号dat>>=1;DQ=1; // 给脉冲信号if(DQ)dat|=0x80 ;delay_18b20(4) ;}return (dat) ;}WriteOneChar(unsigned char dat){unsigned char i=0;for(i=8;i>0;i--){DQ=0;DQ=dat&0x01;delay_18b20(5);DQ=1;dat>>=1;}}Read_Temperature(void){Init_DS18B20() ;WriteOneChar(0xCC) ; // 跳过读序号列号的操作WriteOneChar(0x44) ; // 启动温度转换Init_DS18B20() ;WriteOneChar(0xCC) ; //跳过读序号列号的操作WriteOneChar(0xBE) ; //读取温度寄存器temp_data[0] = ReadOneChar(); //温度低8位temp_data[1] = ReadOneChar(); //温度高8位}Disp_Temperature(){display[4]=temp_data[0]&0x0f;display[0]=ditab[display[4]]; //查表得小数位的值display[4]=((temp_data[0]&0xf0)>>4)|((temp_data[1]&0x0f)<4);display[3]=display[4]/100 ;display[1]=display[4]%100 ;display[2]=display[1]/10;display[1]=display[1]%10;if(display[3]==0) //高位为0,不显示{display[3]=10 ; if(display[2]==0) //次高位为0,不显示display[2]=10 ;}}display1(){P2=tablewe[0]; //P2口低四位位选P0=table[display[3]]; //P0口数码管段选delay_18b20(50);P2=tablewe[1];P0=table[display[2]];delay_18b20(50);P2=tablewe[2];P0=table1[display[1]];delay_18b20(50);P2=tablewe[3];P0=table[display[0]];}void main(){while(1){ Read_Temperature();Disp_Temperature();display1();ad();cal();display2();}}这个程序可以使用,但是就是没有办法显示低温零度以下的,求修改!
DS18B20ADC0832共同使 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)