51单片机按键控制数码管?

电路仿真图如下图所示,开始时8个LED指示灯是熄灭状态。要求:按下K2键,数码管依次显示“0”到“9”的数字;按下K3键, 数码管依次显示“A”到“F”的字母。

#include <reg52.h>    //  可以参考一下

#define uchar unsigned char

#include <stdio.h> 

unsigned char* c;   

sbit  k0=P3^2;

sbit  k1=P3^3;  

sbit  k2=P3^4; 

sbit  k3=P3^5; 

sbit  k4=P3^7; 

sbit  led1=P1^7;

uchar  k;

bit  y0,y1,y2,y3,y4,y5;     

bit  t,s,ld;

uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d};

void SendOneChar(char c)  //发送字节到PC

  {

  TI=0;

  SBUF = c;

  while(!TI);

  TI=0;

  } 

void SendString(char *st)  //发送字符串到PC

  {

  while(*st)

    {

    SendOneChar(*st++);

    }

  } 

void main()

{

  SCON = 0x50;    //SCON: mode 1, 8-bit UART, enable rcvr

  TMOD = 0x20;    //TMOD: timer 1, mode 2, 8-bit reload

PCON = 0x80; // 数据位8、停止位1。效验位无 (11.0592M)

  TH1 = 0xF4;     //TH1:  reload value for [email protected]

  TR1 = 1;        //启动定时器1

  ES = 1;         //允许串口中断位

  EA = 1;         //允许总中断位

  c=0;

  s=1;

  k=0;

  P1 =~table[k];

  while(1)

    {

    if(k0==0 && y0==0)

      {

      y0=1; 

      c = "as";

      SendString(c);              //发送字符串

      }

    if(k0==1 ) { y0=0; }

    if(k1==0 && y1==0) 

      {

      y1=1; 

      c = "bs";

      SendString(c);              //发送字符串

      }

    if(k1==1 ) { y1=0; }

    if(k2==0 && y2==0)

      {

      y2=1; 

      c = "ds";

      SendString(c);              //发送字符串

      }

    if(k2==1 ) { y2=0; }

    if(k3==0 && y3==0) 

      {

      y3=1; 

      c = "es";

      SendString(c);              //发送字符串

      }

    if(k3==1 ) { y3=0; }

    if(k4==0 && y4==0)

      {

      y4=1; 

      c = "fs";

      SendString(c);              //发送字符串

      }

    if(k4==1 ) { y4=0; }

  //  for(i = 0; i < 4000; i++);  //延迟一小段时间

  }

void chuankou(void) interrupt 4

  {

  if(RI==1)

    {

    RI = 0;

    if(t==1)

      {

      if(SBUF=='z') { k++;ld=~ld; }

      s=1; t=0;

      if(k==10) { k=0;}

      P1 =~table[k];

      led1=ld;

      }

    if(SBUF=='a' && s==1) { t=1;s=0;}

    }

 }

/////////////////////////////////////////

#include <reg51.h> // 接收  试试

#define uchar unsigned char

unsigned char* c; 

sbit   led1=P3^2;     //  接收 az  , P.2 输出低电平, P3.3  输出高电平。

sbit   led2=P3^3;     //  接收 aa  , P.2 输出高电平, P3.3   输出低电平。

sbit   k0=P3^5;

bit  y0,t,s;

uchar  k;

uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d};

void SendOneChar(char c)  //发送字节到PC

  {

  TI=0;

  SBUF = c;

  while(!TI);

  TI=0;

  } 

void SendString(char *st)  //发送字符串到PC

  {

  while(*st)

    {

    SendOneChar(*st++);

    }

  } 

void main (void)

  {

  SCON = 0x50;     // REN=1允许串行接受状态,串口工作模式1  

  TMOD = 0x21;     // 定时器工作方式2  

  PCON = 0x80;     // 数据位8、停止位1。效验位无 (11.0592M)  

  TH1 = 0xF4;      // TH1 = 0xFD; 波特率 2400  

  TH0=(65536-54253)/256;  // 50 mS

  TL0=(65536-54253)%256;

  TR1  = 1;  

  ES   = 1;        // 开串口中断

  EA   = 1;        // 开总中断

  led1=1;led2=0;

//  ET0=1;

//  TR0=1;

  s=1;

  k=0;

  P1 =~table[k];

  while(1)

    { 

    if(k0==0 && y0==0)

      {

      y0=1; 

      c = "az";

      SendString(c);              //发送字符串

      }

    if(k0==1 ) { y0=0; }

    }

  }

void chuankou(void) interrupt 4

  {

  if(RI==1)

    {

    RI = 0;

    if(t==1)

    {

   if(SBUF=='s') {led1=~led1;led2=~led2; k++; }

   //if(SBUF=='s') {led1=1;led2=0; k--; }

   s=1; t=0;

   if(k==10) { k=0;}

   if(k==255) { k=9; }

   P1 =~table[k];

   }

//    1号  a    2号  b   ---

 if(SBUF=='a' && s==1) { t=1;s=0;}

    }

 }

void T0_time()interrupt 1

 {

    TH0=(65536-54253)/256;  // 50 mS

    TL0=(65536-54253)%256;

    k++;

    if(k==10)  // 这里修改时间

    {

    k=0;

    led1=1;

    led2=1;

    }

  }

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-05-21
编写一个小程序,即可解决。
编程语言,怎么不指定?
用 C 还是用汇编语言?
第2个回答  2020-05-19
K2 K3是自锁的吗?互锁吗?
相似回答