main.c
#include <smg.h>
#include <ds18b20.h>
void main()
{
u8 i=0;
int temp_value;
u8 temp_buf[6];
ds18b20_init();
while(1)
{
i++;
if(i%50==0)
temp_value=ds18b20_read_temperture()*10;
if(temp_value<0)
{
temp_value=-temp_value;
temp_buf[0]=0x40;
}
else
temp_buf[2]=gsmg_code[temp_value%1000%100/10];
temp_buf[3]=gsmg_code[temp_value%1000%100%10];
smg_display(temp_buf,3);
}
}
/***********************************************************************************
ds18b20.c
#include <ds18b20.h>
#include <intrins.h>
void ds18b20_reset(void)
{
DS18B20_PORT=0;
delay_10us(75);
DS18B20_PORT=1;
delay_10us(2);
}
u8 ds18b20_check(void)
{
u8 time_temp=0;
while(DS18B20_PORT&&time_temp<20)
{
time_temp++;
delay_10us(1);
}
if(time_temp>=20)return 1;
else time_temp=0;
while((!DS18B20_PORT)&&time_temp<20)
{
time_temp++;
delay_10us(1);
}
if(time_temp>=20)return 1;
return 0;
}
u8 ds18b20_read_bit(void)
{
u8 dat=0;
DS18B20_PORT=0;
_nop_();_nop_();
DS18B20_PORT=1;
_nop_();_nop_();
if(DS18B20_PORT)dat=1;
else dat=0;
delay_10us(5);
return dat;
}
u8 ds18b20_read_byte(void)
{
u8 i=0;
u8 dat=0;
u8 temp=0;
for(i=0;i<8;i++)
{
temp=ds18b20_read_bit();
dat=(temp<<7)|(dat>>1);
}
return dat;
}
void ds18b20_write_byte(u8 dat)
{
u8 i=0;
u8 temp=0;
for(i=0;i<8;i++)
{
temp=dat&0x01;
dat>>=1;
if(temp)
{
DS18B20_PORT=0;
_nop_();_nop_();
DS18B20_PORT=1;
delay_10us(6);
}
else
{
DS18B20_PORT=0;
delay_10us(6);
DS18B20_PORT=1;
_nop_();_nop_();
}
}
}
void ds18b20_start(void)
{
ds18b20_reset();
ds18b20_check();
ds18b20_write_byte(0xcc);
ds18b20_write_byte(0x44);
}
u8 ds18b20_init(void)
{
ds18b20_reset();
return ds18b20_check();
}
float ds18b20_read_temperture(void)
{
float temp;
u8 dath=0;
u8 datl=0;
u16 value=0;
ds18b20_start();
ds18b20_reset();
ds18b20_check();
ds18b20_write_byte(0xcc);
ds18b20_write_byte(0xbe);
datl=ds18b20_read_byte();
dath=ds18b20_read_byte();
value=(dath<<8)+datl;
if((value&0xf800)==0xf800)
{
value=(value)+1;
temp=value*(-0.0625);
}
else
{
temp=value*0.0625;
}
return temp;
}
/***********************************************************************************
smg.c
#include <smg.h>
u8 gsmg_code[17]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f};
void delay_10us(u16 ten_us)
{
while(ten_us--);
}
void smg_display(u8 dat[],u8 pos)
{
u8 i=0;
u8 pos_temp=pos-1;
for(i=pos_temp;i<8;i++)
{
switch(i)
{
case 0: LSC=1;LSB=1;LSA=1;break;
case 1: LSC=1;LSB=1;LSA=0;break;
case 2: LSC=1;LSB=0;LSA=1;break;
case 3: LSC=1;LSB=0;LSA=0;break;
case 4: LSC=0;LSB=1;LSA=1;break;
case 5: LSC=0;LSB=1;LSA=0;break;
case 6: LSC=0;LSB=0;LSA=1;break;
case 7: LSC=0;LSB=0;LSA=0;break;
}
SMG_A_DP_PORT=dat[i-pos_temp];
delay_10us(100);
SMG_A_DP_PORT=0x00;
}
}
|