📄 ds18b20.txt
字号:
*----------------------------------------------------------
PC3 采 集 信 号
LED数码管,B口段,D口位,PC3为单总线数据口
罗 一 鸣
v1.1
修改时间:6月4日
修改内容:显示10进制,温度为0.1度阶梯
-----------------------------------------------------------*/
#include <iom8v.h>
#include <macros.h>
/*-----显示控制全局变量------*/
const char DispCode[]={0x3f,0x06,0x5b,0x4F,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7C,0x39,0x5E,0x79,0x71,0x80,0x00,0x40}; //数码管译码表
const char DispBit[] = {0b11101111,0b11011111,0b10111111,0b01111111};
/*常规延时*/
void delay(void)
{
unsigned char i;
for(i=0;i<0xff;i++)
{}
}
void delay_time(unsigned int time)
{
unsigned int i;
for(i=0;i<time;i++)
{}
}
/*显示函数*/
void display(unsigned char des[4])
{
PORTB=DispCode[des[0]];//显示小数位
PORTD=DispBit[0];
delay_time(200);
PORTB=DispCode[des[1]]+(1<<7);//显示小数点
PORTD=DispBit[1];
delay_time(200);
PORTB=DispCode[des[2]];
PORTD=DispBit[2];
delay_time(200);
PORTB=DispCode[des[3]];
PORTD=DispBit[3];
delay_time(200);
PORTB=0x00;
}
/* 十 六 进 制 显 示 调 整 ,供 显 示 十 六 进 制 使 用 */
void dis_hex(unsigned int num,unsigned char num_hex[4])
{
num_hex[0]=num&0xf;
num_hex[1]=(num>>4)&0xf;
num_hex[2]=(num>>8)&0xf;
num_hex[3]=(num>>12)&0xf;
}
void dis_dec(unsigned int temp_num,unsigned char num_dec[4])
{
signed char temp;
temp=(char)(temp_num>>4);
num_dec[3]=temp/100;
num_dec[0]=temp%100;
if ((temp>>15)==1)
{num_dec[3]=18;}
num_dec[2]=num_dec[0]/10;
num_dec[1]=num_dec[0]%10;
temp=((char)(temp_num)&0xf)*6/10;
num_dec[0]=temp;
}
/* 1.5us 延 时 */
void delay_us(unsigned int x) //1.5us左右
{
while(x)
{
x--;
}
}
/* 18b20 初 始 化 */
void init_18b20()
{
PORTC|=(1<<3);
PORTC&=~(1<<3);
delay_us(320); //480us以上
PORTC|=(1<<3);
DDRC&=~(1<<3);
delay_us(40); //15~60us
while(PINC&(1<<3))
{
;
}
DDRC|=(1<<3);
PORTC|=(1<<3);
delay_us(150); //60~240us
}
/* 18b20 写 操 作 函 数,写 一 个 指 令*/
write_18b20(unsigned char x)
{
unsigned char m;
for(m=0;m<8;m++)
{
PORTC&=~(1<<3);
if(x&(1<<m)) //写数据,从低位开始
PORTC|=(1<<3);
else
PORTC&=~(1<<3);
delay_us(40); //15~60us
PORTC|=(1<<3);
}
PORTC|=(1<<3);
}
/* 18b20 读 取 操 作*/
unsigned char read_18b20()
{
unsigned char temp,k,n;
temp="0";
DDRC|=(1<<3);
for(n=0;n<8;n++)
{
PORTC&=~(1<<3);
PORTC|=(1<<3);
DDRC&=~(1<<3);
k=(PINC&(1<<3)); //读数据,从低位开始
if(k)
temp|=(1<<n);
else
temp&=~(1<<n);
delay_us(40); //60~120us
DDRC|=(1<<3);
}
return (temp);
}
/*cahr 转 int*/
unsigned int turn_char_int(unsigned char chl,unsigned char chh)
{
unsigned int ch;
ch=chl+(int)(chh<<8);
return (ch);
}
/*端 口 初 始 化*/
void port_init(void)
{
DDRB=0xff;
DDRD=0xff;
DDRC=0b111111;
PORTB=0x00;
PORTC=0x00;
PORTD=0x00;
}
main()
{
char d[4];
volatile unsigned int pid;
OSCCAL=0x9d;
port_init();
WDTCR = 0x0F;
WDR();
while(1)
{
unsigned char teml,temh,temp,cyc;
unsigned int tem;
init_18b20();
write_18b20(0xcc);
write_18b20(0x44);
delay_us(400);
init_18b20();
WDR();
write_18b20(0xcc);
write_18b20(0xbe);
teml=read_18b20();
temh=read_18b20();
tem=turn_char_int(teml,temh);
WDR();
dis_dec(tem,d);
cyc=0xff;
while(cyc)
{
display(d);
WDR();
cyc--;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -