打印
[51单片机]

1602不能显示所计得的数(恳请大家帮忙)

[复制链接]
1112|5
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
tf2160326|  楼主 | 2013-12-9 15:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include <reg51.h>
#include <intrins.h>
#define   uchar unsigned char
#define   uint  unsigned int  
sbit RS=P2^0;
sbit RW=P2^1;
sbit E=P2^2;
uchar Ncount=0,c;
uchar b,q,w,m ;
//uchar code table[]="0123456789";
void delay(uchar m)        ;
void write_com(uchar j);
void write_data(uchar j) ;
void init()        ;
void init1(void);
void btod(void);
void bintobcd(void);
void display(void);

  void main(void)
{
   init();
   init1();
   while (1)
  {  
     delay(5);  
     display();
  }
}         
void delay(uchar m)
{  uchar i,j;
  for (i=0;i<m;i++)
     for (j=0;j<100;j++);
}
void Busy()
{
uchar busy;
do
{
E=0; //0为关闭使能端,1为打开使能端
RS=0; //1为数据 ,0为指令
RW=1; //1为读,0为写 1
delay(5);
P1=0xFF;//未采用P0口不需要啦!
E=1; //0为关闭使能端,1为打开使能端
busy=P1; //读取P1状态
delay(5);
E=0; //0为关闭使能端,1为打开使能端
}
while(busy&0x80);
     }
void write_com(uchar j)
{E=0;
RS=0;
RW=0;
E=1;
P1=j;
Busy();
E=0;
}
void write_data(uchar j)
{ E=0;
  RS=1;
  RW=0;  
  E=1;
  P1=j;
  Busy();
  E=0;
  }
void init()
{E=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
void init1()
{
  EA=0;
  TMOD=0x05;
  T0=0x0000;
  TR0=1;
  }
/*void btod(void)
{long p=0,a=0x0001;
uchar i,j;
for(i=1;i<8;i++)
{if(c & a)
p=p+2^(i-1);
a<<1;
}
c=p;
}*/
void bintobcd(void)
{
Ncount=T0;
//c=Ncount;
//btod();
//Ncount=c;
  m=Ncount/1000;
  w=Ncount%1000/100;
  q=Ncount%1000%100/10;
  b=Ncount%1000%100%10;
   }
void display(void)
{   
     bintobcd();  
     write_com(0xc0);
         write_data('F');
     write_data('r');
     write_data('e');
     write_data('q');
     write_com(0xc8);
        write_data(m+0x30);  
    write_data(w+0x30);
    write_data(q+0x30);
    write_data(b+0x30);
        write_com(0xce);
     write_data('H');
     write_data('Z');
    }
C:\Documents and Settings\Administrator\桌面

相关帖子

沙发
ayb_ice| | 2013-12-9 16:20 | 只看该作者
能显示其它字符吗?

这种芯片驱动网上一大把

使用特权

评论回复
板凳
craaazyfor| | 2013-12-9 17:32 | 只看该作者
先做简单数字显示,再把计数拆分成一个个数字,送给1602显示出来

使用特权

评论回复
地板
ballack_linux| | 2013-12-9 21:45 | 只看该作者
试试这个
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcden=P2^2;
sbit lcdrs=P2^0;
sbit dula=P2^6;
sbit wela=P2^7;
uchar i;
uchar code table[]="I love China!" ;
uchar code table1[]="Me too.";
void delay(uint z)
{
        uint x,y;
        for(x=z;x>0;x--)
                for(y=110;y>0;y--);
}
void init()
{
        dula=0;
        wela=0;
        lcden=0;
}               
void write_lcd(uchar com,bit j)
{
        lcdrs=j;
        P0=com;
        delay(5);
        lcden=1;
        delay(5);
        lcden=0;
}       
void display_lcd(bit y,uchar x,uchar value)
{
        x&=0x0f;
        if(y)
        {
                 x+=0x40;
        }
        x+=0x80;
        write_lcd(x,0);
        write_lcd(value,1);

}
void main()
{
        init();       
        write_lcd(0x38,0);                         //        显示模式设置
        write_lcd(0x0c,0);                         // 显示开/关及光标设置                                                            
        write_lcd(0x01,0);                         // 使用前先将数据指针和所有显示清零

        for(i=0;i<13;i++)
        {
                display_lcd(0,i,table[i]);
                delay(5);
        }
        for(i=0;i<7;i++)
        {
                display_lcd(1,i+9,table1[i]);
                delay(5);
        }
        while(1);                                                 //使显示固定
}       

使用特权

评论回复
5
tf2160326|  楼主 | 2013-12-10 14:21 | 只看该作者
谢谢你的帮忙,本程序能在PROTEUS仿真中显示"freq 0000 HZ",但其中的四位数字只在'0000'和'0001'间交替变化,而不论计数脉冲信号的频率及计数初值如何变化,望赐教!

使用特权

评论回复
6
tf2160326|  楼主 | 2013-12-10 14:44 | 只看该作者
谢谢你提供这个程序,但我的程序也能显示固定的字符(数组),如“freq    HZ”,但变动的数字不能显示,如对P3.4引脚输入的计数脉冲所记得的数就不能显示,望进一步指点,谢谢!

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

3

帖子

0

粉丝