yayajie 发表于 2013-1-25 17:17:40

新手求教,延时程序问题

/////////////////1602显示///////////
#include<reg52.h>

#define uint unsigned int
#define uchar unsigned char

sbit wela = P2^7;   
sbit dula = P2^6;

uchar code LCD1602firstline[] = " I LOVE YOU ";
uchar code LCD1602secondline[] = " WELCOME ";

sbit LCDRS = P3^5;
sbit LCDWR = P3^6;
sbit LCDEN = P3^4;

void Delay1MS(uint z)
{
        uint x,y;
        for(x=z;x>0;x--)
        {
                        for(y=124;y>0;y--);
}
/*uint i = 124;
        while (z--)   //为什么改成这样就不可以了呢
        {
                i--;
        }*/
}

void WriteCom1602(uchar com)
{
        LCDEN = 0;
        LCDRS = 0;
        LCDWR = 0;
        P0 = com;
        LCDEN = 1;
        Delay1MS(1);
        LCDEN = 0;
}

void WriteDat1602(uchar dat)
{
        LCDEN = 0;
        LCDRS = 1;
        LCDWR = 0;
        P0 = dat;
        LCDEN = 1;
        Delay1MS(1);
        LCDEN = 0;
}
void Writechar1602(uchar x , uchar y , uchar date )
{
        x &= 0x0f;
        y &= 0x01;
       
        if (y==1)
        {
                x += 0x40;
}
        x += 0x80;
       
        WriteCom1602(x);
        WriteDat1602(date);
       
}

void LCD1602Init()
{
LCDEN = 0 ;
        WriteCom1602(0x38);
        WriteCom1602(0x01);
        WriteCom1602(0x06);
        WriteCom1602(0x0c);
}
void main()
{
        uint i = 0 ;
       
        P0 = 0xff ;                     //ÊýÂë¹Ü¹Ø¶Ï
        wela = 1 ;
        wela = 0;
        Delay1MS(5);
       
        LCD1602Init();
        for (i=0 ; i<12 ; i++)
        {
                Writechar1602(i ,0 , LCD1602firstline);
        }
        for (i=0 ; i<9 ; i++)
        {
                Writechar1602(i , 1, LCD1602secondline);
        }
       
        while(1);
       
}



/*
汗,今天我调了好久才发现好像延迟函数出了问题
上面Delay1MS()改成下面之后1602不能显示,是不是我延时函数写的不对
新手求教,还有一般延时函数怎么写好呢
*/

lcw_swust 发表于 2013-1-25 17:22:03

uint i = 124;
      while (z--)   //为什么改成这样就不可以了呢
      {
                i--;
      }
这个循环在干什么?
z一直减到0退出循环,i在每次循环里就减一次
看起来是时间不够

xqn2012 发表于 2013-1-25 17:31:07

显然这个延时时间不够长嘛
while(z--)
    for(i=124;i>0;i--);

yayajie 发表于 2013-1-25 17:52:52

谢谢各位 我知道了
页: [1]
查看完整版本: 新手求教,延时程序问题