uchar table[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,
0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,
0x0e,0x0f}; //数码管的BCD码
//*************************************************************************
// 初始化子程序
//*************************************************************************
void system_init()
{
SFIOR=(1<<2)|SFIOR; //PUD=1;关闭上拉电阻
PORTD=0x70;
DDRD=0x70;
}
void ch451_init()
{
din0; //先低后高,选择4线输入
din1;
}
//*************************************************************************
// 输出命令子程序
// 定义一无符号整型变量存储12字节的命令字
//************************************************************************
void ch451_write(unsigned int command)
{
unsigned char i;
load0; //命令开始
for(i=0;i<12;i++)
{ //送入12位数据,低位在前
if(command&1)
{
din1;
}
else
din0;
dclk0;
dclk1; //上升沿有效
command>>=1;
}
load1; //加载数据
}
//*************************************************************************
// 显示函数
//*************************************************************************
void display(uint b_d,uint s_d,uint g_d)
{
ch451_init();
ch451_write(CH451_SYSOFF); //关显示、键盘、看门狗
ch451_write(CH451_SYSON1); //开显示
ch451_write(CH451_BCD); //设置BCD译码方式
ch451_write(CH451_TWINKLE); //设置闪烁控制
ch451_write(CH451_DIG0|table[b_d]); //显示百位数据
ch451_write(CH451_DIG1|table[s_d]|0x80); //显示十位数据
ch451_write(CH451_DIG2|table[g_d]); //显示个位数据
ch451_write(CH451_DIG3|led3); //其他数码管不显示,熄灭状态
ch451_write(CH451_DIG4|led4);
ch451_write(CH451_DIG5|led5);
ch451_write(CH451_DIG6|led6);
ch451_write(CH451_DIG7|led7);
}
//*************************************************************************
// DS18B20初始化
//*************************************************************************
unsigned char ds1820_reset(void) //初始化和复位
{
unsigned char i;
DQ_OUT;
DQ_CLR;
delay_us(500); //延时500uS(480-960)
DQ_SET;
DQ_IN;
delay_us(80); //延时80uS
i = DQ_R;
delay_us(500); //延时500uS(保持>480uS)
if (i)
{
return 0x00;
}
else
{
return 0x01;
}
}
//*************************************************************************
// DS18B20读一个字节函数
//*************************************************************************
unsigned char ds1820_read_byte(void)
{
unsigned char i;
unsigned char value = 0;
for (i = 8; i != 0; i--)
{
value >>= 1;
DQ_OUT;
DQ_CLR;
delay_us(4); //*延时4uS
DQ_SET;
DQ_IN;
delay_us(10); //*延时10uS
if (DQ_R)
{
value|=0x80;
}
delay_us(60); //*延时60uS
}
return(value);
}
//*************************************************************************
// 向18B20写一个字节函数
//*************************************************************************
/*DS18B20字节写入函数*/
void ds1820_write_byte(unsigned char value)
{
unsigned char i;
for (i = 8; i != 0; i--)
{
DQ_OUT;
DQ_CLR;
delay_us(4); //延时4uS
if (value & 0x01)
{
DQ_SET;
}
delay_us(80); //延时80uS
DQ_SET; //位结束
上一页 [1] [2] [3] 下一页
本文关键字:温度传感器 程序 AVR单片机,单片机-工控设备 - AVR单片机