1、/辽宁工业大学 电子 LINBO/2012 8 15 #include #include typedef unsigned char uint8;typedef unsigned int uint16;typedef char int8;typedef int int16;sbit ENLED=P14;sbit ADDR0=P10;sbit ADDR1=P11;sbit ADDR2=P12;sbit ADDR3=P13;sbit DQ = P32;sbit RS = P10 ;sbit RW = P11 ;sbit EN = P15 ;sbit BUSY = P07; int16 ans;ui
2、nt8 ge,shi;uint16 counter,sce;#define nops(); _nop_(); _nop_(); _nop_(); _nop_(); /定义空指令uint8 num=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90;void delay(uint16 n)while (n-);void wait(void)P0 = 0xFF;doRS = 0;RW = 1;EN = 0;EN = 1;while (BUSY = 1);EN = 0;void w_dat(uint8 dat)wait();EN = 0;P0 = da
3、t;RS = 1;RW = 0;EN = 1;EN = 0;void w_cmd(uint8 cmd)wait();EN = 0;P0 = cmd;RS = 0;RW = 0;EN = 1;EN = 0;void Init_LCD1602(void)w_cmd(0x38); w_cmd(0x0C); w_cmd(0x06); w_cmd(0x01); void delay_ms(uint16 n)uint8 m=120;while (n-)while (m-);void DS18b20_reset(void)bit flag=1;while (flag!=0)while (flag!=0)DQ
4、 = 1;delay(1);DQ = 0;delay(50); / 550usDQ = 1; delay(6); / 66usflag = DQ;delay(45); /延时 500usflag = DQ;DQ=1;void write_byte(uint8 val)uint8 i;for (i=0; i= 1; /右移一位DQ = 1;delay(1); uint8 read_byte(void)uint8 i, value=0;for (i=0; i= 1;DQ = 0;nops(); /4usDQ = 1;nops(); /4us if (DQ!=0)value|=0x80;delay(
5、6); /66usDQ=1;return(value);void start_temp_sensor(void)DS18b20_reset();write_byte(0xCC); / 发 Skip ROM 命令write_byte(0x44); / 发转换命令int16 read_temp(void)uint8 temp_data2; / 读出温度暂放int16 temp;DS18b20_reset(); / 复位write_byte(0xCC); / 发 Skip ROM 命令write_byte(0xBE); / 发读命令temp_data0=read_byte(); /温度低 8 位te
6、mp_data1=read_byte(); /温度高 8 位temp = temp_data1;temp = 4;return temp;void timer1_init()/ TMOD |= 0x01;/ TMOD TH0 = 0xFC;TL0 = 0x67;TR0 = 1;void int_init()ET0 = 1;EA = 1;xianshi()static uint8 j=1;switch(j)case 1: ADDR0=0;ADDR1=0;ADDR2=0;P0=numge;j+;break;case 2: ADDR0=1;ADDR1=0;ADDR2=0;P0=numshi;j=1;
7、break;default :break;void uart_init(void)TMOD = 0x21; SCON = 0x50; TH1 = 0xFD;TL1 = 0xFD;TR1 = 1;void UART_Send_Byte(uint8 dat)SBUF = dat;while (TI = 0);TI = 0;void UART_Send_Dat(uint8 dat)UART_Send_Byte(dat/10%10 + 0);UART_Send_Byte(dat%10 + 0);main()ENLED=0;ADDR3=1;Init_LCD1602();w_cmd(0x80);uart_
8、init();timer1_init();int_init();while (1)EA=0;start_temp_sensor();ans=read_temp();EA=1;delay_ms (1000);if (ans 0)UART_Send_Byte(-);ans = -ans;UART_Send_Dat(ans);UART_Send_Byte(r);UART_Send_Byte(n);void interrupt_timer1() interrupt 1TH0 = 0xFC;TL0 = 0x67;counter+;sce+;if(counter = 10000)counter = 0;ge=ans%10;shi=ans/10%10;if(sce=10000)sce=0;w_dat(ans/10%10 + 0);w_dat(ans%10 + 0);xianshi();