找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1191|回复: 4
收起左侧

如何在已编好的单片机程序中添加初始化显示hello(液晶为LCD1602)

[复制链接]
ID:1008254 发表于 2022-3-29 21:19 | 显示全部楼层 |阅读模式
#include <reg52.h>
#include <intrins.h>

#define uchar unsigned char                // 以后unsigned char就可以用uchar代替
#define uint  unsigned int                // 以后unsigned int 就可以用uint 代替


sbit LcdEn_P    = P2^5;             // 1602液晶的EN管脚
sbit LcdRw_P    = P2^6;             // 1602液晶的RW管脚
sbit LcdRs_P    = P2^7;             // 1602液晶的RS管脚      
sbit Key1_P     = P3^2;                                // 减按键
sbit Key2_P     = P3^3;                                // 加按键
sbit Buzzer_P   = P2^0;                                // 蜂鸣器
sbit LedRed_P   = P1^4;                                // 红灯
sbit LedGreen_P = P1^1;                                // 绿灯

uint  gWarn=1000;                                                        // 报警浓度值
uint  gCH2O;                                                                        // 甲醛浓度值



/*********************************************************/
// 毫秒级的延时函数,time是要延时的毫秒数
/*********************************************************/
void DelayMs(uint time)
{
        uint i,j;
        for(i=0;i<time;i++)
                for(j=0;j<112;j++);
}


/*********************************************************/
// 1602液晶写命令函数,cmd就是要写入的命令
/*********************************************************/
void LcdWriteCmd(uchar cmd)
{
        LcdRs_P = 0;
        LcdRw_P = 0;
        LcdEn_P = 0;
        P0=cmd;
        DelayMs(2);
        LcdEn_P = 1;   
        DelayMs(2);
        LcdEn_P = 0;        
}


/*********************************************************/
// 1602液晶写数据函数,dat就是要写入的数据
/*********************************************************/
void LcdWriteData(uchar dat)
{
        LcdRs_P = 1;
        LcdRw_P = 0;
        LcdEn_P = 0;
        P0=dat;
        DelayMs(2);
        LcdEn_P = 1;   
        DelayMs(2);
        LcdEn_P = 0;
}


/*********************************************************/
// 1602液晶初始化函数
/*********************************************************/
void LcdInit()
{
        LcdWriteCmd(0x38);        // 16*2显示,5*7点阵,8位数据口
        LcdWriteCmd(0x0C);        // 开显示,不显示光标
        LcdWriteCmd(0x06);        // 地址加1,当写入数据后光标右移
        LcdWriteCmd(0x01);        // 清屏
}


/*********************************************************/
// 液晶光标定位函数
/*********************************************************/
void LcdGotoXY(uchar line,uchar column)
{
        // 第一行
        if(line==0)        
                LcdWriteCmd(0x80+column);
         // 第二行
        if(line==1)        
                LcdWriteCmd(0x80+0x40+column);
}


/*********************************************************/
// 液晶输出数字
/*********************************************************/
void LcdPrintNum(uint num)
{
        LcdWriteData(num/10000+48);                                        // 个位
        LcdWriteData('.');                                                                        // 小数点
        LcdWriteData(num%10000/1000+48);                // 小数点后第一位
        LcdWriteData(num%1000/100+48);                        // 小数点后第二位
        LcdWriteData(num%100/10+48);                                // 小数点后第三位
        LcdWriteData(num%10+48);                                                // 小数点后第四位
}


/*********************************************************/
// 液晶输出字符串函数
/*********************************************************/
void LcdPrintStr(uchar *str)
{
        while(*str!='\0')
                LcdWriteData(*str++);
}


/*********************************************************/
// 液晶显示内容初始化
/*********************************************************/
void LcdShowInit()
{
        LcdGotoXY(0,0);                                // 液晶光标定位到第0行第0列
        LcdPrintStr("CH2O       mg/m3");
        LcdGotoXY(1,0);                                // 液晶光标定位到第1行第0列
        LcdPrintStr("warn       mg/m3");
}


/*********************************************************/
// 串口初始化
/*********************************************************/
void UartInit()
{
        SCON = 0x50;        // 配置串口寄存器
        TMOD = 0x20;        // 配置定时器寄存器
        TH1  = 0xfd;        // 计算波特率的值为9600
        TL1  = 0xfd;        // 计算波特率的值为9600
        
        EA   = 1;                        // 打开总中断
        ES   = 1;                        // 打开串口中断
        TR1  = 1;                        // 启动定时器
}


/*********************************************************/
// 按键扫描
/*********************************************************/
void KeyScanf()
{
        /* 减按键被按下 */
        if(Key1_P==0)               
        {
                if(gWarn>100)                                                                                                // 只有报警值大于100,才能完成减操作
                {
                        gWarn=gWarn-100;                                                                        // 报警值减0.01
                        LcdGotoXY(1,5);                                                                                // 液晶定位到第1行第5列
                        LcdPrintNum(gWarn);                                                                // 显示报警浓度值
                        DelayMs(250);
                }
        }
        
        /* 加按键被按下 */
        if(Key2_P==0)
        {
                if(gWarn<64900)                                                                                        // 只有报警值小于64900,才能完成加操作
                {
                        gWarn=gWarn+100;                                                                        // 报警值加0.01
                        LcdGotoXY(1,5);                                                                                // 液晶定位到第1行第5列
                        LcdPrintNum(gWarn);                                                                // 显示报警浓度值
                        DelayMs(250);
                }
        }
}


/*********************************************************/
// 报警判断
/*********************************************************/
void AlarmJudge()
{
        if(gCH2O>gWarn)
        {
                Buzzer_P=0;                        // 开启蜂鸣器报警
                LedRed_P=0;                        // 红灯亮
                LedGreen_P=1;                // 绿灯灭
        }
        else
        {
                Buzzer_P=1;                        // 停止蜂鸣器报警
                LedRed_P=1;                        // 红灯灭
                LedGreen_P=0;                // 绿灯亮
        }
}



/*********************************************************/
// 主函数
/*********************************************************/
void main(void)
{
        uchar i;                                                                // 循环变量
        
        LcdInit();                                                        // 液晶功能初始化
        LcdShowInit();                                        // 液晶显示初始化
        UartInit();                                                        // 串口初始化
        
        LcdGotoXY(1,5);                                        // 液晶定位到第1行第5列
        LcdPrintNum(gWarn);                        // 显示报警浓度值
        
        while(1)
        {
                LcdGotoXY(0,5);                                // 液晶定位到第0行第5列
                LcdPrintNum(gCH2O);                // 显示测量结果
               
                AlarmJudge();                                        // 判断是否需要报警
               
                for(i=0;i<50;i++)
                {
                        KeyScanf();                                        // 进行按键扫描,判断是否有按键按下
                        DelayMs(10);
                }
        }
}


/*********************************************************/
// 串口中断服务程序
/*********************************************************/
void UartInt(void) interrupt 4
{
        uchar Byte4,Byte5;
        
        if(RI==1)
        {
                RI=0;
                if(SBUF==0xFF)                // Byte0(起始位)
                {
                        while(!RI);                        // Byte1
                        RI=0;
                        while(!RI);                        // Byte2
                        RI=0;
                        while(!RI);                        // Byte3
                        RI=0;
                        while(!RI);                        // Byte4
                        Byte4=SBUF;
                        RI=0;
                        while(!RI);                        // Byte5
                        Byte5=SBUF;
                        RI=0;
                        while(!RI);                        // Byte6
                        RI=0;
                        while(!RI);                        // Byte7
                        RI=0;
                        while(!RI);                        // Byte8
                        RI=0;
                        
                        gCH2O=Byte4*256+Byte5;                // 计算检测结果(单位是ppb)
                        gCH2O=gCH2O*13;                                                // 单位由PPM 转换为毫克/立方米
                }
        }
}





回复

使用道具 举报

ID:954171 发表于 2022-3-29 23:33 | 显示全部楼层
在while(1)上方直接调用LcdPrintStr(uchar *str)这个液晶显示字符串函数。
回复

使用道具 举报

ID:584814 发表于 2022-3-30 09:09 | 显示全部楼层
在void LcdShowInit()里
先显示hello,然后显示CH2O mg/m3 warn mg/m3什么的
注意hello显示后要有一定的延时,否则肉眼看不到
回复

使用道具 举报

ID:245053 发表于 2022-3-30 09:36 | 显示全部楼层
把“显示hello”写成一个函数,在你想显示的地方调用即可,为了可见,建议加上适当的延时,否则会一闪而过,看不见。。。
回复

使用道具 举报

ID:1013784 发表于 2022-3-30 11:22 | 显示全部楼层
LcdPrintStr("hello");//记加个延时函数,否则太快了看不见
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表