void usDelay(unsigned int value)
{
for(; value > 0; value --)
{
__nop();
}
}
void SET_GPIODIR_OUTPUT(unsigned char Channel)
{
switch(Channel)
{
case 0: LPC_GPIO1->FIODIR |= ((unsigned int )1<<30); break;
case 1: LPC_GPIO0->FIODIR |= (1<<25); break;
case 2: LPC_GPIO0->FIODIR |= (1<<26); break;
default: break;
}
}
void SET_GPIODIR_INPUT(unsigned char Channel)
{
switch(Channel)
{
case 0: LPC_GPIO1->FIODIR &= ~((unsigned int )1<<30); break;
case 1: LPC_GPIO0->FIODIR &= ~(1<<25); break;
case 2: LPC_GPIO0->FIODIR &= ~(1<<26); break;
default: break;
}
}
void SET_DQ(unsigned char Channel,unsigned char Bit)
{
switch(Channel)
{
case 0:
if(Bit == 1)
{
LPC_GPIO1->FIOSET |= (1<<30);
}
else
{
LPC_GPIO1->FIOCLR |= (1<<30);
}
break;
case 1:
if(Bit == 1)
{
LPC_GPIO0->FIOSET |= (1<<25);
}
else
{
LPC_GPIO0->FIOCLR |= (1<<25);
}
case 2:
if(Bit == 1)
{
LPC_GPIO0->FIOSET |= (1<<26);
}
else
{
LPC_GPIO0->FIOCLR |= (1<<26);
}
default: break;
}
}
unsigned char READ_DQ_INPUT(unsigned char Channel)
{
INT8U Rev;
switch(Channel)
{
case 0: Rev = ((LPC_GPIO1->FIOPIN & (1<<30)) >>30); break;
case 1: Rev = ((LPC_GPIO0->FIOPIN & (1<<25)) >>25); break;
case 2: Rev = ((LPC_GPIO0->FIOPIN & (1<<26)) >>26); break;
default: break;
}
return Rev;
}
unsigned char DsInit(unsigned char Channel)
{
SET_GPIODIR_OUTPUT(Channel);
SET_DQ(Channel,0);
usDelay(9600);
SET_DQ(Channel,1);
usDelay(1200);
SET_GPIODIR_INPUT(Channel);
if(READ_DQ_INPUT(Channel) == 0)
{
usDelay(4800);
if(READ_DQ_INPUT(Channel) == 1)
{
return 0;
}
}
return 1;
}
unsigned char Ds_ReadBit(unsigned char Channel)
{
INT8U Value = 0;
SET_GPIODIR_OUTPUT(Channel);
SET_DQ(Channel,0);
usDelay(40);
SET_DQ(Channel,1);
usDelay(100);
SET_GPIODIR_INPUT(Channel);
if(READ_DQ_INPUT(Channel))
{
Value = 1;
}
else
{
Value = 0;
}
usDelay(1000);
return Value;
}
unsigned char Ds_ReadByte(unsigned char Channel)
{
INT8U Value = 0;
INT8U i,j;
for(i = 1; i <= 8; i ++)
{
j = Ds_ReadBit(Channel);
Value = ((j<<7)|(Value>>1));
}
return Value;
}
void Ds_WriteByte(unsigned char Value,unsigned char Channel)
{
INT8U i;
INT8U Val;
SET_GPIODIR_OUTPUT(Channel); /* ??GPIO_P3.26?????*/
for(i = 1; i <= 8; i ++)
{
Val = Value&0x01; /* ??0X01??????*/
Value >>=1; /* ??????*/
if(Val) /* ????1,write 1*/
{
SET_DQ(Channel,0); /* DQ ??*/
usDelay(220); /* ??11us*/
SET_DQ(Channel,1); /* DQ ??*/
usDelay(1300); /* ??65us*/
}
else /* ????0,write 0*/
{
SET_DQ(Channel,0); /* DQ ??*/
usDelay(1300); /* ??65us*/
SET_DQ(Channel,1); /* DQ ??*/
usDelay(60); /* ??3us*/
}
}
}
float Ds_GetTempValue(unsigned char Channel)
{
float value;
//float tempvalue;
unsigned char low,high; /* ????DS18B20????*/
// unsigned char i;
//unsigned char tempure_buf[3];
while(DsInit(Channel)) /* ?????DS18B20*/
{
return 0; /* ??DS18B20??????*/
}
Ds_WriteByte(0xcc,Channel); /* ??ROM????*/
Ds_WriteByte(0x44,Channel); /* ??????*/
while(DsInit(Channel)) /* ?????DS18B20*/
{
return 0; /* ??DS18B20??????*/
}
Ds_WriteByte(0xCC,Channel); /* ??ROM????*/
Ds_WriteByte(0xbe,Channel); /* ?????????*/
low = Ds_ReadByte(Channel); /* ???????????*/
high = Ds_ReadByte(Channel);
value = (high<<8) | low; /* ????????,???????????????*/
value=(value * 0.062510)+0.5;
return value;
}
void GetTempValue(unsigned char TempChannel)
{
int Send_value = 0x00;
unsigned char Send_High_Data=0x00,Send_Low_Data=0x00;
unsigned char Low_value,High_value; /* ????DS18B20????*/
while(DsInit(TempChannel)) /* ?????DS18B20*/
{
return ; /* ??DS18B20??????*/
}
Ds_WriteByte(0xcc,TempChannel); /* ??ROM????*/
Ds_WriteByte(0x44,TempChannel); /* ??????*/
while(DsInit(TempChannel)) /* ?????DS18B20*/
{
return ; /* ??DS18B20??????*/
}
Ds_WriteByte(0xCC,TempChannel); /* ??ROM????*/
Ds_WriteByte(0xbe,TempChannel); /* ?????????*/
Low_value = Ds_ReadByte(TempChannel); /* ???????????*/
High_value = Ds_ReadByte(TempChannel);
Send_value = (High_value<<8) | Low_value;
if(TempChannel == TEMPERETUREC)
{
Send_value=((Send_value * 0.062510)+0.5 +0.3)*10;
}
else if(TempChannel == TEMPERETUREB)
{
Send_value=((Send_value * 0.062510)+0.5 +1.2)*10;
}
else if(TempChannel == TEMPERETUREA)
{
Send_value=((Send_value * 0.062510)+0.5 +1.5)*10;
}
Send_Low_Data = (Send_value|Send_Low_Data);
Send_High_Data = (Send_value>>8) |Send_High_Data;
Uart0Dev.Buf[8] = Send_High_Data;
Uart0Dev.Buf[9] = Send_Low_Data;
uartSendCom(24); //发送数据
}