ds 18 b 20を用いて温度を試験し、LCD 1602に表示する


メイン関数ファイル:
#include 
#include 
#include 
#include "./delay/delay.h"

sbit ds = P1^0;
bit ack = 0;

sbit RS = P2^4;
sbit RW = P2^5;
sbit E  = P2^6;

#define LCDPORT P0
#define LCD_WRITE_DATA 1
#define LCD_WRITE_COM  0

void lcd1602_write(unsigned char byte,unsigned char flag)
{
	  if(flag)
		{
			  RS = 1;
		}
		else
		{
			  RS = 0; 
		}
		RW = 0; 
		E = 1; 
		LCDPORT = byte;
		delay_us(5);
		E = 0; 
}

void lcd1602_dis_str(unsigned char x, unsigned char y, unsigned char *str)
{
	  if((x > 15) || (y > 1))
		{
			  return ;
		}
		if(0 == y)
		{
			  lcd1602_write(0x80 + x,LCD_WRITE_COM);
		}
		else
		{
			  lcd1602_write(0x80 + 0x40 + x,LCD_WRITE_COM);
		}
		while(*str != '\0')
		{
			  lcd1602_write(*str,LCD_WRITE_DATA);
			  str++;
		}
}

void lcd_init()
{
	  delay_ms(15);
	  lcd1602_write(0x38,LCD_WRITE_COM); 
	  delay_ms(5);
		lcd1602_write(0x38,LCD_WRITE_COM); 
	  delay_ms(5);
		lcd1602_write(0x38,LCD_WRITE_COM);
	  delay_ms(5);
		lcd1602_write(0x38,LCD_WRITE_COM); 
	  delay_ms(5);
		lcd1602_write(0x08,LCD_WRITE_COM); 
	  delay_ms(5);
		lcd1602_write(0x01,LCD_WRITE_COM);
	  delay_ms(5);
	  lcd1602_write(0x06,LCD_WRITE_COM); 
	  delay_ms(5);
		lcd1602_write(0x0c,LCD_WRITE_COM); 
	  delay_ms(5);
}

void ds_reset()
{
	  ds = 1;
	  ds = 0;
	  delay_us(200);
	  delay_us(200);
	  ds = 1;
	  delay_us(30);
	  if(0 == ds)
		{
			  ack = 1;
		}
		else
		{
			  ack = 0;
		}
		delay_us(200);
		delay_us(100);
}

void ds_send_byte(unsigned char byte)
{
	  unsigned char i;
	  for(i = 0; i < 8; i++)
	  {
			  ds = 0;
			  _nop_();
			  _nop_();
			  ds = byte & 0x01;
			  byte >>= 1;
			  delay_us(30);
			  ds = 1;
		}
		delay_us(30);
}

bit ds_read_bit()
{
	  bit tmp;
	  ds = 1;
	  ds = 0;
	  _nop_();
	  _nop_();
	  ds = 1;
	  tmp = ds;
	  delay_us(30);
	  return tmp;
}

unsigned char ds_read_byte()
{
	  unsigned char i,j,k;
	  for(i = 0; i < 8; i++)
	  {
			  j = ds_read_bit();
			  k = (j << 7) | (k >> 1);
		}
		return k;
}

void main()
{
	  unsigned char a;
	  unsigned int temp;
	  unsigned int b;
	
	  float wendu;
	
	  unsigned char disbuf[20];

	  lcd_init();
	  while(1)
		{
				ds_reset();
				ds_send_byte(0xcc);
				ds_send_byte(0x44);
			
				ds_reset();
				ds_send_byte(0xcc);
				ds_send_byte(0xbe);	  
				a = ds_read_byte();
				b = ds_read_byte();
				temp = (b << 8) | a;
				wendu = (float)temp * 0.0625;
			  sprintf(disbuf,"temp is %7.3f",wendu);
			  lcd1602_dis_str(0,0,disbuf);
	  }
}

遅延関数ファイル:
void delay_us(unsigned char t)
{
    while(--t);
}

void delay_ms(unsigned char t)
{
    while(t--)
		{
        delay_us(245);
			  delay_us(245);
    }
}

void delay_s(unsigned char t)
{
    while(t--)
    {
		    delay_ms(200);	
			  delay_ms(200);
			  delay_ms(200);
			  delay_ms(200);
			  delay_ms(200);
    }
}