|
- #include<reg52.h>
- #include<intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit din=P3^0;
- sbit load=P3^1;
- sbit clk=P3^2;
- sbit dq=P3^3;
- uchar dig;
- bit list_flag = 0;
- uchar code tab[]={ 0x7e, //"0"
- 0x30, //"1"
- 0x6d, //"2"
- 0x79, //"3"
- 0x33, //"4"
- 0x5b, //"5"
- 0x5f, //"6"
- 0x70, //"7"
- 0x7f, //"8"
- 0x7b, //"9"
- 0x4e,
- 0x63,
- 0x01,
- 0x00
- }; // 共阴极数码管显示0-9
- uchar data temp_data[2] = {0x00,0x00};
- uchar data display[] = {0x00,0x00,0x00,0x00,0x00,0x00};
- uchar code ditab[] = {0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,
- 0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};
- void delay(uint xms) //定义延时函数
- {
- while(xms--);
- }
- uchar init_ds18b20()
- {
- uchar status;
- dq = 1;
- delay(8);
- dq = 0;
- delay(90);
- dq = 1;
- delay(8);
- status = dq;
- delay(100);
- dq = 1;
- return(status);
- }
- uchar readonebyte()
- {
- uchar i = 0;
- uchar dat = 0;
- for(i = 8;i > 0;i--)
- {
- dq = 0;
- dat >>=1;
- dq = 1;
- _nop_();
- _nop_();
- if(dq)
- {
- dat |= 0x80;
- }
- delay(4);
- dq = 1;
- }
- return(dat);
- }
- void writeonebyte(uchar dat)
- {
- uchar i = 0;
- for(i = 8;i > 0;i--)
- {
- dq = 0;
- dq = dat&0x01;
- delay(5);
- dq = 1;
- dat >>=1;
- }
- }
- void read_temperature()
- {
- if(init_ds18b20() == 1)
- {
- list_flag = 1;
- }
- else
- {
- list_flag = 0;
- writeonebyte(0xcc);
- writeonebyte(0x44);
- init_ds18b20();
- writeonebyte(0xcc);
- writeonebyte(0xbe);
- temp_data[0]=readonebyte();
- temp_data[1]=readonebyte();
- }
- }
- void temperture_trans()
- {
- uchar ng = 0;
- if((temp_data[1]&0xf8) == 0xf8)
- {
- temp_data[1]= ~temp_data[1];
- temp_data[0]= ~temp_data[0]+1;
- if(temp_data[0] == 0x00)
- {
- temp_data[1]++;
- }
- ng = 1;
- }
- display[4] = temp_data[0]&0x0f;
- display[0] = ditab[display[4]];
- display[4] = ((temp_data[0]&0xf0) >> 4)|((temp_data[1]&0x0f) << 4);
- display[3] = display[4]/100;
- display[1] = display[4]%100;
- display[2] = display[1]/10;
- display[1] = display[1]%10;
- if(ng == 1)
- {
- display[5] = 12;
- }
- else
- {
- display[5]=13;
- }
- if(!display[3])
- {
- if(!display[2])
- display[2]=13;
- }
- }
- void write_7219(uchar add,uchar date)
- {
- uchar i;
- load=0;
- for(i=0;i<8;i++)
- {
- clk=0;
- din=add&0x80;
- add<<=1;
- clk=1;
- }
- for(i=0;i<8;i++)
- {
- clk=0;
- din=date&0x80;
- date<<=1;
- clk=1;
- }
- load=1;
- }
- void init_7219()
- {
- write_7219(0x0c,0x01);
- write_7219(0x0a,0x0f);
- write_7219(0x09,0x00);
- write_7219(0x0b,0x07);
- }
- void disp_max7219(uchar dig,uchar date)
- {
- write_7219(dig,date);
- }
- void main()
- {
- init_7219();
- while(1)
- {
- temperture_trans();
- read_temperature();
- if(list_flag==0)
- {
- disp_max7219(1,tab[display[5]]);
- disp_max7219(2,tab[display[3]]);
- disp_max7219(3,tab[display[2]]);
- disp_max7219(4,tab[display[1]]|0x80);
- disp_max7219(5,tab[display[0]]);
- disp_max7219(7,tab[11]);
- disp_max7219(8,tab[10]);
- }
- }
- }
复制代码
|
|