找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2875|回复: 11
收起左侧

求助,用lcd1602显示倒计时40秒,到底是该用中断还是直接delay完事?

[复制链接]
ID:560111 发表于 2019-6-11 16:51 来自手机 | 显示全部楼层 |阅读模式
51单片机控制lcd1602第一行显示个人信息,第二行显示40秒倒计时,我用delay延时总是做不出来,定义一个字符串以后,总是没办法按照我想的一样运行,可是要用中断的话我又不太懂,求助
回复

使用道具 举报

ID:560111 发表于 2019-6-11 16:59 | 显示全部楼层
附上程序
回复

使用道具 举报

ID:560111 发表于 2019-6-11 16:59 | 显示全部楼层
#include <REGX51.H> #include <string.h> sbit RS=P2^0; sbit RW=P2^1; sbit EN=P2^2; unsigned char xuehao1[]="LGL1605230139"; unsigned char xuehao2[]="ZCY1605230140"; unsigned char xuehao3[]="SGJ1605230126"; unsigned char xuehao4[]="XWQ1605230124"; unsigned char shi[]="43210"; unsigned char ge[]="0987654321"; void delay(int n) {  int i,j;  for(i=0;i<n;i++)  for(j=0;j<200;j++); } void LCD1602_write_cmd(unsigned char cmd) {  P0=cmd;  RS=0;  RW=0;  EN=1;  EN=0;  delay(10); }  void LCD1602_write_Data(unsigned char Data) {  P0=Data;  RS=1;  RW=0;  EN=1;  EN=0;  delay(10); } void LCD1602_init() {     LCD1602_write_cmd(0x38);      LCD1602_write_cmd(0x0F);      LCD1602_write_cmd(0x06);      LCD1602_write_cmd(0x01);  }                         void main() {  int i,a=5,b=0;  LCD1602_init();   LCD1602_write_cmd(0X80);   for(i=0;i<strlen(xuehao1);i++)   {    LCD1602_write_Data(xuehao1[i]);   }   LCD1602_write_cmd(0X80);   for(i=0;i<strlen(xuehao2);i++)   {    LCD1602_write_Data(xuehao2[i]);   }   LCD1602_write_cmd(0X80);   for(i=0;i<strlen(xuehao3);i++)   {    LCD1602_write_Data(xuehao3[i]);   }   LCD1602_write_cmd(0X80);   for(i=0;i<strlen(xuehao4);i++)   {    LCD1602_write_Data(xuehao4[i]);   }  while(1)  {   LCD1602_write_cmd(0XC0);   LCD1602_write_Data(shi[a]);   LCD1602_write_cmd(0XC1);   LCD1602_write_Data(ge[b]);   delay(100);   b++;   if(b==9)   {    a--;    b=0;   }  } }
回复

使用道具 举报

ID:160500 发表于 2019-6-11 17:45 | 显示全部楼层
用那个都可以
回复

使用道具 举报

ID:123289 发表于 2019-6-12 08:56 | 显示全部楼层
再读书。
回复

使用道具 举报

ID:561113 发表于 2019-6-12 19:35 | 显示全部楼层
建议用定时器中断,CPU有更多的资源去处理其他业务
回复

使用道具 举报

ID:514901 发表于 2019-6-12 21:24 | 显示全部楼层
给你一个1秒的delay函数,记得加  #include “intrins.h”

void delay1s(void)   //误差 0us
{
    unsigned char a,b,c;
    for(c=167;c>0;c--)
        for(b=171;b>0;b--)
            for(a=16;a>0;a--);
    _nop_();  
}
回复

使用道具 举报

ID:557774 发表于 2019-6-13 03:00 来自手机 | 显示全部楼层
郑汉松 发表于 2019-6-12 21:24
给你一个1秒的delay函数,记得加  #include “intrins.h”

void delay1s(void)   //误差 0us

晶振多少的?
回复

使用道具 举报

ID:560111 发表于 2019-6-14 19:54 | 显示全部楼层
本帖最后由 liquidlin 于 2019-6-15 08:59 编辑

#include <REGX51.H>
#include <string.h>
#define uint unsigned int
#define uchar unsigned char
sbit RS=P2^0;
sbit RW=P2^1;
sbit EN=P2^2;
unsigned char i, x=0;c;
unsigned char xuehao1[]="LGL1605230139";
unsigned char xuehao2[]="ZCY1605230140";
unsigned char xuehao3[]="SGJ1605230126";
unsigned char xuehao4[]="XWQ1605230124";
unsigned char num[]={0X30,0X31,0X32,0X33,0X34,0X35,0X36,0X37,0X38,0X39};

void delay(int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=0;j<200;j++);
}
void timer0()
{
TMOD=1;
TH0=(65536-9216)/256;
TL0=(65536-9216)%256;
EA=1;
ET0=1;
TR0=1;
}
void LCD1602_write_cmd(unsigned char cmd)
{
P0=cmd;
RS=0;
RW=0;
EN=1;
EN=0;
delay(10);
}
void LCD1602_write_Data(unsigned char Data)
{
P0=Data;
RS=1;
RW=0;
EN=1;
EN=0;
delay(10);
}
void LCD1602_init()
{
    LCD1602_write_cmd(0x38);
    LCD1602_write_cmd(0x0F);
    LCD1602_write_cmd(0x06);
    LCD1602_write_cmd(0x01);
}
void disp_LCD(uchar c)
{
   LCD1602_write_cmd(0xC0);
   LCD1602_write_Data(num[c/10]);
   LCD1602_write_cmd(0XC1);
   LCD1602_write_Data(num[c%10]);
}                       
void main()
{
  LCD1602_init();
  LCD1602_write_cmd(0X80);
  for(i=0;i<strlen(xuehao1);i++)
  {
   LCD1602_write_Data(xuehao1);
  }
  LCD1602_write_cmd(0X80);
  for(i=0;i<strlen(xuehao2);i++)
  {
   LCD1602_write_Data(xuehao2);
  }
  LCD1602_write_cmd(0X80);
  for(i=0;i<strlen(xuehao3);i++)
  {
   LCD1602_write_Data(xuehao3);
  }
  LCD1602_write_cmd(0X80);
  for(i=0;i<strlen(xuehao4);i++)
  {
   LCD1602_write_Data(xuehao4);
  }
  timer0();
  while(1)
  {
   c=40;
   disp_LCD(c);
   if(x==100)
   {
    x=0;
        c--;
  }
}
}
void timer() interrupt 1
{
TH0=(65536-9216)/256;
TL0=(65536-9216)%256;
x++;
}

回复

使用道具 举报

ID:560111 发表于 2019-6-14 19:56 | 显示全部楼层

新手...学校发的单片机书上c语言都很少,没有LCD,网上找了很久也没找到什么资源,无奈只好寻求帮助了
回复

使用道具 举报

ID:388197 发表于 2019-6-15 13:00 | 显示全部楼层
建议还是先把中断弄清楚,否则后面的学习问题多多,进展缓慢.
回复

使用道具 举报

ID:560111 发表于 2019-6-15 15:30 | 显示全部楼层
为什么上边信息显示完了以后只显示40一直闪烁呢?
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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