本文引用地址:http://m.butianyuan.cn/article/201611/321024.htmvoid WRITE_LCD_CMD(char cmd)
{
LCD1602_BUSY();
TRISB & =0XF8;
TRISD=0;
LCDDATA=cmd;
LCDRS=0;
LCDRW=0;
LCDE=0;
asm("NOP");
asm("NOP");
LCDE=1;
}
void LCD1602_INIT(void)
{
LCD1602_BUSY();
TRISB &= 0XF8;
TRISD=0;
WRITE_LCD_CMD(CLRLCD);
WRITE_LCD_CMD(LCDMOD);
WRITE_LCD_CMD(TURNON);
WRITE_LCD_CMD(CURMODE);
LCD_Display_location (0,0);
}
void DS18B20_INIT()
{
TRIS_B20=0;
DS18B20=0;
US_delay(20);
TRIS_B20=1;
US_delay(10);
}
void WRITE_DS18B20_CMD(char cmd)
{
char tmp;
char i;
TRIS_B20=0;
for(tmp=8;tmp>0;tmp--)
{
TRIS_B20=0;
DS18B20= 0;
asm ("NOP");
asm ("NOP");
asm ("NOP");
asm ("NOP");
asm ("NOP");
if (cmd & 0x01)
{
TRIS_B20=1;
US_delay(1);
for (i=5;i>0;i--);
}
else
{
DS18B20=0 ;
US_delay(1);
for (i=5;i>0;i--);
TRIS_B20=1;
}
cmd=cmd/2;
}
}
float READ_DS18B20 ()
{
char tmp=0x01;
float t;
union
{
char c[2];
int x;
}temp;
temp.x=0;
while (tmp)
{ // read first 8 bits
TRIS_B20=0;
DS18B20=0;
asm("NOP");
TRIS_B20=1;
if (DS18B20)
temp.c[0] |= tmp;
tmp=tmp<<1;
US_delay(2);
}
tmp=1;
while (tmp)
{ // read first 8 bits
TRIS_B20=0;
DS18B20=0;
asm("NOP");
TRIS_B20=1; // release the bus
asm("NOP");
if (DS18B20) // "1" presented
temp.c[1] |= tmp;
tmp=tmp<<1;
US_delay(2);
}
t=((float) temp.x)/16.0 ;
return t;
}
void itoa10(unsigned char *buf, int i)
{
unsigned int rem;
unsigned char *s,length=0;
s = buf;
if (i == 0)
*s++ = 0;
else
{
if (i < 0)
{
*buf++ = -;
s = buf;
i = -i;
}
while (i)
{
++length;
rem = i % 10;
*s++ = rem + 0;
i /= 10;
}
for(rem=0; ((unsigned char)rem) {
*(buf+length) = *(buf+((unsigned char)rem));
*(buf+((unsigned char)rem)) = *(buf+(length-((unsigned char)rem)-1));
*(buf+(length-((unsigned char)rem)-1)) = *(buf+length);
}
}
*s=0;
}
void US_delay( int i)
{
unsigned char j;
while(i--)
{
j=3;
while(j--);
}
}
評論