微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 求助STC52单片机控制1602液晶显示 调试时只有小方格没字符

求助STC52单片机控制1602液晶显示 调试时只有小方格没字符

时间:10-02 整理:3721RD 点击:
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint  unsigned int
uchar code disp1[]="HELLO WORLD";
uchar code disp2[]="LCD1602 TEST";
//LCD1602引脚定义
//采用8位并行方式,DB0~DB7连接至P20~P27
sbit RS=P1^2;
sbit RW=P1^1;
sbit CS=P1^0;
#define LCDDATA P2
//功能:延时1毫秒
void Delay_xms(uint x)
{
  uint i,j;
  for(i=0;i<x;i++)
    for(j=0;j<122;j++);
}
//功能:12us延时
void Delay_xus(uint t)                                          
{        
  for(;t>0;t--)
   {
         _nop_();
   }
}
//控制LCD写时序
void LCD_en_write(void)      
{
  CS=1;//EN端产生一个高电平脉冲,控制LCD写时序   
  Delay_xus(20);
  CS=0;   
  Delay_xus(20);
}
//写指令函数
void Write_Instruction(uchar command)
{
  RS=0;
  RW=0;
  CS=1;
  LCDDATA=command;
  LCD_en_write();//写入指令数据
}
//写数据函数
void Write_Data(uchar Wdata)
{
  RS=1;
  RW=0;
  CS=1;
  LCDDATA=Wdata;
  LCD_en_write();//写入数据
}
//字符显示初始地址设置
void LCD_SET_XY(uchar X,uchar Y)
{
  uchar address;
  if(Y==0)
    address=0x80+X;//Y=0,表示在第一行显示,地址基数为0x80
  else
    address=0xc0+X;//Y非0时,表时在第二行显示,地址基数为0xC0
  Write_Instruction(address);//写指令,设置显示初始地址
}
//在第X行Y列开始显示,指针*S所指向的字符串
void LCD_write_str(uchar X,uchar Y,uchar *s)
{
  LCD_SET_XY(X,Y);//设置初始字符显示地址
  while(*s)//逐次写入显示字符,直到最后一个字符"/0"
  {
    Write_Data(*s);//写入当前字符并显示
        s++;//地址指针加1,指向下一个待写字符
  }
}
//在第X行Y列开始显示Wdata所对应的单个字符
void LCD_write_char(uchar X,uchar Y,uchar Wdata)
{
  LCD_SET_XY(X,Y);//写地址
  Write_Data(Wdata);//写入当前字符并显示
}
//清屏函数
void LCD_clear(void)
{
  Write_Instruction(0x01);
  Delay_xms(5);
}
//显示屏初始化函数
void LCD_init(void)
{       
        Write_Instruction(0x38);                                //8bit interface,2line,5*7dots
        Delay_xms(5);
        Write_Instruction(0x38);       
        Delay_xms(5);
        Write_Instruction(0x38);       
        Write_Instruction(0x08);        //关显示,不显光标,光标不闪烁
        Write_Instruction(0x01);        //清屏
        Delay_xms(5);
       
        Write_Instruction(0x04);        //写一字符,整屏显示不移动
        //Write_Instruction(0x05);        //写一字符,整屏右移
        //Write_Instruction(0x06);        //写一字符,整屏显示不移动
        //Write_Instruction(0x07);        //写一字符,整屏左移
        Delay_xms(5);
       
        //Write_Instruction(0x0B);        //关闭显示(不显示字符,只有背光亮)
        Write_Instruction(0x0C);        //开显示,光标、闪烁都关闭
        //Write_Instruction(0x0D);        //开显示,不显示光标,但光标闪烁
        //Write_Instruction(0x0E);        //开显示,显示光标,但光标不闪烁
        //Write_Instruction(0x0F);        //开显示,光标、闪烁均显示
}
void main(void)
{
  uchar i;
  Delay_xms(50);//等待系统稳定
  LCD_init();   //LCD初始化
  LCD_clear();  //清屏  
  LCD_write_str(0,0,disp1);//显示开机信息
  LCD_write_str(0,1,disp2);
  Delay_xms(2000);//保持显示2秒钟
  LCD_clear();  //清屏
  for(i=0;i<16;i++)
   {
        LCD_write_char(i,0,0x41+i);//从第0行第0个位置开始显示A~P   
        Delay_xms(500);//延时0.5秒
   }
  for(i=0;i<16;i++)
   {
        LCD_write_char(i,1,0x30+i%10);//从第1行第0个位置开始显示0~9
        Delay_xms(500);//延时0.5秒
   }
  while(1);//程序挂起
}

如图

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top