……

LCD1602显示温度源码程序---STC89C52实时用PCF8591采集温度LCD显示

一、创建头文件lcd1602.h代码如下:

#ifndef __LCD1602_H_
#define __LCD1602_H_

#include <reg52.h>
#include <stdio.h>

#define uint unsigned int    //定义常用数据类型替代码
#define uchar unsigned char
#define dat P0                        //定义LCD1602的数据口为P0

sbit rs=P1^4;                        //定义RS口为P14
sbit rw=P1^5;                        //定义RW口为P15
sbit e=P1^6;                        //定义E口为P16

void display(unsigned int temp);
void int_1602(void);                    //启动LCM程序
void dat_1602(uchar a);                //写数据到LCM程序
void com_1602(uchar a);                //写指令到LCM程序
void busy_1602(void);                //查询忙碌标志信号程序
void delay_1ms(void);                //延时程序
void Delay(unsigned int i);

#endif

二、创建编译文件lcd1602.c代码如下:

#include "lcd1602.h"    //此文件中定义了单片机的一些特殊功能寄存器

uchar busy;                         //1602判忙标志

uint i=0;
uint j=0;

uint temp;

void Delay(unsigned int i)            
{
    while(i--);
}

void delay_1ms(void)                //延时程序
{
    uchar i,j;
    for(i=0;i<10;i++)
    for(j=0;j<20;j++); 
}

void busy_1602(void)                //查询忙碌标志信号程序
{
    do
    {
        e=0;
        rw=1;
        rs=0;
        e=1;
        busy=dat;
        e=0;    
        delay_1ms();
    } while(busy&&0x10==1);
}

void com_1602(uchar a)                //写指令到LCM程序
{
       busy_1602();
       e=0;
       rw=0;
       rs=0;
       e=1;
       dat=a;
       e=0;
}

void dat_1602(uchar a)                //写数据到LCM程序
{
       busy_1602();
       e=0;
       rw=0;
       rs=1;
       e=1;
       dat=a;
       e=0;
}

void int_1602(void)                    //启动LCM程序
{
       com_1602(0x38); 
       com_1602(0x0c);
       com_1602(0x06);
}

void display(unsigned int temp)            //显示温度
{
    uchar rev_data[16]={" -temperature!- "};
    com_1602(0x80);
    for(i=0;i<16;i++)                //发送数据第一行
    {
        dat_1602(rev_data[i]);
    }
    temp*=10;
    com_1602(0xc0);//第二行
    dat_1602('T');
    dat_1602('H');
    dat_1602('=');
    dat_1602(temp/100%10+0x30);
    dat_1602(temp/10%10+0x30);
    dat_1602('.');
    dat_1602(temp%10+0x30);
    dat_1602(0xdf);
    dat_1602('C');
}

三、主程序见:https://www.cnblogs.com/wlei5206/p/13023977.html

posted @ 2020-06-01 11:01  十零  阅读(1971)  评论(0编辑  收藏  举报