📄 1-wire.c
字号:
#include <avr/io.h>
#include <stdlib.h>
#include <util/delay.h>
#include "1-wire.h"
#include "lcd.h"
char buf[8];
unsigned char RESET_PULSE(void)
{
unsigned char PRESENCE;
CLEAR_1Wire;
_delay_us(500);
SET_1Wire;
_delay_us(30);
if (bit_is_clear(PORT_1Wire, WE)) { PRESENCE = 1;} else { PRESENCE = 0; }
_delay_us(470);
if (bit_is_set(PORT_1Wire, WE)) { PRESENCE = 1;} else { PRESENCE = 0; }
return PRESENCE;
}
void send(char bit)
{
CLEAR_1Wire;
_delay_us(5);
if (bit == 1) SET_1Wire;
_delay_us(80);
SET_1Wire;
}
unsigned char read(void)
{
unsigned char PRESENCE = 0;
CLEAR_1Wire;
_delay_us(2);
SET_1Wire;
_delay_us(15);
if (bit_is_set(PORT_1Wire,WE)) PRESENCE =1; else PRESENCE = 0;
return PRESENCE;
}
void send_byte(char wartosc)
{
unsigned char i;
unsigned char pom;
for (i = 0; i < 8; i++)
{
pom = wartosc>>1;
pom &= 0x01;
send(pom);
}
_delay_us(100);
}
unsigned char read_byte(void)
{
unsigned char i;
unsigned char wartosc = 0;
for (i=0; i<8; i++)
{
if (read()) wartosc|=0x01<<i;
_delay_us(15);
}
return wartosc;
}
void get_this_stupid_temperature_now(void)
{
unsigned char sprawdz;
char temp1=0, temp2=0;
sprawdz=RESET_PULSE();
if (sprawdz == 1)
{
send_byte(0xCC);
send_byte(0x44);
_delay_us(750);
sprawdz = RESET_PULSE();
send_byte(0xCC);
send_byte(0xBE);
temp1 = read_byte();
temp2 = read_byte();
sprawdz = RESET_PULSE();
float temp = 0;
//int temp = 0;
//temp = (int)(temp1+(temp2*256))/16;
//int temp = 0;
temp = (float)(temp1+(temp2*256))/16;
//temp = (temp1+(temp2*256))/16;
dtostrf(temp,1,1,buf);
//ltoa(temp,buf,10);
//display buf
lcd_str(buf);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -