天天看點

溫度計(LCD顯示)

/*

 * 123.c

 *

 *  Created on: 2013-6-3

 *      Author: Administrator

 */

#include<reg52.h>

#include <intrins.h>

#define uchar unsigned char

#define uint unsigned int

typedef bit BOOL;

sbit DQ = P3 ^ 7;

uchar Temp_Value[] = { 0x00, 0x00 };

uchar Display_Digit[] = { 0, 0, 0, 0 };

sbit LCD_RW = P2 ^ 5; //寫資料?

sbit LCD_RS = P2 ^ 6;

sbit LCD_EP = P2 ^ 7; //使能?

uchar num;

int temper = 0;

int CurrentT = 0;

bit flag;

void delay(uchar t) {

    while (t--)

        ;

}

 void delay2(uint z)

 {

 uint x,y;

 for(x=z;x>0;x--)

 for(y=110;y>0;y--){;}

 }

void init_ds18b20() {

    //    uchar n;

DQ = 1;

    _nop_();

    DQ = 0;

    delay(80);

    DQ = 1;

    delay(14);

    if (DQ == 0)

        flag = 1; //detect 1820 success!

    else

        flag = 0; //detect 1820 fail!

    delay(20); //20

void write_byte(uchar dat) {

    uchar i;

    for (i = 0; i < 8; i++) {

        DQ = 0;

        _nop_();

        DQ = dat & 0x01; //從最低位開始寫

        delay(3);

        DQ = 1;

        dat >>= 1;

    }

    //    delay(4);

uchar read_byte() {

    uchar i, value = 0;

        value >>= 1;

        if (DQ)

            value |= 0x80;

        delay(2);

    return value;

int readtemperature() {

    uchar templ, temph;

    //    int temp;

    init_ds18b20();

    write_byte(0xcc); //跳過ROM

    write_byte(0x44); //啟動溫度測量

    delay(300);

    write_byte(0xcc);

    write_byte(0xbe);

    //    Temp_Value[0] = ReadOneByte();

    //    Temp_Value[1] = ReadOneByte();

    templ = read_byte();

    temph = read_byte();

    //CurrentT = ((templ&0xf0)>>4) | ((temph&0x07)<<4);

    CurrentT = temph*256 +templ;

    CurrentT = CurrentT * 62.5;

    return CurrentT;

/****************側忙函數************************/

BOOL lcd_bz() {

    BOOL result;

    LCD_RS = 0;

    LCD_RW = 1;

    LCD_EP = 1;

    result = (BOOL) (P0 & 0x80); //檢測P0最高位是否為1

    LCD_EP = 0;

    return result;

/****************寫指令函數************************/

void write_com(uchar cmd) {

    while (lcd_bz())

    LCD_RW = 0;

    //    LCD_EP = 0;

    P0 = cmd;

void lcd_pos(uchar pos) {

    write_com(pos | 0x80);

/****************寫資料函數************************/

void write_data(uchar dat) { //寫入字元顯示資料到LCD

    LCD_RS = 1;

    P0 = dat;

void init() {

    //    dula=0;

    //    wela=0;

    write_com(0x38); //16*2顯示

    write_com(0x0e); //開顯示;顯示光标,光标不閃爍

    write_com(0x06); //光标自動增加111

    write_com(0x01); //顯示清屏

    //    write_com(0x80+0x10);

void main() {

    init();

    while (1) {

        temper = readtemperature();

        lcd_pos(0);

        if (temper < 0) {

            write_data('-');

            temper = 0 - temper;

        } else

            write_data('+');

        lcd_pos(1);

        write_data((temper) / 10000 + 0x30);

        lcd_pos(2);

        write_data(((temper) % 10000 )/ 1000 + 0x30);

        lcd_pos(3);

        write_data('.');

        lcd_pos(4);

        write_data(((temper) % 10000 % 1000 )/ 100 + 0x30);

        lcd_pos(5);

        write_data(((temper) %10000 %1000 % 100 )/10 + 0x30);

        lcd_pos(6);

        write_data((temper) %10000 %1000 % 100 % 10 + 0x30);

        lcd_pos(7);

        write_data(0xdf);

        lcd_pos(8);

        write_data('C');