基于51单片机60秒倒计时_且倒计时到10秒时蜂鸣器响5秒

2019年10月17日08:40:02 评论

本程序基于51单片机开发板<传送门>

本程序开发板原理图<传送门>,提取码: 2bkj

#include"reg52.h"
#define uint unsigned int
#define uchar unsigned char

#define LCD1602_DB P0 //data bus 数据总线
sbit LCD1602_RS = P2^6;
sbit LCD1602_RW = P2^5;
sbit LCD1602_EN = P2^7;

sbit Beep = P2^1 ;

void Read_Busy();
void Lcd1602_Write_Cmd(unsigned char cmd);
void Lcd1602_Write_Data(unsigned char dat);
void InitLcd1602();

uchar a=60;
void display();
void sub_1();
void delayms(uint xms);
void beep();

void main()
{ uchar music=0,i;
   P1=0x0f0;
   InitLcd1602();
   while(1)
   {
       display();
       if(a==10)
           music=5;
       if(music>0)
       { for(i=100;i>0;i++)
          beep();
          music--;
        }
        else
          delayms(1111);
        sub_1();
     }
}

void sub_1()
{
    a--;
    if(a==0)
       a=60;
}

void delayms(uint xms)
{
   uint i,j;
   for(i=xms;i>0;i--)
      for(j=110;j>0;j--)
         ;
}

void display()
{
    Lcd1602_Write_Cmd(0x80);
    Lcd1602_Write_Data('0'+a/10);
    Lcd1602_Write_Data('0'+a%10);
}
void beep()
{
    Beep= 1; //给高电平
    delayms(5); //延时
    Beep= 0; //给低电平
    delayms(5); //延时
}

void Read_Busy() //忙检测函数,判断bit7是0,允许执行;1禁止
{
    unsigned char sta; //
    LCD1602_DB = 0xff;
    LCD1602_RS = 0;
    LCD1602_RW = 1;
    do
    {
        LCD1602_EN = 1;
        sta = LCD1602_DB;
        LCD1602_EN = 0; //使能,用完就拉低,释放总线
     }while(sta & 0x80);
}

void Lcd1602_Write_Cmd(unsigned char cmd) //写命令
{
    Read_Busy();
    LCD1602_RS = 0;
    LCD1602_RW = 0;
    LCD1602_DB = cmd;
    LCD1602_EN = 1;
    LCD1602_EN = 0;
}

void Lcd1602_Write_Data(unsigned char dat) //写数据
{
    Read_Busy();
    LCD1602_RS = 1;
    LCD1602_RW = 0;
    LCD1602_DB = dat;
    LCD1602_EN = 1;
    LCD1602_EN = 0;
}

void InitLcd1602() //1602初始化
{
    Lcd1602_Write_Cmd(0x38); //打开,5*8,8位数据
    Lcd1602_Write_Cmd(0x0c);
    Lcd1602_Write_Cmd(0x06);
    Lcd1602_Write_Cmd(0x01); //清屏
}

发表评论

您必须才能发表评论!