课程设计说明书
for (i = 0;i < 8;i++) {
dq = 0; dat >>= 1;dq = 1;_nop_();_nop_(); if(dq) dat |= 0x80;delay(30);dq = 1; }
return dat; }
void writeonebyte(uchar dat) {
uchar i;
for(i = 0;i < 8;i++)
{dq = 0;dq = dat & 0x01;delay(5); dq = 1;dat >>=1; } }
void read_temperature() {
if( init_ds18b20() ==1) ds18b20_is_ok = 0; else
{writeonebyte(0xcc); writeonebyte(0x44); init_ds18b20(); writeonebyte(0xcc); writeonebyte(0xbe);
temp_value[0] = readonebyte(); temp_value[1] = readonebyte(); alarm_temp_hl[0] = readonebyte(); alarm_temp_hl[1] = readonebyte(); ds18b20_is_ok = 1; } }
void set_alarm_temp_value() {
init_ds18b20(); writeonebyte(0xcc); writeonebyte(0x4e);
writeonebyte(alarm_temp_hl[0]); writeonebyte(alarm_temp_hl[1]); writeonebyte(0x7f); init_ds18b20(); writeonebyte(0xcc);
21
writeonebyte(0x48); }
void display_temperature()
课程设计说明书
{
uchar i;
uchar t = 150;
uchar ng = 0 , np = 0;
char signed_current_temp;
if ( ( temp_value[1] & 0xf8) == 0xf8 ) {temp_value[1] = ~temp_value[1]; temp_value[0] =~temp_value[0] + 1;
if ( temp_value[0] == 0x00 ) temp_value[1]++; ng = 1;np = 0xfd; }
display_digit[0] = df_table[temp_value[0] & 0x0f ];
currentt = ((temp_value[0] & 0xf0)>>4) | ((temp_value[1] & 0x07)<<4); signed_current_temp = ng? -currentt :currentt;
hi_alarm = signed_current_temp >= alarm_temp_hl[0] ? 1 : 0; lo_alarm = signed_current_temp <= alarm_temp_hl[1] ? 1: 0; display_digit[3] = currentt /100;
display_digit[2] = currentt % 100 /10; display_digit[1] = currentt % 10; if (display_digit[3] == 0 ) {
display_digit[3] = 10; np = 0xfb; if( display_digit[2] == 0) { display_digit[2] =10; np = 0xf7; } } for (i = 0;i <30;i ++) {
P0 = 0x39;P2 = 0x7f; delay(t);P2 = 0xff; P0 = 0x63; P2 = 0xbf;delay(t);P2 = 0xff; P0 =dsy_code[display_digit[0]]; P2 = 0xdf;delay(t) ;P2 = 0xff; P0 =(dsy_code[display_digit[1]]) | 0x80; P2 = 0xef;delay(t);P2 = 0xff; P0 =dsy_code[display_digit[2]]; P2 = 0xf7; delay(t) ;P2 = 0xff;
22
课程设计说明书
P0 = dsy_code[display_digit[3]]; P2 = 0xfb;delay(t);P2 = 0xff; if(ng) {
P0 = 0x40;P2 = np;delay(t); P2 = 0xff; } } }
void t0_int() interrupt 1 { TH0 = -1000/256; TL0 = -1000%6; beep = !beep; if (++time0_count == 400) { time0_count = 0; if (hi_alarm) hi_led = ~hi_led ;else hi_led = 1; if (lo_alarm) lo_led = ~lo_led; else lo_led = 1; } }
void main (void) {
IE = 0x82;
TMOD = 0x01; TH0 = -1000 / 256; TL0 = -1000 % 256; TR0 = 0; hi_led = 1; lo_led = 1;
set_alarm_temp_value(); read_temperature(); delay(50000); delay(50000); while(1) {
read_temperature(); if (ds18b20_is_ok) {
if (hi_alarm == 1 || lo_alarm == 1 ) TR0 = 1; else TR0 = 0;
display_temperature(); }
23
else {
P0=0x00; P2=0x00;} } }
课程设计说明书
24