OW_Reset(&PORTD, 2); // 'Reset command to initialize One-Wire
OW_Write(&PORTD, 2, 0xCC); // 'Skip ROM Command
OW_Write(&PORTD, 2, 0x44); // 'Convert_T command
delay_ms(1200); // 'Provide delay for conversion
OW_Reset(&PORTD, 2); // 'Reset command to initialize One-Wire
OW_Write(&PORTD, 2, 0xCC); // 'Skip ROM Command
OW_Write(&PORTD, 2, 0xBE); // 'Read Scratchpad Command
TempL = OW_Read(&PORTD,2); //Read Temperature low byte
TempH = OW_Read(&PORTD,2); //Read Temperature high byte
DP = TempL & 0x01; // 'Check if Temperature is integer or fractional
if (TempH){ //If reading is negative
vDisp[0] = '-';
TempL = ((~TempL) + 1) >> 1;
}
else{
vDisp[0] = '+';
TempL = TempL >> 1; // 'Shift one position right (divide by 2) to get integer reading and get rid of decimal point
}
vDisp[1] = (TempL / 100) + 48; // 'Get hundreds and convert to ASCII
vDisp[2] = ((TempL / 10) % 10) + 48; // 'Get tens and convert to ASCII
vDisp[3] = (TempL % 10) + 48; // 'Get units and convert to ASCII
if (DP){ // 'If reading is fractional, ie has 0.5 at end
vDisp[5] = '5';
}
else{ // 'If reading is a whole number
vDisp[5] = '0';
}
#define Time_Pulse_Delay_Low 10 //10
#define Time_Pulse_Delay_High 40 //40
#define Time_Reset_Low 500
#define Time_After_Reset 350
#define Time_Hold_Down 10
#define One_Wire_Read_ROM 0x33
#define One_Wire_Skip_ROM 0xCC
#define One_Wire_Search_ROM 0xF0
#define One_Wire_Match_ROM 0x55
#define One_Wire_Success 0x00
#define One_Wire_Error_No_Echo 0x01
#define One_Wire_Bus_Low_Error 0x02
#define One_Wire_Device_Busy 0x03
#define One_Wire_CRC_Error 0x04
unsigned int One_Wire_Reset(void);
void One_Wire_Write_Byte(unsigned char Byte);
unsigned char One_Wire_Read_Byte(void);
unsigned char One_Wire_Read_Bit (void);
void One_Wire_Write_Bit (unsigned char Bit);
unsigned int One_Wire_Reset(void)
{
unsigned int tmp;
if ((PIN_SYG(One_Wire_RX))) return One_Wire_Bus_Low_Error;
PIN_OFF(One_Wire_TX);
delay_us(250);
delay_us(250);
PIN_ON(One_Wire_TX);
delay_us(Time_Pulse_Delay_High);
if ((PIN_SYG(One_Wire_RX))) tmp = One_Wire_Success;
else tmp = One_Wire_Error_No_Echo;
delay_us(250);
delay_us(200);
return tmp;
}
void One_Wire_Write_Byte(unsigned char Byte)
{
unsigned char cnt;
for (cnt=0;cnt!=8;cnt++) One_Wire_Write_Bit(Byte&(1<<cnt));
}
void One_Wire_Write_Bit (unsigned char Bit)
{
PIN_OFF(One_Wire_TX);
if (Bit==0)
{
delay_us(Time_Pulse_Delay_High);
PIN_ON(One_Wire_TX);
delay_us(Time_Pulse_Delay_Low);
}
else
{
delay_us(Time_Pulse_Delay_Low);
PIN_ON(One_Wire_TX);
delay_us(Time_Pulse_Delay_High);
}
}
unsigned char One_Wire_Read_Byte(void)
{
unsigned char tmp=0;
unsigned char cnt;
for (cnt=0;cnt!=8;cnt++)
if (One_Wire_Read_Bit()!=0) tmp|=(1<<cnt);
delay_us(Time_Pulse_Delay_High);
return tmp;
}
unsigned char One_Wire_Read_Bit (void)
{
unsigned char tmp;
PIN_OFF(One_Wire_TX);
delay_us(Time_Hold_Down);
PIN_ON(One_Wire_TX);
delay_us(Time_Pulse_Delay_Low);
if ((PIN_SYG(One_Wire_RX))!=0) tmp = 0;
else tmp = 1;
delay_us(Time_Pulse_Delay_High);
return tmp;
}
unsigned char DS1822_Start_Conversion_Skip_Rom (void)
{
unsigned char tmp;
tmp=One_Wire_Reset();
if (tmp!=One_Wire_Success) return tmp;
One_Wire_Write_Byte(One_Wire_Skip_ROM);
One_Wire_Write_Byte(DS1822_CONVERT_T_CMD);
if (One_Wire_Read_Byte()==0) return One_Wire_Success;
else return One_Wire_Device_Busy;
}
unsigned char DS1822_Read_Temp_NoCRC_Skip_Rom (unsigned int * temperatura)
{
unsigned char tmp;
tmp=One_Wire_Reset();
if (tmp!=One_Wire_Success) return tmp;
One_Wire_Write_Byte(One_Wire_Skip_ROM);
One_Wire_Write_Byte(DS1822_READ_STRATCHPAD_CMD);
* temperatura = One_Wire_Read_Byte()|(One_Wire_Read_Byte()<<8);
return One_Wire_Success;
}
Set pin hi/low or read pin state.What is PIN_ON, PIN_SYG, PIN_OFF?
unsigned int One_Wire_Reset(void)
{
unsigned int tmp;
PIN_ON(One_Wire_Pin);
PIN_OUT_OD(One_Wire_Pin);
if ((PIN_SYG(One_Wire_Pin)==0)) return One_Wire_Bus_Low_Error;
PIN_OFF(One_Wire_Pin);
delay_us(250);
delay_us(250);
PIN_ON(One_Wire_Pin);
delay_us(Time_Pulse_Delay_High);
if ((PIN_SYG(One_Wire_Pin))) tmp = One_Wire_Error_No_Echo;
else tmp = One_Wire_Success;
delay_us(250);
delay_us(200);
return tmp;
}
void One_Wire_Write_Byte(unsigned char Byte)
{
unsigned char cnt;
for (cnt=0;cnt!=8;cnt++) One_Wire_Write_Bit(Byte&(1<<cnt));
}
void One_Wire_Write_Bit (unsigned char Bit)
{
PIN_OFF(One_Wire_Pin);
if (Bit==0)
{
delay_us(Time_Pulse_Delay_High);
PIN_ON(One_Wire_Pin);
delay_us(Time_Pulse_Delay_Low);
}
else
{
delay_us(Time_Pulse_Delay_Low);
PIN_ON(One_Wire_Pin);
delay_us(Time_Pulse_Delay_High);
}
}
unsigned char One_Wire_Read_Byte(void)
{
unsigned char tmp=0;
unsigned char cnt;
for (cnt=0;cnt!=8;cnt++)
if (One_Wire_Read_Bit()!=0) tmp|=(1<<cnt);
delay_us(Time_Pulse_Delay_High);
return tmp;
}
unsigned char One_Wire_Read_Bit (void)
{
unsigned char tmp;
//PIN_OUT_PP(One_Wire_Pin);
PIN_OFF(One_Wire_Pin);
delay_us(Time_Hold_Down);
PIN_ON(One_Wire_Pin);
delay_us(Time_Pulse_Delay_Low);
if ((PIN_SYG(One_Wire_Pin))!=0) tmp = 1;
else tmp = 0;
delay_us(Time_Pulse_Delay_High);
return tmp;
}
// Easyrider83's code
#define Time_Pulse_Delay_Low 10 //10
#define Time_Pulse_Delay_High 40 //40
#define Time_Reset_Low 500
#define Time_After_Reset 350
#define Time_Hold_Down 10
#define One_Wire_Read_ROM 0x33
#define One_Wire_Skip_ROM 0xCC
#define One_Wire_Search_ROM 0xF0
#define One_Wire_Match_ROM 0x55
#define One_Wire_Success 0x00
#define One_Wire_Error_No_Echo 0x01
#define One_Wire_Bus_Low_Error 0x02
#define One_Wire_Device_Busy 0x03
#define One_Wire_CRC_Error 0x04
#define One_Wire_Pin 0
void PIN_ON(int ignoreme)
{ // IS IT OK? //
TRISD = 0xFF;
PORTD.B2 = 1;
}
void PIN_OFF(int ignoreme)
{ // IS IT OK?
TRISD = 0xFF;
PORTD.B2 = 0;
}
void PIN_OUT_OD(int ignoreme)
{ // IS IT OK?
TRISD = 0x00;
}
int PIN_SYG(int ignoreme)
{ // IS IT OK?
return PORTD.B2;
}
unsigned int One_Wire_Reset(void)
{
unsigned int tmp;
PIN_ON(One_Wire_Pin);
PIN_OUT_OD(One_Wire_Pin);
if ((PIN_SYG(One_Wire_Pin)==0)) return One_Wire_Bus_Low_Error;
PIN_OFF(One_Wire_Pin);
delay_us(250);
delay_us(250);
PIN_ON(One_Wire_Pin);
delay_us(Time_Pulse_Delay_High);
if ((PIN_SYG(One_Wire_Pin))) tmp = One_Wire_Error_No_Echo;
else tmp = One_Wire_Success;
delay_us(250);
delay_us(200);
return tmp;
}
void One_Wire_Write_Bit (unsigned char Bit) ;
void One_Wire_Write_Byte(unsigned char Byte)
{
unsigned char cnt;
for (cnt=0;cnt!=8;cnt++) One_Wire_Write_Bit(Byte&(1<<cnt));
}
void One_Wire_Write_Bit (unsigned char Bit)
{
PIN_OFF(One_Wire_Pin);
if (Bit==0)
{
delay_us(Time_Pulse_Delay_High);
PIN_ON(One_Wire_Pin);
delay_us(Time_Pulse_Delay_Low);
}
else
{
delay_us(Time_Pulse_Delay_Low);
PIN_ON(One_Wire_Pin);
delay_us(Time_Pulse_Delay_High);
}
}
unsigned char One_Wire_Read_Bit (void)
{
unsigned char tmp;
//PIN_OUT_PP(One_Wire_Pin);
PIN_OFF(One_Wire_Pin);
delay_us(Time_Hold_Down);
PIN_ON(One_Wire_Pin);
delay_us(Time_Pulse_Delay_Low);
if ((PIN_SYG(One_Wire_Pin))!=0) tmp = 1;
else tmp = 0;
delay_us(Time_Pulse_Delay_High);
return tmp;
}
unsigned char One_Wire_Read_Byte(void)
{
unsigned char tmp=0;
unsigned char cnt;
for (cnt=0;cnt!=8;cnt++)
if (One_Wire_Read_Bit()!=0) tmp|=(1<<cnt);
delay_us(Time_Pulse_Delay_High);
return tmp;
}
unsigned char DS1822_Start_Conversion_Skip_Rom (void)
{
unsigned char tmp;
tmp=One_Wire_Reset();
if (tmp!=One_Wire_Success) return tmp;
One_Wire_Write_Byte(One_Wire_Skip_ROM);
One_Wire_Write_Byte(DS1822_CONVERT_T_CMD);
if (One_Wire_Read_Byte()==0) return One_Wire_Success;
else return One_Wire_Device_Busy;
}
unsigned char DS1822_Read_Temp_NoCRC_Skip_Rom (unsigned int * temperatura)
{
unsigned char tmp;
tmp=One_Wire_Reset();
if (tmp!=One_Wire_Success) return tmp;
One_Wire_Write_Byte(One_Wire_Skip_ROM);
One_Wire_Write_Byte(DS1822_READ_STRATCHPAD_CMD);
* temperatura = One_Wire_Read_Byte()|(One_Wire_Read_Byte()<<8);
return One_Wire_Success;
}
#define DS1822_CONVERT_T_CMD 0x44
#define DS1822_WRITE_STRATCHPAD_CMD 0x4E
#define DS1822_READ_STRATCHPAD_CMD 0xBE
#define DS1822_COPY_STRATCHPAD_CMD 0x48
#define DS1822_RECALL_E_CMD 0xB8
#define DS1822_READ_POWER_SUPPLY_CMD 0xB4
#define DS1822_STRATCHPAD_SIZE 0x09
#define DS1822_SERIAL_NUM_SIZE 0x08
what is
DS1822_CONVERT_T_CMD
?
and
DS1822_READ_STRATCHPAD_CMD
?
Can you check my code?
Code:// Easyrider83's code #define Time_Pulse_Delay_Low 10 //10 #define Time_Pulse_Delay_High 40 //40 #define Time_Reset_Low 500 #define Time_After_Reset 350 #define Time_Hold_Down 10 #define One_Wire_Read_ROM 0x33 #define One_Wire_Skip_ROM 0xCC #define One_Wire_Search_ROM 0xF0 #define One_Wire_Match_ROM 0x55 #define One_Wire_Success 0x00 #define One_Wire_Error_No_Echo 0x01 #define One_Wire_Bus_Low_Error 0x02 #define One_Wire_Device_Busy 0x03 #define One_Wire_CRC_Error 0x04 unsigned char One_Wire_Reset(void) { unsigned char tmp; TRISD.B2 = 1; //switch for input if (PORTD.B2 == 0) return One_Wire_Bus_Low_Error; TRISD.B2 = 0; //switch for output PORTD.B2 = 0; //force to low delay_us(500); TRISD.B2 = 1; //switch for input delay_us(Time_Pulse_Delay_High); if (PORTD.B2) tmp = One_Wire_Error_No_Echo; else tmp = One_Wire_Success; delay_us(450); return tmp; } void One_Wire_Write_Byte(unsigned char Byte) { unsigned char cnt; for (cnt=0;cnt!=8;cnt++) One_Wire_Write_Bit(Byte&(1<<cnt)); } void One_Wire_Write_Bit (unsigned char Bit) { TRISD.B2 = 0; PORTD.B2 = 0; if (Bit==0) { delay_us(Time_Pulse_Delay_High); PORTD.B2 = 1; delay_us(Time_Pulse_Delay_Low); } else { delay_us(Time_Pulse_Delay_Low); PORTD.B2 = 1; delay_us(Time_Pulse_Delay_High); } } unsigned char One_Wire_Read_Bit (void) { unsigned char tmp; TRISD.B2 = 0; PORTD.B2 = 0; delay_us(Time_Hold_Down); TRISD.B2 = 1; delay_us(Time_Pulse_Delay_Low); if (PORTD.B2) tmp = 1; else tmp = 0; delay_us(Time_Pulse_Delay_High); return tmp; } unsigned char One_Wire_Read_Byte(void) { unsigned char tmp=0; unsigned char cnt; for (cnt=0;cnt!=8;cnt++) if (One_Wire_Read_Bit()!=0) tmp|=(1<<cnt); delay_us(Time_Pulse_Delay_High); return tmp; } unsigned char DS1822_Start_Conversion_Skip_Rom (void) { unsigned char tmp; tmp=One_Wire_Reset(); if (tmp!=One_Wire_Success) return tmp; One_Wire_Write_Byte(One_Wire_Skip_ROM); One_Wire_Write_Byte(DS1822_CONVERT_T_CMD); if (One_Wire_Read_Byte()==0) return One_Wire_Success; else return One_Wire_Device_Busy; } unsigned char DS1822_Read_Temp_NoCRC_Skip_Rom (unsigned int * temperatura) { unsigned char tmp; tmp=One_Wire_Reset(); if (tmp!=One_Wire_Success) return tmp; One_Wire_Write_Byte(One_Wire_Skip_ROM); One_Wire_Write_Byte(DS1822_READ_STRATCHPAD_CMD); * temperatura = One_Wire_Read_Byte()|(One_Wire_Read_Byte()<<8); return One_Wire_Success; }
unsigned char One_Wire_Reset(void)
{
unsigned char tmp;
TRISD.B2 = 1; //switch for input
if (PORTD.B2 == 0) return One_Wire_Bus_Low_Error;
TRISD.B2 = 0; //switch for output
PORTD.B2 = 0; //force to low
delay_us(500);
TRISD.B2 = 1; //switch for input
delay_us(Time_Pulse_Delay_High);
[COLOR="#00FF00"] if (PORTD.B2) tmp = One_Wire_Error_No_Echo;[/COLOR]
else tmp = One_Wire_Success;
delay_us(450);
return tmp;
}
main()
{
--- WAIT A WHILE
--- READ TEMPERATURE RIDERS METHOD TWICE
--- INIT USB
--- SEND DATA
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?