找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1406|回复: 1
打印 上一主题 下一主题
收起左侧

请师傅们看一下,1602显示数组元素两位数时正常 ,当百位是1后十位出现乱码,请指点。

[复制链接]
跳转到指定楼层
楼主
ID:597248 发表于 2019-12-28 19:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include<stc12c5a60s2.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//这三个引脚参考资料
sbit LCD1602_EN=P3^3; //1602使能引脚
sbit LCD1602_RW=P3^4; //1602读写引脚
sbit LCD1602_RS=P3^5; //1602数据/命令选择引脚


#define LCDPORT P0          //液晶的数据口

unsigned char code ucForum[]="123456789";          //在CODE区定义一个用于显示的常量字符串
unsigned char ab;
void Delay(unsigned int uiCount);        //延时函数
void LCD1602_CheckBusy(void);        //液晶忙检测
void LCD1602_WriteInformation(unsigned char ucData,bit bComOrData);        //在液晶上写数据或者写命令,0为命令,1为数据
void LCD1602_Init(void);        //液晶初始化
void LCD1602_MoveToPosition(unsigned char x,unsigned char y);        //液晶的坐标移动到指定位置
void LCD1602_DisplayOneCharOnAddr(unsigned char x,unsigned char y,unsigned char ucData);        //在液晶指定位置显示字符
void LCD1602_DisplayString(unsigned char *ucStr);        //在液晶上显示字符串

void re()
{ab=100;
}
/******************************************************************************
函数名称:main
函数功能:程序主函数
入口参数:无
返回值:无
备注:无
*******************************************************************************/
void main(void)
{
        LCD1602_Init();          //液晶初始化
re();
        while(1)                 //程序循环
        {
                LCD1602_DisplayOneCharOnAddr(0,0,'A');        //在液晶的第1个位置显示1
                LCD1602_DisplayOneCharOnAddr(0,1,'B');        //在液晶的第2个位置显示A
                LCD1602_DisplayOneCharOnAddr(0,2,'C');        //在液晶的第3个位置显示2
                LCD1602_DisplayOneCharOnAddr(0,3,'D');        //在液晶的第4个位置显示B
                LCD1602_DisplayOneCharOnAddr(0,4,'E');        //在液晶的第5个位置显示3
                LCD1602_DisplayOneCharOnAddr(0,5,'F');        //在液晶的第6个位置显示C

                LCD1602_DisplayOneCharOnAddr(0,7,ab/100+0x30);
                LCD1602_DisplayOneCharOnAddr(0,8,ab/10+0x30);
                LCD1602_DisplayOneCharOnAddr(0,9,ab%10+0x30);
               

                LCD1602_MoveToPosition(1,0);        //显示位置移动到指定位置
                LCD1602_DisplayString(ucForum);        //显示的内容
                while(1);
        }       
}

void LCD1602_CheckBusy(void)               
{
        unsigned char i = 255;
        LCDPORT = 0xFF;  //读之前先置位,准备读取IO口数据
        LCD1602_RS = 0;
        LCD1602_RW = 1;         //使液晶处于读数据状态
        LCD1602_EN = 1;         //使能液晶,高电平有效
        while((i--) && (LCDPORT & 0x80));        //忙检测
        LCD1602_EN = 0;
}

void LCD1602_WriteInformation(unsigned char ucData,bit bComOrData)         
{
        LCD1602_CheckBusy();         //在写入数据或者命令前先进行忙检测
        LCDPORT = ucData;                 //先将数据或者命令送至IO
        LCD1602_RS = bComOrData;        //确定是写入数据还是写命令
        LCD1602_RW = 0;                //使液晶处于写入信息状态
        LCD1602_EN = 1;                //使能液晶,高电平有效
        LCD1602_EN = 0;
}

void LCD1602_Init(void)                 
{
        LCD1602_WriteInformation(0x38,0);
        Delay(300);
        LCD1602_WriteInformation(0x38,0);
        Delay(100);
        LCD1602_WriteInformation(0x38,0);
        Delay(100);
        LCD1602_WriteInformation(0x38,0);         //写入命令,5x7点阵工作方式,8位数据接口
        Delay(100);
        LCD1602_WriteInformation(0x0c,0);        //显示设置,开显示,光标不显示,不闪烁
        Delay(20);
        LCD1602_WriteInformation(0x01,0);        //清屏指令
        Delay(20);
}

void LCD1602_MoveToPosition(unsigned char x,unsigned char y)       
{
        if(0 == x)
                LCD1602_WriteInformation((0x80 | y),0);           //光标定位到第一行的y列
        if(1 == x)
                LCD1602_WriteInformation((0xC0 | y),0);           //光标定义到第二行的y列
}


void LCD1602_DisplayOneCharOnAddr(unsigned char x,unsigned char y,unsigned char ucData)
{
        LCD1602_MoveToPosition(x,y);   //光标位置
        LCD1602_WriteInformation(ucData,1);          //写入数据
}

void LCD1602_DisplayString(unsigned char *ucStr)       
{
        while(*ucStr != '\0')           //字符串结束之前,循环显示
        {
                 LCD1602_WriteInformation(*ucStr,1);         //依次写入每一个字符
                 ucStr++;                                                                 //指针增加
        }
}

void Delay(unsigned int uiCount)
{
        unsigned char j = 244;
        for(;uiCount > 0;uiCount--) while(--j);       
}

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:597248 发表于 2019-12-28 21:49 | 只看该作者
问题已解决
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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